Update gradle to AGP8.0 because of a warning

Original link: https://i.lckiss.com/?p=8279

background

I had nothing to do, so I updated the IDE to Hedgehog, took a look at the compilation warning, and saw this line:

 'compileDebugJavaWithJavac' task (current target is 11) and 'kaptGenerateStubsDebugKotlin' task (current target is 1.8) jvm target compatibility should be set to the same Java version.

After understanding, it is actually caused by the kotlinOptions configuration no longer taking effect. I don’t know in which version the kotlin configuration method has changed.

 android {  
  
    compileOptions {  
     sourceCompatibility JavaVersion. VERSION_11  
     targetCompatibility JavaVersion. VERSION_11  
}  
  
    //kotlinOptions {  
 // jvmTarget = "11"  
//}  
  
    kotlin {  
 jvmToolchain(11)  
    }  
}

After a while of operation, the warning disappeared. I thought it was perfectly solved, but after looking at the project, first of all, there was a pop-up window in the lower right corner, with the following content:

 Android Gradle plugin version 7.3.1 has an upgrade available. Start the AGP Upgrade Assistant to update this project's AGP version.

Secondly, in the past, all lib dependencies in ext mode reported warning:

 Unrecognized dependency notation

Looking at the official warning analysis on the dependency side, I found a keyword: Version Catalogs.

The link: https://docs.gradle.org/current/userguide/platforms.html , you can roughly understand it. New specifications have been added. In this case, let’s fix it.

AGP8 Version Catalogs

Android dependency management has a development process. It is generally in the form of ext extended fields, and is similar to implementation project.lib.compose_ui . The disadvantages are that it cannot jump to references and does not prompt for version updates. The advantage is that it is simple.

The others are buildSrc, to buildSrc + Kotlin DSL, which is essentially the plug-in wheel made by groovy rebuilt with kotlin, and later there is a more magical Gradle Composite builds. Fortunately, with Gradle 8, the official has pointed out the clear path, so I just jump to the AGP8 officially recommended version catalogs .

PS: Although it is said to be AGP8, it has been available since Gradle 7.0, but it was still an experimental feature at that time. You need to use enableFeaturePreview('VERSION_CATALOGS') to enable it. AG8 is enabled by default.

change

The running environment of some compilation chains needs to be changed to Java 17 recommended by the IDE:

 android {  
    compileOptions {  
        sourceCompatibility JavaVersion.VERSION_17  
        targetCompatibility JavaVersion.VERSION_17  
    }  
    kotlin {  
        jvmToolchain(17)  
    }  
}

The relevant gradle plug-in needs to be upgraded (it is recommended to use Menu->Tools->AGP Upgrade Assistant):

 #build.gradle  
classpath "com.android.tools.build:gradle:8.1.1"  
  
#gradle-wrapper.properties  
distributionUrl=https\://services.gradle.org/distributions/gradle-8.2-bin.zip

Not surprisingly, namespace is added to each module’s build.gradle, and the compilation will not pass after Sync. The answer is also here. You need to turn off several configurations that are enabled by default in AGP8.

 #gradle.properties  
  
# Turn on buildconfig, otherwise the BuildConfig class android.defaults.buildfeatures.buildconfig=true cannot be found  
# Configure the non-transitive R class. After it is turned on, the R files generated by each module will be independent . It is uncomfortable for one class to reference multiple R classes android.nonTransitiveRClass=false  
# Warning for R.id android.nonFinalResIds=false  
# Unused classes will be removed from dex and reflection will crash. Don’t say they are not needed, Gson?  
android.enableR8.fullMode=false

As expected, it should be able to run normally, but this is just the beginning, and the dependency warning is still there.

Use toml for dependency management

There is a prerequisite here, that is, the versionCatalogs block is kotlin DSL, so setting.gradle needs to be changed to setting.gradle.kts. The general rule is to add () and replace single quotes with double quotes. The result is as follows:

 dependencyResolutionManagement {  
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)  
    repositories {  
        google()  
        gradlePluginPortal()  
        mavenCentral()  
        maven { setUrl("https://jitpack.io") } // Big changes jcenter() // Warning: this repository is going to shut down soon  
    }  
    //New dependency versionCatalogs {  
        create("libs") {  
            from(files("./libs.versions.toml"))  
        }  
    }  
}  
  
rootProject.name = "Your app name."  
include(":app")  
include(":Resource")

Among them, versionCatalogs is added after DSL. There are also some considerations here. For example, libs is the reference method when using this configuration file. You can use libs.xxx to access certain attributes of this configuration. If you want to distinguish or customize, continue Just add it.

The toml configuration file is very simple to write. It is roughly as follows. If there are dependencies on a common version and those that do not require a common version, put a copy here. You can distinguish version.ref and version by yourself. One is a reference and the other is a string.

 [versions]  
savedState = "1.2.1"  
  
[libraries]  
savedstate = { group = "androidx.savedstate", name = "savedstate", version.ref = "savedState" }  
supportcompat = { group = "com.android.support", name = "support-compat", version = "28.0.0" }

After changing all the existing dependencies of the project to this configuration, you can freely configure it in the module build.gradle:

 dependencies {  
    implementationlibs.savedstate  
}

If you don’t want to reference a dependency, but just want to use one of the version values, you can use it like this:

 def saved_state = libs.versions.savedState.get() // "1.2.1"

Others, such as the upgrade of custom plug-ins, etc., will not be discussed here. It is mainly not covered and is quite mature. Let’s use it~

refer to

Chinese:

Android not running after update? Come and adapt AGP8 and Flamingo/JDK 17

Migrate to Gradle 7.x and use Version Catalogs to manage dependencies

Dependency management using Kotlin DSL

[Gradle7.0] A new way of relying on unified management, learn about it~

English/Official:

Sharing dependency versions between projects

Configure a Gradle project

Migrate your build to version catalogs

This article is reproduced from: https://i.lckiss.com/?p=8279
This site is only for collection, and the copyright belongs to the original author.