如何用 Android Gradle 替换字符串资源 [英] How to replace strings resources with Android Gradle

查看:37
本文介绍了如何用 Android Gradle 替换字符串资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Android Studio 中使用 gradle 制作了一个新应用程序,现在我需要制作大约 10 个版本,在资源中具有不同的包名称和值.我在示例中制作了自定义风格,并想用自定义值替换此自定义风格中的一些字符串.我找到了这样的例子:

I made a new app with gradle in Android Studio, and now I need to make about 10 versions with different package names and values in resources. I made custom flavors as in example and want to replace some strings in this custom flavors with custom values. I found example like this:

filter(org.apache.tools.ant.filters.ReplaceTokens, tokens: ['version': '2.2'])

但是我不知道把它放在哪里.据我了解,我需要将其放入单独的任务中,但如何让 IDE 调用此任务?

But i don't know where to put it. As i understand i need to put it into separate task, but how to make this task called by IDE?

此外,我需要替换 Java 类和 Content Provider 的身份验证中的几个变量,也许我需要通过将文件复制到 flavor1 文件夹中并让 gradle 合并它来做到这一点,但是存储许多文件副本似乎是错误的解决方案一行的区别......也许我需要为所有这些使用其他解决方案?

Also i need to replace few variables inside Java classes and Content Provider's auth, maybe i need to do this by copy files into flavor1 folder and let gradle to merge it, but it seems like wrong solution to store many copies of files with difference in one line... Maybe i need to user some other solution for all this?

这里是 build.gradle:

Here is build.gradle:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.4.2'
    }
}
apply plugin: 'android'

dependencies {
    compile fileTree(dir: 'libs', include: '*.jar')
    compile project(':JazzyListView')
    compile project(':ABS')
    compile project(':Volley')
}

android {
    compileSdkVersion 17
    buildToolsVersion "17.0.0"

    defaultConfig {
        versionCode 5
        versionName "3.0"
        minSdkVersion 8
        targetSdkVersion 17
    }

    sourceSets {
        main {
            manifest.srcFile 'src/main/AndroidManifest.xml'
            java.srcDirs = ['src/main/java']
            res.srcDirs = ['src/main/res']
        }
    }

    productFlavors {
        flavor1 {
            packageName "com.example.flavor1"               
        }

        flavor2 {
            packageName "com.example.flavor2"
        }

    }

}

推荐答案

我遇到了类似的问题.我想将 Jenkins 内部版本号添加到从 strings.xml 合并的字符串中.这是我从 Android Gradle 插件 0.12.+ 开始的解决方案.

I had a similar problem. I wanted to add the Jenkins build number to the strings that get merged from strings.xml. Here's my solution as of Android Gradle plugin 0.12.+.

// Insert the build number into strings.xml
android.applicationVariants.all{ variant ->
    variant.mergeResources.doLast{
        ext.env = System.getenv()
        def buildNumber = env.BUILD_NUMBER
        if (buildNumber != null) {
            File valuesFile = file("${buildDir}/intermediates/res/${variant.dirName}/values/values.xml")
            println("Replacing revision number in " + valuesFile)
            println("Build number = " + buildNumber)
            String content = valuesFile.getText('UTF-8')
            content = content.replaceAll(/devBuild/, buildNumber)
            valuesFile.write(content, 'UTF-8')
        }
    }
}

您可能希望根据您想要执行的操作挂钩不同的 Gradle 任务.查看属于 Android 构建的一部分的任务以找出答案.

You might want to hook into a different Gradle task depending on what you want to do. Take a look at the tasks that are part of the Android build to figure that out.

http://tools.android.com/tech-docs/new-build-系统/用户指南

更新:在某些时候,Android Gradle 插件改变了遍历应用程序变体关键字的方式,从 eachall.我的答案已更新以反映更改,但如果此代码未向控制台打印任何内容,请尝试切换到 each.

UPDATE: At some point, the Android Gradle plugin changed the way to iterate through application variants keyword from each to all. My answer has been updated to reflect the change, but try switching to each if this code doesn't print anything to the console.

这篇关于如何用 Android Gradle 替换字符串资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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