如何从的build.gradle

问题描述:

的productFlavours取代string.xml值在strings.xml中如何从的build.gradle

<resources> 
<string>name="app_name">App_Name</string> 
<string>name="app_port">https://example.com</string> 
<string>name="app_key">App_Key</string> 
</resources> 

在的build.gradle

productFlavors { 
    production { 
     applicationId = "com.example.production" 
    } 
    staging { 
     applicationId = "com.example.staging" 
     //code to manipulate strings.xml value because for production and staging app_name, app_port and key are different 
    } 
} 

我需要一个代码,代码操作从productFlavours的strings.xml的build.gradle文件,而不是为分段和生产创建单独的文件夹。

+0

他们有不同的值或者是按键本身有什么不同? – mixel

+0

我想下面的链接可以帮助您 [此处输入链接的描述] [1] [1]:http://stackoverflow.com/a/17201265/1182022 –

+0

@mixel我在strings.xml中指定什么都文件是不同的分期。 – Archana

我找到了答案。使用resValue与在gradle这个

productFlavors { 
production { 
    applicationId = "com.example.production" 
    resValue 'string', 'APP_NAME', 'app_name' 
} 
staging { 
    applicationId = "com.example.staging" 
    resValue 'string', 'APP_NAME', 'app_name_stage' 
} 

它将在目录下创建文件generated.xml与如下。 要获取值的代码,使用生成的资源所有其他人一样如下,

getResources().getString(R.string.APP_NAME)