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
The picture's content is what I'm need,but the generated apk file's content is this:
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