简体   繁体   中英

NoClassDefFoundError: Class not found using the boot loader class loader Android Studio

I've been working on a very basic app that sends a notification. I am getting this error and I have no idea why. I have looked at every similar question on stack overflow and nothing has worked. I have reinstalled and updated my.gradle file, changed dependencies and switched instant run off.

I am trying to use a NotificationChannel, and it is not finding the library.

Here is my build.gradle(:app)

apply plugin: 'com.android.application'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.3"

    defaultConfig {
        applicationId "com.example.labdirectreplynotification"
        minSdkVersion 16
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])

    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}

and the location in which the error occurs

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        nMgr = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        createNotificationChannel(chID, "My News", "Example Direct Reply Notification");
    }

    private void createNotificationChannel(String id, String name, String desc) {
        NotificationChannel ch = new NotificationChannel(id, name, NotificationManager.IMPORTANCE_HIGH);
        ch.setDescription(desc); ch.enableLights(true); ch.setLightColor(Color.RED);
        ch.enableVibration(true);
        ch.setVibrationPattern(new long[] {100,200,300,400,500,400,300,200,400});
        nMgr.createNotificationChannel(ch);
        processInlineReply();
    }

and the error

java.lang.NoClassDefFoundError: Failed resolution of: Landroid/app/NotificationChannel;
        at com.example.labdirectreplynotification.MainActivity.createNotificationChannel(MainActivity.java:40)
        at com.example.labdirectreplynotification.MainActivity.onCreate(MainActivity.java:32)
        at android.app.Activity.performCreate(Activity.java:6877)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1136)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3209)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3352)
        at android.app.ActivityThread.access$1100(ActivityThread.java:223)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1797)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:158)
        at android.app.ActivityThread.main(ActivityThread.java:7231)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
     Caused by: java.lang.ClassNotFoundException: Didn't find class "android.app.NotificationChannel" on path: DexPathList[[zip file "/data/app/com.example.labdirectreplynotification-1/base.apk"],nativeLibraryDirectories=[/data/app/com.example.labdirectreplynotification-1/lib/arm64, /vendor/lib64, /system/lib64]]
        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
        at com.example.labdirectreplynotification.MainActivity.createNotificationChannel(MainActivity.java:40) 
        at com.example.labdirectreplynotification.MainActivity.onCreate(MainActivity.java:32) 
        at android.app.Activity.performCreate(Activity.java:6877) 
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1136) 
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3209) 
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3352) 
        at android.app.ActivityThread.access$1100(ActivityThread.java:223) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1797) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:158) 
        at android.app.ActivityThread.main(ActivityThread.java:7231) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 
        Suppressed: java.lang.ClassNotFoundException: android.app.NotificationChannel
        at java.lang.Class.classForName(Native Method)
        at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
        at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
                ... 15 more
     Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack trace available

Try following source code in create notificationChannel

 NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);

    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
        NotificationChannel channel = new NotificationChannel("simplifiedcoding", "simplifiedcoding", NotificationManager.IMPORTANCE_DEFAULT);
        notificationManager.createNotificationChannel(channel);
    }

    NotificationCompat.Builder notification = new NotificationCompat.Builder(getApplicationContext(), "simplifiedcoding")
            .setContentTitle(title)
            .setContentText(task)
            .setVibrate(longArrayOf(1000, 1000, 1000, 1000, 1000))
            .setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
            .setSmallIcon(R.mipmap.ic_launcher);

    notificationManager.notify(1, notification.build());

Notification channels are not supported below api <26, you have to handle version check while creating notification channel

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
        NotificationChannel channel = new NotificationChannel("simplifiedcoding", "simplifiedcoding", NotificationManager.IMPORTANCE_DEFAULT);
        notificationManager.createNotificationChannel(channel);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM