将AndroidX库包含在Unity项目中

时间:2018-06-05 09:53:54

标签: android unity3d aar androidx

我在运行Unity应用程序时遇到类错误。我有一个我写的编译为aar的本机代码库。我刚刚更新了一些代码以支持Android 8上的本地通知,并且我使用了Android X“Worker”类。我的编译很好,但我想我错过了一个包含在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)

1 个答案:

答案 0 :(得分:0)

正如我所看到的,您已将库添加为aar文件,但该文件本身并不包含库依赖关系。如果您使用的是Gradle,则还应添加库依赖关系,在这种情况下为android-workmanager

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

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

    • 还包括库依赖项(在Android Studio上库的build.gradle文件中)。
  • Gradle 系统上,将库作为​​ Maven 工件:

    • 添加依赖关系并进行同步。 Gradle将包括库依赖项本身。
  • 使用 Jar解析器,并带有 aar 文件:
    • 与Gradle一样,也将库依赖项添加到xml文件中。
  • 使用 Jar解析器和库中的 maven 工件
    • 将所有依赖项添加到编辑器xml中。
  • 使用内部构建系统,而没有依赖项管理器:
    • 获取所有依赖项的aar或jar,并将其添加到库旁边。