Android Gradle从strings.xml中读取应用程序名称 [英] Android Gradle Read App Name from strings.xml

查看:3142
本文介绍了Android Gradle从strings.xml中读取应用程序名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正尝试为每个构建变体重命名我的APK文件,以包含应用程序名称,版本名称,版本代码和内部版本号。到目前为止,除了应用程序名称外,其他所有工作都已经完成。



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

  resValuestring,app_name,Some new value 

但我只想读取这个值并用它来命名我的APK文件。 p>

  android.applicationVariants.all {variant  - > 
variant.outputs.each {输出 - >
renameApk(variant,output)
}
}

def renameApk(variant,output){
def apkPath = output.outputFile.parent
def baseName = project.archivesBaseName

aseName + = - $ {variant.buildType.name}

//添加版本名称和版本代码
baseName + =-v $ {variant.mergedFlavor.versionName} - $ {variant.mergedFlavor.versionCode}

//如果在jenkins ci上创建,添加jenkins内部版本号:
def buildNumber = System.getenv('BUILD_NUMBER')
if(buildNumber&& buildNumber.size()> 0){
baseName + =-b $ {buildNumber}
}

//如果变体不是zipAligned,则指定
if(!output.zipAlign){
baseName + ='-unaligned'
}

//设置输出文件
output.outputFile = new File(apkPath,$ {baseName} .apk);
}


解决方案

可以轻松完成。资源分辨率是在移动设备上完成的,以适应诸如屏幕方向,本地化等等。例如,Gradle构建系统无法知道使用哪种语言环境。如果你坚持从资源中获取价值,你可以打开你想使用的特定的strings.xml文件,解析XML并自己获取价值。在我看来,这是一个巨大的矫枉过正,而且会非常缓慢和丑陋。

应用程序名称不会经常更改,所以我会对其进行硬编码感到满意(特别是因为apk文件名对最终用户不可见,所以即使错误发生,影响会很小)。如果您正在使用白标签应用程序并且必须支持动态应用程序名称,那么将值提取到gradle.properties文件(或您正在使用的某种其他类型的配置文件)应该是更好的选择,而不是使用应用程序的资源。


I am trying to rename my APK files for each build variant to include the application name, versionName, versionCode and build number when present. So far I have everything working except the application name.

I want to use the same value that the AndroidManifest.xml file uses for android:label. This comes from a string resource @string/app_name. I have seen the ability to replace the resource values by using:

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

But I would just like to read this value and use it to name my APK file.

android.applicationVariants.all { variant ->
    variant.outputs.each { output ->
        renameApk(variant, output)
    }
}

def renameApk(variant, output) {
    def apkPath = output.outputFile.parent
    def baseName = project.archivesBaseName

    baseName += "-${variant.buildType.name}"

    // add version name and version code
    baseName += "-v${variant.mergedFlavor.versionName}-${variant.mergedFlavor.versionCode}"

    // if built on jenkins ci, add jenkins build number:
    def buildNumber = System.getenv('BUILD_NUMBER')
    if (buildNumber && buildNumber.size() > 0) {
        baseName += "-b${buildNumber}"
    }

    // if the variant will not be zipAligned, specify that
    if (!output.zipAlign) {
        baseName += '-unaligned'
    }

    // set the output file
    output.outputFile = new File(apkPath, "${baseName}.apk");
}

解决方案

I don't think this can be done easily. Resource resolution is done on the mobile device to accommodate for things like screen orientation, localization and so on. The Gradle build system has no way of knowing which locale to use for example. If you insist on getting the value from the resources, you can open the specific strings.xml file you'd like to use, parse the XML and get the value yourself. In my opinion this is a huge overkill and would be pretty slow and ugly.

App name is not changed often, so I would be comfortable with having it hardcoded (especially since the apk file name is not visible to the end user, so even if mistakes happen, the impact would be minimal). If you are working on a white label application and have to support dynamic app name, extracting the value to the gradle.properties file (or some other type of configuration file, you are using) should be a better option rather than using the app's resources.

这篇关于Android Gradle从strings.xml中读取应用程序名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆