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

风流意气都作罢 提交于 2019-12-12 07:45:44

问题


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?


回答1:


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
        }
    }
}



回答2:


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))


来源:https://stackoverflow.com/questions/45510504/how-can-variant-outputs-be-manipulated-using-the-android-gradle-plugin-3-0-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!