Android Studio编译动态替换清单文件AndroidManifest.xml内容(找不到AndroidManifest路径问题解决)
4.+
applicationVariants.all { variant ->
variant.outputs.all { output ->
output.processManifest.doLast {
def outputDir = multiApkManifestOutputDirectory.asFile.get()
String manifestPath = "$outputDir/AndroidManifest.xml"
def manifestContent = file(manifestPath).getText()
manifestContent = manifestContent.replaceFirst('替换原文','替换内容')
file(manifestPath).write(manifestContent)
}
}
applicationVariants.all { variant ->
//参数配置
variant.outputs[0].processManifest.doLast {
def manifestFile = "${manifestOutputDirectory}/AndroidManifest.xml"
def updatedContent = new File(manifestFile).getText('UTF-8')
.replaceAll("UMENG_APPKEY_VALUE", "-") //友盟appkey
.replaceAll("QQ_APPID_VALUE", "-") //QQappId
.replaceAll("QQ_APPKEY_VALUE", "-") //QQappkey
.replaceAll("WX_APPID_VALUE", "-") //微信appId
.replaceAll("WX_APPKEY_VALUE", "-") //微信appkey
new File(manifestFile).write(updatedContent, 'UTF-8')
}
}
习惯性的模块里面的很多东西能在gradle配置的我都喜欢在gradle上面配置,今天做项目的时候获取清单文件找不到路径,尴尬,之后去看了一下编译目录,发现之前的目录都没有了,于是搜索了一番AndroidManifest.xml到底在哪里,我现在的环境是3.2的,找到在下图的位置
以前的:
直接写死测试了一下,替换AndroidManifest.xml的参数值,最后发现就是这个清单文件,不过直接写死感觉很不灵活,后来去谷歌了一番,在官方里面的已知问题里面找到了一个更简单的获取参数 ${manifestOutputDirectory}
详情请看官方 https://developer.android.com/studio/known-issues
1. 3.2版本
def manifestFile = "${manifestOutputDirectory}/AndroidManifest.xml"
来源:https://developer.android.com/studio/known-issues
2. 3.x 版本
def manifestFile = "${buildDir}/intermediates/manifests/full/${variant.dirName}/AndroidManifest.xml"
3. 3.0之前 版本
def manifestFile = "${buildDir}/intermediates/manifests/full/${variant.outputs[0].dirName/AndroidManifest.xml"