首页 > 解决方案 > Untiy android插件类未找到异常

问题描述

我正在为统一编写一个 android 插件,以便能够检查是否为游戏启用了通知。我有一个带有检查通知是否启用的方法的 java 类。当我构建插件和统一的 .apk 时,一切正常。但安装后,调用方法时出现以下异常:

java.lang.NoClassDefFoundError: Failed resolution of: Landroidx/core/app/NotificationManagerCompat;

Java 类

package com.example.plugin;

import android.app.Activity;
import androidx.core.app.NotificationManagerCompat;

public class NotificationPlugin {

    public static boolean areNotificationEnabled(Activity unityActivity) {
        return NotificationManagerCompat.from(unityActivity).areNotificationsEnabled();
    }
}

buld.gradle

apply plugin: 'com.android.library'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.2"


    defaultConfig {
        minSdkVersion 14
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        consumerProguardFiles 'consumer-rules.pro'
    }

    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.0.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
}

gradle.properties

org.gradle.jvmargs=-Xmx1536m
android.useAndroidX=true
android.enableJetifier=true

还没有在其他地方找到解决方案。预先感谢。这可能是我想念的一个超级简单的修复。

更新

以上都没有解决问题

解决方案

正如 Hamid Yusifli 在他的回答中建议的那样,需要启用自定义 gradle 构建模板(项目设置>播放器>发布设置>自定义主 Gralde 模板),并且需要添加库的依赖项(implementation 'androidx.core:core:1.5.0'在我的情况下)。这解决了这个问题。

标签: androidunity3d

解决方案


因此,在您的情况下,似乎统一忽略了您的库 gradle 依赖项,这可能发生的原因有很多。要强制统一包含缺少的依赖项,您必须提供自定义Gradle 构建模板并在该文件中添加您的依赖项。


推荐阅读