Onesignal-android-sdk: How to use OneSignal in a library project?

Created on 12 Jan 2017  路  11Comments  路  Source: OneSignal/OneSignal-Android-SDK

Is it possible to use OneSignal Android SDK in a library project and provide common onesignal_app_id and onesignal_google_project_number only in this library project and not for every app using this library (common "keys" for all haps using my library)?

I added necessary dependencies in my library's build.gradle and also manifestPlaceholders

manifestPlaceholders = [onesignal_app_id: "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX",
                                // Project number pulled from dashboard, local value is ignored.
                                onesignal_google_project_number: "XXXXXXXXXXXX"]

The problem is that when I try to build a demo app which uses my library to which I added OneSignal I get these errors:

/path_to_project/demoapp/demoapp/src/main/AndroidManifest.xml Error:
    Attribute meta-data#onesignal_app_id@value at AndroidManifest.xml requires a placeholder substitution but no value for <onesignal_app_id> is provided.
/path_to_project/demoapp/demoapp/src/main/AndroidManifest.xml Error:
    Attribute meta-data#onesignal_google_project_number@value at AndroidManifest.xml requires a placeholder substitution but no value for <onesignal_google_project_number> is provided.

See http://g.co/androidstudio/manifest-merger for more information about the manifest merger.

:demoapp:processDebugManifest FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':demoapp:processDebugManifest'.
> Manifest merger failed with multiple errors, see logs

I didn't add anything manually to any AndroidManifest.xml (neither application's nor library's).

I want to use OneSignal SDK in my library and be able to add my library to any app, so that onesignal_app_id and onesignal_google_project_numberare the same for all apps (even third party ones) using my library.

Is it possible to achieve? Is it compatible with the terms of use?

Duplicate

Most helpful comment

@jkasten2
Still the same error in my SDK's build.gradle:

Error:(36, 0) Could not get unknown property 'onesignal_app_id' for ProductFlavor_Decorated{name=main, dimension=null, minSdkVersion=DefaultApiVersion{mApiLevel=18, mCodename='null'}, targetSdkVersion=DefaultApiVersion{mApiLevel=24, mCodename='null'}, renderscriptTargetApi=null, renderscriptSupportModeEnabled=null, renderscriptSupportModeBlasEnabled=null, renderscriptNdkModeEnabled=null, versionCode=2, versionName=1.1, applicationId=null, testApplicationId=null, testInstrumentationRunner=android.support.test.runner.AndroidJUnitRunner, testInstrumentationRunnerArguments={}, testHandleProfiling=null, testFunctionalTest=null, signingConfig=null, resConfig=null, mResValues={}, mProguardFiles=[], mConsumerProguardFiles=[/path_to_project/build/intermediates/proguard-files/proguard-android.txt-2.2.3, /path_to_project/sdk/proguard-rules.pro], mManifestPlaceholders={}, mWearAppUnbundled=null} of type com.android.build.gradle.internal.dsl.ProductFlavor.
<a href="openFile:/path_to_project/sdk/build.gradle">Open File</a>

This is how defaultConfig in my application's build.gradle looks like:

defaultConfig {
        applicationId "com.package.app"
        minSdkVersion 18
        targetSdkVersion 24
        versionCode 4
        versionName VERSION_NAME
        multiDexEnabled true

        // One Signal:
        manifestPlaceholders = [onesignal_app_id: "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"]
    }

This is how defaultConfig in my SDK's build.gradle looks like:

defaultConfig {
        minSdkVersion 18
        targetSdkVersion 24
        versionCode 2
        versionName VERSION_NAME
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        buildConfigField "String", "ENDPOINTS_URL", "\"$ENDPOINTS_URL\""
        consumerProguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'

        // One Signal:
        manifestPlaceholders = [onesignal_app_id: "${onesignal_app_id}",
                                onesignal_google_project_number: "REMOTE"]
    }

All 11 comments

@6franek Your onesignal_app_id must be different for each Android package name. You can set onesignal_google_project_number to "REMOTE" as this is always pull from the app settings on the OneSignal dashboard starting with the 3.4.0 SDK. You can use the same Google Project number (Sender ID) for of your apps however if you choose.

@OlegTarashkevich is correct, the link above should work for you use case.

Thanks.

@jkasten2 I get it now that I have to put one_signal_app_id for each app using my library individually, but how can I pass the values from the app's build.gradle to the library project?

This solution which you've posted doesn't work for me. I get this error while syncing grade, so I can't even start building the app:

Error:(36, 0) Could not get unknown property 'onesignal_app_id' for ProductFlavor_Decorated{name=main, dimension=null, minSdkVersion=DefaultApiVersion{mApiLevel=18, mCodename='null'}, targetSdkVersion=DefaultApiVersion{mApiLevel=24, mCodename='null'}, renderscriptTargetApi=null, renderscriptSupportModeEnabled=null, renderscriptSupportModeBlasEnabled=null, renderscriptNdkModeEnabled=null, versionCode=2, versionName=1.1, applicationId=null, testApplicationId=null, testInstrumentationRunner=android.support.test.runner.AndroidJUnitRunner, testInstrumentationRunnerArguments={}, testHandleProfiling=null, testFunctionalTest=null, signingConfig=null, resConfig=null, mResValues={}, mProguardFiles=[], mConsumerProguardFiles=[/path_to_project/build/intermediates/proguard-files/proguard-android.txt-2.2.3, /path_to_project/sdk/proguard-rules.pro], mManifestPlaceholders={}, mWearAppUnbundled=null} of type com.android.build.gradle.internal.dsl.ProductFlavor.
<a href="openFile:/path_to_project/sdk/build.gradle">Open File</a>

I've already tried removing this from the sdk's build.gradle:

        // One Signal:
        manifestPlaceholders = [onesignal_app_id: "$onesignal_app_id",
                                // Project number pulled from dashboard, local value is ignored.
                                onesignal_google_project_number: "$onesignal_google_project_number"]

But I get the error similar to the one which I've posted in the first post of this issue, but related with the SDK, not the app.

Your SDK's bundle.gradle should be.

   manifestPlaceholders = [onesignal_app_id: "${onesignal_app_id}",
                           onesignal_google_project_number: "REMOTE"]

Your app's bundle.gralde should be .

   manifestPlaceholders = [onesignal_app_id: "YOUR_APP_ID_HERE"]

@jkasten2
Still the same error in my SDK's build.gradle:

Error:(36, 0) Could not get unknown property 'onesignal_app_id' for ProductFlavor_Decorated{name=main, dimension=null, minSdkVersion=DefaultApiVersion{mApiLevel=18, mCodename='null'}, targetSdkVersion=DefaultApiVersion{mApiLevel=24, mCodename='null'}, renderscriptTargetApi=null, renderscriptSupportModeEnabled=null, renderscriptSupportModeBlasEnabled=null, renderscriptNdkModeEnabled=null, versionCode=2, versionName=1.1, applicationId=null, testApplicationId=null, testInstrumentationRunner=android.support.test.runner.AndroidJUnitRunner, testInstrumentationRunnerArguments={}, testHandleProfiling=null, testFunctionalTest=null, signingConfig=null, resConfig=null, mResValues={}, mProguardFiles=[], mConsumerProguardFiles=[/path_to_project/build/intermediates/proguard-files/proguard-android.txt-2.2.3, /path_to_project/sdk/proguard-rules.pro], mManifestPlaceholders={}, mWearAppUnbundled=null} of type com.android.build.gradle.internal.dsl.ProductFlavor.
<a href="openFile:/path_to_project/sdk/build.gradle">Open File</a>

This is how defaultConfig in my application's build.gradle looks like:

defaultConfig {
        applicationId "com.package.app"
        minSdkVersion 18
        targetSdkVersion 24
        versionCode 4
        versionName VERSION_NAME
        multiDexEnabled true

        // One Signal:
        manifestPlaceholders = [onesignal_app_id: "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"]
    }

This is how defaultConfig in my SDK's build.gradle looks like:

defaultConfig {
        minSdkVersion 18
        targetSdkVersion 24
        versionCode 2
        versionName VERSION_NAME
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        buildConfigField "String", "ENDPOINTS_URL", "\"$ENDPOINTS_URL\""
        consumerProguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'

        // One Signal:
        manifestPlaceholders = [onesignal_app_id: "${onesignal_app_id}",
                                onesignal_google_project_number: "REMOTE"]
    }

I see, you are using a build flavor. Can you try adding these to each? Similar to this issue.
https://github.com/OneSignal/OneSignal-Android-SDK/issues/9

I've added placeholders in all buildTypes of both my app and my SDK and still the same error. I guess that there is a problem with getting onesignal_app_id in the SDK's build.gradle.

@6franek Could you attach your full build.gradle?

@jkasten2
Unfortunately I can't, but I've made it work. I don't know if I am doing it correctly, but I set the values in the SDK's build.gradle to empty ones"

        // One Signal:
        manifestPlaceholders = [onesignal_app_id               : "",
                                onesignal_google_project_number: ""]

and provided correct values only in the application's build.gradle

        // One Signal:
        manifestPlaceholders = [onesignal_app_id: "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
                                onesignal_google_project_number: "REMOTE"]

and the project works now. I don't get any build errors and notifications are delivered correctly.

BTW. I have a question about this solution you posted in the other issue. If I initialize OneSignal using the method from this post, I overwrite the values provided in the build.gradle? If yes, why is it necessary to provide values in the build.gradle file? Is it correct to put empty strings as onesignal_app_id manifest placeholders and put the proper app ID only in this initializing method?

@6franek The values shouldn't change after the app has been released so setting the value at build time makes the most sense. In addition this is part of the road work need for users to be subscribed automatically after the developer pushes out an update with OneSignal added to it. Also this this provides a more consistent way to add set these values for our wrapper SDKs. We haven't implemented these yet but these values need to be outside of run time code for the above to be possible.

However some developer have setup their apps to with different environments or provide white labeling that wasn't designed with gradle configs in mind. We have kept our old init method as an option for these cases.

Was this page helpful?
0 / 5 - 0 ratings