簡體   English   中英

如何解決 Unity android 插件中的 java.lang.NoClassDefFoundError

[英]How to resolve java.lang.NoClassDefFoundError in Unity android plugin

我正在嘗試為我的 Unity 項目制作一個 android 插件,該插件顯示帶有一些操作的通知欄。 我將 android 插件導出為 AAR 文件並將其導入 Unity。 這一切都很好,直到昨天我編寫了一些小的新功能。 我構建了模塊,將 aar 重新導入到 unity 中,然后推送到設備,但在應用程序嘗試實例化 NotificationCompat.Builder 時卻出現此錯誤:

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

奇怪的是,在昨天之前,通知工作得很好,我不知道我更改的代碼如何導致這樣的運行時錯誤。 我不知何故弄亂了從我的插件到 Unity 的集成管道,並且不知道我是如何導致它或如何修復它的。

我看過相關帖子,聲稱您需要手動將 sdk 中的依賴庫注入 unity。 然而,我覺得這很難相信,因為它運行良好,而且我從來沒有做過任何那樣的事情。 我確實繼續嘗試將“support-v4-25.3.1.aar”從sdk粘貼到插件文件夾中,但無濟於事,嘗試顯示通知時出現同樣的錯誤。

這是基本設置:

我的服務.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());

    }
...

以及模塊的 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'
}

應用程序崩潰java NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext(),id); 與描述的錯誤。 令我感到非常好奇的是,這是一個運行時錯誤而不是編譯時錯誤......我不太了解 unity-android 插件的機制,但缺少類定義聽起來像是應該在編譯時失敗的東西(除非這是一些時髦的反射惡作劇,Unity 正在做些什么來推動解決方案?)。 當我沒有改變我的構建過程時,我一直在糾結這會如何變得一團糟,任何建議都非常感謝!

遇到同樣的問題。 通過將依賴項從 Android Studio 復制到 Unity mainTemplate.gradle 來設法修復它。 我必須復制的依賴項是

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

這是我的 mainTemplate.gradle 在空的統一項目上的外觀,只有 android 插件。 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM