I'm trying to integrate libsodium library on Android project. I'm using Android Studio (0.5.8) with gradle (0.9) and android build tools (19.1). I compiled libsodium with scripts that can be found in the library. I get four preBuild libs (.so files) for different architectures (arm, arm-v7a, mips & x86). I put this files inside the jniLibs folder. I declare in my build.gradle file that I'm using NDK and configure the path on local.properties file.
In this version of gradle, it's not necessary to write a makefile (http://ph0b.com/android-studio-gradle-and-ndk-integration/)
I declare some native functions in my activity after do a static call:
static {
System.loadLibrary("sodium");
}
public final static native String sodium_version_string();
onCreate() {
...
Log.d(getClass().getName(), "Sodium version:" + sodium_version_string());
}
Logcat output is:
05-29 23:14:10.538 481-1569/system_process I/ActivityManager﹕ Start proc com.example.myapplication2.app.x86 for activity com.example.myapplication2.app.x86/com.example.myapplication2.app.MainActivity: pid=1584 uid=10056 gids={50056, 1028}
05-29 23:14:10.554 1584-1584/com.example.myapplication2.app.x86 D/dalvikvm﹕ Trying to load lib /data/app-lib/com.example.myapplication2.app.x86-2/libsodium.so 0xa4ed8520
05-29 23:14:10.562 1584-1584/com.example.myapplication2.app.x86 D/dalvikvm﹕ Added shared lib /data/app-lib/com.example.myapplication2.app.x86-2/libsodium.so 0xa4ed8520
05-29 23:14:10.562 1584-1584/com.example.myapplication2.app.x86 D/dalvikvm﹕ No JNI_OnLoad found in /data/app-lib/com.example.myapplication2.app.x86-2/libsodium.so 0xa4ed8520, skipping init
05-29 23:14:10.578 1584-1584/com.example.myapplication2.app.x86 W/dalvikvm﹕ No implementation found for native Lcom/example/myapplication2/app/MainActivity;.sodium_version_string:()Ljava/lang/String;
05-29 23:14:10.578 1584-1584/com.example.myapplication2.app.x86 D/AndroidRuntime﹕ Shutting down VM
05-29 23:14:10.578 1584-1584/com.example.myapplication2.app.x86 W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xa4c46648)
05-29 23:14:10.578 1584-1584/com.example.myapplication2.app.x86 E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.UnsatisfiedLinkError: Native method not found: com.example.myapplication2.app.MainActivity.sodium_version_string:()Ljava/lang/String;
at com.example.myapplication2.app.MainActivity.sodium_version_string(Native Method)
at com.example.myapplication2.app.MainActivity.onCreate(MainActivity.java:28)
Do I missing to declare something in another file? Is it really necessary kalium-JNI ? How can this lib be included in the project?
Thanks.
Right now your Java code has no way to know how to use your native library. You need to do the JNI part, either by using Kalium-JNI or implementing only the parts you need.
In the case of sodium_version_string()
, this should work:
jstring java_com_example_myapplication2_app_MainActivity_sodium_version_string(JNIEnv* env, jclass cls)
{
return (env*)->NewStringUTF(env, sodium_version_string());
}
Here java_com_example_myapplication2_app_MainActivity_sodium_version_string
implementation will be automatically associated to your static native String sodium_version_string()
thanks to JNI convention.
You can use JNI_Onload()
instead to manually declare such association, but you still need to deal with JNI environment and objects inside the implementations.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With