5 Ways to Prepare Your App Builds for Android Studio Flamingo Releases

Link to the original text .

When you upgrade to Android Studio Flamingo and the 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 make these changes. When you use it, it suggests to keep the existing build behavior by adding a line of code to opt out of the build behavior change. Migrate to the new behavior later by removing these lines of code.

Note that in this article we refer to build.gradle.ktsthe file , but if you're using Groovy, the same changes apply to build.gradlethe file. Let's take a look at the changes.

Declare namespace attributes using the DSL

namespaceThe DSL attribute represents the Kotlin or Java package name of the generated Rand BuildConfigclasses and replaces the previously defined packageattribute . To move to namespace configuration, add namespacethe DSL build.gradle.ktsto android {}the block in the module-level file and remove packagethe attribute .

// Android manifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
       xmlns:tools="http://schemas.android.com/tools"
-       package="com.example.app"
       >

// 模块 build.gradle.kts
android {
+    namespace = "com.example.app"
    compileSdk = 33
    ...
}
复制代码

The Android Studio AGP upgrade assistant will help you with the migration by moving packages from packageproperties in the Android manifest to properties in the build file .namespace

1_aGl-XX_Pzk6O3x8iQX2kpA.webp

To understand why we made this change, let's look at the previous behavior.

Previously, packageproperties were used for both settings applicationIdand resource namespaces, unnecessarily coupling these two largely unrelated concepts.

通过禁止在清单文件中设置 package 名称并引入 namespace 属性,我们将用于您app标识的 applicationId 从资源命名空间中分离出来。这阐明了命名空间值的来源,并让您在不影响您的 applicationId 的情况下重构app的代码和资源

1_oy9XphDn18TVZkCMs-2KUg.webp

验证 R 类在默认情况下对于库模块是不可传递的

库模块的 R 类现在默认是不可传递的,这意味着每个 R 类只包含库模块本身声明的资源,而不是来自其依赖项的资源。 这意味着您在引用资源时必须使用完全限定的命名空间调用。

gradle.properties 文件中的 nonTransitiveRClass 标志控制着 R 类的行为。从 AGP 8.0 开始,未指定时为 true,因此成为默认值。

-val foo = R.drawable.android_ext_lib_2
+val foo = com.example.extra_lib_2.R.drawable.android_ext_lib_2

// gradle.properties
-android.nonTransitiveRClass=true
复制代码

要获得使用 Android Studio 更新您的 R 类调用的帮助,请转至 Refactor > Migrate to Non-Transitive R Classes。此重构操作将所有 R 调用转换为完全限定的 R 调用,并将在 gradle.properties 文件中设置 android.nonTransitiveRClass = true(如果标志设置为 false)。

1_UZ8qotL7zi_dHDTcSDKjEQ.webp

R 类是生成的类,可将您的资源名称映射到代码中的 ID。在 Android Studio Bumblebee/AGP 7.1 之前,R 类是可传递的。这意味着构建不仅为库R类生成资源 ID,而且还为库所依赖的所有模块生成资源 ID。这一代导致更大的可执行文件大小和更长的构建时间。

1_ZYbmlhTw-HFplcaiQgRVCA.webp

在非传递行为中,每个库模块 R 类仅包含模块本身声明的资源,从而减少了该模块的R 类的大小。

1_eDKdzO7RTLcnO2F-lY7sKg.webp

仅对需要的模块启用 BuildConfig

如果您从模块代码调用 BuildConfig 类,则需要在模块的 build.gradle.kts 文件的 android {} 块中启用 buildConfig 。否则,不再自动生成 BuildConfig 文件。

// 模块 build.gradle.kts
android {
  buildFeatures {
+    buildConfig = true
  }
}
复制代码

BuildConfig 文件是一个 Java 文件,其中包含有关您当前构建的静态信息,例如 namespace 名称、flavor 名称、debug 标志等。 以前 AGP 总是为所有 Android 模块生成 BuildConfig 文件。如果您开发一个多模块 app,您最终可能会得到大量 AGP 需要处理的 BuildConfig 文件,这会影响您的构建速度。但是,大多数模块不需要来自 BuildConfig 类的任何信息。

此外,BuildConfig 是一个 Java 文件。 假设您的 app 是使用 Kotlin 编写的,在同一模块中混合使用 Java 和 Kotlin 会进一步影响构建性能。为了缓解这种情况,我们在 gradle.properties 中引入了 android.enableBuildConfigAsBytecode 标志集。当 android.enableBuildConfigAsBytecode=true 时,BuildConfig 文件不是生成为 Java 文件,而是生成为编译文件。这避免了 Java 编译步骤!

// gradle.properties
+android.defaults.buildfeatures.buildconfig=true
+android.enableBuildConfigAsBytecode=true
复制代码

如果您需要所有模块的旧行为,请在您的 gradle.properties 文件中设置 android.defaults.buildfeatures.buildconfig=true

为需要的模块启用 AIDL 和 RenderScript

BuildConfigSimilar to , AIDLand are off by RenderScriptdefault . To enable them for a specific module, build.gradle.ktsset the andandroid {} options in the module's file's block to :aidlrenderScripttrue

// 模块 build.gradle.kts
android {
  buildFeatures {
+   aidl = true
+   renderScript = true 
  }
}
复制代码

You can use a similar approach to re-enable AIDL or RenderScript for modules that require it or for your entire project, but note that RenderScript is deprecated in Android 12 , so you should migrate from it .

// 在 gradle.properties 为整个项目设置 AIDL 和 RenderScript
+android.defaults.buildfeatures.aidl=true
+android.defaults.buildfeatures.renderScript=true
复制代码

Default R8 full mode

One final behavior change: R8 is now in full mode by default, which reduces app size and improves performance. You don't need to update anything for this change, but if you experience build or runtime failures, you should double check that your keep rules are configured correctly. See Minify .

epilogue

Collectively, these are five ways to prepare your app builds for the Flamingo version of Android Studio using AGP 8.0. If you develop plugins, please read our blog post about plugin changes. If you want to learn more about the build changes, watch this video and the AGP release notes .

Code snippet license:

Copyright 2023 Google LLC.
SPDX-License-Identifier: Apache-2.0
复制代码

Guess you like

Origin juejin.im/post/7223767530980671525