I posted this at stack overflow as I think it is more related to Android, but though I may get an answer here as well.
I'm trying to replicate the this tutorial for building and using Rust lib in Android app, I build the library successful, and uploaded the generated libs here
The function required to be called by Android using the Java_<Package>_Class_function theme is:
Java_com_mozilla_greetings_RustGreetings_greeting
My android app structure is as below:
I'm getting the below error at my JNI wrapper:
Cannot resolve corresponding JNI function
Java_com_mozilla_greetings_RustGreetings_greeting
The JNI wrapper is:
package com.mozilla.greetings;
public class RustGreetings {
private static native String greeting(final String pattern);
public String sayHello(String to) {
return greeting(to);
}
}
And the main class is:
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);
}
}
I solved it using JNA whcih I think is slower than JNI, I'll write my solution below using JNA hoping someone provide the required fix using JNI
My app structure is as below, using kotlin:
I added the libjnidispatch.so to each library build folder, this can be obtained by extracted the requiredandroid architecture from here, download the required jar, then extract it to get the libjnidispatch.so
I created interbface for jna
JNA.kt
package com.mozilla.greetings
import com.sun.jna.Library
interface JNA : Library {
fun rust_greeting(pattern: String): String
}
I created wrapper for 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)
}
Main activity, 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")
}
}
}
Note:
In order to avoid using findViewById I used kotlin extension, as explained here and addeding the below to the build.gradle (module):
It looks it is an IDE issue nothing with the code, the app had been executed.
I re-wrote it using Kotlin, and it was executed smoothly as well, below my kotlin code:
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)
}