AGP 升级问题
(1)Could not get unknown property ‘project’ for settings,on project.buildscript
问题
Could not get unknown property ‘project’ for settings ‘AGP1’ of type org.gradle.initialization.DefaultSettings.
agp4 时 Android 工程可运行模块同级目录下的自定义 Gradle 脚本能够直接使用内置的 project 属性(对象),到 apg7 怎么就不行了,如何兼容?
代码
// agp4 正常使用,apg7 下报错
project.buildscript.repositories {add(maven {url "https://******"})
}
APG4
println("==> config.gradle project: " + project)
println("==> config.gradle project.buildscript: " + project.buildscript)==> config.gradle project: root project 'newyingxiaomb'
==> config.gradle project.buildscript: org.gradle.api.internal.initialization.DefaultScriptHandler@2afa7b34
APG7
println("==> config.gradle this: " + this)
println("==> config.gradle this.buildscript: " + this.buildscript)
println("==> config.gradle this.project: " + this.project)==> config.gradle this: script
==> config.gradle this.buildscript: org.gradle.api.internal.initialization.DefaultScriptHandler@435d11ae
this 是 script 对象,this.project 没有输出,但 this.buildscript 和 agp4 对象类型还是一致的,满足我们的要求,可以使用 buildscript。注意:你可能知道再升级到 AGP7+ 之后工程配置发生了稍微的变化,比如以前写在 build.gradle 的配置后来移到 setting.gradle 里面去了!
解决
setting.gradle 引入的脚本执行 project.buildscript
找不到 project 属性,可以替换为 this.buildscript
。
(2)Could not get unknown property ‘project’ for settings,on project.getProjectDir()
问题
Caused by: groovy.lang.MissingPropertyException: Could not get unknown property ‘projectDir’ for settings ‘AGP1’ of type org.gradle.initialization.DefaultSettings.
同上,AGP7 setting 里面没有 project 对象了。
代码
def gradleWrapperFilePath = project.getProjectDir().getAbsolutePath() + "/gradle/wrapper/gradle-wrapper.properties"
AGP4、AGP7
println("==> config.gradle this.rootProject: " + this.rootProject)
println("==> config.gradle this.rootProject.projectDir: " + this.rootProject.projectDir)println("==> config.gradle this.rootDir: " + this.rootDir)
println("==> config.gradle this.rootDir.path: " + this.rootDir.path)// setting.gradle 里面输出 rootProject 竟然是空?但是 rootProject.projectDir 输出又是有值的!
==> config.gradle this.rootProject: :
==> config.gradle this.rootProject.projectDir: E:\ALLMine\tmpDemo\AGP1==> config.gradle this.rootDir: E:\ALLMine\tmpDemo\AGP1
==> config.gradle this.rootDir.path: E:\ALLMine\tmpDemo\AGP1
解决
setting.gradle 引入的脚本执行 project.getProjectDir() 找不到 project 属性,可以替换为 rootProject.projectDir 或 rootDir.path
。
(3)不允许 http 协议的仓库
问题
Using insecure protocols with repositories, without explicit opt-in, is unsupported. Switch Maven repository to redirect to a secure protocol (like HTTPS) or allow insecure protocols.
代码
maven {url "http://****"
}
解决
- 存在 https 仓库的直接从 http 改为 https
- 不存在 https 的,可以使用
allowInsecureProtocol true
允许不安全的 http
maven {allowInsecureProtocol trueurl "http://***"
}//报错
maven { url 'http://developer.huawei.com/repo/' }
//存在 https 的,可以直接更改
maven { url 'https://developer.huawei.com/repo/' }
Tip:
- dependencyResolutionManagement:全局仓库配置
- pluginManagement:插件配置
(4)setting.gradle 里不存在 dependencies
- Could not find method classpath() for arguments
- all buildscript {} blocks must appear before any plugins {} blocks in the script
问题
Could not find method dependencies() for arguments [settings_***_closure4@6c051893] on object of type org.gradle.plugin.management.internal.DefaultPluginManagementSpec.
AGP7
//项目根目录下 build.gradle
buildscript {dependencies {classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.72"classpath "com.bytedance.android.byteX:shrink-r-plugin:0.3.0"classpath "com.android.plugin:art:2.1.6"classpath "com.google.plugin.kt:remove-metadata:1.0.5"classpath "com.android.plugin:reann:1.0.1"classpath "com.android.plugin:resGuard:1.0.6"}
}
解决
(5)org.gradle.BuildListener 中 buildStarted 被移除
问题
‘buildStarted’ from class ‘com.*****$2’ does not override method from its superclass or interfaces but is annotated with @Override.
代码
project.gradle.addBuildListener(new BuildListener() {@Overridevoid buildStarted(Gradle gradle) {}@Overridevoid settingsEvaluated(Settings settings) {}@Overridevoid projectsLoaded(Gradle gradle) {}@Overridevoid projectsEvaluated(Gradle gradle) {}@Overridevoid buildFinished(BuildResult buildResult) {if (isDeleteObs) {removeObsJar(mProject)}}})
解决
- 移除
buildStarted
方法即可
(6)registerTransform、Transform 过时【暂不处理】
问题
The Transform API is planned to be removed in Android Gradle plugin 8.0.
There is no single replacement. For more information about how to migrate, see https://developer.android.com/studio/releases/gradle-plugin-roadmap
计划在 8.0 移除,我现在更新到 7+,躺平先不处理
代码
def android = project.extensions.getByType(AppExtension)
android.registerTransform(transform)
(7)Kotlin Gradle 版本过低
问题
The Android Gradle plugin supports only Kotlin Gradle plugin version 1.5.20 and higher.
The following dependencies do not satisfy the required version:
root project ‘AGP1’ -> org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.32
解决
按提示升级即可
dependencies {classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.20"//classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.72"
}
(8)Build was configured to prefer settings repositories over project repositories but repository ‘maven’ was added
问题
Build was configured to prefer settings repositories over project repositories but repository ‘maven’ was added by plugin ‘com.primer.plugin’
插件内在 apply 方法中通过 buildscript 动态添加仓库
代码
target.parent.buildscript {repositories {mavenCentral()jcenter()google()// 其他第三方仓库}
}
解决
github issues
//setting.gradle
dependencyResolutionManagement {//新建项目默认配置//repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)//repositoriesMode.set(RepositoriesMode.PREFER_SETTINGS)repositoriesMode.set(RepositoriesMode.PREFER_PROJECT)
}
还是有其他问题,暂未解决,见下一节吧,18:30 该下班了~