81

由于我更新到 Android SDK Tools 25.1.6 和 Android Support Repository 32.0.0(今天早上),我收到以下错误,我没有更改我的代码中的任何内容,它仍在我同事的计算机上运行(Android SDK Tools 25.1.1 + Android 支持存储库 30.0.0)。

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.xxxxxxx.utils.RegistrationIntentService.onHandleIntent(RegistrationIntentService.java:55)
     at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
     at android.os.Handler.dispatchMessage(Handler.java:102)
     at android.os.Looper.loop(Looper.java:145)
     at android.os.HandlerThread.run(HandlerThread.java:61)

这是一段崩溃的代码:

InstanceID instanceID = InstanceID.getInstance(this); // <-- crash here
String instanceIDToken = instanceID.getToken(getString(R.string.google_app_id),
GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);

当我尝试从 Google Cloud Messaging 获取令牌时。

我正在使用拆分播放服务在 Gradle 中导入 GCM:

 compile 'com.google.android.gms:play-services-analytics:9.0.0' 
 compile 'com.google.android.gms:play-services-maps:9.0.0'
 compile 'com.google.android.gms:play-services-location:9.0.0' 
 compile 'com.google.android.gms:play-services-gcm:9.0.0' 
 compile 'com.google.android.gms:play-services-base:9.0.0'

编辑 禁用 GCM 解决了问题,所以我猜我应该迁移到 Firebase Cloud Message

EDIT2 我的设备收到 Google Play Services 9.0(昨天是 8.4.x)。现在它不再崩溃,但抱怨模块描述符

 Failed to load module descriptor class: Didn't find class "com.google.android.gms.dynamite.descriptors.com.google.firebase.auth.ModuleDescriptor"
 Firebase API initialization failure.

有没有人有类似的错误,以及如何解决它?

已修复特别感谢@stegranet ./gradlew -q app:dependencies --configuration compile帮助您确定哪些依赖项包括 SDK 24.x

主要问题是某些库使用+符号而不是版本导入最新的支持库。这会导致问题,包括最新的可用版本。

所以避免+登录依赖项;)

4

15 回答 15

36

我使用 gradle 依赖树为我解决了这个错误。

只需运行gradle -q app:dependencies --configuration compile 并检查以下条目的输出:

+--- com.mcxiaoke.viewpagerindicator:library:2.4.1
|    \--- com.android.support:support-v4:+ -> 24.0.0-beta1 (*)

正如Diego Giorgini所说,这个版本太高了(>=24)。所以更新依赖build.gradle

compile('com.mcxiaoke.viewpagerindicator:library:2.4.1') {
    exclude module: 'support-v4';
}
compile 'com.android.support:support-v4:23.4.0'
于 2016-05-20T20:58:02.843 回答
34

5月27日更新:

我们刚刚发布了更新 ( version 9.0.1) 以修复我在第一次编辑中提到的不兼容问题。
请更新您的依赖项,如果这仍然是一个问题,请告诉我们。

谢谢!


5月20日的原始答案:

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

您应该能够通过针对支持库的早期版本来修复它。喜欢:

compile 'com.android.support:appcompat-v7:23.4.0'
于 2016-05-20T02:53:32.993 回答
5

我已经更新了播放服务依赖项build.gradle

dependencies {
    compile 'com.google.android.gms:play-services:9.0.0'
}

要通过更新 google-services 插件的版本来解决版本冲突 - 我必须更新build.gradle项目根文件夹下的google-services

dependencies {
    classpath 'com.google.gms:google-services:3.0.0'
}

您可以在此处获取 google-services 的最新更新。

尽管它没有避免异常,但它不再使我的应用程序崩溃。

更新

我可以通过从 Beta 频道更新 Android Studio 来避免崩溃。然后更新您的platform/build-tools内部 SDK。

在此处输入图像描述

于 2016-05-19T06:01:53.987 回答
5

我的工作如下:

应用级分级

dependencies {
 compile 'com.android.support:appcompat-v7:23.4.0'
 compile 'com.android.support:design:23.4.0'
 compile 'com.google.android.gms:play-services:9.0.0'
}

根级梯度

dependencies {
    classpath 'com.google.gms:google-services:3.0.0'
}
于 2016-05-19T12:05:10.347 回答
4

更新到最新的 google play services 版本为我解决了这个问题。

应用插件:底部的“com.google.gms.google-services”...

dependencies {
    compile 'com.google.android.gms:play-services:9.0.0'
}

https://developers.google.com/android/guides/setup#add_google_play_services_to_your_project

于 2016-05-19T03:11:11.317 回答
3

由于facebook更新了sdk而发生在我身上,我有

compile 'com.facebook.android:facebook-android-sdk:4.+'

将其替换为

compile 'com.facebook.android:facebook-android-sdk:4.15.0'

解决了我的问题。

参考:https ://developers.facebook.com/docs/android/change-log-4.x

于 2016-09-28T15:00:44.663 回答
1

通过包括所有播放服务的包

dependencies {
  compile 'com.google.android.gms:play-services:9.0.0'
}

您抑制了错误,但最终结果是,GCM 令牌检索不起作用,我们也无法获得 GCM 的实例。所以这不是我书中的解决方案。如果有人知道发生了什么,请赐教。

编辑:

我用 firebase 替换了 GCM,将 android studio 从 2.1 更新到 2.2 以解决 firebase 分析的即时运行问题,将构建工具更新为 24-rc4,将平台工具更新为 24-rc3,并将我的支持库版本保持为 23.4.0。现在一切似乎都运行良好。

于 2016-05-19T11:13:50.333 回答
1

我遇到了同样的问题,从 Android Support Repository 32.0.0 恢复到 Android Support Repository 31.0.0 解决了它。

于 2016-05-22T15:00:13.723 回答
1

对于带有 GCM 2016 的 Android 推送通知:

1) 在 Android SDK-> SDK Tools 中检查 Google Play 服务

2)在gradle中只添加一行:

compile 'com.google.android.gms:play-services-gcm:9.4.0'

(没有特定的类路径,它对我有用)

3)你必须创建3个类(GCMPushReceiverService、GCMRegistrationIntentService、GCMTokenRefreshListenerService)

4.1) GCMTokenRefreshListenerService 的代码:

package com.myapp.android;

/**
 * Created by skygirl on 02/08/2016.
 */
import android.content.Intent;
import com.google.android.gms.iid.InstanceIDListenerService;

public class GCMTokenRefreshListenerService extends InstanceIDListenerService {

    //If the token is changed registering the device again
    @Override
    public void onTokenRefresh() {
        Intent intent = new Intent(this, GCMRegistrationIntentService.class);
        startService(intent);
    }
}

4.2) GCMRegistrationIntentService 的代码(用你的项目号更改authorizedEntity):

package com.myapp.android;

/**
 * Created by Skygirl on 02/08/2016.
 */
import android.app.IntentService;
import android.content.Intent;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;

import com.google.android.gms.gcm.GoogleCloudMessaging;
import com.google.android.gms.iid.InstanceID;

public class GCMRegistrationIntentService extends IntentService {
    //Constants for success and errors
    public static final String REGISTRATION_SUCCESS = "RegistrationSuccess";
    public static final String REGISTRATION_ERROR = "RegistrationError";

    //Class constructor
    public GCMRegistrationIntentService() {
        super("");
    }


    @Override
    protected void onHandleIntent(Intent intent) {
        //Registering gcm to the device
        registerGCM();
    }

    private void registerGCM() {
        //Registration complete intent initially null
        Intent registrationComplete = null;

        //Register token is also null
        //we will get the token on successfull registration
        String token = null;
        try {
            //Creating an instanceid
            InstanceID instanceID = InstanceID.getInstance(this);
            String authorizedEntity = "XXXXXXXXXX"; //  your project number

            //Getting the token from the instance id
            token = instanceID.getToken(authorizedEntity, GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);

            //Displaying the token in the log so that we can copy it to send push notification
            //You can also extend the app by storing the token in to your server
            Log.w("GCMRegIntentService", "token:" + token);

            //on registration complete creating intent with success
            registrationComplete = new Intent(REGISTRATION_SUCCESS);

            //Putting the token to the intent
            registrationComplete.putExtra("token", token);
        } catch (Exception e) {
            //If any error occurred
            Log.w("GCMRegIntentService", "Registration error");
            registrationComplete = new Intent(REGISTRATION_ERROR);
        }

        //Sending the broadcast that registration is completed
        LocalBroadcastManager.getInstance(this).sendBroadcast(registrationComplete);
    }
}

4.3) GCMPushReceiverService 的代码:

package com.myapp.android;

/**
 * Created by Skygirl on 02/08/2016.
 */
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.media.RingtoneManager;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;

import com.google.android.gms.gcm.GcmListenerService;

//Class is extending GcmListenerService
public class GCMPushReceiverService extends GcmListenerService {

    //This method will be called on every new message received
    @Override
    public void onMessageReceived(String from, Bundle data) {
        //Getting the message from the bundle
        String message = data.getString("message");
        //Displaying a notiffication with the message
        sendNotification(message);
    }

    //This method is generating a notification and displaying the notification
    private void sendNotification(String message) {
        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        int requestCode = 0;
        PendingIntent pendingIntent = PendingIntent.getActivity(this, requestCode, intent, PendingIntent.FLAG_ONE_SHOT);
        NotificationCompat.Builder noBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.your_logo)
                .setContentTitle("Your Amazing Title")
                .setContentText(message)
                .setPriority(Notification.PRIORITY_MAX)
                .setContentIntent(pendingIntent);
        noBuilder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));

        NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(0, noBuilder.build()); //0 = ID of notification
    }
}

5)不要忘记更改包名

6)在您的 mainActivity 粘贴此代码:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Setup view
        setContentView(R.layout.main);
    mRegistrationBroadcastReceiver = new BroadcastReceiver() {

        //When the broadcast received
        //We are sending the broadcast from GCMRegistrationIntentService

        public void onReceive(Context context, Intent intent) {
            //If the broadcast has received with success
            //that means device is registered successfully
            if(intent.getAction().equals(GCMRegistrationIntentService.REGISTRATION_SUCCESS)){
                //Getting the registration token from the intent
                String token = intent.getStringExtra("token");
                //Displaying the token as toast
                Toast.makeText(getApplicationContext(), "Registration token:" + token, Toast.LENGTH_LONG).show();

                //if the intent is not with success then displaying error messages
            } else if(intent.getAction().equals(GCMRegistrationIntentService.REGISTRATION_ERROR)){
                Toast.makeText(getApplicationContext(), "GCM registration error!", Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(getApplicationContext(), "Error occurred", Toast.LENGTH_LONG).show();
            }
        }
    };

    //Checking play service is available or not
    int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());

    //if play service is not available
    if(ConnectionResult.SUCCESS != resultCode) {
        //If play service is supported but not installed
        if(GooglePlayServicesUtil.isUserRecoverableError(resultCode)) {
            //Displaying message that play service is not installed
            Toast.makeText(getApplicationContext(), "Google Play Service is not install/enabled in this device!", Toast.LENGTH_LONG).show();
            GooglePlayServicesUtil.showErrorNotification(resultCode, getApplicationContext());

            //If play service is not supported
            //Displaying an error message
        } else {
            Toast.makeText(getApplicationContext(), "This device does not support for Google Play Service!", Toast.LENGTH_LONG).show();
        }

        //If play service is available
    } else {
        //Starting intent to register device
        Intent itent = new Intent(this, GCMRegistrationIntentService.class);
        startService(itent);
    }
}

//Unregistering receiver on activity paused
@Override
public void onPause() {
    super.onPause();
    Log.w("MainActivity", "onPause");
    LocalBroadcastManager.getInstance(this).unregisterReceiver(mRegistrationBroadcastReceiver);
}



    @Override
    public void onResume() {
 super.onResume();
        Log.w("MainActivity", "onResume");
        LocalBroadcastManager.getInstance(this).registerReceiver(mRegistrationBroadcastReceiver,
                new IntentFilter(GCMRegistrationIntentService.REGISTRATION_SUCCESS));
        LocalBroadcastManager.getInstance(this).registerReceiver(mRegistrationBroadcastReceiver,
                new IntentFilter(GCMRegistrationIntentService.REGISTRATION_ERROR));
    }

7) 在您的 AndroidManifest.xml 添加以下行:

<uses-permission android:name="android.permission.INTERNET" />

8) 在您的控制台 logcat 中复制您的令牌并粘贴到此站点上 ,添加您的项目编号、您的令牌和一条消息。这对我来说很好:)

于 2016-08-04T08:24:05.680 回答
0

我遇到了这个问题,我将我的应用程序 gradle 版本从 1.5.0 更改为 2.0.0。

更改类路径

com.android.tools.build:gradle:1.5.0

classpath 'com.android.tools.build:gradle:2.0.0

于 2016-08-09T10:15:49.440 回答
0

好吧,我只是使用Android的初学者。我想Firebase按照 Firebase 网站中提供的说明来测试创建用户。

我在指定的地方添加了这些行。

类路径 'com.google.gms:google-services:3.0.0'

编译'com.google.firebase:firebase-auth:9.2.0'

应用插件:'com.google.gms.google-services'

但是 createUserWithEmailAndPassword 方法一直显示创建用户失败。这就是为什么我访问了这个问题以找出我的问题。我阅读了所有内容并应用了每个建议。但 IT 一直显示失败。但是当我升级时Android Studio from 2.1.1 to 2.1.2,我可以成功创建用户。

但是当我检查时logcat,它首先显示"Firebase API initialization failure"然后显示“FirebaseApp 初始化成功”。

07-09 18:53:37.012 13352-13352/jayground.firebasetest A/FirebaseApp: Firebase API initialization failure. How can I solve

这个?java.lang.reflect.InvocationTargetException at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.google.firebase.FirebaseApp.zza(Unknown来源)在 com.google.firebase.FirebaseApp.initializeApp(未知来源)在 com.google.firebase.FirebaseApp。

于 2016-07-09T10:19:11.540 回答
0

解决方案1:

dependencies {
 compile `com.android.support:appcompat-v7:23.4.0`
 compile `com.android.support:support-v4:23.4.0`
 compile `com.android.support:design:23.4.0`
 compile `com.google.android.gms:play-services:9.0.0`
}

解决方案 2:在文件夹 .idie/libraries/ 上检测不兼容。有时您声明 play-services-ads:8.4.0 与 play-services-gcm:9.0.0 并发。您必须覆盖您检测到的 build.grade 不兼容库

于 2016-08-09T16:33:04.417 回答
0

我有同样的问题。我将 SDK 工具更新到 25.1.7 rc1,然后问题就消失了。

于 2016-05-22T06:47:55.437 回答
0

将 SDK 工具更新至 25.1.7 并修复了此问题。

于 2016-06-08T22:35:06.723 回答
0

经过一整天的努力,我可以 100% 确认 Optimizely 库也以某种方式发生冲突并导致此错误。具体来说,我通过 Fabric 使用 Optimizely。以这种方式使用 Optimizely 时,不可能让 Firebase 进行初始化(也许以所有方式?)。

我已经在他们的 github 上发布了关于它的信息,并将直接与他们联系......

https://github.com/optimizely/Optimizely-Android-SDK/issues/11

于 2016-05-20T23:04:18.607 回答