This minor update supports compatibility with new default settings and features for package visibility in Android In previous versions of Android, it was possible to view a list of all apps installed on a device.
Starting with Android 11 API level 30 , by default apps have access to only a filtered list of installed packages. For more information on this new feature, see Package visibility in Android The Build Analyzer window helps you understand and diagnose issues with your build process, such as disabled optimizations and improperly configured tasks.
This feature is available when you use Android Studio 4. You can open the Build Analyzer window from Android Studio as follows:. The Build Analyzer window organizes possible build issues in a tree on the left.
You can inspect and click on each issue to investigate its details in the panel on the right. When Android Studio analyzes your build, it computes the set of tasks that determined the build's duration and provides a visualization to help you understand the impact of each of these tasks. You can also get details on warnings by expanding the Warnings node.
In Android Studio 4. This means that you can now include standard language APIs that were available only in recent Android releases such as java. The desugaring process rewrites your app's code to instead use this library at runtime. To enable support for these language APIs, include the following in your app module 's build. Note that you may also need to include the above code snippet in a library module 's build.
The library module's instrumented tests use these language APIs either directly or through the library module or its dependencies. You want to run lint on the library module in isolation. This is to help lint recognize valid usages of the language APIs and avoid reporting false warnings. When new features are added, they will be disabled, by default.
You can then use the buildFeatures block to enable only the features you want, and it helps you optimize the build performance for your project.
You can set the options for each module in the module-level build. Keep in mind, you can still use the buildFeatures block in the module-level build. In previous versions of the Android Gradle plugin, all feature modules could depend only on the app's base module. When using Android Gradle plugin 4. That is, a :video feature can depend on the :camera feature, which depends on the base module, as shown in the figure below.
Feature module :video depends on feature :camera , which depends on the base :app module. This means that when your app requests to download a feature module, the app also downloads other feature modules it depends on. For example, the :video module declares a dependency on :camera as follows:. When building your app using Android Gradle plugin 4. When uploading your app, the Play Console inspects this metadata to provide you with the following benefits:. The data is compressed, encrypted by a Google Play signing key, and stored in the signing block of your release app.
When you follow the configuration steps described below, Gradle automatically makes these native libraries available to use with your external native build system, such as CMake.
Note that Gradle only makes these libraries available to your build; you must still configure your build scripts to use them. Libraries are exported using the Prefab package format.
Each dependency can expose at most one Prefab package, which comprises one or more modules. A Prefab module is a single library, which could be either a shared, static, or header-only library. Typically, the package name matches the Maven artifact name and the module name matches the library name, but this is not always true. Because you need to know the package and module name of the libraries, you might need to consult the dependency's documentation to determine what those names are.
CMake ndk-build. This value will be set automatically by Gradle when CMake is invoked, so if your build system modifies this variable, be sure to append rather than assign to it. This command searches for config-file packages that match the given package name and version and exposes the targets it defines to be used in your build. For example, if your application defines libapp. When you build your project, your external native build system automatically links libapp.
For additional information, refer to the curl prefab sample. The behavior for app signing configurations in the signingConfig block has changed to the following:. These changes allow AGP to optimize builds by disabling the signing mechanism based on whether the user has explicitly enabled these flags. Prior to this release, it was possible for v1Signing to be disabled even when explicitly enabled, which could be confusing. Android Gradle plugin 3.
In Android Gradle plugin 4. So, to use the latest Android Gradle plugin, you need to migrate your instant app to support Android App Bundles. By migrating your instant apps, you can leverage the benefits of app bundles and simplify your app's modular design. The ability to separate annotation processing into a dedicated task has been removed. This option was used to maintain incremental Java compilation when non-incremental annotation processors are used in Java-only projects; it was enabled by setting android.
Instead, you should migrate to using incremental annotation processors to improve build performance. The Android Gradle plugin no longer checks for or includes annotation processors you declare on the compile classpath, and the annotationProcessorOptions. If you include annotation processors on the compile classpath, you might get the following error:.
To resolve this issue, you must include annotation processors in your build. To learn more, read Add annotation processors. Prior versions of the Android Gradle Plugin required that you explicitly package any prebuilt libraries used by your CMake external native build by using jniLibs.
With Android Gradle Plugin 4. External native build now automatically packages those libraries, so explicitly packaging the library with jniLibs results in a duplicate. To avoid the build error, move the prebuilt library to a location outside jniLibs or remove the jniLibs configuration from your build.
Changes in Android Gradle plugin 4. Gradle 5. View binding provides compile-time safety when referencing views in your code. You can now replace findViewById with the auto-generated binding class reference. To start using View binding, include the following in each module's build.
The Android Gradle plugin includes support for the Maven Publish Gradle plugin , which allows you to publish build artifacts to an Apache Maven repository.
The Android Gradle plugin creates a component for each build variant artifact in your app or library module that you can use to customize a publication to a Maven repository. To learn more, go to the page about how to use the Maven Publish plugin.
When building the debug version of your app, the plugin uses a new packaging tool, called zipflinger , to build your APK. This new tool should provide build speed improvements. If the new packaging tool doesn't work as you expect, please report a bug.
You can revert to using the old packaging tool by including the following in your gradle. Gradle can output a Chrome trace that contains timestamps for these compiler events so you can better understand the time required to build your project. To output this build attribution file, do the following:. Add the flag -Pandroid. The file is named profile- timestamp. When you build your app, the plugin now sets extractNativeLibs to "false" by default.
That is, your native libraries are page aligned and packaged uncompressed. While this results in a larger upload size, your users benefit from the following:. If you want the Android Gradle plugin to instead package compressed native libraries, include the following in your app's manifest:. If you download multiple versions of the NDK, the Android Gradle plugin now selects a default version to use in compiling your source code files.
Previously, the plugin selected the latest downloaded version of the NDK. Use the android. The Android Gradle plugin simplifies the compile classpath by generating only one R class for each library module in your project and sharing those R classes with other module dependencies.
This optimization should result in faster builds, but it requires that you keep the following in mind:. This behavior change should result in fewer Resource Not Found runtime exceptions and improved build speed. When compiling your app, D8 now respects when annotations apply a CLASS retention policy, and those annotations are no longer available at runtime.
Android Lint can take much longer to complete on some projects due to a regression in its parsing infrastructure, resulting in slower computation of inferred types for lambdas in certain code constructs. If your app defines custom permissions in its manifest, the Android Gradle plugin typically generates a Manifest.
The plugin packages this class with your app, so you can more easily reference those permissions at runtime. Generating the manifest class is broken in Android Gradle plugin 3. If you build your app with this version of the plugin, and it references the manifest class, you might see a ClassNotFoundException exception.
To resolve this issue, do one of the following:. Notably, improving project build speed was a main focus for this update. For information about these and other Project Marble updates, read the Android Developers blog post or the sections below. This minor update supports Android Studio 3. To see a list of noteable bug fixes, read the related post on the Release Updates blog. The Data Binding annotation processor supports incremental annotation processing if you set android.
This optimization results in improved incremental build performance. For a full list of optimized annotation processors, refer to the table of incremental annotation processors. Additionally, KAPT 1. When you enable unit tests to use Android resources, assets, and manifests by setting includeAndroidResources to true , the Android Gradle plugin generates a test config file containing absolute paths, which breaks cache relocatability.
You can instruct the plugin to instead generate the test config using relative paths, which allows the AndroidUnitTest task to be fully cacheable, by including the following in your gradle. When using Kotlin Gradle plugin 1. To resolve this issue , upgrade the plugin to version 1. New lint check dependency configurations: The behavior of lintChecks has changed and a new dependency configuration, lintPublish , has been introduced to give you more control over which lint checks are packaged in your Android libraries.
The following code sample uses both dependency configurations in a local Android library project. In general, packaging and signing tasks should see an overall build speed improvement. If you notice a performance regression related to these tasks, please report a bug. To make sure you can still build you instant app on future versions of the plugin, migrate your instant app to using the dynamic feature plugin , which also allows you to publish both your installed and instant app experiences from a single Android App Bundle.
R8 enabled by default: R8 integrates desugaring, shrinking, obfuscating, optimizing, and dexing all in one step—resulting in noticeable build performance improvements. R8 was introduced in Android Gradle plugin 3.
Now, with R8, desugaring, shrinking, obfuscating, optimizing, and dexing D8 are all completed in one step, as illustrated below. So, in this unlikely situation, you might need to add additional rules to keep that code in your build output.
The correct usage of unique package names are currently not enforced but will become more strict on later versions of the plugin. On Android Gradle plugin version 3. To learn more about setting a package name through the Android Gradle plugin, see Set the application ID. Gradle 4. New features Improved classpath synchronization: When resolving dependencies on your runtime and compile time classpaths, the Android Gradle plugin attempts to fix certain downstream version conflicts for dependencies that appear across multiple classpaths.
For example, if the runtime classpath includes Library A version 2. However, if the runtime classpath includes Library A version 1. To learn more, see Fix conflicts between classpaths. Improved incremental Java compilation when using annotation processors: This update decreases build time by improving support for incremental Java compilation when using annotation processors.
For projects not using Kapt Java-only projects : If the annotation processors you use all support incremental annotation processing , incremental Java compilation is enabled by default. To monitor incremental annotation processor adoption, watch Gradle issue If, however, one or more annotation processors do not support incremental builds, incremental Java compilation is not enabled. Instead, you can include the following flag in your gradle. When you include this flag, the Android Gradle plugin executes the annotation processors in a separate task and allows the Java compilation task to run incrementally.
Better debug info when using obsolete API: When the plugin detects that you're using an API that's no longer supported, it can now provide more-detailed information to help you determine where that API is being used. To see the additional info, you need to include the following in your project's gradle. You can also enable the flag by passing -Pandroid. Calling certain older methods in the Variants API, such as variant.
To make sure that your build is optimized for lazy task configuration, invoke new methods that instead return a TaskProvider object, such as variant. Faster R class generation for library projects: Previously, the Android Gradle plugin would generate an R. The plugin now generates a JAR containing your app's compiled R class directly, without first building intermediate R. This optimization may significantly improve build performance for projects that include many library subprojects and dependencies, and improve the indexing speed in Android Studio.
This optimization avoids the need for the device to make a copy of the library and thus reduces the on-disk size of your app. If you'd rather disable this optimization, add the following to your gradle.
Single-variant project sync : Syncing your project with your build configuration is an important step in letting Android Studio understand how your project is structured. However, this process can be time-consuming for large projects. If your project uses multiple build variants, you can now optimize project syncs by limiting them to only the variant you have currently selected.
You need to use Android Studio 3. When you meet these requirements, the IDE prompts you to enable this optimization when you sync your project. The optimization is also enabled by default on new projects. When enabling the optimization for projects with Kotlin content, Gradle sync falls back to using full variants internally. To learn more, read Auto-download missing packages with Gradle. The Android Gradle plugin now uses version You no longer have to build, sign, and manage multiple APKs, and users get smaller downloads that are optimized for their device.
To learn more, read About Android App Bundles. Support for improved incremental build speeds when using annotation processors: The AnnotationProcessorOptions DSL now extends CommandLineArgumentProvider , which enables either you or the annotation processor author to annotate arguments for the processor using incremental build property type annotations. Using these annotations improves the correctness and performance of incremental and cached clean builds.
To learn more, read Pass arguments to annotation processors. Using this migration tool also sets the following flags to true in your gradle. To learn more, read the AndroidX overview. New code shrinker, R8: R8 is a new tool for code shrinking and obfuscation that replaces ProGuard.
To use AAPT2, make sure that you have the google dependency in your build. Native multidex is now enabled by default.
Previous versions of Android Studio enabled native multidex when deploying the debug version of an app to a device running Android API level 21 or higher. The plugin now enforces a minimum version of the protobuf plugin 0. The feature module plugin, com. For example, if your feature module name includes dashes, you get a build error.
This behavior matches that of the dynamic feature plugin. DEX compilation is the process of transforming. Compared to the previous compiler, called DX, D8 compiles faster and outputs smaller DEX files, all while having the same or better app runtime performance.
D8 shouldn't change your day-to-day app development workflow. However, if you experience any issues related to the new compiler, please report a bug. You can temporarily disable D8 and use DX by including the following in your project's gradle.
For projects that use Java 8 language features , incremental desugaring is enabled by default. You can disable it by specifying the following in your project's gradle. Preview users: If you're already using a preview version of D8, note that it now compiles against libraries included in the SDK build tools —not the JDK. Because local modules can't depend on your app's test APK, adding dependencies to your instrumented tests using the androidTestApi configuration, instead of androidTestImplementation , causes Gradle to issue the following warning:.
Some of these changes break existing builds. So, you should consider the effort of migrating your project before using the new plugin.
Otherwise, you can set it up yourself by downloading the latest Gradle distribution and integrating it with your operating system as an environment variable. There are two packages available for manual setup, whether you are looking only for the binaries only or are interested in the documentation and sources as well.
After setting up Gradle as an environment variable, it can be called from a command-line interface to view the variable commands. For example, you can ask the program to not rebuild project dependencies -a, -no-rebuild , set the system property of the JVM -D, --system-prop, such as -Dmyprop-myvalue , specify an initialization script -I, --init-script and point out to a task that will be excluded from execution -x, --exclude-task.
Additionally, it's possible to enable continuous building mode so that Gradle doesn't exit and re-executes tasks as soon as the file input changes -t, -continuous , stop the Gradle Daemon if it's running --stop , set the log level to worn -w, -warn , as well as create a build scan, whether the tool shows a warning if the build scan plugin wasn't applied --scan.
Taking into account the rich features provided by this app builder, Gradle is definitely worth looking into. We haven't had any trouble getting it up and running on Windows 10 in our tests using the latest JRE.
0コメント