Search code examples
androidappcompatactivity

Android - java.lang.NoClassDefFoundError when using AppCompatActivity


After I migrated my app to Androidx, I have this warning when debugging the app:

Rejecting re-init on previously-failed class java.lang.Class: java.lang.NoClassDefFoundError: Failed resolution of: Landroid/view/View$OnUnhandledKeyEventListener; at void androidx.core.view.ViewCompat.setOnApplyWindowInsetsListener(android.view.View, androidx.core.view.OnApplyWindowInsetsListener) (ViewCompat.java:2421)

However it is just a warning, but I did not find any way to fix this.

The only thing I've found out the issue is related to AppCompatActivity - which I am using in every activity:

public class MainActivity extends AppCompatActivity

When I replace AppCompatActivity with Activity, the warning is gone.

But I don't think it is a good solution, because of backward support. My app's minSdkVersion is 23, but can someone confirm I can replace AppCompatActivity with Activity?

I think the future features won't be compatible backwards...

Otherwise I don't think there is another easy fix for this issue.

Here is a part of my code in MainActivity:

import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.preference.PreferenceManager;

import com.google.firebase.messaging.FirebaseMessaging;

import java.util.Locale;
import java.util.Set;

public class MainActivity extends AppCompatActivity  {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Locale locale = new Locale(lng);
        Locale.setDefault(locale);
        Configuration config = new Configuration();
        config.locale = locale;
        getBaseContext().getResources().updateConfiguration(config,
                getBaseContext().getResources().getDisplayMetrics());

new DatabaseHelper(this); 

        this.setContentView(R.layout.activity_main);

ImageView language = findViewById(R.id.lang);
        ImageView logo = findViewById(R.id.logo);

        if (lng.equals("sk")) {
            language.setImageResource(R.drawable.en);
            logo.setImageResource(R.drawable.logo);
        }

        else { language.setImageResource(R.drawable.sk);
            logo.setImageResource(R.drawable.logoen);
        }

        ImageView notify = findViewById(R.id.settings);

        if (readState()) {
            notify.setImageResource(R.drawable.notifyon); }
        else {notify.setImageResource(R.drawable.notifyoff);}

    }

    public void ClickSearch(View v)
    {
        Intent intent = new Intent(MainActivity.this, Search.class);
        startActivity(intent);
    }

}

The warning in another part also points to this line in my code: this.setContentView(R.layout.activity_main);

And my dependencies:

 implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation "androidx.preference:preference:1.1.0"
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation 'com.google.android.gms:play-services-maps:17.0.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    implementation 'com.google.firebase:firebase-core:17.2.1'
    implementation 'com.google.firebase:firebase-analytics:17.2.1'
    implementation 'com.crashlytics.sdk.android:crashlytics:2.10.1'
    implementation 'com.google.firebase:firebase-messaging:20.0.0'
    androidTestImplementation 'androidx.test:runner:1.2.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    implementation 'com.squareup.picasso:picasso:2.5.2'
    implementation 'com.google.android.material:material:1.0.0'


}
apply plugin: 'com.google.gms.google-services'

Solution

  • For now the only thing that worked is replacing AppCompatActivity with Activity and the warning is gone. Will see in the future, if the appcompat library conflict will be solved.