java.lang.LinkageError: MainActivity
Asked Answered
S

2

6

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.

Squat answered 3/12, 2015 at 9:3 Comment(0)
S
22

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.

Squat answered 3/12, 2015 at 9:40 Comment(5)
It is hidden in the documentation, yes (see @hide). But you can find it in the source code: github.com/android/platform_frameworks_base/blob/master/core/…Goutweed
Good old fashioned Android fun.Hodeida
Incredible, thanks for this one. Three years later, I had the same issue in Kotlin: var isResumed: BooleanCheddar
@Mario huizinga did you get any solution? I am getting same issueBuoyancy
@KhyatiChitroda Just use a different name for your varCheddar
S
1

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

Seamstress answered 29/11, 2016 at 12:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.