argeting S+

argeting S+ ,第1张

偷懒的解决方案:

在app目录下的 build.gradle 中添加以下内容

//下面两行添加上
import groovy.xml.XmlUtil
import kotlin.text.Charsets

android {
    compileSdkVersion 1.0
    buildToolsVersion 31.0.1
    
    //从这里开始拷贝,如果缺了反}括弧的自行补上即可
    android {
        applicationVariants.all { variant ->
            variant.outputs.all { output ->
                output.processResources.doFirst { pm ->
                    //适配安卓12,手动把三方sdk添加了 intent-filter ,但是缺少 android:exported的情况
                    String manifestPath = output.processResources.manifestFile
                    println "=====Adapt Android 12. modify manifestPath=====$manifestPath"
                    def manifestFile = file(manifestPath)
                    def xml = new XmlParser().parseText(manifestFile.getText())
                    def childNode = xml.children()
                    String keyStaff = "{http://schemas.android.com/apk/res/android}exported"
                    String addAttributeKey = "android:exported"
                    String matchKey = "intent-filter"
                    def manifestRootName = "application"
                    childNode.each {
                        if (it instanceof Node) {
                            def applicationNode = (Node) it
                            if (applicationNode.name() == manifestRootName) {
                                applicationNode.each { aNode ->
                                    //查看当前节点下面有没有intent-filter
                                    boolean hasFilterNode = false
                                    boolean hasExportAttribute = false
                                    def putKey = keyStaff
                                    if (aNode.children() != null) {
                                        aNode.children().each {
                                            def aCNode = (Node) it
                                            if (aCNode.name().toString() == matchKey) {
                                                hasFilterNode = true
                                                return
                                            }
                                        }
                                    }
                                    Map attribute = aNode.attributes()
                                    if (attribute != null) {
                                        attribute.keySet().each {
                                            if (it.toString() == keyStaff) {
                                                putKey = it
                                                hasExportAttribute = true
                                            }
                                            return
                                        }
                                    }
                                    //如果存在filter标签,同时没有配置exported,则添加
                                    if (hasFilterNode && !hasExportAttribute) {
                                        //没有的默认是false
                                        aNode.attributes().put(addAttributeKey, false)
                                        println "=====modify childNode xml=====$aNode"
                                    }
                                }
                            }
                        }
                    }
                    def serialize = XmlUtil.serialize(xml.clone())
                    manifestFile.write(serialize, Charsets.UTF_8.name())
                    println "=====write manifest content complete====="
                }
            }
        }
    }
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/871262.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-13
下一篇 2022-05-13

发表评论

登录后才能评论

评论列表(0条)

保存