Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.lang.LinkageError: MainActivity

I use productFlavours in my app. I'm also using multi dex.

 defaultConfig {
         multiDexEnabled true
         minSdkVersion 17
         targetSdkVersion 22
         ... 
 }

productFlavors {
  prodFlavor1{...}
  prodFlavor2{...}
 }

dependencies {
    compile 'com.android.support:multidex:1.0.1'
...
}

In the Application class:

@Override
    public void onCreate() {
        MultiDex.install(getApplicationContext());
        super.onCreate();
}

I have added the above code for Android versions below 5 support.

Then the following structure, with 2 slightly different versions of MainActivity (MainActivity extends AppCompatActivity) starts at a certain moment in the app, on a button press.

app/src/prodFlavor1/.../MainActivity
app/src/prodFlavor2/.../MainActivity

In prodFlavor1, MainActivity starts immediately after SplashActivity. In prodFlavor2, MainActivity.

This worked for a long period before and even after I added that part with multidex, but suddenly stooped working with no explanation. Even if I remove the code referencing multidex, I get the same thing.

When I switch to prodVersion2 everything works. But when I switch to prodVersion1 I get:

startActivity(new Intent(TutorialActivity.this,MainActivity.class));

Nexus 5 with Android 6:

Process: com.mpackage, PID: 30807
                                                             java.lang.LinkageError: com.mpackage.activities.MainActivity
                                                                 at dalvik.system.DexFile.defineClassNative(Native Method)
                                                                 at dalvik.system.DexFile.defineClass(DexFile.java:226)
                                                                 at dalvik.system.DexFile.loadClassBinaryName(DexFile.java:219)
                                                                 at dalvik.system.DexPathList.findClass(DexPathList.java:338)
                                                                 at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:54)
                                                                 at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
                                                                 at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
                                                                 at com.gossip.activities.TutorialActivity$1.handleMessage(TutorialActivity.java:52)
                                                                 at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                 at android.os.Looper.loop(Looper.java:148)
                                                                 at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                 at java.lang.reflect.Method.invoke(Native Method)
                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

On Nexus4 Emulator with Android 4.4:

java.lang.NoClassDefFoundError: com.mpackage.activities.MainActivity
                                                               at com.gossip.activities.TutorialActivity$1.handleMessage(TutorialActivity.java:52)
                                                               at android.os.Handler.dispatchMessage(Handler.java:102)
                                                               at android.os.Looper.loop(Looper.java:136)
                                                               at android.app.ActivityThread.main(ActivityThread.java:5017)
                                                               at java.lang.reflect.Method.invokeNative(Native Method)
                                                               at java.lang.reflect.Method.invoke(Method.java:515)
                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
                                                               at dalvik.system.NativeStart.main(Native Method)

UPDATE:

Seems that the error was caused by the fact that I have added a method called isResumed() in MainActivity, that's why (Native Method) error, different on 6.0 from 4.4.

like image 596
AlexAndro Avatar asked Dec 03 '15 09:12

AlexAndro


2 Answers

Finally solved after found this useful thread:

I have created a method isResumed() in my class.

public boolean isResumed(){
    return isResumed;
}

Immediate after I removed/edit it everything went to normal. I didn't find this method in Activity or AppCompatActivity in the documentation.

like image 110
AlexAndro Avatar answered Oct 20 '22 18:10

AlexAndro


Conflicting name of function with one of base classes. Rename your function to something custom i.e. isBaseResumed()

like image 33
T. pac Avatar answered Oct 20 '22 20:10

T. pac