[ad_1]
When you upgrade to Android Studio Flamingo and Android Gradle Plugin (AGP) 8.0, you need to update your app build files to accommodate five important build behavior changes.
The AGP Upgrade Assistant can help you with these changes. When you use it, some changes it suggests, keep the existing build behavior by adding code lines to opt out of the build behavior changes. Migrate to the new behaviors at a later point by removing these code lines.
Note that in this article we refer to the build.gradle.kts
file, but the same changes apply to the build.gradle
file if you’re using Groovy. Let’s take a look at these changes.
The namespace
DSL property represents the Kotlin or Java package name for the generated R
and BuildConfig
classes, and replaces the package
attribute previously defined in the Android manifest. To move to the namespace configuration, add the namespace
DSL to the android {}
block in the module-level build.gradle.kts
file and remove the package
attribute in the manifest.
The Android Studio AGP Upgrade Assistant will help you with the migration by moving the package from Android manifest package
attribute to the namespace
property in build files.
To understand why we’re making this change, let’s look at the previous behavior.
Previously, the package
attribute was used both for setting the applicationId
and resource namespaces, unnecessarily coupling these two mostly unrelated concepts.
By disallowing setting the package
name in the manifest file, and introducing the namespace
property, we’re separating the applicationId
used for your app’s identity from the resource namespaces. This clarifies where the namespace value comes from, and lets you refactor your app’s code and resources without affecting your applicationId
R
classes for library modules are now non-transitive by default, which means that each R
class only includes the resources declared in the library module itself, not resources from its dependencies. This means that you must use fully qualified, namespaced calls when you’re referencing resources.
The nonTransitiveRClass
flag in the gradle.properties
file controls the R
class behavior. From AGP 8.0, it’s true
when not specified, and thus becomes the default.
To get help updating your R
class calls using Android Studio, go to Refactor > Migrate to Non-Transitive R Classes. This refactoring action converts all R
calls to fully qualified R
calls and will set android.nonTransitiveRClass = true
(if the flag was set to false
) in the gradle.properties
file.
R
classes are generated classes that map your resource names to IDs in your code. Until Android Studio Bumblebee/AGP 7.1 the R
classes were transitive. That means the build was generating resource IDs not only for the library R
classes but also for all modules that the library depends ons. This generation resulted in bigger executable sizes and longer build times.
In the non-transitive behavior each library module R
class includes only the resources declared in the module itself, thus reducing the size of the R
class for that module.
If you call the BuildConfig
class from your module code, you need to enable buildConfig
in the android {}
block in your module’s build.gradle.kts
file. Otherwise, the BuildConfig
file isn’t automatically generated anymore.
The BuildConfig
file is a Java file containing static information about your current build such as the namespace
name, flavor
name, debug
flag, and others. Previously AGP always generated the BuildConfig
file for all Android modules. If you develop a multi-module app you can end up with a lot of BuildConfig
files that AGP needs to process, which affects your build speed. However, most modules don’t need any of the information from the BuildConfig
class.
In addition, BuildConfig
is a Java file. Assuming your app is written with Kotlin, mixing Java and Kotlin in the same module further affects build performance. To mitigate this, we introduced the android.enableBuildConfigAsBytecode
flag set in gradle.properties
. When android.enableBuildConfigAsBytecode=true
, the BuildConfig
file isn’t generated as a Java file, but as a compiled file. This avoids the Java compilation step!
If you need the old behavior for all modules, set the android.defaults.buildfeatures.buildconfig=true
in your gradle.properties
file.
Similar to BuildConfig
, AIDL
and RenderScript
are off by default. To enable them for a particular module, set the aidl
and renderScript
options to true
in the android {}
block of the module’s build.gradle.kts
file:
You can use similar methods to re-enable AIDL or RenderScript for modules that require it, or for your entire project, however please note that RenderScript was deprecated in Android 12 so you should migrate from it.
The last behavior change: R8 is now in full mode by default, enabling app size reductions and performance improvement. You shouldn’t need to update anything for this change, but if you encounter build or runtime failures you should double-check that your keep rules are configured correctly. For guidance on how to configure the keep rules, see Shrink, obfuscate, and optimize your app.
To sum it up, these are the five ways to prepare your app build for the Android Studio Flamingo release with AGP 8.0. If you develop a plugin please read our blog post for plugin changes. If you want to learn more about the changes to build, see the video from Android Dev Summit ’22 and the AGP release notes.
Code snippets license:
Copyright 2023 Google LLC.
SPDX-License-Identifier: Apache-2.0
[ad_2]
Source link