使用AndroidManifest.xml中的gradle任务更改活动属性

问题描述 投票:1回答:2

我想为我的清单文件和gradle文件中的所有活动设置一个属性(android:configChanges =“ screenSize | smallestScreenSize | screenLayout”),而我尝试执行的操作如下:我在我的应用模块的build.gradle文件中添加了一些代码片段:

android.applicationVariants.all { variant ->
    print("check variant:${variant.getClass()}\n")
    variant.outputs.each { output ->
        print("check output all task:${output.getName()}\n")
        def processorTask = output.processManifestProvider.getOrNull()
        print("check processorTask:${processorTask.getName()}\n")
        //ProcessApplicationManifest
        //https://android.googlesource.com/platform/tools/base/+/studio-master-dev/build-system/gradle-core/src/main/java/com/android/build/gradle/tasks/ProcessApplicationManifest.java
        processorTask.doLast {task ->
            def directory = task.getBundleManifestOutputDirectory()
            def srcManifestFile = "$directory${File.separator}AndroidManifest.xml"
            print("check manifest file:$srcManifestFile\n")
            def manifestContent = new File(srcManifestFile).getText()
            def xml = new XmlParser(false, false).parseText(manifestContent)
            print("check manifest application:${xml.application.size()}")
            xml.application[0].activity.forEach{
                it.attributes().put('android:configChanges', 'screenSize|smallestScreenSize|screenLayout')
            }
            def serializeContent = groovy.xml.XmlUtil.serialize(xml)
            def targetFile = new File(srcManifestFile)
            targetFile.write(serializeContent)
        }

    }
}

执行命令gradlew assembleDebug后,它会生成一个文件和一个apk。文件内容类似于此图

AndroidManifest.xml

图片的内容是我所需要的,但是生成的apk文件的内容是这样的:manifest in apk file它失去了添加在gradle任务中的属性。我想知道如何使其工作,请提供帮助。

我的环境

  • OS:Windows 10 64位
  • Android Studio版本:3.5.2
  • com.android.tools.build:gradle:3.5.2
android gradle android-manifest
2个回答
0
投票

您可以使用清单占位符解决:

  • Manifest

    <activity
        android:name=".YourActivity"
        android:configChanges="${configChanges}">          
         <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    
  • Gradle:

    defaultConfig {manifestPlaceholders = [ configChanges:"orientation"]}


0
投票

几天后,我以正确的方式自行解决了此问题:

project.afterEvaluate {
    def variants = null
    try {
        variants = android.applicationVariants
    } catch (Throwable t) {
        try {
            variants = libraryVariants
        } catch (Throwable tt) {}
    }
    if (variants != null) {
        variants.all { variant ->
            variant.outputs.each { output ->
                def task = output.processManifestProvider.get()
                if (task == null) {
                    return
                }
                task.doLast {
                    def manifestFile = new File(task.manifestOutputDirectory.get().getAsFile(),"AndroidManifest.xml")
                    if (manifestFile == null || !manifestFile.exists()) {
                        return
                    }
                    def parser = new XmlParser(false, true)
                    def manifest = parser.parse(manifestFile)
                    def app = manifest.'application'[0]
                    app.'activity'.each{act->
                        String value = act.attributes()['android:configChanges']
                        if (value == null) {
                            value = "keyboardHidden|orientation|screenSize"
                        } else {
                            String[] valueSplit = value.split("\\|")
                            if (!valueSplit.contains("keyboardHidden")) {
                                value+="|keyboardHidden"
                            }
                            if (!valueSplit.contains("orientation")) {
                                value+="|orientation"
                            }
                            if (!valueSplit.contains("screenSize")) {
                                value+="|screenSize"
                            }
                        }
                        act.attributes()["android:configChanges"] = value
                    }
                    manifestFile.setText(XmlUtil.serialize(manifest), "utf-8")
                }
            }
        }
    }
}

将此代码段添加到app/build.gradle的末尾。

嗯,以上方法仅适用于活动的每个要素均不包含属性android:configChanges或会出现另一个问题的情况,我将其发布here

© www.soinside.com 2019 - 2024. All rights reserved.