Android Xml 命名空间声明:自动替换包名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10448006/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Xml namespace declaration: auto-substitute package name
提问by Alexey
I have an android project with multiple build targets (using ant). For testing purposes, those build targets all have different package names (so my package name is com.mycompany.myapp for release build and com.mycompany.myapp.test for test build).
我有一个具有多个构建目标的 android 项目(使用 ant)。出于测试目的,这些构建目标都有不同的包名(所以我的包名是 com.mycompany.myapp 用于发布构建,com.mycompany.myapp.test 用于测试构建)。
This works well for the most part, except for when it comes to custom xml namespaces in layout files. So this:
这在大多数情况下都很有效,除了布局文件中的自定义 xml 命名空间。所以这:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res/com.mycompany.myapp" />
will stop working as soon as package name is replaced with com.mycompany.myapp.test.
Because of that, I have to replace com.mycompany.myapp value each time during prebuilt. And since all this files should be in vcs, and should not conflict every time one person switches configuration and them merges, I had to move layout files into specific config folder, where they would look like:
将在包名称替换为 com.mycompany.myapp.test 后立即停止工作。
因此,我必须在预构建期间每次替换 com.mycompany.myapp 值。由于所有这些文件都应该在 vcs 中,并且每次一个人切换配置并将它们合并时都不应该发生冲突,我不得不将布局文件移动到特定的配置文件夹中,它们看起来像:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res/@CONFIG.PACKAGENAME@" />
Now this files are stored in vcs, and @CONFIG.PACKAGENAME@ is replaced during prebuilt and then the file is copied from ./config/file.xml to ./res/layout/file.xml.
现在这些文件存储在 vcs 中,并在预构建期间替换 @CONFIG.PACKAGENAME@,然后将文件从 ./config/file.xml 复制到 ./res/layout/file.xml。
This is extremely inconvinient and doesn't really scale well (I can't imagine mentioning every one of like 50 files in build script).
这非常不方便,并且不能很好地扩展(我无法想象在构建脚本中提到 50 个文件中的每一个)。
So my question is: is there a way to automatically use current package name in namespace declaration? Or at least modife layout files (or build files?) so that I wont have to replace com.mycompany.myapp every time I change package name.
所以我的问题是:有没有办法在命名空间声明中自动使用当前包名?或者至少修改布局文件(或构建文件?),这样我就不必每次更改包名称时都替换 com.mycompany.myapp。
回答by Alexey
Turns out that there is a postfix for that: res-auto.
原来有一个后缀:res-auto。
So all you need to do is write
所以你需要做的就是写
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res-auto" />
This will automatically use current package name.
这将自动使用当前的包名称。