Use package name in XML Use package name in XML xml xml

Use package name in XML


Here's another temporary workaround until the Android Gradle plugin gets support for placeholders in resource files.

You can dynamically create a String resource via Gradle in the defaultConfig and/or buildTypes and/or productFlavors closure:

android {    defaultConfig {        applicationId 'com.foo.bar'        resValue 'string', 'package_name', applicationId    }}

You can then reference it in your authenticator.xml file just like you do for the label:

<account-authenticator xmlns:android="http://schemas.android.com/apk/res/android"    android:accountType="@string/package_name"    android:icon="@drawable/icon"    android:label="@string/app_name"/> 


While uou can use ${packageName} to fill in the generated package name in the Manifest:

<account-authenticator xmlns:android="http://schemas.android.com/apk/res/android"    android:accountType="${packageName}"    android:icon="@drawable/icon"    android:label="@string/app_name"/> 

This currently (Android Gradle 0.11) doesn't work with resource files. :-(

Instead, you must create a separate version of the resource file for each build type / variant you want with a different account type:

  • src
    • debug/res/xml/authenticator.xml (with debug package name)
    • main/res/xml/authenticator.xml (with default package name)


On top of jenzz's answer, i had to do this, since my case is just like as question, i add applicationIdSuffix for debug version of app.

android {    ...    defaultConfig {        applicationId "..."        ...    }    ....    buildTypes {        release {            ...            resValue 'string', 'application_id', android.defaultConfig.applicationId        }        debug {            ...            applicationIdSuffix '.debug'            resValue 'string', 'application_id', android.defaultConfig.applicationId + applicationIdSuffix        } }

Then in any xml resource, i can get application Id as like this: @string/application_id