简体   繁体   中英

How to resolve java.lang.NoClassDefFoundError in Unity android plugin

I am trying to make an android plugin for my Unity project that displays a notification bar with some actions. I export the android plugin as an AAR file and import it into Unity. This was all working fine, up until just yesterday when I wrote in some minor new features. I built the module, re-imported the aar into unity, and pushed to a device, only to get this error when the app tries to instantiate a NotificationCompat.Builder:

java java.lang.NoClassDefFoundError: Failed resolution of: Landroid/support/v4/app/NotificationCompat$Builder

The strange thing is that prior to yesterday the notification was working just fine, and I have no idea how code I changed could result in a run-time error like this. I have somehow messed up the integration pipeline from my plugin to Unity, and have no idea how I caused it or how to fix it.

I have seen related posts that claim you need to manually inject dependent libraries from the sdk into unity. However, I find this hard to believe because it was working fine and I have never had to do any of that. I did go ahead and try sticking "support-v4-25.3.1.aar" from the sdk into the plugins folder, but to no avail, same error on trying to display the notification.

Here's the basic setup:

MyService.java:

import android.support.v4.app.NotificationCompat;
...

 public void showNotification(){
        mNotificationManager.cancelAll();


            int importance = NotificationManager.IMPORTANCE_HIGH;
            NotificationChannel mChannel = mNotificationManager.getNotificationChannel(id);
            if (mChannel == null) {
                mChannel = new NotificationChannel(id, title, importance);
                mChannel.setSound(null, null);
                mNotificationManager.createNotificationChannel(mChannel);
            }

            NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext(),id);
            builder.setSmallIcon(R.drawable.audiotilesplayer_notification);
            builder.setContentTitle("MyApp");
            builder.setContentText("App is running");
            builder.setContentIntent(contentIntent);
            builder.addAction(buttonID, "Do Some Action",
                    buttonPendingIntent);
            Notification noti = builder.build();
            mNotificationManager.notify(NOTIFY_ID, builder.build());

    }
...

And the module's build.gradle:

apply plugin: 'com.android.library'

android {
    compileSdkVersion 28



    defaultConfig {
        minSdkVersion 16
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

    }

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

}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'com.android.support:appcompat-v7:28.0.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    implementation files('libs/classes.jar')
    implementation 'com.android.support:support-compat:28.0.0'
}

The app crashes on java NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext(),id); with the described error. What's very curious to me is that this is a runtime error and not a compile time error... I don't quite understand the mechanics of unity-android plugins, but missing a class definition sounds like something that should fail at compile time (unless this is some funky reflection shenanigans unity is doing to push off resolution?). I have been pulling my hair out over how this could have gotten messed up when I have changed nothing about my build process, any advice greatly appreciated!

Encountered same issue. Managed to fix it by copying dependencies from Android Studio to Unity mainTemplate.gradle. The dependencies I had to copy were

  • implementation 'androidx.appcompat:appcompat:1.3.0'
  • implementation 'com.google.android.material:material:1.4.0'

Here is how my how mainTemplate.gradle looks on empty unity project, with just android plugin. Unity 2019.3, enabled mainTemplate from publishing settings.

    // GENERATED BY UNITY. REMOVE THIS COMMENT TO PREVENT OVERWRITING WHEN EXPORTING AGAIN

apply plugin: 'com.android.library'
**APPLY_PLUGINS**

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.3.0'
    implementation 'com.google.android.material:material:1.4.0'
**DEPS**}

android {
    compileSdkVersion **APIVERSION**
    buildToolsVersion '**BUILDTOOLS**'

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    defaultConfig {
        minSdkVersion **MINSDKVERSION**
        targetSdkVersion **TARGETSDKVERSION**
        ndk {
            abiFilters **ABIFILTERS**
        }
        versionCode **VERSIONCODE**
        versionName '**VERSIONNAME**'
        consumerProguardFiles 'proguard-unity.txt'**USER_PROGUARD**
    }

    lintOptions {
        abortOnError false
    }

    aaptOptions {
        ignoreAssetsPattern = "!.svn:!.git:!.ds_store:!*.scc:.*:!CVS:!thumbs.db:!picasa.ini:!*~"
    }**PACKAGING_OPTIONS**
}**REPOSITORIES****SOURCE_BUILD_SETUP**
**EXTERNAL_SOURCES**

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