Skip to content
Vitaliy Khudenko edited this page Feb 24, 2017 · 8 revisions

A guide on how to run Javacpp on Android studio together with gradle

Created Dec 21, 2015 by shekmun

Introduction

If you are using Android studio and want to use Javacpp for running the native code, here is a simple reference. The Android studio is using a new build manager gradle which is similar to maven. The gradle will do a lot of work automatically including running the command line with javacpp.jar to produce the .so library.

Getting Started

To Add the javacpp.jar to the project, you just need to download the newest version of .jar file and put it into the project/module/libs folder and add this line to the dependencies (which should be added automatically by the Android studio).

compile fileTree(include: ['*.jar'], dir: 'libs')

or add this line to the dependencies and gradle will download it for you:

compile 'org.bytedeco:javacpp:1.1'
// or compile group: 'org.bytedeco', name: 'javacpp', version: '1.1'

After doing this, we should be able to access JavaCPP classes and annotations.

Running Javacpp at Build Time

To build the native bindings, we also need to execute javacpp from within the gradle lifecycle. The following code of gradle will help us with this.

android {
    applicationVariants.all { variant ->
        variant.javaCompiler.doLast {
            println 'javacpp ' + variant.name
            javaexec {
                main 'org.bytedeco.javacpp.tools.Builder'
                classpath '/path/to/javacpp.jar'
                args '-cp', variant.javaCompiler.destinationDir,
                        '-properties', 'android-arm',
                        '-Dplatform.root=/path/to/android-ndk-r10e',
                        '-Dplatform.compiler=/path/to/android-ndk-r10e/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/bin/arm-linux-androideabi-g++',
                        '-Dplatform.includepath=/path/to/android-ndk-r10e/sources/cxx-stl/gnu-libstdc++/4.9/include:/path/to/android-ndk-r10e/sources/cxx-stl/gnu-libstdc++/4.9/libs/armeabi/include:/path/to/your/own/header_file_folder',
                        '-Dplatform.linkpath=/path/to/android-ndk-r10e/sources/cxx-stl/gnu-libstdc++/4.9/libs/armeabi:/path/to/your/own/library_folder',
                        '-d', 'libs/armeabi'
            }
            println 'javacpp done'
        }
    }
}

The two line of println *** will print the message when you rebuild (Build->Rebuild project) your project and shows up the the gradle console. The main part named javaexec will do exactly the same work as the following command do:

java javacpp.jar -cp *** -properties *** ...

And after that a .cpp file will created by javacpp.jar according the javacpp code and annotaions you write in your android java code. The file will be deleted automatically if the javacpp finished properly. If it failed you will see it in your libs/armeabi folder. And then another command will run (you can see it in the gradle console):

/path/to/android-ndk-r10e/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/bin/arm-linux-androideabi-g++ --sysroot=/path/android-ndk-r10e/platforms/android-14/arch-arm/ -I/path/to/android-ndk-r10e/sources/cxx-stl/gnu-libstdc++/4.9/include -I/path/to/android-ndk-r10e/sources/cxx-stl/gnu-libstdc++/4.9/libs/armeabi/include -I/path/to/your/own/header_file_folder libs/armeabi/name_after_your_annotation.cpp -march=armv5te -mtune=xscale -msoft-float -Wl,-rpath,lib/ -DANDROID -ffunction-sections -funwind-tables -fstack-protector -funswitch-loops -finline-limit=300 -Wall -O3 -nodefaultlibs -fPIC -shared -Wl,--no-undefined -s -o libs/armeabi/name_after_your_annotation.so -L/path/to/android-ndk-r10e/sources/cxx-stl/gnu-libstdc++/4.9/libs/armeabi -L/path/to/your/own/library_folder -lyour_own_lib -llog -lgnustl_static -lgcc -ldl -lz -lm -lc 

See, it's very similar to the g++ command line. So you should know what the parameter means in the gradle code.

And remember to remove the parameter like /your/own/library_folder or -lyour_own_lib if you don't have one.

Importance

To make the javacpp work properly, we need to disable the default NDK build call of Android studio with the following code:

android {
    sourceSets.main {
        jniLibs.srcDir 'libs'
        jni.srcDirs = [] // disable automatic ndk-build call
    }
}

Conclusion

Ather adding the three part of code into your gradle file, your javacpp should be able to work now.

Tips

I'm working on ubuntu 15.10 (x64), Android studio 1.5, gradle 1.3.0 and javacpp 1.1.