Whenever you improve to Android Studio Flamingo and Android Gradle Plugin (AGP) 8.0, you could replace your app construct information to accommodate 5 essential construct conduct adjustments.
The AGP Improve Assistant can assist you with these adjustments. Whenever you use it, some adjustments it suggests, maintain the prevailing construct conduct by including code strains to decide out of the construct conduct adjustments. Migrate to the brand new behaviors at a later level by eradicating these code strains.
Word that on this article we discuss with the construct.gradle.kts
file, however the identical adjustments apply to the construct.gradle
file for those who’re utilizing Groovy. Let’s check out these adjustments.
The namespace
DSL property represents the Kotlin or Java bundle identify for the generated R
and BuildConfig
lessons, and replaces the bundle
attribute beforehand outlined within the Android manifest. To maneuver to the namespace configuration, add the namespace
DSL to the android {}
block within the module-level construct.gradle.kts
file and take away the bundle
attribute within the manifest.
The Android Studio AGP Improve Assistant will assist you with the migration by shifting the bundle from Android manifest bundle
attribute to the namespace
property in construct information.
To grasp why we’re making this variation, let’s have a look at the earlier conduct.
Beforehand, the bundle
attribute was used each for setting the applicationId
and useful resource namespaces, unnecessarily coupling these two largely unrelated ideas.
By disallowing setting the bundle
identify within the manifest file, and introducing the namespace
property, we’re separating the applicationId
used in your app’s identification from the useful resource namespaces. This clarifies the place the namespace worth comes from, and allows you to refactor your app’s code and sources with out affecting your applicationId
R
lessons for library modules at the moment are non-transitive by default, which signifies that every R
class solely consists of the sources declared within the library module itself, not sources from its dependencies. Which means that you need to use totally certified, namespaced calls while you’re referencing sources.
The nonTransitiveRClass
flag within the gradle.properties
file controls the R
class conduct. From AGP 8.0, it’s true
when not specified, and thus turns into the default.
To get assist updating your R
class calls utilizing Android Studio, go to Refactor > Migrate to Non-Transitive R Lessons. This refactoring motion converts all R
calls to completely certified R
calls and can set android.nonTransitiveRClass = true
(if the flag was set to false
) within the gradle.properties
file.
R
lessons are generated lessons that map your useful resource names to IDs in your code. Till Android Studio Bumblebee/AGP 7.1 the R
lessons have been transitive. Which means the construct was producing useful resource IDs not just for the library R
lessons but in addition for all modules that the library relies upon ons. This technology resulted in larger executable sizes and longer construct occasions.
Within the non-transitive conduct every library module R
class consists of solely the sources declared within the module itself, thus decreasing the scale of the R
class for that module.
If you happen to name the BuildConfig
class out of your module code, you could allow buildConfig
within the android {}
block in your module’s construct.gradle.kts
file. In any other case, the BuildConfig
file isn’t robotically generated anymore.
The BuildConfig
file is a Java file containing static details about your present construct such because the namespace
identify, taste
identify, debug
flag, and others. Beforehand AGP all the time generated the BuildConfig
file for all Android modules. If you happen to develop a multi-module app you may find yourself with a whole lot of BuildConfig
information that AGP must course of, which impacts your construct velocity. Nonetheless, most modules don’t want any of the data from the BuildConfig
class.
As well as, BuildConfig
is a Java file. Assuming your app is written with Kotlin, mixing Java and Kotlin in the identical module additional impacts construct efficiency. To mitigate this, we launched the android.enableBuildConfigAsBytecode
flag set in gradle.properties
. When android.enableBuildConfigAsBytecode=true
, the BuildConfig
file isn’t generated as a Java file, however as a compiled file. This avoids the Java compilation step!
If you happen to want the previous conduct for all modules, set the android.defaults.buildfeatures.buildconfig=true
in your gradle.properties
file.
Just like BuildConfig
, AIDL
and RenderScript
are off by default. To allow them for a specific module, set the aidl
and renderScript
choices to true
within the android {}
block of the module’s construct.gradle.kts
file:
You need to use related strategies to re-enable AIDL or RenderScript for modules that require it, or in your complete venture, nonetheless please word that RenderScript was deprecated in Android 12 so it is best to migrate from it.
The final conduct change: R8 is now in full mode by default, enabling app dimension reductions and efficiency enchancment. You shouldn’t must replace something for this variation, however for those who encounter construct or runtime failures it is best to double-check that your maintain guidelines are configured accurately. For steerage on the way to configure the maintain guidelines, see Shrink, obfuscate, and optimize your app.
To sum it up, these are the 5 methods to organize your app construct for the Android Studio Flamingo launch with AGP 8.0. If you happen to develop a plugin please learn our weblog publish for plugin adjustments. If you wish to study extra concerning the adjustments to construct, see the video from Android Dev Summit ’22 and the AGP launch notes.
Code snippets license:
Copyright 2023 Google LLC.
SPDX-License-Identifier: Apache-2.0