speedy1034
speedy1034

Reputation: 302

Use package name in XML

I'm using Android Studio to build my application. I would like to use gradle buildTypes. I add a suffix to the package name with applicationIdSuffix to modify the package name for a test build type.

buildTypes {       
    debug {
        runProguard false
        proguardFile 'proguard-rules.txt'
        applicationIdSuffix  '.dev'
        versionNameSuffix  '-dev'
    }
}

Is it possible to use this information in a xml file. My plan is to modify the account type:

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

Thanks!

Upvotes: 16

Views: 10449

Answers (5)

Abner Esc&#243;cio
Abner Esc&#243;cio

Reputation: 2785

I'm using ${applicationId} like follow for example:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.abnerescocio.embaixadordorei.presentation">

    ...

    <application
    ... 
    >

        <provider
            android:name="androidx.core.content.FileProvider"
            android:authorities="${applicationId}.android.fileprovider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths"/>
        </provider>

    ...

    </application>

</manifest>

Upvotes: 2

Paulo Costa
Paulo Costa

Reputation: 433

I've just created a gradle plugin for this: com.inutilfutil.android-resource-placeholder

Just include it and placeholders will be replaced on all XML resources

Upvotes: 0

Jemshit
Jemshit

Reputation: 10038

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

Upvotes: 10

jenzz
jenzz

Reputation: 7269

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

Upvotes: 34

ianhanniballake
ianhanniballake

Reputation: 199795

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)

Upvotes: 10

Related Questions