XML命名空间声明:自动替代包名 [英] Xml namespace declaration: auto-substitute package name

查看:275
本文介绍了XML命名空间声明:自动替代包名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有多个构建目标(使用ANT)的Andr​​oid项目。出于测试目的,这些建设目标,都有着不同的包名(所以我的包名是com.mycompany.myapp发布构建和com.mycompany.myapp.test测试版本)。

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).

这非常适用于在大多数情况下,除非涉及到自定义XML命名空间布局文件。 所以这样的:

This works well for the most part, except for when it comes to custom xml namespaces in layout files. So this:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:custom="http://schemas.android.com/apk/res/com.mycompany.myapp" />

将停止只要包名被替换com.mycompany.myapp.test工作。
正因为如此,我在prebuilt每次更换com.mycompany.myapp值。而且,由于所有这些文件应该在VCS中,每一个人交换机配置的时间应该不会冲突,并将它们合并,我不得不把布局文件到特定的config文件夹中,在那里他们将是这样的:

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:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:custom="http://schemas.android.com/apk/res/@CONFIG.PACKAGENAME@" />

现在该文件存储在VCS,并在prebuilt @ CONFIG.PACKAGENAME @被替换,然后将文件从./config/file.xml复制到./res/layout/file.xml。

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.

这是非常inconvinient并没有真正很好地扩展(我无法想象提的构建脚本中的每一个像50个文件)。

This is extremely inconvinient and doesn't really scale well (I can't imagine mentioning every one of like 50 files in build script).

所以我的问题是:有没有办法在命名空间声明中自动使用当前的包叫什么名字?或者至少modife布局文件(或构建文件?),所以我不会有我每次更改包名称来代替com.mycompany.myapp。

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.

推荐答案

原来,有一个后缀为:RES-汽车

Turns out that there is a postfix for that: 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.

这篇关于XML命名空间声明:自动替代包名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆