2016-12-16 31 views
2

我想根据构建变体/构建类型为string.xml中的字符串赋值不同的值。我想是这样的:string.xml值根据构建变体而变化

res/values-debug/string.xml 
    <string name="my_string">some debug value</string> 

res/values-release/string.xml 
    <string name="my_string">some release value</string> 

,但我没有看到这样的事情在那里。 这可能吗?

+0

当您使用buildVariant而不是buildType时,这会更容易。在你的例子中'buildType'使用 –

+0

有趣,这个页面上的答案是用于构建类型,而不是构建变体... – L7ColWinters

+0

那么使用buildVariant会是什么样子? –

回答

2

是的,这是可能的!在您的项目在src文件夹

buildTypes { 
    release { 
     minifyEnabled false 
     proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt' 
     signingConfig signingConfigs.release 
    } 
    debug { 
     applicationIdSuffix ".debug" 
     minifyEnabled false 
     debuggable true 
     signingConfig signingConfigs.release 
    } 
} 

那么你应该有一个称为主文件夹,添加一个在它旁边叫debug

在你的build.gradle您可以添加这样的事情。 Den只要您正在构建调试版本,调试文件夹中的任何资源都将替换主版本中的那些版本文件夹。

应该是这样的:

src/ 
    main/ 
     java/ -- all your java code 
     res/ 
      ... 
      values/ 
       strings.xml 
    debug/ 
     res/ 
      ... 
      values/ 
       strings.xml 

编辑:从其他两个答案的办法正常工作为好。但是,如果你有很多字符串保持它们为xml可能更容易处理。

+1

我想使用这种方法,因为那样我也可以添加mipmap文件夹。尽管如此,我还是无法使用它。我已经添加了这个问题来向你展示我在做什么。你能看到我做错了什么吗? –

+1

我现在明白了。我没有正确的目录结构。 –

+1

我很高兴你喜欢这种方式!我发现能够区分两个应用程序,调试和释放图标,名称,网址和其他内容非常有用,这种方法是最好的方式。 :) –

2
resValue 'string', '<string_name>', "some string" 

在你的build.gradle定义是为不同的构建变量/产品口味

4

可以通过您的build.gradle文件

buildTypes { 
    release { 
     resValue "string", "my_string", "some release value" 
    } 
    debug { 
     resValue "string", "my_string", "some debug value" 
    } 
} 

然后,你可以使用像@string/my_string这个值你想要的地方

+0

我删除strings.xml中的条目? –

+0

@AlLelopath是的,现在你可以删除它的形式xml –