How to debug android app on release mode?

Viewed 4504

When I create the apk on release mode it crashes on start up

The problem happens when I enable minifyEnabled for proguard and I solved it by adding below code to proguard-rules.pro

-keep class my.package.name.** {*;}

I think the reason is proguard delete some of my code but I don't know which part because it obscure my code and reading logcat is useless . Is there anyway I understand the logcat message ?

java.lang.NullPointerException: throw with null exception
    at e.a.z.a(:176)
    at i.n.run(:71)
4 Answers

Since this is a production running application, do NOT compromise obfuscation (using keepattributes) if you just need to understand crash reports.

This is detailed in the android / google guidelines. You can upload symbol mapping files created by proguard that allows the crash reports to be de-obfuscated.

The mapping files are usually generated here:

build/outputs/mapping/release/mapping.txt

This is explained here : https://developer.android.com/studio/build/shrink-code#decode-stack-trace

Proguard has a Retrace API described here : https://www.guardsquare.com/en/products/proguard/manual/retrace

And here on to upload to google-play to get de-obfuscated reports : https://support.google.com/googleplay/android-developer/answer/6295281

You can configure proguard to have some more information.

-keepattributes SourceFile,LineNumberTable

This would preserve file name and line numbers as well, so you will have more data in your logcat.

Also, use minifyEnabled on your debug build so that proguard would be applied to your debug build and you would be able to debug it better.

Once you find and fix the issue, you can remove it from the proguard.

Android requires the following proguard rules to keep an Android app working:

-keep public class * extends android.app.Activity
-keep public class * extends androidx.appcompat.app.AppCompatActivity
-keep public class * extends android.app.Service
-keep public class * extends android.content.BroadcastReceiver
-keep public class * extends android.content.ContentProvider


-keep public class * extends android.view.View {
    public <init>(android.content.Context);
    public <init>(android.content.Context, android.util.AttributeSet);
    public <init>(android.content.Context, android.util.AttributeSet, int);
    public void set*(...);
}

-keepclasseswithmembers class * {
    public <init>(android.content.Context, android.util.AttributeSet);
}

-keepclasseswithmembers class * {
    public <init>(android.content.Context, android.util.AttributeSet, int);
}

-keepclassmembers class * extends android.content.Context {
   public void *(android.view.View);
   public void *(android.view.MenuItem);
}

-keepclassmembers class * implements android.os.Parcelable {
    static ** CREATOR;
}

-keepclassmembers class **.R$* {
    public static <fields>;
}

-keepclassmembers class * {
    @android.webkit.JavascriptInterface <methods>;
}

If you want to de-obfuscate the stacktrace, take a look at this guide.

If you would like to go step-by-step through your release build (the same way as you do in debug), try adding debuggable true to your gradle configuration (app\build.gradle):

android
{
  buildTypes
  {
    release {
      proguardFiles 'your-proguard-config.pro'
      debuggable true   //<-- add this
    }
  }
}
Related