Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can Variant Outputs be manipulated using the Android Gradle Plugin 3.0.0+?

The latest version (3.0.0) of the Android Plugin for Gradle has broken its API for manipulating Variant Outputs. This API was used for manipulating files creating during builds (such as AndroidManifest.xml), and has been removed to improve configuration times.

What new APIs are available to manipulate Variant Outputs, and how do they differ to the 2.X APIs?

like image 669
fractalwrench Avatar asked Aug 04 '17 15:08

fractalwrench


People also ask

What is variant in Gradle?

Build variants are the result of Gradle using a specific set of rules to combine settings, code, and resources configured in your build types and product flavors. Although you do not configure build variants directly, you do configure the build types and product flavors that form them.

What is Android plugin for Gradle?

The Android Gradle plugin (AGP) is the official build system for Android applications. It includes support for compiling many different types of sources and linking them together into an application that you can run on a physical Android device or an emulator.

What does Gradle plugin do?

Basically, it runs the process that bundles all the resources (images, layout XML files, string resources, etc), your Kotlin or Java source code, any libraries added to your project. Note: It is important to understand that Gradle in itself is not the compiler, linker or bundler itself.

How does Gradle work in Android Studio?

Android Studio uses Gradle, an advanced build toolkit, to automate and manage the build process, while allowing you to define flexible custom build configurations. Each build configuration can define its own set of code and resources, while reusing the parts common to all versions of your app.


1 Answers

The changes to outputFiles has now been documented on the Android Developer site.

Essentially, instead of accessing the outputFile directly from the gradle API, the recommendation is to access the directory containing the file instead. The snippet below demonstrates this with a manifest file, but can be applied to other outputFiles as well.

android.applicationVariants.all { variant ->
    variant.outputs.all { output ->
        output.processManifest.doLast {

            String manifestPath = "$manifestOutputDirectory/AndroidManifest.xml"
            def manifestContent = file(manifestPath).getText()

            // Manipulate the file as needed
        }
    }
}
like image 79
fractalwrench Avatar answered Oct 23 '22 12:10

fractalwrench