So, I encountered this error only yesterday in Android Studio, which I had never seen before, caused by a piece of code that has not been touched on for the last 3 months. This came from the code used to register for push notifications, and this week is working fine, but now it leads to an 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 a line of code calling the above class from my MainActivity class:
RegistrationIntentService.startService(getApplicationContext());
Some info on my MainActivity, if that helps ...
public class MainActivity extends SocialActivity implements SeekBar.OnSeekBarChangeListener public abstract class SocialActivity extends AppCompatActivity implements GraphRequest.GraphJSONObjectCallback
I tried to revert to a few older commits, but got the same error. This led me to believe that there might be a problem with my version of Android Studio or Java. I updated my version of Java and reinstalled Android Studio, still the same error. Also tried on a separate computer with older versions, still the same error.
I even tried moving from GCM to Firebase according to this guide and got the same error again.
I can comment on the line that causes the application to crash, and it will work fine, but then I no longer receive notifications.
Any help or advice on this would be greatly appreciated!
EDIT: Here are my respective compile / classpath instructions ...
In 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 build.gradle application:
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'