Android通过gradle在strings.xml中更改Appname

时间:2014-12-02 17:06:21

标签: android gradle android-gradle build.gradle

我正在尝试通过gradle脚本更改strings.xml中的app_name。我正在使用Eclipse,因此我无法创建不同的文件夹结构来定义不同的strings.xml文件。我写了以下脚本,

task changeAppNameForBuildType01AndBuildType02 << {

    println "Changing application name in strings.xml"

    println "App name will be changed to ${applicationName}."

    File stringsXmlFile = new File("res/values/strings.xml")
    String contents = stringsXmlFile.getText()
    stringsXmlFile.setText(contents.replaceAll("<string name=\"app_name\">[^<]*</string>", "<string name=\"app_name\">${applicationName}</string>"))

}

task changeAppNameForLiveBuild << {

    println "Changing application name in strings.xml"

    println "App name will be changed to AppName."
    File stringsXmlFile = new File("res/values/strings.xml")
    String contents = stringsXmlFile.getText()
    stringsXmlFile.setText(contents.replaceAll("<string name=\"app_name\">[^<]*</string>", "<string name=\"app_name\">AppName</string>"))

}

tasks.whenTaskAdded { task ->
    if (task.name == 'processReleaseManifest') {
        task.dependsOn stripResources
    }

    /*if (task.name == 'preDexBuildType01' || task.name == 'preDexBuildType02' ) {
        task.dependsOn changeAppNameForBuildType01AndBuildType02
    }*/

    if (task.name == 'preDexRelease') {
        task.dependsOn changeAppNameForLiveBuild
    } else {
        task.dependsOn changeAppNameForBuildType01AndBuildType02
    }
}

此脚本仅适用于BuildType01,但不会更改BuildType02或Release的appname。任何人都可以帮我解决这个问题。为什么它不起作用?谢谢!

0 个答案:

没有答案