多个构建风格的 applicationId 清单占位符不起作用

applicationId manifest placeholder for multiple build flavors not working

我正在修改当前的 android 项目,以便它可以安装在同一设备上以实现多种风格和构建配置。

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>

当我同时编译相同风格的调试版和发布版时,我收到错误信息:

...

INSTALL_FAILED_DUPLICATE_PERMISSION

烫发=com.myapp.permission.C2D_MESSAGE

pkg=com.myapp.dev

...

manifestApplicationId 占位符来自 AndroidManifest.xml OneSignal 库,按照 https://documentation.onesignal.com/docs/android-sdk-setup

上的说明

有人知道如何解决这个问题吗?谢谢。

OneSignal 需要将 manifestPlaceholdersmanifestApplicationId 设置为您的 applicationId(也就是您的包名称)。

这可以通过在您的 buildTypes 中设置来完成,如下所示。

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"]
      }
   }
}

更新 1:OneSignal-Android 3.3.0 不再需要 manifestApplicationId.

更新 2:OneSignal-Android 4.0.0 不再需要任何 manifestPlaceholders 值。 Instead OneSignal.setAppId(ONESIGNAL_APP_ID) needs to be called at runtime.