Rob*_*ill 6 gradle android-gradle-plugin gradle-kotlin-dsl
我正在通过 Gradle 构建文件迁移到 Kotlin DSL,但遇到了一个问题。
在我的父母身上build.gradle,我有以下一段代码
buildscript {
repositories {
google()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.4.2'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:${Version.kotlin}"
}
}
allprojects {
repositories {
google()
mavenCentral()
}
}
subprojects {
afterEvaluate { project ->
if (project.plugins.findPlugin('com.android.application') ?:
project.plugins.findPlugin('com.android.library')) {
android {
compileSdkVersion = Android.SDK_COMPILE
defaultConfig {
minSdkVersion Android.SDK_MIN
targetSdkVersion Android.SDK_TARGET
versionCode = Android.VERSION_CODE
versionName = Android.VERSION_NAME
}
...
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
这允许我只在一个地方配置所有作为 android 应用程序或库的模块。
但是,当我迁移到 kotlin 时,这似乎不起作用:
buildscript {
repositories {
google()
mavenCentral()
}
dependencies {
classpath(Dependency.androidGradle)
classpath(Dependency.kotlinGradle)
}
}
allprojects {
repositories {
google()
mavenCentral()
}
}
subprojects {
afterEvaluate {
if (project.plugins.findPlugin("com.android.application") != null ||
project.plugins.findPlugin("com.android.library") != null) {
android { <<<<------------ Unresolved reference: android
compileSdkVersion(Android.SDK_COMPILE)
defaultConfig {
minSdkVersion(Android.SDK_MIN)
targetSdkVersion(Android.SDK_TARGET)
versionCode = Android.VERSION_CODE
versionName = Android.VERSION_NAME
}
...
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
错误是Unresolved reference: android,看起来android{}脚本编译器无法识别该块。
我的理论是,if检查子项目类型是不够的,我可能必须强制转换或获取对某个对象的引用,我可以在其中调用android{}块,但老实说,我知道的还不够多。
有什么线索吗?
efe*_*ney 13
Gradle Kotlin DSL从 gradle 类路径和应用的插件中确定每个脚本的依赖关系。这就是为什么它建议使用plugins { ... }在 Kotlin DSL 中块。
您需要将 android 和 kotlin 插件添加到您的根目录而不应用它。
plugins {
id("<android-plugin>") version "<plugin-version>" apply false
id("<kotlin-plugin>") version "<plugin-version>" apply false
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,这仍然不会为根构建脚本生成静态访问器,但它会让您访问脚本中的插件类,您可以像这样引用它们:
subprojects {
// BasePlugin is the common superclass of the AppPlugin and LibraryPlugin which are the plugin classes that "com.android.application" and "com.android.library" apply
plugins.withType<BasePlugin> {
// BaseExtension is the common superclass of the AppExtension and LibraryExtension which are the extension classes registered by the two plugins to the name "android"
configure<BaseExtension> {
// This block is typed correctly
defaultConfig {
// ...
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2065 次 |
| 最近记录: |