通过adb install安装apk时,Application类会发生ClassNotFoundException,但在运行android studio时工作正常

时间:2017-11-06 12:12:36

标签: android adb

我正在使用Android studio 3.0 beta 9来构建apk。但是最近我遇到了当我在模拟器或真实设备上通过 adb install 运行apk时,它会弹出 ClassNotFoundException ,在dexPathList中找不到应用程序类.....

但是当我通过Android Studio运行时,它运行正常。不知道是什么原因?

以下是错误日志: -

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.example.demo, PID: 27637
                  java.lang.RuntimeException: Unable to instantiate application
com.example.demo.MyApplication:java.lang.ClassNotFoundException: Didn't find class "com.example.demo.MyApplication" on path: DexPathList[[zip file "/data/app/com.example.demo-1/base.apk"],nativeLibraryDirectories=[/data/app/com.example.demo-1/lib/arm, /data/app/com.example.demo-1/base.apk!/lib/armeabi-v7a, /vendor/lib, /system/lib]]
at android.app.LoadedApk.makeApplication(LoadedApk.java:680)
                                                                         at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6429)
                                                                         at android.app.ActivityThread.access$1800(ActivityThread.java:229)
                                                                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1898)
                                                                         at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                         at android.os.Looper.loop(Looper.java:148)
                                                                         at android.app.ActivityThread.main(ActivityThread.java:7402)
                                                                         at java.lang.reflect.Method.invoke(Native Method)
                                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
                                                                      Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.demo.MyApplication" on path: DexPathList[[zip file "/data/app/com.example.demo-1/base.apk"],nativeLibraryDirectories=[/data/app/com.example.demo-1/lib/arm, /data/app/com.example.demo-1/base.apk!/lib/armeabi-v7a, /vendor/lib, /system/lib]]
                                                                         at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
                                                                         at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
                                                                         at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
                                                                         at android.app.Instrumentation.newApplication(Instrumentation.java:1005)
                                                                         at android.app.LoadedApk.makeApplication(LoadedApk.java:670)
                                                                         at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6429) 
                                                                         at android.app.ActivityThread.access$1800(ActivityThread.java:229) 
                                                                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1898) 
                                                                         at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                         at android.os.Looper.loop(Looper.java:148) 
                                                                         at android.app.ActivityThread.main(ActivityThread.java:7402) 
                                                                         at java.lang.reflect.Method.invoke(Native Method) 
                                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 
                                                                        Suppressed: java.lang.ClassNotFoundException: com.example.demo.MyApplication
                                                                         at java.lang.Class.classForName(Native Method)
                                                                         at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
                                                                         at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
                                                                         at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
                                                                                ... 12 more
                                                                      Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack trace available

2 个答案:

答案 0 :(得分:1)

这是因为你的程序最小化。因此,要么编辑您的proguard属性并排除您的类,请为release apk

禁用proguard

build.gradleminifyEnabled属性设置为false,以便在release apk中停用该程序。

buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
        debug {
            minifyEnabled false 
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }

    }

答案 1 :(得分:0)

我终于得到了答案,有两个问题,第一个是提到的 here,当我在Android设备上直接运行应用程序时,我试图在Android上安装adb安装。

提到了第二个问题here

因此,当我尝试通过构建gradle任务构建apk时,它显示错误: -

Error:Failed to complete Gradle execution.

Cause:
The version of Gradle you are using (3.3) does not support the forTasks() method on BuildActionExecuter. Support for this is available in Gradle 3.5 and all later versions.

因此我将Gradle版本升级到最新版本并解决了问题。