Android~老项目项目升级Gradle&AGP 7.0踩坑

结合最近对公司项目做了一下升级,踩了点坑。记录一下,方便后续有人参考。

module模块build.gradle变化
  • apply plugin变成了plugins
  • 对部分android闭包下面的属性也做了缩短优化,但但可以不修改。
  • apply from 还可正常使用,plugins的顺序还要放前面。
  • 去除dexOptions, 删除javaCompileOptions注解处理器配置否则项目无法正常sync,使用kapt替换。
plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android' version '1.6.10' apply false
    id 'kotlin-android'
    id 'kotlin-kapt'
    id 'com.google.firebase.crashlytics' apply false
    id 'com.google.gms.google-services'
}
//apply plugin: 'com.android.application'
//apply plugin: 'kotlin-android'
//apply plugin: 'kotlin-android-extensions'
//apply plugin: 'kotlin-kapt'
//apply plugin: 'com.google.firebase.crashlytics'
apply from: 'config.gradle'
android {
    compileSdk 31
    defaultConfig {
        versionCode 1
        versionName '1.0'
        minSdk 21
        targetSdk 31
	}
//    dexOptions {
//        preDexLibraries true
//        maxProcessCount 8
//        threadCount 8
//        javaMaxHeapSize "6g"
//        jumboMode true
//    }
}

项目gradle
  • seeting.gradle新增了插件管理
  • 如果使用了http,需要在maven中配置allowInsecureProtocol = true
pluginManagement {
    repositories {
        gradlePluginPortal()
        google()
        mavenCentral()
    }
}
dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()
    }
}

maven {
		url 'http://maven.aliyun.com/nexus/content/groups/public/'
		allowInsecureProtocol = true
}
问题记录

A problem occurred evaluating project ‘:app’.
Failed to apply plugin ‘com.android.internal.application’.
The option ‘android.enableBuildCache’ is deprecated.
The current default is ‘false’.
It was removed in version 7.0 of the Android Gradle plugin.
The Android-specific build caches were superseded by the Gradle build cache (https://docs.gradle.org/current/userguide/build_cache.html).

  1. gradle.properties文件,把 android.enableBuildCache=true 和 android.buildCacheDir=./build/buildCache/注销掉。

Firebase Crashlytics - java.lang.IllegalStateException: The Crashlytics build ID is missing.

  1. 遇到问题,脚本能正常编译后运行程序启动奔溃,有如下日志,原来是crashlytics没有被启动apply false 改为true。

Method ‘buildStarted’ from class ‘BuildTraceListener’ does not override method from its superclass or interfaces but is annotated with @Override.

  1. Gradle 7.0移除了BuildListener.buildStarted(Gradle),直接删除buildStarted方法即可。
    改用BuildListener.beforeSettings(Settings)代替。

java.lang.RuntimeException: Manifest merger failed with multiple errors

  1. 打开每个模块的清单文件切到Merged Manifest视图查看合并错误。
    截图1

using Jetifier. Reason: AmbiguousStringJetifierException, message: The given
Execution failed for task ‘:app:javaPreCompileDebug’.
Could not resolve all files for configuration ‘:app:debugAnnotationProcessorClasspath’.
Failed to transform butterknife-compiler-8.8.1.jar (com.jakewharton:butterknife-compiler:8.8.1) to match attributes {artifactType=processed-jar, org.gradle.category=library, org.gradle.libraryelements=jar, org.gradle.status=release, org.gradle.usage=java-runtime}.
Execution failed for JetifyTransform: /Users/mahui/.gradle/caches/modules-2/files-2.1/com.jakewharton/butterknife-compiler/8.8.1/b2f4505a1babb7b7c11abbbf8ea4c90b18c3aeac/butterknife-compiler-8.8.1.jar.
Failed to transform ‘/Users/mahui/.gradle/caches/modules-2/files-2.1/com.jakewharton/butterknife-compiler/8.8.1/b2f4505a1babb7b7c11abbbf8ea4c90b18c3aeac/butterknife-compiler-8.8.1.jar’ using Jetifier. Reason: AmbiguousStringJetifierException, message: The given artifact contains a string literal with a package reference ‘android.support.v4.content’ that cannot be safely rewritten. Libraries using reflection such as annotation processors need to be updated manually to add support for androidx… (Run with --stacktrace for more details.)
This is a known exception, and Jetifier won’t be able to jetify this library.
Suggestions:
- If you believe this library doesn’t need to be jetified (e.g., if it already supports AndroidX, or if it doesn’t use support libraries/AndroidX at all), add android.jetifier.blacklist = {comma-separated list of regular expressions (or simply names) of the libraries that you don’t want to be jetified} to the gradle.properties file.
- If you believe this library needs to be jetified (e.g., if it uses old support libraries and breaks your app if it isn’t jetified), contact the library’s authors to update this library to support AndroidX and use the supported version once it is released.
If you need further help, please leave a comment at https://issuetracker.google.com/issues/140747218.

  1. 出现了support和androidx迁移遗留问题,虽然我们项目已经迁移了androidx,但可能不彻底。这边暂时的解决办法是根据提示信息在gradle.properties配置文件中增加:android.jetifier.ignorelist=bundletool-1.6.0.jar,同步一下重新编译居然通过了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不会画板子的物联网工程师

如果文章还不错,欢迎点赞收藏~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值