【问题标题】:Kotlin not compiling lambdas in android libraryKotlin 没有在 android 库中编译 lambdas
【发布时间】:2019-06-03 15:43:53
【问题描述】:

我有一个从 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 确实未能显示 Android 认为缺少的类的任何来源......

【问题讨论】:

    标签: android unity3d kotlin lambda


    【解决方案1】:

    所以我想通了。安装 apk 时报告了真正的错误,说我的 lambda 类由于缺少 kotlin 基础库中的类而无法解析。为了解决这个问题,我必须在构建中包含正确版本的kotlin-stdlib(在我的例子中是 1.3.11)。为此,我将Google JAR resolverDependencies.xml 一起使用:

    <dependencies>
      <androidPackages>
        <androidPackage spec="org.jetbrains.kotlin:kotlin-stdlib:1.3.11" />
      </androidPackages>
    </dependencies>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-20
      • 2017-07-05
      • 2018-08-26
      • 2023-04-02
      • 2016-05-31
      相关资源
      最近更新 更多