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

2

12

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?

Stet answered 4/8, 2017 at 15:25 Comment(5)
What action are you specifically interested in?Garret
I'm specifically interested in how to manipulate outputFiles, as it is no longer guaranteed that these will be available to variant tasks during the configuration phase. If there have been any other substantial changes to this API, then I feel this deserves a canonical answerStet
I can't come up with a canonical answer, would you be happy with a working example? I was about to set my gradle 4 android project to produce an output apk with a custom name, is that what you need?Motion
No, as far as I'm aware it's still possible to rename APKs using the same syntax as in the 2.x plugin. This question is focused on the manipulation of ouputFiles (e.g. AndroidManifest, mapping.txt)Stet
Related Google IssueTracker ticket: issuetracker.google.com/issues/64747519Stet
S
3

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
        }
    }
}
Stet answered 4/9, 2017 at 13:9 Comment(2)
this is not working any more (com.android.tools.build:gradle:3.4.0) , pls updateEpisode
above 3.3 , you should use this code:https://mcmap.net/q/1012391/-39-manifestoutputdirectory-39-with-gradle-plugin-3-3-0-not-returning-resultLudewig
S
0

Looks like they've changed this interface again. (android gradle plugin 3.3+ or Gradle 5.4+)

I'm using the following to retrieve the manifestPath:

def manifestPath = "${manifestOutputDirectory.get().asFile}/AndroidManifest.xml"

Figured it out from here

Was getting a java.io.FileNotFoundException with the following in the path

property(interface org.gradle.api.file.Directory, fixed(class org.gradle.api.internal.file.DefaultFilePropertyFactory$FixedDirectory, /Users/me/app/build/intermediates/merged_manifests/debug))
Subsidiary answered 26/4, 2019 at 15:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.