'manifestOutputDirectory' with gradle-plug

2019-08-20 09:02发布

问题:

Trying to get manifest output directory, with below code in app build.gradle

def manifestOutDir = manifestOutputDirectory

But instead of returning directory path, when I run build and print it, it shows this string

property(interface org.gradle.api.file.Directory, transform(property(interface org.gradle.api.file.Directory,
...

I am using gradle-plugin-3.3.0 with Android Studio 3.3

Is it not working with gradle-android-plugin-3.3.0 or removed? Anything else that I am missing?

回答1:

This post [https://stackoverflow.com/a/46037817/4181904] indicates that this feature is broken.

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:

In high gradle version maybe above 3.3 , the Library's AndroidManifest.xml is moved from package .../merged_manifests to .../library_manifest. So if you use this code to get the path will not work. More detail you could see Android Plugin for Gradle 3.0.0

 def manifestPath = manifestOutputDirectory.asFile.get()

NOW, You should just use this to get the AndroidManifest.xml:

 def manifest = manifestOutputFile.asFile.get()

Have fun