Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Override applicatoinID inside library project's manifest with Gradle

I have a main project which references a library project. They both are compiled with Gradle.

This is the defaultConfig for the main project gradle file:

defaultConfig {
    applicationId "com.example.app"
    minSdkVersion 15
    targetSdkVersion 21
    versionCode 2
    versionName "1.0"
}

And this is the defaultConfig for the library project gradle file:

defaultConfig {
    minSdkVersion 11
    targetSdkVersion 21
    versionCode 1
    versionName "1.0"
}

As you can see, I don't declare any applicationId inside the library.

I have a permission defined inside my library project as below:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example.library">

<permission android:name="${applicationId}.permission.C2D_MESSAGE"
            android:protectionLevel="signature" />

When I build the project, the applicationId gets replaced with com.example.library (the library package name). And that's not what I want. I want it to be replaced with com.example.app since it's my app's applicationId.

If I put the ${applicationID} placeholder inside app's manifest file, everything works.

Does anyone knows if this can be accomplished and how?

like image 963
edoardotognoni Avatar asked Jan 16 '15 16:01

edoardotognoni


1 Answers

I see the following solution:

Project build.gradle

ext {
   applicationId = "com.yourproject"
}

App build.gradle

defaultConfig {
        applicationId rootProject.applicationId
}

Library build.gradle

buildTypes {
        release {
            buildConfigField 'String', 'ROOT_APPLICATION_ID', rootProject.applicationId
        }
        debug {
            buildConfigField 'String', 'ROOT_APPLICATION_ID', "\"" + rootProject.applicationId + ".dev\""
        }
}

Also, if you want to use the variable in the Manifest use the following way:

Library build.gradle

android {

    defaultConfig {        
        manifestPlaceholders = [ROOT_APPLICATION_ID:rootProject.applicationId]
    }

 buildTypes {
            release {
                buildConfigField 'String', 'ROOT_APPLICATION_ID', "\"" + manifestPlaceholders.get("ROOT_APPLICATION_ID")+ "\""
            }
            debug {
               manifestPlaceholders.put("ROOT_APPLICATION_ID", rootProject.applicationId + ".dev");
               buildConfigField 'String', 'ROOT_APPLICATION_ID', "\"" + manifestPlaceholders.get("ROOT_APPLICATION_ID")+ "\""
            }
    }

}

The variable from defaultConfig will be available in mainfest and variables from buildTypes will be available in the code.

Manifest

<provide android:name="com.lazada.core.storage.db.LazadaContentProvider"
            android:authorities="${ROOT_APPLICATION_ID}.authority"
            android:exported="false" />
like image 166
Alexey Glukharev Avatar answered Nov 15 '22 11:11

Alexey Glukharev