15

I am modifying current android project so it can be installed on same device for multiple flavors and build configs.

build.gradle:

{
    // ...
    defaultConfig {
        applicationId "com.myapp"
        manifestPlaceholders = [
            manifestApplicationId: "${applicationId}",
            onesignal_app_id: "xxxx",
            onesignal_google_project_number: "xxxx"
        ]
    // ...
    }

    productFlavors {
        production {
            applicationId "com.myapp"
            // ...
        }

        dev {
            applicationId "com.myapp.dev"
            // ...
        }

        // ...
    }

    buildTypes {
        release {
            // ...
        }

        debug {
            applicationIdSuffix ".debug"
            // ...
        }
    }

    // ...
}

AndroidManifest.xml:

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

    <receiver
        android:name="com.onesignal.GcmBroadcastReceiver"
        android:permission="com.google.android.c2dm.permission.SEND" >
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <category android:name="${applicationId}" />
        </intent-filter>
    </receiver>

    <!-- ... -->
</manifest>

When I compile both debug and release version of the same flavor, I got error message:

...

INSTALL_FAILED_DUPLICATE_PERMISSION

perm=com.myapp.permission.C2D_MESSAGE

pkg=com.myapp.dev

...

manifestApplicationId placeholder came from AndroidManifest.xml on OneSignal library as instructed on https://documentation.onesignal.com/docs/android-sdk-setup

Anybody have a clue how to fix this problem? Thank you.

Gdeglin
  • 12,432
  • 5
  • 49
  • 65
Wellsen
  • 325
  • 1
  • 3
  • 9
  • I think this is a duplicate of http://stackoverflow.com/questions/27043933/install-failed-duplicate-permission-c2d-message. You need to uninstall the app from the device and reinstall it. – Blehi Sep 01 '16 at 19:30
  • 1
    @Blehi: Of course uninstalling the installed variant will get rid of the error. But my goal is to install all variants to the same device. Thank you. – Wellsen Sep 02 '16 at 03:20

1 Answers1

21

OneSignal requires the manifestPlaceholders key manifestApplicationId to be set to your applicationId (AKA your package name).

This can be done by setting it in your buildTypes like the following.

buildTypes {
   debug {
      defaultConfig {
         manifestPlaceholders = [manifestApplicationId          : "${applicationId}",
                                 onesignal_app_id               : "11111111-1111-1111-1111-111111111111",
                                 onesignal_google_project_number: "111111111"]
       }
   }

   release {
      defaultConfig {
         manifestPlaceholders = [manifestApplicationId          : "${applicationId}",
                                 onesignal_app_id               : "22222222-2222-2222-2222-222222222222",
                                 onesignal_google_project_number: "222222222"]
      }
   }
}

Update 1: OneSignal-Android 3.3.0 no longer requires manifestApplicationId.

Update 2: OneSignal-Android 4.0.0 no longer requires any manifestPlaceholders values. Instead OneSignal.setAppId(ONESIGNAL_APP_ID) needs to be called at runtime.

jkasten
  • 3,899
  • 3
  • 32
  • 51
  • Thank you for your response. I added these placeholders inside defaultConfig closure on every buildType like above code, now I got build failed: Error:Execution failed for task ':app:processProductionReleaseManifest'. > No record for key [permission#${manifestApplicationId}.permission.C2D_MESSAGE] – Wellsen Sep 02 '16 at 03:44
  • Edit: I forgot to remove tools:overrideLibrary and tools:override on my manifest which I added before. After I did that, the error is back to "duplicate permission". Still trying to find the solution by now... – Wellsen Sep 02 '16 at 04:19
  • @Wellsen After you make this `.gradle` change you will need to uninstall both apps then reinstall them. One of them most likely has the wrong permission so this is needed. – jkasten Sep 02 '16 at 20:50
  • 1
    Are the nested `defaultConfig` blocks intentional here? – stkent Dec 14 '16 at 21:18
  • 1
    without `defaultConfig` nesting. gradlew fails to build on mine – Jan Jun 05 '17 at 10:30
  • Do we need to create two different projects in google console for debug and production? – Ram Mandal May 29 '18 at 11:55
  • @RamMandal No, you can use the same FCM SenderID / Project number for any many package names as you want. – jkasten May 29 '18 at 22:30
  • How can we use it for different flavours build type? – Ram Mandal May 30 '18 at 05:22
  • Can we create two different Onesignal project with same server api key and project no. For live and production? – Ram Mandal Jun 05 '18 at 06:14
  • I use version `3.6` with productFlavors, it still need to set `manifestApplicationId`. – 林果皞 Dec 03 '18 at 12:12