8

I have a program that needs to execute a libsodium encryption. I found this library libsodium but I think it needs to be used with NDK. And so I started to read tutorials about NDK but I still don't know where to start on using this library. If someone could give a hint or very useful stuff to give an idea on how to integrate this library, I would be so happy.

Thanks

junmats
  • 1,816
  • 1
  • 21
  • 36

1 Answers1

13

To integrate libsodium into your Android app, you need:

  • The libsodium library compiled for your Android platform(s)
  • A JNI binding like kalium-jni

If you trust random people on the Internet (which you should not!), download this tarball and extract it into your project source. Otherwise, follow the instructions below to compile it yourself.

libsodium

You need a Linux box/VM with Android NDK to compile the libsodium shared libraries, and it seems like you need the current git master branch to compile it with the NDK. Once you checked it out, compile the Android library code for ARM, ARMv7 and x86:

./autogen.sh
./dist-build/android-arm.sh # for older ARMv6 devices
./dist-build/android-armv7-a.sh # for the more recent ARMv7 devices
./dist-build/android-x86.sh # for the emulator / x86 devices
# Provide the directory names nkd-build expects
ln -s libsodium-android-armv6 libsodium-android-armeabi
ln -s libsodium-android-armv7-a libsodium-android-armeabi-v7a
ln -s libsodium-android-i686 libsodium-android-x86

kalium-jni

To compile kalium, you will need SWIG installed. Then, you need to generate the SWIG C wrapper, compile the libkaliumjni native code for your target platform(s), install it into your app libs/ directory and include the JAR.

In the kalium-jni/jni sub-directory, create the SWIG wrapper and the native libkaliumjni.so for your host (it will be needed for testing the JAR):

./compile.sh

Afterwards, modify jni/Android.mk and replace /installs/ with wherever you have compiled libsodium, and $(TARGET_ARCH) with $(TARGET_ARCH_ABI) then run in the kalium-jni directory:

ndk-build APP_ABI=armeabi,armeabi-v7a,x86
[...]
[x86] Install        : libkaliumjni.so => libs/x86/libkaliumjni.so
[armeabi] Install        : libkaliumjni.so => libs/armeabi/libkaliumjni.so
[armeabi-v7a] Install        : libkaliumjni.so => libs/armeabi-v7a/libkaliumjni.so

Now the libs/ directory contains the native kalium libraries. Copy it into your Android project.

Finally, you need to compile the kalium JAR:

mvn clean install

It should end up in ~/.m2/repository/org/abstractj/kalium/kalium-jni/1.0.0-SNAPSHOT/kalium-jni-1.0.0-SNAPSHOT.jar. Copy that to your libs directory as well. It is accompanied by javadoc and sources JARs, which you can add into Eclipse to get the references.

ge0rg
  • 1,738
  • 1
  • 24
  • 36
  • Your armv7 sodium build isn't going to be picked, the armv5 will be used for armv5 and armv7 builds. In order to change that, you should rename `libsodium-android-arm` to `libsodium-android-armeabi`, and change `TARGET_ARCH` by `TARGET_ARCH_ABI` inside *jni/Android.mk*. Also, you can add easily x86 to the list of supported platforms: `./dist-build/android-x86.sh && ln -s libsodium-android-i686 libsodium-android-x86`, and `ndk-build APP_ABI=armeabi,armeabi-v7a,x86` – ph0b Feb 23 '15 at 12:46
  • 1
    Thanks @ph0b, I've updated the instructions and added a new tarball with x86 .so and (hopefully) fixed arm7 build – ge0rg Feb 24 '15 at 08:16
  • I followed the libsodium instructions. Now after running the do_the_job.sh script I end up with the .so files. How can I now get the .jar? Because now I don't know how to use the library. – user1007522 Nov 06 '15 at 11:04
  • 1
    It's now published as an AAR: https://github.com/GerardSoleCa/Robosodium/releases – benhylau Feb 28 '16 at 22:50
  • @ge0rg do you know how to build libsodiumjni.so with libsodium 1.0.12? – henry74918 Apr 11 '17 at 12:05