Android Gradle从strings.xml中读取应用程序名称

前端之家收集整理的这篇文章主要介绍了Android Gradle从strings.xml中读取应用程序名称前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试为每个构建变体重命名我的APK文件,以包含应用程序名称,versionName,versionCode和内部版本号.到目前为止,除了应用程序名称之外,我的所有工

我想使用AndroidManifest.xml文件用于android:label的相同值.这来自字符串资源@ string / app_name.我已经看到了使用以下方法替换资源值的能力:

  1. resValue "string","app_name","Some new value"

但我只想读取此值并使用它来命名我的APK文件.

  1. android.applicationVariants.all { variant ->
  2. variant.outputs.each { output ->
  3. renameApk(variant,output)
  4. }
  5. }
  6.  
  7. def renameApk(variant,output) {
  8. def apkPath = output.outputFile.parent
  9. def baseName = project.archivesBaseName
  10.  
  11. baseName += "-${variant.buildType.name}"
  12.  
  13. // add version name and version code
  14. baseName += "-v${variant.mergedFlavor.versionName}-${variant.mergedFlavor.versionCode}"
  15.  
  16. // if built on jenkins ci,add jenkins build number:
  17. def buildNumber = System.getenv('BUILD_NUMBER')
  18. if (buildNumber && buildNumber.size() > 0) {
  19. baseName += "-b${buildNumber}"
  20. }
  21.  
  22. // if the variant will not be zipAligned,specify that
  23. if (!output.zipAlign) {
  24. baseName += '-unaligned'
  25. }
  26.  
  27. // set the output file
  28. output.outputFile = new File(apkPath,"${baseName}.apk");
  29. }

解决方法

我认为这不容易做到.资源分辨率在移动设备上完成,以适应屏幕方向,本地化等内容.例如,Gradle构建系统无法知道要使用的语言环境.如果您坚持从资源中获取值,则可以打开要使用的特定strings.xml文件,解析XML并自行获取值.在我看来,这是一个巨大的矫枉过正,会非常缓慢和丑陋.

应用程序名称不会经常更改,因此我很乐意对其进行硬编码(特别是因为最终用户看不到apk文件名,所以即使出现错误,影响也会很小).如果您正在处理白标应用程序并且必须支持动态应用程序名称,则将值提取到gradle.properties文件(或您正在使用的某些其他类型的配置文件)应该是更好的选择,而不是使用应用程序的资源.

猜你在找的Android相关文章