0

I run an app in a real device (Android 7.0 Table) from Android Studio, it's OK.

Now I create a APK file of the app, then I install it on my real device, finish I run the app.

I get the following errors, why ?

Why can an app run in real device by Android Studio and failed by install?

E:\>cd E:\Android_SDK\platform-tools

E:\Android_SDK\platform-tools>adb logcat --buffer=crash

03-20 12:05:29.378 15996 15996 E AndroidRuntime: FATAL EXCEPTION: main
03-20 12:05:29.378 15996 15996 E AndroidRuntime: Process: info.dodata.voicerecorder, PID: 15996
03-20 12:05:29.378 15996 15996 E AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{info.dodata.voicerecorder/info.dodata.voicerecorder.ui.ActivityMain}: android.view.InflateException: Binary XML file line #25: Binary XML file line #25: Error inflating class fragment
03-20 12:05:29.378 15996 15996 E AndroidRuntime:        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2804)
03-20 12:05:29.378 15996 15996 E AndroidRuntime:        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2875)
03-20 12:05:29.378 15996 15996 E AndroidRuntime:        at android.app.ActivityThread.-wrap12(ActivityThread.java)
03-20 12:05:29.378 15996 15996 E AndroidRuntime:        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1578)
03-20 12:05:29.378 15996 15996 E AndroidRuntime:        at android.os.Handler.dispatchMessage(Handler.java:105)
03-20 12:05:29.378 15996 15996 E AndroidRuntime:        at android.os.Looper.loop(Looper.java:156)
03-20 12:05:29.378 15996 15996 E AndroidRuntime:        at android.app.ActivityThread.main(ActivityThread.java:6617)
03-20 12:05:29.378 15996 15996 E AndroidRuntime:        at java.lang.reflect.Method.invoke(Native Method)
03-20 12:05:29.378 15996 15996 E AndroidRuntime:        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:942)
03-20 12:05:29.378 15996 15996 E AndroidRuntime:        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:832)
03-20 12:05:29.378 15996 15996 E AndroidRuntime: Caused by: android.view.InflateException: Binary XML file line #25: Binary XML file line #25: Error inflating class fragment
03-20 12:05:29.378 15996 15996 E AndroidRuntime: Caused by: android.view.InflateException: Binary XML file line #25: Error inflating class fragment
03-20 12:05:29.378 15996 15996 E AndroidRuntime: Caused by: java.lang.RuntimeException: Exception inflating info.dodata.voicerecorder:navigation/nav_graph line 66
03-20 12:05:29.378 15996 15996 E AndroidRuntime:        at g.q.r.c()
03-20 12:05:29.378 15996 15996 E AndroidRuntime:        at androidx.navigation.NavController.j(:2)
03-20 12:05:29.378 15996 15996 E AndroidRuntime:        at androidx.navigation.fragment.NavHostFragment.L(:32)
03-20 12:05:29.378 15996 15996 E AndroidRuntime:        at g.m.b.r.U(:19)
03-20 12:05:29.378 15996 15996 E AndroidRuntime:        at g.m.b.p.onCreateView(:5)
03-20 12:05:29.378 15996 15996 E AndroidRuntime:        at g.m.b.e.onCreateView(:4)
03-20 12:05:29.378 15996 15996 E AndroidRuntime:        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:788)
03-20 12:05:29.378 15996 15996 E AndroidRuntime:        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:738)
03-20 12:05:29.378 15996 15996 E AndroidRuntime:        at android.view.LayoutInflater.rInflate(LayoutInflater.java:869)03-20 12:05:29.378 15996 15996 E AndroidRuntime:        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:832)
03-20 12:05:29.378 15996 15996 E AndroidRuntime:        at android.view.LayoutInflater.inflate(LayoutInflater.java:518)
03-20 12:05:29.378 15996 15996 E AndroidRuntime:        at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
03-20 12:05:29.378 15996 15996 E AndroidRuntime:        at android.view.LayoutInflater.inflate(LayoutInflater.java:377)
03-20 12:05:29.378 15996 15996 E AndroidRuntime:        at g.b.c.n.v()
03-20 12:05:29.378 15996 15996 E AndroidRuntime:        at g.b.c.k.setContentView()
03-20 12:05:29.378 15996 15996 E AndroidRuntime:        at info.dodata.voicerecorder.ui.ActivityMain.onCreate(:2)
03-20 12:05:29.378 15996 15996 E AndroidRuntime:        at android.app.Activity.performCreate(Activity.java:6910)
03-20 12:05:29.378 15996 15996 E AndroidRuntime:        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
03-20 12:05:29.378 15996 15996 E AndroidRuntime:        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2757)
03-20 12:05:29.378 15996 15996 E AndroidRuntime:        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2875)
03-20 12:05:29.378 15996 15996 E AndroidRuntime:        at android.app.ActivityThread.-wrap12(ActivityThread.java)
03-20 12:05:29.378 15996 15996 E AndroidRuntime:        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1578)
03-20 12:05:29.378 15996 15996 E AndroidRuntime:        at android.os.Handler.dispatchMessage(Handler.java:105)
03-20 12:05:29.378 15996 15996 E AndroidRuntime:        at android.os.Looper.loop(Looper.java:156)
03-20 12:05:29.378 15996 15996 E AndroidRuntime:        at android.app.ActivityThread.main(ActivityThread.java:6617)
03-20 12:05:29.378 15996 15996 E AndroidRuntime:        at java.lang.reflect.Method.invoke(Native Method)
03-20 12:05:29.378 15996 15996 E AndroidRuntime:        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:942)
03-20 12:05:29.378 15996 15996 E AndroidRuntime:        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:832)
03-20 12:05:29.378 15996 15996 E AndroidRuntime: Caused by: java.lang.RuntimeException: java.lang.ClassNotFoundException: info.dodata.voicerecorder.model.MVoice
03-20 12:05:29.378 15996 15996 E AndroidRuntime:        at g.q.r.d(:1)
03-20 12:05:29.378 15996 15996 E AndroidRuntime:        at g.q.r.b(:1)
03-20 12:05:29.378 15996 15996 E AndroidRuntime:        at g.q.r.b(:27)
03-20 12:05:29.378 15996 15996 E AndroidRuntime:        ... 28 more
03-20 12:05:29.378 15996 15996 E AndroidRuntime: Caused by: java.lang.ClassNotFoundException: info.dodata.voicerecorder.model.MVoice
03-20 12:05:29.378 15996 15996 E AndroidRuntime:        at java.lang.Class.classForName(Native Method)
03-20 12:05:29.378 15996 15996 E AndroidRuntime:        at java.lang.Class.forName(Class.java:400)
03-20 12:05:29.378 15996 15996 E AndroidRuntime:        at java.lang.Class.forName(Class.java:326)
03-20 12:05:29.378 15996 15996 E AndroidRuntime:        ... 31 more
03-20 12:05:29.378 15996 15996 E AndroidRuntime: Caused by: java.lang.ClassNotFoundException: Didn't find class "info.dodata.voicerecorder.model.MVoice" on path: DexPathList[[zip file "/data/app/info.dodata.voicerecorder-1/base.apk"],nativeLibraryDirectories=[/data/app/info.dodata.voicerecorder-1/lib/arm64, /system/lib64, /vendor/lib64, /system/vendor/lib64, /product/lib64]]
03-20 12:05:29.378 15996 15996 E AndroidRuntime:        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
03-20 12:05:29.378 15996 15996 E AndroidRuntime:        at java.lang.ClassLoader.loadClass(ClassLoader.java:380)
03-20 12:05:29.378 15996 15996 E AndroidRuntime:        at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
03-20 12:05:29.378 15996 15996 E AndroidRuntime:        ... 34 more

Added Content

To Amin: Thanks!

My app can work well when I run it in Android Studio.

In order to shrink file size, I use the Code A in my Build.gradle, but it maybe cause the error which I don't image when I install and run the alone apk file.

Do you mean I should handle my classes just like Code B? It's too complex.

BTW, I think "minifyEnabled true" is too terrible, the shrunk apk file only cause error in runtime, I can't test it in Android Studio.

Code A

   Build.gradle
    
        buildTypes {
            release {
                minifyEnabled true
                shrinkResources true
                proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
            }
        debug {
        }
    }

Code B

-keep public class info.dodata.voicerecorder.model.** 
{
  public protected *;
}
HelloCW
  • 843
  • 22
  • 125
  • 310

1 Answers1

1

I feel like proguard/R8 had removed info.dodata.voicerecorder.model.MVoice class because I don't see a .debug postfix in the package name, I'm assuming that you're installing the release variant with minify enabled. Try to keep your model classes using following, or disable minify to find out if it's the cause or not

-keep public class info.dodata.voicerecorder.model.** 
{
  public protected *;
}
Amin
  • 3,056
  • 4
  • 23
  • 34
  • Thanks! It's OK when I set minify as false – HelloCW Mar 20 '21 at 06:53
  • And more, `minify` is terrible, too terrible, big terrible!!! You know an app can run in real device by Android Studio and failed by install. I will not test an app via downloading and installing it by Google Play, I will not find the big error !!! – HelloCW Mar 20 '21 at 06:57
  • Does Android Studio test `minify`? and does Android Studio give the developer warning information? – HelloCW Mar 20 '21 at 06:59
  • @HelloCW I don't know what you mean by test minify, but I usually enable minify on debug variant too, and keep classes/methods/properties which should be kept and they're not – Amin Mar 20 '21 at 10:47
  • Thanks! Would you please to see my added content in the question? – HelloCW Mar 20 '21 at 11:40
  • @HelloCW, writing proguard rules are sometimes frustrating, but it's usually not that hard, because most libraries usually give their config in their github page. In addition to those, you have to keep your pojos, DAOs, data binding stuff and anything that it's name/structure matters – Amin Mar 20 '21 at 12:26
  • Thanks! Is there some guide about writing proguard rules? How can I keep my pojos, DAOs, data binding? Is there some sample code? – HelloCW Mar 21 '21 at 01:09
  • @HelloCW you can take a look at this https://medium.com/androiddevelopers/practical-proguard-rules-examples-5640a3907dc9 – Amin Mar 21 '21 at 02:17
  • Thanks! I have browser some sample project, I have hardly find to set `minifyEnabled true`, does it mean `minifyEnabled true` is dangerous? – HelloCW Mar 22 '21 at 01:10
  • And more, some projects write rules in proguard-rules.pro and still set `minifyEnabled false`, why ? – HelloCW Mar 22 '21 at 01:17
  • If I set `minifyEnabled false`, I needn't write rules in proguard-rules.pro, right? – HelloCW Mar 22 '21 at 01:17
  • 1
    @HelloCW Yes you're right, but if you disable it, people can easily decompile your code and get 99% of your source code with some simple clicks. It's kinda like releasing your source code to google play, publicly available :D – Amin Mar 22 '21 at 04:10