按 flavor / debug-build 更改字符串资源
Change string resource by flavor / debug-build
假设我们有 strings_test.xml,它存储用于测试的字符串值,应该在调试版本中显示。当 apk 被构建为发布版本时,所有值都应更改为空字符串,例如<string name="test_some_card_text">@string/empty</string>
.
有没有可能实现这个?
一如既往,提前致谢。
创建 apk 后无法更改字符串值。
但是您可以在创建 apk 后动态地将值分配给 text 或 edittext ... etx。
是的,Gradle 允许您覆盖字符串。
在你的 app/build.gradle
的 buildTypes{} 中添加这个
调试{
applicationIdSuffix "debug"
}
那应该在 main
旁边创建一个名为 debug
的目录。如果没有,则手动创建一个。 (说真的,我没试过这个,但我知道这是可能的。)
然后,如果您的 strings_test.xml
在 res/values
下,请在 debug/ 下创建类似的目录结构,并将带有调试特定字符串的 strings_text.xml 放在那里。这将显示在您的调试版本中。 release/main/res/values 下的那些将显示在您的发布版本中。
PS:您可以根据buildTypes和flavor像这样覆盖所有资源和资产数据。你不能 覆盖 Java 文件,但是你可以 添加 它们。
正如@Aditya Naik 所说,可以使用 Flavors。
官方文档说
BuildType -> Flavor -> main -> Dependencies.
This means that if a resource is declared in both the Build Type and in main, the one from Build Type will be selected.
Note that for the scope of the merging, resources of the same (type, name) but different qualifiers are handled separately.
This means that if src/main/res has
- res/layout/foo.xml
res/layout-land/foo.xml
and src/debug/res has
res/layout/foo.xml
Then the merged resource folder will contain the default foo.xml from src/debug/res but the landscape version from src/main/res
有关详细信息,请访问 Official doc - Resource Merging
是的,您可以在您的应用程序中 gradle 在 buildTypes..
下执行此操作
buildTypes {
mybuild {
resValue "string", "test_some_card_text", '"test"'
resValue "string", "other_text", '"other"'
}
debug {
resValue "string", "test_some_card_text", '"test"'
resValue "string", "other_text", '"other"'
}
}
然后像这样访问它。
getApplicationContext().getResources().getString(R.string.test_some_card_text);
getApplicationContext().getResources().getString(R.string.other_text);
对于构建,您需要 select build variants
并且必须构建它。
对于那些来这里寻找某种方法将类似方法应用于 raw
资源的人,我使用 buildConfigField
.
来处理它
gradle
...
buildTypes {
debug {
...
buildConfigField "int", "shared_resource_name", 'R.raw.debug_resource_name'
...
}
prod {
...
buildConfigField "int", "shared_resource_name", 'R.raw.prod_resource_name'
...
}
}
注意引号。之后,将 BuildConfig.shared_resource_name
放在 R.raw.resource_value
过去直接访问的文件中。
我认为这可以用于其他资源。
假设我们有 strings_test.xml,它存储用于测试的字符串值,应该在调试版本中显示。当 apk 被构建为发布版本时,所有值都应更改为空字符串,例如<string name="test_some_card_text">@string/empty</string>
.
有没有可能实现这个?
一如既往,提前致谢。
创建 apk 后无法更改字符串值。 但是您可以在创建 apk 后动态地将值分配给 text 或 edittext ... etx。
是的,Gradle 允许您覆盖字符串。
在你的 app/build.gradle
的 buildTypes{} 中添加这个调试{ applicationIdSuffix "debug" }
那应该在
main
旁边创建一个名为debug
的目录。如果没有,则手动创建一个。 (说真的,我没试过这个,但我知道这是可能的。)然后,如果您的
strings_test.xml
在res/values
下,请在 debug/ 下创建类似的目录结构,并将带有调试特定字符串的 strings_text.xml 放在那里。这将显示在您的调试版本中。 release/main/res/values 下的那些将显示在您的发布版本中。
PS:您可以根据buildTypes和flavor像这样覆盖所有资源和资产数据。你不能 覆盖 Java 文件,但是你可以 添加 它们。
正如@Aditya Naik 所说,可以使用 Flavors。 官方文档说
BuildType -> Flavor -> main -> Dependencies.
This means that if a resource is declared in both the Build Type and in main, the one from Build Type will be selected.
Note that for the scope of the merging, resources of the same (type, name) but different qualifiers are handled separately.
This means that if src/main/res has
- res/layout/foo.xml
res/layout-land/foo.xml
and src/debug/res has
res/layout/foo.xml
Then the merged resource folder will contain the default foo.xml from src/debug/res but the landscape version from src/main/res
有关详细信息,请访问 Official doc - Resource Merging
是的,您可以在您的应用程序中 gradle 在 buildTypes..
下执行此操作 buildTypes {
mybuild {
resValue "string", "test_some_card_text", '"test"'
resValue "string", "other_text", '"other"'
}
debug {
resValue "string", "test_some_card_text", '"test"'
resValue "string", "other_text", '"other"'
}
}
然后像这样访问它。
getApplicationContext().getResources().getString(R.string.test_some_card_text);
getApplicationContext().getResources().getString(R.string.other_text);
对于构建,您需要 select build variants
并且必须构建它。
对于那些来这里寻找某种方法将类似方法应用于 raw
资源的人,我使用 buildConfigField
.
gradle
...
buildTypes {
debug {
...
buildConfigField "int", "shared_resource_name", 'R.raw.debug_resource_name'
...
}
prod {
...
buildConfigField "int", "shared_resource_name", 'R.raw.prod_resource_name'
...
}
}
注意引号。之后,将 BuildConfig.shared_resource_name
放在 R.raw.resource_value
过去直接访问的文件中。
我认为这可以用于其他资源。