简体   繁体   中英

Android app crashes in Release but not in Debug NullPointerException

I recently changed 2 lines of code as a workaround due to changes to the data being returned from an API I'm using. Now the app is crashing when using the release apk and aab. However, when I'm using the app through the Android Emulator on API 27 and connecting an API 27 device to my computer running the debug apk, the app works flawlessly.

I'm really stumped on this problem, and do not understand the error messages at all.

FATAL EXCEPTION: main
Process: com.guy.aqi, PID: 8328
java.lang.NullPointerException: throw with null exception
    at com.guy.aqi.n.a(Unknown Source:3)
    at com.guy.aqi.m.b(CurrentAirQualityFragment.java:8)
    at com.guy.aqi.m.b(CurrentAirQualityFragment.java:6)
    at com.guy.aqi.d.a(Unknown Source:4)
    at b.a.a.a.m.c(StringRequest.java:4)
    at b.a.a.a.m.a(StringRequest.java:1)
    at b.a.a.h$a.run(ExecutorDelivery.java:4)
    at android.os.Handler.handleCallback(Handler.java:790)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6494)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

My API stopped sending the "main pollutant" String, so I changed this line:

textViewMainPollutantUS.setText("U.S. Main Pollutant: " decodePollutant(mainPollutantUS));

to

textViewMainPollutantUS.setText("");

and this line:

textViewMainPollutantCN.setText("China Main Pollutant: " decodePollutant(mainPollutantCN));

to

textViewMainPollutantCN.setText("");

I expected changing these lines would fix the issue. But now the issue seems to be fixed in debug version of the app, but not the release version.

proguard-rules.pro

# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
#   http://developer.android.com/guide/developing/tools/proguard.html

# 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 *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile

# JSR 305 annotations are for embedding nullability information.
-dontwarn javax.annotation.**

# A resource is loaded with a relative path so the package of this class must be preserved.
-keepnames class okhttp3.internal.publicsuffix.PublicSuffixDatabase

# Animal Sniffer compileOnly dependency to ensure APIs are compatible with older versions of Java.
-dontwarn org.codehaus.mojo.animal_sniffer.*

# OkHttp platform used only on JVM and when Conscrypt dependency is available.
-dontwarn okhttp3.internal.platform.ConscryptPlatform

# Prevent Proguard from inlining methods that are intentionally extracted to ensure locals have a
# constrained liveness scope by the GC. This is needed to avoid keeping previous request references
# alive for an indeterminate amount of time. See also https://github.com/google/volley/issues/114
-keepclassmembers,allowshrinking,allowobfuscation class com.android.volley.NetworkDispatcher {
    void processRequest();
}
-keepclassmembers,allowshrinking,allowobfuscation class com.android.volley.CacheDispatcher {
    void processRequest();
}

##---------------Begin: proguard configuration for Gson  ----------
# Gson uses generic type information stored in a class file when working with fields. Proguard
# removes such information by default, so configure it to keep all of it.
-keepattributes Signature

# For using GSON @Expose annotation
-keepattributes *Annotation*

# Gson specific classes
-dontwarn sun.misc.**
#-keep class com.google.gson.stream.** { *; }

# Application classes that will be serialized/deserialized over Gson
-keep class com.google.gson.examples.android.model.** { *; }

# Prevent proguard from stripping interface information from TypeAdapterFactory,
# JsonSerializer, JsonDeserializer instances (so they can be used in @JsonAdapter)
-keep class * implements com.google.gson.TypeAdapterFactory
-keep class * implements com.google.gson.JsonSerializer
-keep class * implements com.google.gson.JsonDeserializer

##---------------End: proguard configuration for Gson  ----------

-keep class com.crashlytics.** { *; }
-keepattributes SourceFile,LineNumberTable

For me, adding lines (you may have other name for package where you put your POJO files):

-keep class [mypackagename].model.** { *; }

-keep class [mypackagename].datamodel.** { *; }

to proguard.rules worked perfectly, then options:

android
{
    ...

    buildTypes {

        release {

            minifyEnabled true 
            shrinkResources true

        }
    }
}

are set in build.gradle (Module: app)

Edit:-

Replace this

minifyEnabled true

to

minifyEnabled false

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM