简体   繁体   中英

Code injecting into merged-manifest with Gradle plugin of android project

I tried to code injecting into "merged-manifest" file(app/build/intermediates/merged_manifests/../AndroidManifest.xml) because I have a library and I want to not appear for the developer.

I wrote a Gradle script and I execute this script after each task(just for testing).

Although, I can see my code is injected correctly but in the apk there is not any change.

Part of my script:

gradle.taskGraph.afterTask {
task ->
        def manifestPath = "${buildDir.path}/intermediates/merged_manifests/${getCurrentFlavorAndBuildType()}/AndroidManifest.xml"
        def manifestFile = new File(manifestPath)
        if (manifestFile.exists()) {
            def manifestContent = manifestFile.getText()
            manifestContent = manifestContent.replaceAll("com.example", android.defaultConfig.applicationId)
            def fos = new FileOutputStream(manifestFile.getPath())
            fos.write(manifestContent.getBytes())
            fos.flush()
            fos.close()
            println("rootProject:$manifestPath")
        }

}

Finally, My script is working well:

applicationVariants.all { variant ->
        variant.outputs.each { output ->
            output.processManifest.doLast {
                def manifestPath = "${buildDir.path}/intermediates/merged_manifests/${getCurrentFlavorAndBuildType()}/AndroidManifest.xml"
                def manifestFile = new File(manifestPath)
                if (manifestFile.exists()) {
                    def manifestContent = manifestFile.getText()
                    manifestContent = manifestContent.replaceAll("com.example", android.defaultConfig.applicationId)
                    def fos = new FileOutputStream(manifestFile.getPath())
                    fos.write(manifestContent.getBytes())
                    fos.flush()
                    fos.close()
                    println("rootProject:$manifestPath")
                }

            }
        }
    }

I should execute my script into these blocks:

applicationVariants.all { variant ->
        variant.outputs.each { output ->
            output.processManifest.doLast {
         // something
    }
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM