NotFoundException class extends Android MultiDexApplication class

I spend hours trying to solve this problem, but cannot find a solution between dozens of answers to similar problems.

So this is my problem. I am creating a simple android app with multiple apps and I need to do some action when the app starts. So, I extended the MultiDexApplication class, added the classpath to the application element in the AndroidManifest.xml file, but it fails every time it starts with the following exception:

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) 

      

If I remove the android: name attribute from the AndroidManifest.xml file (so it uses the default Android app class) everything works fine, but I NEED does some action in the onCreate method of my app.

I am leaving my files here so you can see them and maybe see something wrong (already 2 days trying to solve this problem for me)


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'

}

      

I am working on macOS 10.11.6

+3


source to share


1 answer


Follow the instructions on this link . They are fixing the error you are facing.

According to Android Documentation



dex primary classes should contain the classes needed to call these class methods.

those. MyApplication must be part of the .dex class because it calls the MultiDex # install (Context) method. The instructions in the above link will help you decide which classes should be included in the main dex file (classes.dex)

0


source







All Articles