[英]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.我正在尝试为我的 Unity 项目制作一个 android 插件,该插件显示带有一些操作的通知栏。 I export the android plugin as an AAR file and import it into Unity.我将 android 插件导出为 AAR 文件并将其导入 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:我构建了模块,将 aar 重新导入到 unity 中,然后推送到设备,但在应用程序尝试实例化 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.我不知何故弄乱了从我的插件到 Unity 的集成管道,并且不知道我是如何导致它或如何修复它的。
I have seen related posts that claim you need to manually inject dependent libraries from the sdk into unity.我看过相关帖子,声称您需要手动将 sdk 中的依赖库注入 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.我确实继续尝试将“support-v4-25.3.1.aar”从sdk粘贴到插件文件夹中,但无济于事,尝试显示通知时出现同样的错误。
Here's the basic setup:这是基本设置:
MyService.java:我的服务.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:以及模块的 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);
应用程序崩溃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?).令我感到非常好奇的是,这是一个运行时错误而不是编译时错误......我不太了解 unity-android 插件的机制,但缺少类定义听起来像是应该在编译时失败的东西(除非这是一些时髦的反射恶作剧,Unity 正在做些什么来推动解决方案?)。 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.通过将依赖项从 Android Studio 复制到 Unity mainTemplate.gradle 来设法修复它。 The dependencies I had to copy were我必须复制的依赖项是
Here is how my how mainTemplate.gradle looks on empty unity project, with just android plugin.这是我的 mainTemplate.gradle 在空的统一项目上的外观,只有 android 插件。 Unity 2019.3, enabled mainTemplate from publishing settings. Unity 2019.3,从发布设置启用 mainTemplate。
// 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**
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.