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.

Valery Viktorovsky
  • 6,487
  • 3
  • 39
  • 47
Alexey
  • 7,262
  • 4
  • 48
  • 68

1 Answers1

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.

Alexey
  • 7,262
  • 4
  • 48
  • 68
  • 1
    Reference: http://developer.android.com/tools/sdk/eclipse-adt.html (section ADT 17.0.0). – Pang Jan 05 '13 at 05:28
  • "Added support for custom views with custom attributes in libraries. Layouts using custom attributes must use the namespace URI http://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." – flup Apr 03 '13 at 23:02
  • 2
    Will it work even if my project uses a library that holds custom views? – android developer Apr 15 '13 at 12:54
  • Is this Eclipse/ADT specific? – lhunath Apr 22 '13 at 16:25
  • No, it's not eclipse specific. It'll work with ant and what not. – Alexey Apr 22 '13 at 18:47
  • 1
    Android Studio and Gradle are included in Alexey's "what not". Works. – user1978019 Mar 05 '14 at 18:59
  • 1
    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. – RichieHH Mar 14 '14 at 10:46
  • How to resolve in Android Studio I am using other module as a dependency and that module contains different namespace which is not found while building can we change namespace while building the app. – Ganesh Jogam Sep 18 '18 at 14:23
  • @GaneshJogam Since during build all resources are merged (e.g. styles from all dependency libraries will end up in your app styles) you should be able to access any properties using res-auto namespace: xmlns:app="http://schemas.android.com/apk/res-auto" – Alexey Sep 18 '18 at 14:40
  • Can we solve this issue in gradle files while building instead of replacing – Ganesh Jogam Sep 19 '18 at 07:17
  • @Alexey I got your point that it will work with xmlns:app="schemas.android.com/apk/res-auto" but in my case I am having module with namespace xmlns:latin="http://schemas.android.com/apk/res/com.android.inputmethod.latin" in the layout file so I am getting compile time error No resource identifier found for attribute 'keyLetterSize' in package 'com.android.inputmethod.latin' because during build it is not converting it to app namespace. – Ganesh Jogam Sep 20 '18 at 04:34
  • To solve this issue I have to find and replace all occurences with http://schemas.android.com/apk/res-auto I was thinking is it possible to configure it during build process – Ganesh Jogam Sep 20 '18 at 04:37
  • @GaneshJogam Well the whole point of res-auto is to replace it with your package name during build process. So you have to do it once. Just do Edit -> Find -> Replace in path and repalce `res/com.android.inputmethod.latin` with `res-auto` in all xml files. It'll take 1 minute and after everything will work just fine. – Alexey Sep 20 '18 at 08:46