ClassNotFoundException扩展Android MultiDexApplication类

时间:2017-04-10 19:58:29

标签: android android-manifest classnotfoundexception multidex

我花了好几个小时来解决这个问题,但是在针对类似问题的几十个回复之间找不到解决方案。

所以我的问题就出现了。我正在创建一个简单的multidex Android应用程序,我需要在app启动时执行一些操作。所以,我已经扩展了MultiDexApplication类,在AndroidManifest.xml文件中添加了应用程序元素的类路径,但每次运行它时崩溃都有以下异常:

java.lang.RuntimeException: Unable to instantiate application rmpt.app.MyApplication: java.lang.ClassNotFoundException: Didn't find class "rmpt.app.MyApplication" on path: DexPathList[[zip file "/data/app/rmpt.app-1.apk"],nativeLibraryDirectories=[/data/app-lib/rmpt.app-1, /vendor/lib, /system/lib, /system/lib/arm]]
   at android.app.LoadedApk.makeApplication(LoadedApk.java:509)
   at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4472)
   at android.app.ActivityThread.access$1300(ActivityThread.java:144)
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1355)
   at android.os.Handler.dispatchMessage(Handler.java:99)
   at android.os.Looper.loop(Looper.java:150)
   at android.app.ActivityThread.main(ActivityThread.java:5162)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:525)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:744)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
   at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassNotFoundException: Didn't find class "rmpt.app.MyApplication" on path: DexPathList[[zip file "/data/app/rmpt.app-1.apk"],nativeLibraryDirectories=[/data/app-lib/rmpt.app-1, /vendor/lib, /system/lib, /system/lib/arm]]
   at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:53)
   at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
   at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
   at android.app.Instrumentation.newApplication(Instrumentation.java:975)
   at android.app.LoadedApk.makeApplication(LoadedApk.java:504)
   at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4472) 
   at android.app.ActivityThread.access$1300(ActivityThread.java:144) 
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1355) 
   at android.os.Handler.dispatchMessage(Handler.java:99) 
   at android.os.Looper.loop(Looper.java:150) 
   at android.app.ActivityThread.main(ActivityThread.java:5162) 
   at java.lang.reflect.Method.invokeNative(Native Method) 

如果我从AndroidManifest.xml文件中删除android:name属性(所以它使用默认的android Application类)一切正常,但我需要在我的onCreate方法上执行一些操作应用

我把文件留在这里,这样你就可以看到它们,也许看错了(我已经有2天试图为我解决这个问题了)

的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="rmpt.app">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        android:name=".MyApplication">

        <activity android:name=".view.LoginActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    </application>
</manifest>

MyApplication.java

package rmpt.app;

import android.support.multidex.MultiDexApplication;

public class MyApplication extends MultiDexApplication {

    @Override
    public void onCreate() {
        super.onCreate();

        // do my stuff
    }
}

的build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "25.0.2"

    defaultConfig {
        applicationId "rmpt.app"
        minSdkVersion 18
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        multiDexEnabled true

        jackOptions {
            enabled true
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    dexOptions {
        javaMaxHeapSize "2g"
    }

    packagingOptions{
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/LICENSE'
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.4.0'
    compile 'com.android.support:multidex:1.0.1'
    compile 'com.google.android.gms:play-services:9.4.0'
    compile 'com.android.support:design:23.4.0'

}

我正在使用MacOS 10.11.6

1 个答案:

答案 0 :(得分:0)

请按照at this link的说明操作。他们修复了您遇到的错误。

根据Android Documentation

  

主要的classes.dex必须包含调用所需的类   这类方法。

即,MyApplication必须是classes.dex的一部分,因为它调用MultiDex #install(Context)方法。上述链接中的说明可帮助您确定主要dex文件中必须包含哪些类(classes.dex)