统一设置四大组件export属性
Gradle插件版本
classpath 'com.android.tools.build:gradle:4.1.0'
gradle wrapper
distributionUrl=https://downloads.gradle-dn.com/distributions/gradle-7.4-all.zip
在app的build.gradle中添加
android{
applicationVariants.all { variant ->
variant.outputs.each { output ->
//println("=============== ${variant.getBuildType().name.toUpperCase()} ===============")
//println("=============== ${variant.getFlavorName()} ===============")
def vn
if (variant.getFlavorName() != null && variant.getFlavorName() != "") {
vn = variant.name;
} else {
if (variant.getBuildType().name == "release") {
vn = "Release"
} else {
vn = "Debug"
}
}
def taskName = "process${vn}MainManifest";
try {
println("=============== taskName ${taskName} ===============")
project.getTasks().getByName(taskName)
} catch (Exception e) {
return
}
///你的自定义名字
project.getTasks().getByName(taskName).doFirst {
//def method = it.getClass().getMethods()
it.getManifests().getFiles().each {
if (it.exists() && it.canRead()) {
def manifestFile = it
def exportedTag = "android:exported"
def nameTag = "android:name"
///这里第二个参数是 false ,所以 namespace 是展开的,所以下面不能用 androidSpace,而是用 nameTag
def xml = new XmlParser(false, false).parse(manifestFile)
if (xml.application != null && xml.application.size() > 0) {
def nodes = xml.application[0].'*'.findAll {
//挑选要修改的节点,没有指定的 exported 的才需要增加
//如果 exportedTag 拿不到可以尝试 it.attribute(androidSpace.exported)
(it.name() == 'activity' || it.name() == 'receiver' || it.name() == 'service') && it.attribute(exportedTag) == null
}
if (nodes.application != null && nodes.application.size() > 0) {
nodes.each {
def isMain = false
def t = it
it.each {
it.each {
if (it.name() == "action") {
//如果 nameTag 拿不到可以尝试 it.attribute(androidSpace.name)
if (it.attributes().get(nameTag) == "android.intent.action.MAIN") {
isMain = true
}
}
}
}
it.attributes().put(exportedTag, "${isMain}")
}
PrintWriter pw = new PrintWriter(manifestFile)
pw.write(groovy.xml.XmlUtil.serialize(xml))
pw.close()
}
}
}
}
}
}
}
}
统一处理PendingIntent报错情况
android{
defaultConfig {
configurations.all {
// resolutionStrategy { force 'androidx.work:work-runtime:2.6.0' }
resolutionStrategy { force 'androidx.work:work-runtime:2.7.1' }
resolutionStrategy { force 'androidx.work:work-runtime-ktx:2.7.1' }
}
}