无法解析相应的JNI函数Java_com_mozilla_greetings_RustGreetings_greeting

时间:2018-11-25 05:37:17

标签: android rust java-native-interface

我正在尝试复制this教程,以在Android应用程序中构建和使用Rust库,我成功构建了库,并上传了生成的库here

Android需要使用Java_<Package>_Class_function主题调用的函数是:

Java_com_mozilla_greetings_RustGreetings_greeting

我的android应用结构如下:

enter image description here

我的JNI包装器出现以下错误:

  

无法解析相应的JNI函数   Java_com_mozilla_greetings_RustGreetings_greeting

JNI包装器是:

package com.mozilla.greetings;

public class RustGreetings {

    private static native String greeting(final String pattern);

    public String sayHello(String to) {
        return greeting(to);
    }
}

主要类别是:

package com.mozilla.greetings;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class GreetingsActivity extends AppCompatActivity {

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

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

        RustGreetings g = new RustGreetings();
        String r = g.sayHello("world");
        ((TextView) findViewById(R.id.greetingField)).setText(r);
    }
}

1 个答案:

答案 0 :(得分:0)

我使用JNA解决了这个问题,我认为它比JNI慢,我将在下面使用JNA编写解决方案,希望有人使用JNI提供所需的修复程序< / p>

我的应用程序结构如下,使用kotlin

  • 我将libjnidispatch.so添加到每个库构建文件夹中,可以通过从here中提取所需的Android体系结构,下载所需的jar并将其提取以获取libjnidispatch.so来获得。
  • 我为jna创建了界面

JNA.kt

package com.mozilla.greetings

import com.sun.jna.Library

   interface JNA : Library {
      fun rust_greeting(pattern: String): String
   }
  1. 我为jna RustGreetings.kt创建了包装器:

    package com.mozilla.greetings
    
    import com.sun.jna.Native
    
    class RustGreetings {
    
    fun sayHello(to: String): String =
            Native.loadLibrary<JNA>("greetings", JNA::class.java).rust_greeting(to)
    }
    
  2. 主要活动GreetingsActivity.kt:

    package com.mozilla.greetings
    
    import android.support.v7.app.AppCompatActivity
    import android.os.Bundle
    import kotlinx.android.synthetic.main.activity_greetings.*
    
    
    class GreetingsActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_greetings)
    
        val g = RustGreetings()
        val r = g.sayHello("Rust")
        greetingField.text = r
    }
    
    companion object {
        init {
            System.loadLibrary("greetings")
        }
    }
    }
    

注意: 为了避免使用findViewById,我使用了kotlin扩展名,如here所述,并将以下内容添加到了build.gradle(模块)中:

apply plugin: 'kotlin-android-extensions'

更新

看起来这是IDE没问题的代码,该应用已执行。 我使用Kotlin重新编写了该代码,并且在我的kotlin代码下面也顺利执行了该代码:

GreetingsActivity.kt

package com.mozilla.greetings

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_greetings.*

class GreetingsActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_greetings)

        val g = RustGreetings()
        val r = g.sayHello("My Rust")
        greetingField.text = r
    }

    companion object {

        init {
            System.loadLibrary("greetings")
        }
    }
}

RustGreetings.kt

package com.mozilla.greetings

class RustGreetings {

    private external fun greeting(pattern: String): String

    fun sayHello(to: String): String = greeting(to)
}

build.gradle

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android-extensions'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.mozilla.greetings"
        minSdkVersion 28
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

activity_greetings.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".GreetingsActivity">

    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello World!"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" android:id="@+id/greetingField"/>

</android.support.constraint.ConstraintLayout>

在结构和执行之下,apk为here

enter image description here