6

当我运行我的 Unity 应用程序时,我遇到了丢失的类错误。我有一个编写为 aar 的本机代码库。我刚刚更新了一些代码以支持 Android 8 上的本地通知,并且我使用了 Android X“Worker”类。我的 aar 编译得很好,但我想我缺少一个要包含在 Unity 项目中的库。我应该添加哪个库?

错误是

06-05 10:57:52.970: E/AndroidRuntime(21759): FATAL EXCEPTION: IntentService[com.help.stressfree.localnotifications.NotificationService]
06-05 10:57:52.970: E/AndroidRuntime(21759): Process: com.help.stressfree, PID: 21759
06-05 10:57:52.970: E/AndroidRuntime(21759): java.lang.NoClassDefFoundError: Failed resolution of: Landroidx/work/Data$Builder;
06-05 10:57:52.970: E/AndroidRuntime(21759):    at com.help.stressfree.localnotifications.NotificationService.ScheduleNotification(NotificationService.java:99)
06-05 10:57:52.970: E/AndroidRuntime(21759):    at com.help.stressfree.localnotifications.NotificationService.onHandleIntent(NotificationService.java:82)
06-05 10:57:52.970: E/AndroidRuntime(21759):    at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:68)
06-05 10:57:52.970: E/AndroidRuntime(21759):    at android.os.Handler.dispatchMessage(Handler.java:105)
06-05 10:57:52.970: E/AndroidRuntime(21759):    at android.os.Looper.loop(Looper.java:164)
06-05 10:57:52.970: E/AndroidRuntime(21759):    at android.os.HandlerThread.run(HandlerThread.java:65)
06-05 10:57:52.970: E/AndroidRuntime(21759): Caused by: java.lang.ClassNotFoundException: Didn't find class "androidx.work.Data$Builder" on path: DexPathList[[zip file "/data/app/com.help.stressfree-FN_ED1fyI9LY4tcsB1RWqQ==/base.apk"],nativeLibraryDirectories=[/data/app/com.help.stressfree-FN_ED1fyI9LY4tcsB1RWqQ==/lib/arm, /data/app/com.help.stressfree-FN_ED1fyI9LY4tcsB1RWqQ==/base.apk!/lib/armeabi-v7a, /system/lib, /vendor/lib]]
06-05 10:57:52.970: E/AndroidRuntime(21759):    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:93)
06-05 10:57:52.970: E/AndroidRuntime(21759):    at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
06-05 10:57:52.970: E/AndroidRuntime(21759):    at java.lang.ClassLoader.loadClass(ClassLoader.java:312)

如果相关,这是我的 aar 库的 gradle 文件:

//indicates that this is a library
apply plugin: 'com.android.library'

android {
    compileSdkVersion 27
    buildToolsVersion "27.0.3"

    sourceSets {
        main {
            //Path to your source code
            java {
                srcDir 'src/main/java'
            }
        }
    }

    defaultConfig {
        minSdkVersion 14
        targetSdkVersion 27
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    lintOptions {
        abortOnError false
    }
}

dependencies {
    compileOnly fileTree(include: ['*.jar'], dir: 'libs')
    androidTestImplementation('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation 'com.android.support:support-v4:27.1.1'
    implementation 'com.android.support:support-media-compat:27.1.1'
    testImplementation 'junit:junit:4.12'
    compileOnly files('libs/classes.jar', 'libs/libmessaging_unity_player_activity.jar')

    def work_version = "1.0.0-alpha02"

    implementation "android.arch.work:work-runtime:$work_version" // use -ktx for Kotlin
}

//task to delete the old jar
task deleteOldJar(type: Delete) {
    delete 'default/AndroidPlugin.jar'
}

//task to export contents as jar
task exportJar(type: Copy) {
    from('build/intermediates/bundles/default/')
    into('libs/jar')
    include('classes.jar')
    ///Rename the jar
    rename('classes.jar', 'AndroidPlugin.jar')

}

exportJar.dependsOn(deleteOldJar, build)

//task to delete the old jar
task deleteOldAAR(type: Delete) {

}

//task to export contents as jar
task exportAAR(type: Copy) {
    from('build/outputs/aar/')
    into('libs/aars')
    include('app-debug.aar')
    ///Rename the jar
    rename('app-debug.aar', 'AndroidPlugin.aar')

}

exportAAR.dependsOn(deleteOldAAR, build)
4

1 回答 1

0

正如我所看到的,您已将您的库添加为一个aar文件,其中 deos 不包含它自己的库依赖项。如果您使用的是 Gradle,则还应添加库依赖项,在这种情况下为android-workmanager.

因此,在这些情况下,请执行以下解决方案:

  • Gradle系统上,带有aar文件。

    • 还包括库依赖项(在build.gradleandroid studio 上的库文件上)。
  • Gradle系统上,库作为Maven工件:

    • 添加依赖项并同步。Gradle 将包含库依赖项本身。
  • 使用Jar resolveraar文件:
    • 与 Gradle 相同,也将库依赖项添加到 xml 文件中。
  • 使用Jar 解析器,以及库的maven工件
    • 将所有依赖项添加到编辑器 xml。
  • 使用内部构建系统,没有依赖管理器:
    • 获取所有依赖项的 aar 或 jar 并将其添加到库旁边。
于 2019-07-11T05:27:47.137 回答