47

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

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" /> 

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@" /> 

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.

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

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.

1 Answer 1

118

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.

Sign up to request clarification or add additional context in comments.

13 Comments

Reference: developer.android.com/tools/sdk/eclipse-adt.html (section ADT 17.0.0).
"Added support for custom views with custom attributes in libraries. Layouts using custom attributes must use the namespace URI schemas.android.com/apk/res-auto instead of the URI that includes the app package name. This URI is replaced with the app specific one at build time."
Will it work even if my project uses a library that holds custom views?
Android Studio and Gradle are included in Alexey's "what not". Works.
Confirmed it also works with IntelliJ. Why the Android docs dont specify exactly what it is during the tutorial is a mystery - it's what bought me here.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.