需要帮助处理由java.lang.IncompatibleClassChangeError引起的致命异常

Bra*_*n W 8 java android google-cloud-messaging android-studio

因此,我昨天在Android Studio中遇到了这个错误,这是我以前从未见过的,这是由过去3个月未触及过的一段代码引起的.这来自用于注册推送通知的代码,本周早些时候工作正常,但现在它导致了这个错误:

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)
Run Code Online (Sandbox Code Playgroud)

这是我的RegistrationIntentService类:

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();
        }
    }
Run Code Online (Sandbox Code Playgroud)

这是从我的MainActivity类调用上面的类的代码行:

RegistrationIntentService.startService(getApplicationContext());
Run Code Online (Sandbox Code Playgroud)

有关我的MainActivity的一些信息,如果它有帮助......

public class MainActivity extends SocialActivity implements SeekBar.OnSeekBarChangeListener

public abstract class SocialActivity extends AppCompatActivity implements GraphRequest.GraphJSONObjectCallback
Run Code Online (Sandbox Code Playgroud)

我试过跳回几个较旧的提交,但得到了同样的错误.这让我相信我的Android Studio或java版本可能存在问题.我更新了我的java版本并重新安装了Android Studio,仍然出现同样的错误.也尝试在旧版本的单独计算机上,仍然是相同的错误.

我甚至尝试根据本指南从GCM迁移到Firebase ,最终再次收到相同的错误.

我可以注释崩溃应用程序的行,它会正常运行,但之后我不再收到通知.

任何有关此事的帮助或建议将不胜感激!

编辑:这是我的相关编译/类路径语句...

在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
    }
}
Run Code Online (Sandbox Code Playgroud)

在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'
Run Code Online (Sandbox Code Playgroud)

Die*_*ini 7

更新可能27:

我们刚刚发布了一个update(version 9.0.1)来修复我在第一次编辑中提到的不兼容性.
请更新您的依赖项,如果仍有问题,请告知我们.

谢谢!


原答案5月20日:

您遇到的问题是由于
play-services / firebase sdk v9.0.0和 com.android.support:appcompat-v7 >= 24
(与android-N sdk发布的版本)不兼容

您应该能够通过定位支持库的早期版本来修复它.喜欢:

compile 'com.android.support:appcompat-v7:23.4.0'
Run Code Online (Sandbox Code Playgroud)