android - 从 Google Play 下载应用程序后首次启动时崩溃(ExceptionInInitializerError)

标签 android react-native gradle

RN 0.55.4
Gradle: 2.2.3 

Android 上存在一个问题。我们更新了一些 node_modules 并对版本 0.55 进行了原生 react ,一切看起来都很好,但是当我们将这个新版本发布到 google beta 中然后我们下载它来测试应用程序时,它总是在第一次运行时崩溃。我们再次运行它,一切正常,应用程序运行良好。我们不知道出了什么问题 我们从 fabric.io 获得的错误,当我们从 play 下载该应用程序时,它只会发生一次。

   Fatal Exception: java.lang.ExceptionInInitializerError
       at tv.dailyme.android.util.ServerConfig.getUrlRegisterWithoutPhonenr(Unknown Source)
       at tv.dailyme.android.util.UserRegistration.setInstallReferrer(Unknown Source)
       at tv.dailyme.android.core.receiver.InstallReceiver.onReceive(Unknown Source)
       at android.app.ActivityThread.handleReceiver(ActivityThread.java:3102)
       at android.app.ActivityThread.-wrap18(ActivityThread.java)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1598)
       at android.os.Handler.dispatchMessage(Handler.java:102)
       at android.os.Looper.loop(Looper.java:173)
       at android.app.ActivityThread.main(ActivityThread.java:6459)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:938)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:828)

这也是同时发生的

Fatal Exception: java.lang.RuntimeException: Unable to start receiver tv.dailyme.android.core.receiver.Autostart: java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.sqlite.SQLiteDatabase android.content.Context.openOrCreateDatabase(java.lang.String, int, android.database.sqlite.SQLiteDatabase$CursorFactory, android.database.DatabaseErrorHandler)' on a null object reference
       at android.app.ActivityThread.handleReceiver(ActivityThread.java:3116)
       at android.app.ActivityThread.-wrap18(ActivityThread.java)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1598)
       at android.os.Handler.dispatchMessage(Handler.java:102)
       at android.os.Looper.loop(Looper.java:173)
       at android.app.ActivityThread.main(ActivityThread.java:6459)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:938)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:828)

我认为我们的 gradle 设置有问题

    def enableSeparateBuildPerCPUArchitecture = false

/**
 * Run Proguard to shrink the Java bytecode in release builds.
 */
def enableProguardInReleaseBuilds = false

// NOTE LSA: if you change this value, You will have to change same value in "Project: Awe" => build.gradle
def googlePlayServiceVersion  = '11.0.4'

android {
    compileSdkVersion 26
    buildToolsVersion "26.0.3"

    defaultConfig {
        applicationId "com.awesome.app"
        minSdkVersion 17
        targetSdkVersion 25
        versionCode 1007138
        versionName "6.6.0"
        renderscriptTargetApi 23
        renderscriptSupportModeEnabled true
        jackOptions {
            enabled true
        }
        vectorDrawables.useSupportLibrary = true
        ndk {
            abiFilters "armeabi-v7a", "x86"
        }
        multiDexEnabled true
        manifestPlaceholders = [manifestApplicationId: "${applicationId}",
                                onesignal_app_id: "7xxxxxxxxxxxxxxxxxxxDDc",
                                onesignal_google_project_number: "9999999999"]
    }
    compileOptions {
      sourceCompatibility JavaVersion.VERSION_1_8
      targetCompatibility JavaVersion.VERSION_1_8
    }
    dexOptions {
        javaMaxHeapSize "4g"
    }
    lintOptions {
        abortOnError false
    }
    signingConfigs {
        release {
            storeFile file(MYAPP_RELEASE_STORE_FILE)
            storePassword MYAPP_RELEASE_STORE_PASSWORD
            keyAlias MYAPP_RELEASE_KEY_ALIAS
            keyPassword MYAPP_RELEASE_KEY_PASSWORD
        }
    }
    splits {
        abi {
            reset()
            enable enableSeparateBuildPerCPUArchitecture
            universalApk false  // If true, also generate a universal APK
            include "armeabi-v7a", "x86"
        }
    }
    buildTypes {

        debug {
            manifestPlaceholders = [excludeSystemAlertWindowPermission: "false"]
        }

        release {
            manifestPlaceholders = [excludeSystemAlertWindowPermission: "true"]
            minifyEnabled enableProguardInReleaseBuilds
            proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
            signingConfig signingConfigs.release
        }

    }
    packagingOptions {
        exclude 'META-INF/license.txt'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/notice.txt'
        exclude 'jsr305_annotations/Jsr305_annotations.gwt.xml'
        exclude 'META-INF/DEPENDENCIES'

        exclude 'error_prone/Annotations.gwt.xml'
        exclude 'third_party/java_src/error_prone/project/annotations/Annotations.gwt.xml'
        exclude 'third_party/java_src/error_prone/project/annotations/Google_internal.gwt.xml'
    }
    // 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]
            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
            }
        }
    }
}

repositories {
    jcenter()
    maven { url 'https://maven.fabric.io/public' }
}

dependencies {
    compile project(':react-native-social-share')
    compile project(':react-native-device-info')
    compile project(':react-native-fetch-blob')
    compile project(':react-native-blur')
    compile project(':react-native-admob')
    compile project(':react-native-vector-icons')
    compile project(':react-native-orientation')
    compile project(':react-native-spinkit')
    compile project(':react-native-i18n')
    compile project(':react-native-share')
    compile project(':react-native-cookies')
    compile fileTree(dir: "libs", include: ["*.jar"])
    // TODO LSA when we upgrade RN to 0.56 and gradle 3 we will have to remove all FORCE = TRUE and update com.android.support libs
    compile "com.android.support:appcompat-v7:26.1.0"
    compile "com.android.support:palette-v7:26.1.0"
    compile("com.facebook.react:react-native:0.55.4") { force = true }

    compile project(':react-native-onesignal')

    compile 'tv.nexx:nexxplay-android:3.2.1'

    compile(name: 'library-release', ext: 'aar')

    compile('org.simpleframework:simple-xml:2.7.1') {
        exclude group: 'stax', module: 'stax-api'
        exclude group: 'xpp3', module: 'xpp3'
    }

    compile project(":react-native-device-info")
    compile fileTree(dir: "node_modules/react-native-device-info/android/libs", include: ["*.jar"])
    compile 'org.apache.mina:mina-statemachine:2.0.9'

    compile("com.google.android.gms:play-services-analytics:${googlePlayServiceVersion}") {
        force = true;
    }

    compile("com.google.android.gms:play-services-ads:${googlePlayServiceVersion}") {
        force = true;
    }

    compile("com.google.android.gms:play-services-gcm:${googlePlayServiceVersion}") {
        force = true;
    }

    compile("com.google.android.gms:play-services-location:${googlePlayServiceVersion}") {
        force = true;
    }

    compile("com.google.android.gms:play-services-base:${googlePlayServiceVersion}") {
        force = true;
    }

    compile(name: 'infonlinelib_1.1.5.1', ext: 'aar')
    compile(name: 'IRSurveyLib-1.4.0', ext:'aar')

    compile 'com.android.support:multidex:1.0.1'

    compile group: 'com.alibaba', name: 'fastjson', version: '1.1.25'
    compile('com.crashlytics.sdk.android:crashlytics:2.7.1@aar') {
        transitive = true;
    }
    compile project(':react-native-fabric')
    compile 'com.alibaba:fastjson:1.1.25'
    compile 'com.adjust.sdk:adjust-android:4.7.0'
    compile 'ad.nugg.android:sdk:3.2.2'
}

我们的应用程序 ProGuard 规则

# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in /usr/local/Cellar/android-sdk/24.3.3/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the proguardFiles
# directive in build.gradle.
#
# For more details, see
#   http://developer.android.com/guide/developing/tools/proguard.html

# Add any project specific keep options here:

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
#   public *;
#}

# Disabling obfuscation is useful if you collect stack traces from production crashes
# (unless you are using a system that supports de-obfuscate the stack traces).
-dontobfuscate

# React Native

# Keep our interfaces so they can be used by other ProGuard rules.
# See http://sourceforge.net/p/proguard/bugs/466/
-keep,allowobfuscation @interface com.facebook.proguard.annotations.DoNotStrip
-keep,allowobfuscation @interface com.facebook.proguard.annotations.KeepGettersAndSetters
-keep,allowobfuscation @interface com.facebook.common.internal.DoNotStrip

# Do not strip any method/class that is annotated with @DoNotStrip
-keep @com.facebook.proguard.annotations.DoNotStrip class *
-keep @com.facebook.common.internal.DoNotStrip class *
-keepclassmembers class * {
    @com.facebook.proguard.annotations.DoNotStrip *;
    @com.facebook.common.internal.DoNotStrip *;
}

-keepclassmembers @com.facebook.proguard.annotations.KeepGettersAndSetters class * {
  void set*(***);
  *** get*();
}

-keep class * extends com.facebook.react.bridge.JavaScriptModule { *; }
-keep class * extends com.facebook.react.bridge.NativeModule { *; }
-keepclassmembers,includedescriptorclasses class * { native <methods>; }
-keepclassmembers class *  { @com.facebook.react.uimanager.UIProp <fields>; }
-keepclassmembers class *  { @com.facebook.react.uimanager.annotations.ReactProp <methods>; }
-keepclassmembers class *  { @com.facebook.react.uimanager.annotations.ReactPropGroup <methods>; }

-dontwarn com.facebook.react.**

# TextLayoutBuilder uses a non-public Android constructor within StaticLayout.
# See libs/proxy/src/main/java/com/facebook/fbui/textlayoutbuilder/proxy for details.
-dontwarn android.text.StaticLayout

# okhttp

-keepattributes Signature
-keepattributes *Annotation*
-keep class okhttp3.** { *; }
-keep interface okhttp3.** { *; }
-dontwarn okhttp3.**

# okio

-keep class sun.misc.Unsafe { *; }
-dontwarn java.nio.file.*
-dontwarn org.codehaus.mojo.animal_sniffer.IgnoreJRERequirement
-dontwarn okio.**


#GAID
-keep class * extends java.util.ListResourceBundle {
  protected Object[][] getContents();
}

-keep public class com.google.android.gms.common.internal.safeparcel.SafeParcelable {
  public static final *** NULL;
}

-keepnames @com.google.android.gms.common.annotation.KeepName class *
-keepclassmembernames class * {
  @com.google.android.gms.common.annotation.KeepName *;
}

-keepnames class * implements android.os.Parcelable {
  public static final ** CREATOR;
}

-keepattributes EnclosingMethod
-keepattributes InnerClasses
-dontwarn InnerClasses
-dontoptimize
-keep class com.newrelic.** { *; }
-dontwarn com.newrelic.**
-keepattributes Exceptions, Signature, InnerClasses

最佳答案

Fatal Exception: java.lang.RuntimeException: Unable to start receiver tv.dailyme.android.core.receiver.Autostart: java.lang.NullPointerException:

Attempt to invoke virtual method 'android.database.sqlite.SQLiteDatabase android.content.Context.openOrCreateDatabase(java.lang.String, int, android.database.sqlite.SQLiteDatabase$CursorFactory, android.database.DatabaseErrorHandler)'

on a null object reference

看起来,好像您的 ProGuard 配置可能不足以混淆某些东西。

你可以添加一个 != null 检查类 tv.dailyme.android.core.receiver.Autostart,为了至少不产生抛出 NPE。似乎需要改进 ProGuard -keep 规则。

关于android - 从 Google Play 下载应用程序后首次启动时崩溃(ExceptionInInitializerError),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51981277/

相关文章:

android - MPAndroidChart - 如何仅在最后一个条目中绘制圆圈?

android - Android Studio:无法同步Gradle项目

react-native - 导航器已被弃用,即使我不使用导航器也已被删除

android - Detox 至少 75% 的 View 区域显示给用户

javascript - ListView.DataSource 为 React Native 循环数据

android - 建立apk时有什么方法可以排除xxxhdpi资源

java - 类(class)不变

android - 如何在 Android Studio 1.2x 中启用自动导入

Android Firebase 远程配置 : Application name is not set. 调用 Builder#setApplicationName

android - 如何直接从 APK 运行嵌入式 DEX 代码