Gradle 8.11.1的升级之旅

1,111 阅读3分钟

如果大家的gradle版本之前是4.0.1或者4.2.0这种低版本,大家不要一下子就升到8.0版本,一定要一步步升级

第一步:gradle 4.0.1升级到7.4.2

build.gradle

classpath('com.android.tools.build:gradle:7.4.2') {
    exclude group: 'org.jetbrains.kotlin', module: 'kotlin-stdlib-jdk8'
}

gradle-wrapper.properties

#Fri Jun 04 10:47:21 CST 2021
#Mon Dec 23 09:50:17 CST 2024
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https://services.gradle.org/distributions/gradle-7.5-bin.zip

maven插件修改为maven-publish

apply plugin: 'maven-publish'

uploadArchives移除替换

uploadArchives {
    repositories.mavenDeployer {
        //本地仓库路径,以放到项目根目录下的 repo 的文件夹为例
        repository(url: uri('../repo'))

        //groupId
        pom.groupId = 'com.peakmain'

        //artifactId
        pom.artifactId = 'android.plugin'

        //插件版本号
        pom.version = '1.0.0'
    }
afterEvaluate {
    publishing {
        publications {
            release(MavenPublication) {
                groupId = 'com.peakmain'
                artifactId = 'android.plugin'
                version = '1.0.0'
                from components.java  // 只在 Java 项目中使用
                // 自动生成 POM 依赖配置
                pom.withXml {
                    def dependenciesNode = asNode().appendNode('dependencies')
                    configurations.implementation.allDependencies.each {
                        if (it.group != null && it.name != null) {
                            def dependencyNode = dependenciesNode.appendNode('dependency')
                            dependencyNode.appendNode('groupId', it.group)
                            dependencyNode.appendNode('artifactId', it.name)
                            dependencyNode.appendNode('version', it.version)
                        }
                    }
                }
            }
        }

        repositories {
            maven {
                url = uri('../repo')
            }
        }
    }
}

更推荐使用升级助手,可以节约大量时间

image.png

image.png

第二步:gradle 7.4.2 升级到 8.11.1

先说下我们公司的目前kotlin和gradle版本

kotlin版本gradle插件版本gradle版本minSdkVersiontargetSdkVersionjava版本
1.6.104.1.16.7.1213311

我们升级后的目标是

kotlin版本gradle插件版本gradle版本minSdkVersiontargetSdkVersionjava版本
2.1.08.10.18.11.1213317

build.gradle

buildscript {
    ext.kotlin_version = '1.9.20'
    dependencies {
        classpath('com.android.tools.build:gradle:8.10.1') {
            exclude group: 'org.jetbrains.kotlin', module: 'kotlin-stdlib-jdk8'
        }
    }
}

gradle-wrapper.properties

distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https://services.gradle.org/distributions/gradle-8.11.1-bin.zip

TheRouter升级

  • 官网的版本文档暴露的是1.2.4,但是这个不支持AGP 8,在官网的Issues中找到了相关解决办法
  • agp8.8.0 not support #247
// root build.gradle 
classpath 'cn.therouter:plugin:1.3.0-rc4'

// app module 
apply plugin: 'therouter'

// dependencies
kapt "cn.therouter:apt:1.3.0-rc4"
implementation "cn.therouter:router:1.3.0-rc4"

com.huawei.agconnect升级

错误:

What went wrong:
An exception occurred applying plugin request [id: 'com.huawei.agconnect']
> Failed to apply plugin 'com.huawei.agconnect'.
   > API 'android.registerTransform' is removed.

升级版本即可

classpath 'com.huawei.agconnect:agcp:1.9.1.301'

火山埋点升级

classpath 'com.bytedance.applog:RangersAppLog-All-plugin-agp8:6.17.1'

错误:Compose Compiler

Starting in Kotlin 2.0, the Compose Compiler Gradle plugin is required when compose is enabled. See the following link for more information: d.android.com/r/studio-ui…

从 Kotlin 2.0 开始,Jetpack Compose 不再自动使用默认的编译器插件,你必须手动声明 Compose Compiler 插件版本。

build.gradle

plugins {
    id 'com.android.library'
    id 'kotlin-android'
    id 'org.jetbrains.kotlin.plugin.compose' version '2.1.0'
}

gradle.properties

  • 需要添加以下配置,否则会不生产BuildConfig类
android.defaults.buildfeatures.buildconfig=true
android.nonTransitiveRClass=false
android.nonFinalResIds=false

Namespace not specified

Namespace not specified. Specify a namespace in the module's build file:

模块缺少 namespace 配置项,而从 Android Gradle Plugin (AGP) 8.0 开始,namespace 是强制要求的,否则会构建失败

android {
    namespace 'com.yourcompany.common' // ← 替换为该模块实际的包名
    ...
}

通常 namespace 对应你这个模块的 AndroidManifest.xml 中的 package 值。

比如,basic/common/src/main/AndroidManifest.xml 里有

<manifest package="com.yourcompany.common">

那么就配置成

android {
    namespace 'com.yourcompany.common'
}

tasks 'compileDebugJavaWithJavac' (1.8) and 'kaptGenerateStubsDebugKotlin' (17).

Execution failed for task ':basic:library:kaptGenerateStubsDebugKotlin'. > Inconsistent JVM-target compatibility detected for tasks 'compileDebugJavaWithJavac' (1.8) and 'kaptGenerateStubsDebugKotlin' (17).

两者 JVM 版本不一致,导致构建失败

build.gradle(所有对应模块的 build.gradle)里,设置统一的 Java 和 Kotlin 编译目标版本:

android {
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_17
        targetCompatibility JavaVersion.VERSION_17
    }

    kotlinOptions {
        jvmTarget = "17"
    }
}

补充

我在构建项目的时候莫名有个文件找不到了

Unresolved reference 'ZuoYunMeetRoomCouponLookListAdapter'

但是我点击进入源码之后,源码能找到对应的代码,并且也能链接过去,但是构建就是报错,后面仔细发现是包名多了个空格

com.peakmain. ui.ZuoYunMeetRoomCouponLookListAdapter

删除对应空格就好了

最后

大家最好还是用「升级助手」来升级,可以避免很多无谓的坑,因为它会给你自动添加一些配置。 也不要试图一下子从Gradle 4 升级到Gradle 8,先升级到7,再升级到8,可以避免很多莫名的坑