前言
在做的一個Kotlin項目用到的數(shù)據(jù)庫框架是greenDAO,在將項目升級到AGP 8.0版本后,編譯時由于greenDAO版本不兼容導致編譯不過,經(jīng)過一番摸索,最終解決了greenDAO在AGP 8.0+版本下的編譯報錯問題,特此記錄一下??
環(huán)境
| 名稱 | 版本 |
|---|---|
| Android Studio | Flamingo,2022.2.1 Patch 2 |
| JDK | 17.0.6 |
| Gradle | 8.0.2 |
| Android Gradle Plugin | 8.0.2 |
| targetSdkVersion | 33 |
升級greenDAO插件版本到3.3.1
修改項目根目錄下的build.gradle文件,內(nèi)容如下:
buildscript {
repositories {
google()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:8.0.2'
classpath 'org.greenrobot:greendao-gradle-plugin:3.3.1'
}
}
升級greenDAO依賴版本到3.3.0
修改項目模塊如app模塊下的build.gradle文件,內(nèi)容如下:
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'kotlin-android-extensions'
id 'kotlin-kapt'
id 'org.greenrobot.greendao'
}
greendao {
schemaVersion 19
daoPackage 'xxx'
targetGenDir 'src/main/java'
}
dependencies {
implementation 'org.greenrobot:greendao:3.3.0'
}
然后編譯,還是編譯不過,報錯內(nèi)容如下:
org.gradle.internal.execution.WorkValidationException: Some problems were found with the configuration of task ':app:greendao' (type 'DefaultTask').
- Gradle detected a problem with the following location: '/Users/kongpf/Desktop/mail-android/app/src/main/java'.
Reason: Task ':app:compileOfficialDebugKotlin' uses this output of task ':app:greendao' without declaring an explicit or implicit dependency. This can lead to incorrect results being produced, depending on what order the tasks are executed.
Possible solutions:
1. Declare task ':app:greendao' as an input of ':app:compileOfficialDebugKotlin'.
2. Declare an explicit dependency on ':app:greendao' from ':app:compileOfficialDebugKotlin' using Task#dependsOn.
3. Declare an explicit dependency on ':app:greendao' from ':app:compileOfficialDebugKotlin' using Task#mustRunAfter.
Please refer to https://docs.gradle.org/8.0.2/userguide/validation_problems.html#implicit_dependency for more details about this problem.
翻譯一下,報錯的意思就是任務':app:compileOfficialDebugKotlin'使用任務':app:greendao'的輸出,但沒有聲明顯式或隱式依賴,并貼心的給出了幾種解決辦法和相關的gradle鏈接.
修改一下build.gradle腳本,添加依賴關系,編譯kotlin的任務依賴greendao任務,如下:
tasks.whenTaskAdded { task ->
if (task.name.matches("compile\w*Kotlin")) {
task.dependsOn('greendao')
}
}
然后再重新編譯就可以編譯通過?了,最終的build.gradle腳本如下:
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'kotlin-android-extensions'
id 'kotlin-kapt'
id 'org.greenrobot.greendao'
}
greendao {
schemaVersion 19
daoPackage 'xxx'
targetGenDir 'src/main/java'
}
//添加依賴關系
tasks.whenTaskAdded { task ->
if (task.name.matches("compile\w*Kotlin")) {
task.dependsOn('greendao')
}
}
dependencies {
implementation 'org.greenrobot:greendao:3.3.0'
}