Override applicatoinID inside library project's manifest with Gradle
Asked Answered
N

4

6

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?

Nepheline answered 16/1, 2015 at 16:14 Comment(1)
According to this https://mcmap.net/q/458071/-using-applicationid-in-library-manifest I think it should just work?Redbud
A
1

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" />
Adorno answered 11/5, 2017 at 15:16 Comment(0)
O
0

Here you can see how to change values in a manifest http://tools.android.com/tech-docs/new-build-system/user-guide/manifest-merger

In the section Placeholder support you will see more details

Ostler answered 16/1, 2015 at 16:52 Comment(1)
Doesn't seem there is a solution in your link. With the manifest merger doc, they only refer to the main app module gradle file. Or at least I didn't see anything about my problemNepheline
F
0

in your app.gradle

    def packagename = "com.packagename";
    android {
        defaultConfig {
        applicationId packagename
    }

    buildTypes {
        debug {
            applicationIdSuffix ".dev"
            manifestPlaceholders = [finalApplicationId: packagename + applicationIdSuffix]
        }
        release {
            applicationIdSuffix ""
            manifestPlaceholders = [finalApplicationId: packagename + applicationIdSuffix]
        }

In your library's manifest

<provider android:name="an.example"
                  android:authorities="${finalApplicationId}.recommendation" />
Fated answered 24/6, 2015 at 16:56 Comment(1)
This kind of works, but fails for tasks like :lib:processDebugAndroidTestManifestRedbud
L
-4

Set library applicationId in library gradle

buildTypes {

release { 
applicationId "com.example.library"}}
Leuko answered 16/1, 2015 at 19:9 Comment(2)
You mean that if I set the applicationId inside the library, it will get replaced with the main project one? I'll have a try and I'll let you knowNepheline
Unfortunately you can't set the applicationId inside a library project. So this is not the right answer, I'm sorryNepheline

© 2022 - 2024 — McMap. All rights reserved.