在使用几个不同的变体和唯一 ID 时遇到了同样的问题,最终在 Gradle 构建应用程序时替换了占位符键,有点像这样:
摇篮 3+
android.applicationVariants.all { variant ->
variant.outputs.each { output ->
output.processManifest.doLast {
File manifestFile = file("$manifestOutputDirectory/AndroidManifest.xml")
replaceInFile(manifestFile, 'P_AUTHORITY', variant.applicationId)
}
}
}
def replaceInFile(file, fromString, toString) {
def updatedContent = file.getText('UTF-8')
.replaceAll(fromString, toString)
file.write(updatedContent, 'UTF-8')
}
梯度 < 3
android.applicationVariants.all { variant ->
variant.outputs.each { output ->
output.processManifest.doLast{
replaceInManifest(output, 'P_AUTHORITY', variant.applicationId)
}
}
}
def replaceInManifest(output, fromString, toString) {
def manifestOutFile = output.processManifest.manifestOutputFile
def updatedContent = manifestOutFile.getText('UTF-8').replaceAll(fromString, toString)
manifestOutFile.write(updatedContent, 'UTF-8')
}
然后在清单中:
<provider
android:name=".core.MyContentProvider"
android:authorities="P_AUTHORITY"
android:exported="false"/>
好几次都派上用场了