使用Gradle任务在AndroidManifest.xml中更改活动属性。

huangapple go评论100阅读模式
英文:

Use gradle task change activity attribute in AndroidManifest.xml

问题

我想在我的清单文件中为所有活动设置一个属性(android:configChanges="screenSize|smallestScreenSize|screenLayout"),并且我尝试的方式如下:

我已经在我的应用程序模块的build.gradle文件中添加了一些代码片段:

  1. android.applicationVariants.all { variant ->
  2. print("check variant:${variant.getClass()}\n")
  3. variant.outputs.each { output ->
  4. print("check output all task:${output.getName()}\n")
  5. def processorTask = output.processManifestProvider.getOrNull()
  6. print("check processorTask:${processorTask.getName()}\n")
  7. // ProcessApplicationManifest
  8. // https://android.googlesource.com/platform/tools/base/+/studio-master-dev/build-system/gradle-core/src/main/java/com/android/build/gradle/tasks/ProcessApplicationManifest.java
  9. processorTask.doLast { task ->
  10. def directory = task.getBundleManifestOutputDirectory()
  11. def srcManifestFile = "$directory${File.separator}AndroidManifest.xml"
  12. print("check manifest file:$srcManifestFile\n")
  13. def manifestContent = new File(srcManifestFile).getText()
  14. def xml = new XmlParser(false, false).parseText(manifestContent)
  15. print("check manifest application:${xml.application.size()}")
  16. xml.application[0].activity.forEach{
  17. it.attributes().put('android:configChanges', 'screenSize|smallestScreenSize|screenLayout')
  18. }
  19. def serializeContent = groovy.xml.XmlUtil.serialize(xml)
  20. def targetFile = new File(srcManifestFile)
  21. targetFile.write(serializeContent)
  22. }
  23. }
  24. }

执行命令gradlew assembleDebug后,它会生成一个文件和一个apk文件。文件内容如下所示:

使用Gradle任务在AndroidManifest.xml中更改活动属性。

图片内容是我需要的,但生成的apk文件内容如下所示:

使用Gradle任务在AndroidManifest.xml中更改活动属性。

它丢失了在gradle任务中添加的属性。我想知道如何使它工作,请帮忙。

我的环境

  • 操作系统:Windows 10 64位

  • Android Studio版本:3.5.2

  • com.android.tools.build:gradle:3.5.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 :

  1. android.applicationVariants.all { variant ->
  2. print("check variant:${variant.getClass()}\n")
  3. variant.outputs.each { output ->
  4. print("check output all task:${output.getName()}\n")
  5. def processorTask = output.processManifestProvider.getOrNull()
  6. print("check processorTask:${processorTask.getName()}\n")
  7. //ProcessApplicationManifest
  8. //https://android.googlesource.com/platform/tools/base/+/studio-master-dev/build-system/gradle-core/src/main/java/com/android/build/gradle/tasks/ProcessApplicationManifest.java
  9. processorTask.doLast {task ->
  10. def directory = task.getBundleManifestOutputDirectory()
  11. def srcManifestFile = "$directory${File.separator}AndroidManifest.xml"
  12. print("check manifest file:$srcManifestFile\n")
  13. def manifestContent = new File(srcManifestFile).getText()
  14. def xml = new XmlParser(false, false).parseText(manifestContent)
  15. print("check manifest application:${xml.application.size()}")
  16. xml.application[0].activity.forEach{
  17. it.attributes().put('android:configChanges', 'screenSize|smallestScreenSize|screenLayout')
  18. }
  19. def serializeContent = groovy.xml.XmlUtil.serialize(xml)
  20. def targetFile = new File(srcManifestFile)
  21. targetFile.write(serializeContent)
  22. }
  23. }
  24. }

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

使用Gradle任务在AndroidManifest.xml中更改活动属性。

The picture's content is what I'm need,but the generated apk file's content is this:
使用Gradle任务在AndroidManifest.xml中更改活动属性。
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

答案1

得分: 1

以下是已翻译的代码部分:

  1. project.afterEvaluate {
  2. def variants = null
  3. try {
  4. variants = android.applicationVariants
  5. } catch (Throwable t) {
  6. try {
  7. variants = libraryVariants
  8. } catch (Throwable tt) {}
  9. }
  10. if (variants != null) {
  11. variants.all { variant ->
  12. variant.outputs.each { output ->
  13. def task = output.processManifestProvider.get()
  14. if (task == null) {
  15. return
  16. }
  17. task.doLast {
  18. def manifestFile = new File(task.manifestOutputDirectory.get().getAsFile(),"AndroidManifest.xml")
  19. if (manifestFile == null || !manifestFile.exists()) {
  20. return
  21. }
  22. def parser = new XmlParser(false, true)
  23. def manifest = parser.parse(manifestFile)
  24. def app = manifest.'application'[0]
  25. app.'activity'.each{act->
  26. String value = act.attributes()['android:configChanges']
  27. if (value == null) {
  28. value = "keyboardHidden|orientation|screenSize"
  29. } else {
  30. String[] valueSplit = value.split("\\|")
  31. if (!valueSplit.contains("keyboardHidden")) {
  32. value+="|keyboardHidden"
  33. }
  34. if (!valueSplit.contains("orientation")) {
  35. value+="|orientation"
  36. }
  37. if (!valueSplit.contains("screenSize")) {
  38. value+="|screenSize"
  39. }
  40. }
  41. act.attributes()["android:configChanges"] = value
  42. }
  43. manifestFile.setText(XmlUtil.serialize(manifest), "utf-8")
  44. }
  45. }
  46. }
  47. }
  48. }

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

英文:

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

  1. project.afterEvaluate {
  2. def variants = null
  3. try {
  4. variants = android.applicationVariants
  5. } catch (Throwable t) {
  6. try {
  7. variants = libraryVariants
  8. } catch (Throwable tt) {}
  9. }
  10. if (variants != null) {
  11. variants.all { variant ->
  12. variant.outputs.each { output ->
  13. def task = output.processManifestProvider.get()
  14. if (task == null) {
  15. return
  16. }
  17. task.doLast {
  18. def manifestFile = new File(task.manifestOutputDirectory.get().getAsFile(),"AndroidManifest.xml")
  19. if (manifestFile == null || !manifestFile.exists()) {
  20. return
  21. }
  22. def parser = new XmlParser(false, true)
  23. def manifest = parser.parse(manifestFile)
  24. def app = manifest.'application'[0]
  25. app.'activity'.each{act->
  26. String value = act.attributes()['android:configChanges']
  27. if (value == null) {
  28. value = "keyboardHidden|orientation|screenSize"
  29. } else {
  30. String[] valueSplit = value.split("\\|")
  31. if (!valueSplit.contains("keyboardHidden")) {
  32. value+="|keyboardHidden"
  33. }
  34. if (!valueSplit.contains("orientation")) {
  35. value+="|orientation"
  36. }
  37. if (!valueSplit.contains("screenSize")) {
  38. value+="|screenSize"
  39. }
  40. }
  41. act.attributes()["android:configChanges"] = value
  42. }
  43. manifestFile.setText(XmlUtil.serialize(manifest), "utf-8")
  44. }
  45. }
  46. }
  47. }
  48. }

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.

答案2

得分: 0

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

  • 清单

    1. <activity
    2. android:name=".YourActivity"
    3. android:configChanges="${configChanges}">
    4. <intent-filter>
    5. <action android:name="android.intent.action.MAIN" />
    6. <category android:name="android.intent.category.LAUNCHER" />
    7. </intent-filter>
    8. </activity>
  • Gradle

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

英文:

You can solve by using manifest placeholders:

  • Manifest:

    1. <activity
    2. android:name=".YourActivity"
    3. android:configChanges="${configChanges}">
    4. <intent-filter>
    5. <action android:name="android.intent.action.MAIN" />
    6. <category android:name="android.intent.category.LAUNCHER" />
    7. </intent-filter>
    8. </activity>
  • Gradle:

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

huangapple
  • 本文由 发表于 2020年1月3日 20:43:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/59578860.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定