Android的工作室 - 摇篮:如何替换变量在一个文件中

Android的工作室 - 摇篮:如何替换变量在一个文件中

问题描述:

林相当新的摇篮和使用Eclipse和Ant来完成所有的构建已。在我们的应用程序,我们有位于资产文件夹在同一级别src和资源等。在此文件中,我们有以下一个config.properties文件:

Im fairly new to Gradle and have been using Eclipse and Ant to do all the builds. Within our app we have a config.properties file located in the assets folder at the same level as src and res etc. In this file we have the following:

developmentsettings=true
defaultLogLevel=4
prodEnvironment=false

我怎么能替换的build.gradle文件这3个变量的值?难道我为此创建一个新的任务,如果是这样,这是Android的标签内或没有?

How can I replace the values of these 3 variables in the build.gradle file? Do I create a new task for this, and if so, is this within the android tag or not?

任何及所有的帮助将是必须的AP preciated

Any and all help would be must appreciated

我不知道,如果你特别需要这些在一个单独的文件......但如果​​没有,BuildConfig似乎是要走的路。你在你的文件的gradle做这样的事:

I'm not sure if you specifically need these in a separate file... but if not, BuildConfig seems like the way to go. You do something like this in your gradle file:

buildTypes {
    debug {
        buildConfigField "boolean" "DEVELOPMENT_SETTINGS" "true"
        buildConfigField "int" "DEFAULT_LOG_LEVEL" "4"
        buildConfigField "boolean" "PROD_ENVIRONMENT" "false"
    }
    release {
        buildConfigField "boolean" "DEVELOPMENT_SETTINGS" "false"
        buildConfigField "int" "DEFAULT_LOG_LEVEL" "4"
        buildConfigField "boolean" "PROD_ENVIRONMENT" "true"
    }
}

,然后从源的android code,你引用这些领域,像这样:

And then from your source android code, you reference these fields like so:

BuildConfig.DEVELOPMENT_SETTINGS
BuildConfig.DEFAULT_LOG_LEVEL
BuildConfig.PROD_ENVIRONMENT

你基本上声明常量,从gradle这个,可以在你的java code使用。

You're essentially declaring constants, from gradle, that can be used in your java code.