如何修复“未找到int sample.Sample.checkMe()的实现”

时间:2019-08-06 09:41:20

标签: kotlin kotlin-native

因为我想使用K / M为android / ios / middle ware生成.so文件...
我尝试使用此

但是我遇到了如下错误

我将IntelliJ IDEA CE用于新的“ Mulitplatforn库| Gradle”项目,
然后添加“ androidNativeArm64”

plugins {
    id 'org.jetbrains.kotlin.multiplatform' version '1.3.41'
}
repositories {
    mavenCentral()
}
group 'com.example'
version '0.0.1'

apply plugin: 'maven-publish'

kotlin {
    jvm()
    js {
        browser {
        }
        nodejs {
        }
    }
    // For ARM, should be changed to iosArm32 or iosArm64
    // For Linux, should be changed to e.g. linuxX64
    // For MacOS, should be changed to e.g. macosX64
    // For Windows, should be changed to e.g. mingwX64
    macosX64("macos")
    androidNativeArm64("android64"){
        binaries {
            sharedLib{
                baseName = "AndroidNative64"
            }
        }
    }
    sourceSets {
        commonMain {
            dependencies {
                implementation kotlin('stdlib-common')
            }
        }
        commonTest {
            dependencies {
                implementation kotlin('test-common')
                implementation kotlin('test-annotations-common')
            }
        }
        jvmMain {
            dependencies {
                implementation kotlin('stdlib-jdk8')
            }
        }
        jvmTest {
            dependencies {
                implementation kotlin('test')
                implementation kotlin('test-junit')
            }
        }
        jsMain {
            dependencies {
                implementation kotlin('stdlib-js')
            }
        }
        jsTest {
            dependencies {
                implementation kotlin('test-js')
            }
        }
        macosMain {
        }
        macosTest {
        }
        android64Main {
        }
        android64Test {
        }
    }
}


使用默认代码几乎不会修改

Sample.kt

commonMain
---kotlin
------sample
---------Sample.kt
package sample

expect class Sample() {
    fun checkMe(): Int
}

expect object Platform {
    val name: String
}

fun hello(): String = "Hello from ${Platform.name}"



SampleAndroid64.kt

android64Main
---kotlin
------sample
---------SampleAndroid64.kt
package sample

actual class Sample {
    actual fun checkMe() = 87
}

actual object Platform {
    actual val name: String = "Android64"
}


成功构建然后生成“ libAndroidNative64.so”
将此.so用于我的Android项目

src
---main
------java
---------sample
------------Sample.kt
------jniLibs
---------arm64-v8a
------------libAndroidNative64.so


Sample.kt

package sample;

public class Sample {

    public native String hello();

    public native int checkMe();

    static {
        System.loadLibrary("AndroidNative64");
    }
}

我的电话是

public class Main2Activity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        sample.Sample t1 = new sample.Sample();

        // Error
        // java.lang.UnsatisfiedLinkError: No implementation found for int sample.Sample.checkMe() (tried Java_sample_Sample_checkMe and Java_sample_Sample_checkMe__)
        t1.checkMe();


        // Error
        // java.lang.UnsatisfiedLinkError: No implementation found for java.lang.String sample.Sample.hello() (tried Java_sample_Sample_hello and Java_sample_Sample_hello__)
        t1.hello();
    }
}


如何解决。
谢谢大家。

0 个答案:

没有答案