preface

Android Gradle Plugin (AGP) will be released in version 7.0 on December 12. Suddenly some confused, before is not still in 3, 4 version? Why suddenly jumped to 7, carefully looked down to roughly understand. – We will separate the AGP and Android Studio versions and develop them separately. The reason for the change is that Gradle is currently running Gradle 7. It is also currently in Alpha, so there will be more changes in the future. Here’s a rundown of the official announcements.

Officer xuan content

New version control

Beginning with 7.0, the concept of semantic versioning was introduced, which can be roughly summarized as follows:

  • The version number consists of [Major] • [Minor] • [Revised]
  • The API is compatible as long as the main version does not move, and moving means the API is incompatible

The frequency of updates is limited to one major release per year, right after Gradle’s annual major release.

The API to be removed is Deprecated one year in advance.

Need Java11

AGP 7.x requires minimal Java JDK 11 support.

API

Includes API in the pipeline and significant API changes.

Apis incubated in AGP4.1 may also change in the future, which do not follow the deprecation principle mentioned above.

Important API changes

  • onVariants | onProperties | onVariantPropertiesCode blocks will be removed (in AGP 4.2 Beta)
  • So this is replaced by thisandroidComponentsIn the code blockbeforeVariants | onVariants.VariantSelectorCan be used to generate the above DSL dependenciesvariantCallbacks, these callbacks will pass through AGPafterEvcaluateTrigger after calculation of variant combinations
  • onVariantsThe nested attributes in the
  • Other such asbuildType | name | flavorYou can usewithBuildType | withName | withFlavorFor similar purposes
  • androidComponentsAlso added test relatedbeforeUnitTest | unitTest | beforeAndroidTest | androidTest
  • Two classes have been renamed: the originalVariantnamedVariantBuilderIn thebeforeVariantsThe use of.VariantPropertiesnamedVariantAs aonVariantsAn input parameter to a code block

The sample

Take onVariants and beforeVariants as an example:

android {
   ...
   //onVariants.withName("release") {
   / /...
   / /}
  
   //onVariantProperties {
   / /...
   / /}. } androidComponents { val release = selector().withBuildType("release") beforeVariants(release) { variant -> ... } onVariants { variant -> ... }}Copy the code

Pay attention to

The above changes should be made in plugin code, not build.gradle in your project. Gradle plugin authors need to adapt as soon as 7.0 stabilizes, but should not rely on incubated apis.

reference

  • AGP 7.0