2

I want to set an attribue(android:configChanges="screenSize|smallestScreenSize|screenLayout") for all activities in my manifest file with gradle file,and what I'm trying like follow: I had add some code snippet in my app module's build.gradle file :

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)
        }

    }
}

After execute command gradlew assembleDebug it generates a file and an apk.File content like this picture

AndroidManifest.xml

The picture's content is what I'm need,but the generated apk file's content is this: manifest in apk file It lose attributes which added in gradle task.I want to know how to make it works,please help.

My environment

  • OS:Windows 10 64 bit
  • Android Studio Version: 3.5.2
  • com.android.tools.build:gradle:3.5.2
aolphn
  • 2,950
  • 2
  • 21
  • 30

2 Answers2

1

After several days,I addressed this issue by my self,the correct way:

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")
                }
            }
        }
    }
}

Add this code snippet into end of app/build.gradle.


Well,the above way is only for the situation which every element of activity is not include attribute android:configChanges or will got another question,I post it here.


Well again,after several try,I fixed this problem by myself and I post correct answer in my another issue.

aolphn
  • 2,950
  • 2
  • 21
  • 30
0

You can solve by using manifest placeholders:

  • 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"]}

Sanjay Bhalani
  • 2,424
  • 18
  • 44
  • I want to change all activities include aar's activities.And I want to know why my gradle task does't work. – aolphn Jan 04 '20 at 04:36
  • @Aolphn you can use placeholders to all activities include aar's activities too. – Sanjay Bhalani Jan 04 '20 at 04:53
  • how to add it into third aar without modify aar source code? – aolphn Jan 04 '20 at 05:53
  • Please refer https://stackoverflow.com/questions/32955764/how-to-keep-placeholders-in-an-aars-manifest/37264618#37264618 – Sanjay Bhalani Jan 04 '20 at 06:04
  • I checked your link,that's not what I'm need,I can't modify all aar dependencies belong third party and my original intention is changing it by groovy script.Any way ,thanks for your help. – aolphn Mar 11 '20 at 08:52