Android Gradle Read App Name from strings.xml

后端 未结 3 890
醉话见心
醉话见心 2021-01-17 17:29

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 w

3条回答
  •  一整个雨季
    2021-01-17 18:13

    I don't see any method in Android Plugin docs for accessing resources, so here is the code you can use to find your app's name by searching resources:

    def getAppName() {
        def stringsFile = android.sourceSets.main.res.sourceFiles.find { it.name.equals 'strings.xml' }
        return new XmlParser().parse(stringsFile).string.find { it.@name.equals 'app_name' }.text()
    }
    

    BUT I completely agree with @Samuil Yanovski in that it is not worth it - better hardcode a string. I don't think it will slow down building process, but it is just unnecessary.

提交回复
热议问题