在自定义插件中修改AndroidManifest.xml

时间:2016-03-25 13:36:33

标签: android android-studio gradle android-manifest

在gradle中,我想在我的自定义插件中修改android:minSdkVersion

<uses-sdk
        android:minSdkVersion="19"
        android:targetSdkVersion="19" />

我想完成如下。

void apply(Project project) {
    File manifest = project.file("${project.buildDir}/intermediates/manifests/full/${projectFlavorName}/${buildTypeName}/AndroidManifest.xml")
    def manifestXml = new XmlParser().parse(manifest)
    manifestXml.uses-sdk.attribute("android:minSdkVersion") = 20
}

怎么做?

1 个答案:

答案 0 :(得分:0)

显示如下:

File Manifest = getManifest()
        def manifestXml = new XmlParser().parse(Manifest)
        manifestXml."uses-sdk".replaceNode{
            'uses-sdk'(' android:minSdkVersion="12"\n' +
                    '    android:targetSdkVersion="14"')
        }
        def fw=new FileWriter(Manifest.toString())
        new XmlNodePrinter(new PrintWriter(fw)).print(manifestXml)