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

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

问题描述

我有一个带有多个构建目标的 android 项目(使用 ant).出于测试目的,这些构建目标都有不同的包名(所以我的包名是 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 后立即停止工作.
因此,我必须在预构建期间每次替换 com.mycompany.myapp 值.由于所有这些文件都应该在 vcs 中,并且每次一个人切换配置并将它们合并时都不应该发生冲突,我不得不将布局文件移动到特定的配置文件夹中,它们看起来像:

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中,并且@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.

这非常不方便,并且不能很好地扩展(我无法想象在构建脚本中提到 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).

所以我的问题是:有没有办法在命名空间声明中自动使用当前包名?或者至少修改布局文件(或构建文件?),这样我就不必每次更改包名称时都替换 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-auto.

Turns out that there is a postfix for that: res-auto.

所以你需要做的就是写

<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天全站免登陆