Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need help dealing with a Fatal Exception caused by java.lang.IncompatibleClassChangeError

So, I ran into this error just yesterday in Android Studio that I've never seen before, caused by a piece of code that hasn't been touched over the past 3 months. This came from the code being used to register for push notifications and was working fine earlier this week, but now it results in this error:

05-20 10:37:02.064 22471-22681/com.appname E/AndroidRuntime: FATAL EXCEPTION: IntentService[RegIntentService]
                                                               Process: com.appname, PID: 22471
                                                               java.lang.IncompatibleClassChangeError: The method 'java.io.File android.support.v4.content.ContextCompat.getNoBackupFilesDir(android.content.Context)' was expected to be of type virtual but instead was found to be of type direct (declaration of 'java.lang.reflect.ArtMethod' appears in /system/framework/core-libart.jar)
                                                                   at com.google.android.gms.iid.zzd.zzeb(Unknown Source)
                                                                   at com.google.android.gms.iid.zzd.<init>(Unknown Source)
                                                                   at com.google.android.gms.iid.zzd.<init>(Unknown Source)
                                                                   at com.google.android.gms.iid.InstanceID.zza(Unknown Source)
                                                                   at com.google.android.gms.iid.InstanceID.getInstance(Unknown Source)
                                                                   at com.appname.services.RegistrationIntentService.onHandleIntent(RegistrationIntentService.java:32)
                                                                   at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
                                                                   at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                   at android.os.Looper.loop(Looper.java:135)
                                                                   at android.os.HandlerThread.run(HandlerThread.java:61)

Here is my RegistrationIntentService class:

package com.appname.services;

import android.app.IntentService;
import android.content.Context;
import android.content.Intent;
import com.google.android.gms.gcm.GoogleCloudMessaging;
import com.google.android.gms.iid.InstanceID;
import com.appname.MainSharedPreferences;
import com.appname.R;
import java.io.IOException;

public class RegistrationIntentService extends IntentService {

    private static final String TAG = "RegIntentService";

    public RegistrationIntentService() {
        super(TAG);
    }
    public static void startService(final Context context) {
        final Intent intent = new Intent(context, RegistrationIntentService.class);
        context.startService(intent); //HERE is the line that is causing the crash
    }

    @Override
    public void onHandleIntent(Intent intent) {
        InstanceID instanceID = InstanceID.getInstance(this);
        try {
            String token = instanceID.getToken(getString(R.string.gcm_defaultSenderId) , GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
            MainSharedPreferences.saveGCMInstanceToken(token, getApplicationContext());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

This is the line of code that is calling the above class from my MainActivity class:

RegistrationIntentService.startService(getApplicationContext());

Some info on my MainActivity if it helps...

public class MainActivity extends SocialActivity implements SeekBar.OnSeekBarChangeListener

public abstract class SocialActivity extends AppCompatActivity implements GraphRequest.GraphJSONObjectCallback

I've tried jumping back to several older commits, but got the same error. This led me to believe that maybe there was problem with my Android Studio or java version. I updated my java version and reinstalled Android Studio, still the same error. Also tried on a separate computer with older versions, still the same error.

I even tried migrating from GCM to Firebase according to this guide and ended up getting the same error again.

I can comment out the line that crashes the app and it will run fine, but then I no longer receive notifications.

Any help or advice on this matter would be much appreciated!

EDIT: Here are my relevant compile/classpath statements as well...

In the appname build.gradle:

buildscript {
    repositories {
        jcenter()
    }
    repositories { mavenCentral() }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.1.0'
        classpath 'com.google.gms:google-services:3.0.0'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

In the app build.gradle:

apply plugin: 'com.android.application'
apply plugin: 'io.fabric'

repositories {
    //...
}

allprojects {
    //...
}

android {
   //...
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.4.0'
    compile 'com.google.code.gson:gson:2.5'
    compile 'com.android.support:design:23.4.0'
    compile 'com.android.support:recyclerview-v7:+'
    compile "com.google.firebase:firebase-messaging:9.0.0"
    //...
}

apply plugin: 'com.google.gms.google-services'
like image 434
Brandon W Avatar asked May 20 '16 17:05

Brandon W


1 Answers

update may 27:

we just released an update (version 9.0.1) to fix the incompatibility I mentioned in my first edit.
Please update your dependencies and let us know if this is still an issue.

Thanks!


original answer May 20:

The issue you are experiencing is due to an incompatibility between
play-services / firebase sdk v9.0.0 and com.android.support:appcompat-v7 >= 24
(the version released with android-N sdk)

You should be able to fix it by targeting an earlier version of the support library. Like:

compile 'com.android.support:appcompat-v7:23.4.0'
like image 79
Diego Giorgini Avatar answered Oct 24 '22 07:10

Diego Giorgini