Android 个使用 Kotlin DSL 的子项目
Android subprojects with Kotlin DSL
我正在通过 Gradle 构建文件迁移到 Kotlin DSL,但我遇到了一个问题。
在我的 parent 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
}
...
}
}
}
}
这让我可以在一个地方配置所有 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
}
...
}
}
}
}
错误是 Unresolved reference: android
,似乎脚本编译器无法识别 android{}
块。
我的理论是 if
检查子项目类型是不够的,我可能必须强制转换或获取对某些 object 的引用,我可以在其中调用 android{}
块,但老实说我还不够了解。
有线索吗?
Gradle Kotlin DSL 从 gradle 类路径和应用的插件中确定 每个脚本 的依赖关系。这就是为什么它建议在 Kotlin DSL 中使用 plugins { ... }
块。
您需要将 android 和 kotlin 插件添加到您的根目录而不应用它。
plugins {
id("<android-plugin>") version "<plugin-version>" apply false
id("<kotlin-plugin>") version "<plugin-version>" apply false
}
不幸的是,这仍然不会为根构建脚本生成静态访问器,但它会让您访问脚本中的插件 类,您可以像这样引用它们:
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 {
// ...
}
}
}
}
我正在通过 Gradle 构建文件迁移到 Kotlin DSL,但我遇到了一个问题。
在我的 parent 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
}
...
}
}
}
}
这让我可以在一个地方配置所有 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
}
...
}
}
}
}
错误是 Unresolved reference: android
,似乎脚本编译器无法识别 android{}
块。
我的理论是 if
检查子项目类型是不够的,我可能必须强制转换或获取对某些 object 的引用,我可以在其中调用 android{}
块,但老实说我还不够了解。
有线索吗?
Gradle Kotlin DSL 从 gradle 类路径和应用的插件中确定 每个脚本 的依赖关系。这就是为什么它建议在 Kotlin DSL 中使用 plugins { ... }
块。
您需要将 android 和 kotlin 插件添加到您的根目录而不应用它。
plugins {
id("<android-plugin>") version "<plugin-version>" apply false
id("<kotlin-plugin>") version "<plugin-version>" apply false
}
不幸的是,这仍然不会为根构建脚本生成静态访问器,但它会让您访问脚本中的插件 类,您可以像这样引用它们:
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 {
// ...
}
}
}
}