安卓应用在接收OneSignal推送通知时崩溃

8

我开发了一个React Native应用程序,每当应用程序处于活动状态时收到来自OneSignal的推送通知时都会崩溃。

错误是java.lang.NoSuchMethodError: No static method zzc

如果在应用程序处于后台时接收到通知,则没有问题。

这是我的build.gradle文件:

buildscript {
    ext {
        googlePlayServicesLocationVersion = "17.0.0"
        buildToolsVersion = "28.0.3"
        minSdkVersion = 21
        compileSdkVersion = 28
        targetSdkVersion = 28
        supportLibVersion = "28.0.0"
        appCompatVersion = "1.0.2"
    }
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.3.0'
    }
}

allprojects {
    repositories {

        configurations.all {
            resolutionStrategy.eachDependency { DependencyResolveDetails details ->
            def requested = details.requested
           if (requested.group == 'com.google.android.gms') {
              details.useVersion '17.0.0'
           }
           if (requested.group == 'com.google.firebase') {
              details.useVersion '17.0.0'
             }
           }
         }
        google()
        mavenLocal()

        jcenter()
        maven {
            // All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
            url "$rootDir/../node_modules/react-native/android"
        }

        maven {
           url "$rootDir/../node_modules/react-native-background-geolocation/android/libs"
        }

            maven {
                url "$rootDir/../node_modules/react-native-background-fetch/android/libs"
            }
    }
}


task wrapper(type: Wrapper) {
    gradleVersion = '4.10'
    distributionUrl = distributionUrl.replace("bin", "all")
}

我的app/build.gradle文件:

apply plugin: "com.android.application"

import com.android.build.OutputFile

project.ext.react = [
    entryFile: "index.js"
]

apply from: '../../node_modules/react-native-unimodules/gradle.groovy'
apply from: "../../node_modules/react-native/react.gradle"

Project background_geolocation = project(':react-native-background-geolocation')
apply from: "${background_geolocation.projectDir}/app.gradle"


def enableSeparateBuildPerCPUArchitecture = false

def enableProguardInReleaseBuilds = false

android {
    compileSdkVersion rootProject.ext.compileSdkVersion
    buildToolsVersion rootProject.ext.buildToolsVersion

    defaultConfig {
        applicationId "XXX"
        minSdkVersion rootProject.ext.minSdkVersion
        targetSdkVersion rootProject.ext.targetSdkVersion
        versionCode 200
        versionName "24.10"
    }
    splits {
        abi {
            reset()
            enable enableSeparateBuildPerCPUArchitecture
            universalApk false  // If true, also generate a universal APK
            include "armeabi-v7a", "x86", "arm64-v8a", "x86_64"
        }
    }
    signingConfigs {
        release {
            if (project.hasProperty('MYAPP_UPLOAD_STORE_FILE')) {
                storeFile file(MYAPP_UPLOAD_STORE_FILE)
                storePassword MYAPP_UPLOAD_STORE_PASSWORD
                keyAlias MYAPP_UPLOAD_KEY_ALIAS
                keyPassword MYAPP_UPLOAD_KEY_PASSWORD
            }
        }
    }
    buildTypes {
        release {
            minifyEnabled enableProguardInReleaseBuilds
            proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
            signingConfig signingConfigs.release
        }
    }
    // applicationVariants are e.g. debug, release
    applicationVariants.all { variant ->
        variant.outputs.each { output ->
            // For each separate APK per architecture, set a unique version code as described here:
            // http://tools.android.com/tech-docs/new-build-system/user-guide/apk-splits
            def versionCodes = ["armeabi-v7a":1, "x86":2, "arm64-v8a": 3, "x86_64": 4]
            def abi = output.getFilter(OutputFile.ABI)
            if (abi != null) {  // null for the universal-debug, universal-release variants
                output.versionCodeOverride =
                        versionCodes.get(abi) * 1048576 + defaultConfig.versionCode
            }
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {
    implementation project(':@react-native-community_netinfo')
    implementation project(':react-native-fast-image')
    implementation project(':react-native-device-info')
    implementation project(':react-native-orientation-locker')
    implementation project(':@react-native-community_slider')
    implementation project(':react-native-webview')
    implementation project(':react-native-splash-screen')
    implementation project(':@react-native-community_async-storage')
    implementation project(':react-native-sentry')
    implementation project(':react-native-background-fetch')
    implementation project(':react-native-maps')
    implementation project(':react-native-iap')
    implementation project(':react-native-gesture-handler')
    implementation project(':react-native-background-geolocation')
    implementation project(':react-native-webrtc')
    implementation project(':react-native-svg')
    implementation project(':react-native-onesignal')
    implementation project(':react-native-incall-manager')
    implementation project(':react-native-fs')
    implementation project(':react-native-reanimated')
    implementation fileTree(dir: "libs", include: ["*.jar"])
    implementation "com.android.support:appcompat-v7:${rootProject.ext.supportLibVersion}"
    implementation "com.facebook.react:react-native:+"  // From node_modules
    implementation 'com.android.support:multidex:1.0.3'
    implementation 'com.facebook.fresco:animated-gif:1.10.0'
    implementation "com.facebook.fresco:animated-base-support:1.3.0"
    addUnimodulesDependencies()
}

// Run this once to be able to run the application with BUCK
// puts all compile dependencies into folder libs for BUCK to use
task copyDownloadableDepsToLibs(type: Copy) {
    from configurations.compile
    into 'libs'
}

// [Added by react-native-background-geolocation] Purge debug sounds from release build.
def purgeBackgroundGeolocationDebugResources(applicationVariants) {
    if ((rootProject.ext.has("removeBackgroundGeolocationDebugSoundsInRelease")) && (rootProject.ext.removeBackgroundGeolocationDebugSoundsInRelease == false)) return
    applicationVariants.all { variant ->
        if (variant.buildType.name == "release") {
            println("[react-native-background-geolocation] Purging debug resources in release build")
            variant.mergeResources.doLast {
                delete(fileTree(dir: variant.mergeResources.outputDir, includes: ["raw_tslocationmanager*"]))
            }
        }
    }
}

你知道问题可能出自哪里吗?

你使用的是哪个版本的OneSignal? - Zun
似乎OneSignal库https://github.com/OneSignal/OneSignal-Android-SDK/issues/286中存在一个错误,他们建议使用他们的插件https://github.com/OneSignal/OneSignal-Android-SDK/issues/286#issuecomment-337885909。 - Bracadabra
我怀疑这可能是代码混淆的问题。如果你仔细观察方法名,即“zzc”,它是一个重命名的方法。你需要检查你的mapping.txt文件并找到这个字符串,这样你就能找到相应的方法名。只需在你的proguard配置文件中标记一个条目以跳过对该方法的混淆。 - Susheel Tickoo
4个回答

3
这可能是由以下原因之一引起的:
  1. OneSignal SDK版本低于3.6.0
  2. 您使用的Android Support库版本早于26
  3. 或者如果在您的应用程序中还有其他GCM/FCM广播接收器也在显示通知
根据您的构建脚本,第2项没问题。您需要在项目中应用OneSignal gradle插件。
    buildscript {
    repositories {
        maven { url 'https://plugins.gradle.org/m2/'}
    }
    dependencies {
        classpath 'gradle.plugin.com.onesignal:onesignal-gradle-plugin: [0.10.1, 0.99.99]'
    }
}
apply plugin: 'com.onesignal.androidsdk.onesignal-gradle-plugin'

1

1
请将以下代码添加到app/build.gradle文件的顶部。
buildscript {
    repositories {
        maven { url 'https://plugins.gradle.org/m2/' } // Gradle Plugin Portal 
    }
    dependencies {
        classpath 'gradle.plugin.com.onesignal:onesignal-gradle-plugin:[0.12.1, 0.99.99]'
    }
}

apply plugin: 'com.onesignal.androidsdk.onesignal-gradle-plugin'

在添加了上述代码后,打开命令提示符,导航到React Native项目的android文件夹并运行gradlew clean。最后运行该项目。
如果遇到相同的问题,请告诉我。

你好,我仍然在Android 9和10上遇到相同的问题。低版本完美运行。 - Pankaj Chaturvedi

0

你必须把这个添加到最上面。我认为你可能没有激活插件。

buildscript {
    repositories {
        maven { url 'https://plugins.gradle.org/m2/' } // Gradle Plugin Portal 
    }
    dependencies {
        classpath 'gradle.plugin.com.onesignal:onesignal-gradle-plugin:[0.12.1, 0.99.99]'
    }
}

apply plugin: 'com.onesignal.androidsdk.onesignal-gradle-plugin'

仍然无法在 Android 设备的 <8 版本中运行。 - Pankaj Chaturvedi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接