我有一个要从Unity内部调用的android库(但这并不重要)。我有以下代码:
class UnityInterop {
companion object {
@JvmStatic
fun initialize() = initInternal()
private fun initInternal(): Boolean {
executePluginFunction {
// do some stuff here...
}
return false
}
fun executePluginFunction(func: () -> Unit) {
try {
UnityPlayer.currentActivity.runOnUiThread {
func()
}
} catch (e: Exception) {
}
}
}
}
运行它时,出现此错误,似乎是在说为lambda生成的类不存在:
06-04 20:07:09.767 5469 5493 E Unity : java.lang.NoClassDefFoundError: Failed resolution of: Lcom/onehand/nativekeyboard/UnityInterop$Companion$initInternal$1;
06-04 20:07:09.767 5469 5493 E Unity : at com.onehand.nativekeyboard.UnityInterop$Companion.initInternal(UnityInterop.kt:87)
06-04 20:07:09.767 5469 5493 E Unity : at com.onehand.nativekeyboard.UnityInterop$Companion.initialize(UnityInterop.kt:81)
06-04 20:07:09.767 5469 5493 E Unity : at com.onehand.nativekeyboard.UnityInterop.initialize(Unknown Source:2)
06-04 20:07:09.767 5469 5493 E Unity : at com.unity3d.player.UnityPlayer.nativeRender(Native Method)
06-04 20:07:09.767 5469 5493 E Unity : at com.unity3d.player.UnityPlayer.c(Unknown Source:0)
06-04 20:07:09.767 5469 5493 E Unity : at com.unity3d.player.UnityPlayer$e$1.handleMessage(Unknown Source:88)
06-04 20:07:09.767 5469 5493 E Unity : at android.os.Handler.dispatchMessage(Handler.java:104)
06-04 20:07:09.767 5469 5493 E Unity : at android.os.Looper.loop(Looper.java:166)
06-04 20:07:09.767 5469 5493 E Unity : at com.unity3d.player.UnityPlayer$e.run(Unknown Source:20)
06-04 20:07:09.767 5469 5493 E Unity : Caused by: java.lang.ClassNotFoundException: com.onehand.nativekeyboard.UnityInterop$Companion$initInternal$1
所以我从aar内部反编译了jar,据jd-gui告诉我,lambda确实不存在。这里发生了什么事?以下是gradle脚本,以防万一:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext.kotlin_version = '1.3.31'
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.4.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 28
defaultConfig {
minSdkVersion 16
targetSdkVersion 28
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compileOnly fileTree(dir: 'libs', include: ['*.jar'])
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'com.android.support:appcompat-v7:28.0.0'
}
编辑:我在错误的位置寻找。所有的课程都在罐子里。我仍然不知道为什么它们没有被加载。而且jd-gui did 无法显示Android认为缺少的类的任何源代码...
答案 0 :(得分:0)
所以我知道了。真正的错误是在安装apk时报告的,原因是我的lambda的类无法解析,原因是kotlin的基础库中缺少类。要解决此问题,我必须在构建中包括正确版本的kotlin-stdlib
(在我的情况下为1.3.11)。为此,我在此Dependencies.xml
中使用了Google JAR resolver:
<dependencies>
<androidPackages>
<androidPackage spec="org.jetbrains.kotlin:kotlin-stdlib:1.3.11" />
</androidPackages>
</dependencies>