未在路径中找到 class "com.onesignal.GcmBroadcastReceiver":DexPathList

Didn't find class "com.onesignal.GcmBroadcastReceiver" on path: DexPathList

我是 Xamarin 的新手,正在尝试让 OneSignal 推送通知在 Android 中工作。当 运行 模拟器和我从 OneSignal 发出推送通知时,它在模拟器上成功通过。但是,当我通过 Play 商店部署应用程序并打开应用程序时,触发推送通知会使应用程序崩溃并出现以下错误:

java.lang.RuntimeException: Unable to instantiate receiver com.onesignal.GcmBroadcastReceiver: java.lang.ClassNotFoundException: Didn't find class "com.onesignal.GcmBroadcastReceiver" on path: DexPathList[[zip file "/data/app/com.woodfordgroup.whybuycars-Bl5_t81uXklo5mpGZRvJcw==/base.apk"],nativeLibraryDirectories=[/data/app/com.woodfordgroup.whybuycars-Bl5_t81uXklo5mpGZRvJcw==/lib/arm64, /data/app/com.woodfordgroup.whybuycars-Bl5_t81uXklo5mpGZRvJcw==/base.apk!/lib/arm64-v8a, /system/lib64]]
    at android.app.ActivityThread.handleReceiver(ActivityThread.java:3432)
    at android.app.ActivityThread.access00(ActivityThread.java:207)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1712)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:201)
    at android.app.ActivityThread.main(ActivityThread.java:6831)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:927)
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.onesignal.GcmBroadcastReceiver" on path: DexPathList[[zip file "/data/app/com.woodfordgroup.whybuycars-Bl5_t81uXklo5mpGZRvJcw==/base.apk"],nativeLibraryDirectories=[/data/app/com.woodfordgroup.whybuycars-Bl5_t81uXklo5mpGZRvJcw==/lib/arm64, /data/app/com.woodfordgroup.whybuycars-Bl5_t81uXklo5mpGZRvJcw==/base.apk!/lib/arm64-v8a, /system/lib64]]
    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:171)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
    at android.app.AppComponentFactory.instantiateReceiver(AppComponentFactory.java:84)
    at androidx.core.app.CoreComponentFactory.instantiateReceiver(Unknown Source:0)
    at android.app.ActivityThread.handleReceiver(ActivityThread.java:3425)
    ... 8 more

这是因为 OneSignal 库没有包含在版本中吗?我还看到提到 AndroidX 或 multi-dex 问题。这是 android 清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="15" android:versionName="1.0" package="com.woodfordgroup.whybuycars" android:installLocation="auto">
    <uses-sdk android:minSdkVersion="21" android:targetSdkVersion="29" />
    <application android:label="WhyBuyCars" android:theme="@style/MainTheme" android:icon="@drawable/whybuycars_icon_512x512">
        <provider android:name="android.support.v4.content.FileProvider" android:authorities="${applicationId}.fileprovider" android:exported="false" android:grantUriPermissions="true">
            <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths" />
        </provider>
        <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="com.woodfordgroup.whybuycars" />
            </intent-filter>
        </receiver>
    </application>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-feature android-name="android.hardware.camera" android:required="false" android:glEsVersion="0x00030002" />
    <permission android:name="com.woodfordgroup.whybuycars.permission.C2D_MESSAGE" android:protectionLevel="signature" />
    <uses-permission android:name="com.woodfordgroup.whybuycars.permission.C2D_MESSAGE" />
</manifest>

这是我的 android 项目属性(目标版本是 29):

我还想知道在我从 Nuget 安装 OneSignal 时包含各种库是否出了问题。这是我的项目参考列表:

我遇到了完全相同的问题,如果在设置中禁用 R8 代码收缩器(或在 android .csproject 文件中的 <AndroidLinkTool>r8</AndroidLinkTool> 之后添加 <AndroidR8ExtraArguments>--no-tree-shaking</AndroidR8ExtraArguments>,我可以收到通知而不会崩溃) . onesignal 广播接收器的收缩器似乎存在一些问题 class

将其添加到混淆文件中。

-keep class com.onesignal.** { *; }

来源:参见 oncky 对问题的评论。