How do I wrap an existing C library for use with Android Studio using SWIG, given that I have headers?
I have an existing C library built for Android. Now I need to interact with it using JNI. It looks like SWIG is a sane way to do it. But with all the SWIG examples I can find the c assembly code in the library, which is then wrapped by SWIG.
I have all the library headers I have that is needed to execute the SWIG, but I cannot figure out how to include the library in the Android Studio build process.
The gradle build file looks like this one example project , but I don't see how it knows how to include the built one. so the file into the project.
apply plugin: 'com.android.application'
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
applicationId "com.sureshjoshi.android.ndkexample"
minSdkVersion 15
targetSdkVersion 21
versionCode 1
versionName "1.0"
ndk {
moduleName "SeePlusPlus" // Name of C++ module (i.e. libSeePlusPlus)
cFlags "-std=c++11 -fexceptions" // Add provisions to allow C++11 functionality
stl "gnustl_shared" // Which STL library to use: gnustl or stlport
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.3'
compile 'com.jakewharton:butterknife:6.0.0'
}
// Location of where to store the jni wrapped files
def coreWrapperDir = new File("${projectDir}/src/main/java/com/sureshjoshi/core")
task createCoreWrapperDir {
coreWrapperDir.mkdirs()
}
// For this to work, it assumed SWIG is installed
// TODO: This only works when called from Command Line (gradlew runSwig)
task runSwig(type:Exec, dependsOn: ['createCoreWrapperDir']) {
String osName = System.getProperty("os.name").toLowerCase();
if (osName.contains("windows")) {
workingDir '/src/main/jni' // This implicitly starts from $(projectDir) evidently
commandLine 'cmd', '/c', 'swig'
args '-c++', '-java', '-package', 'com.sureshjoshi.core', '-outdir', coreWrapperDir.absolutePath, 'SeePlusPlus.i'
}
else {
commandLine 'swig'
args '-c++', '-java', '-package', 'com.sureshjoshi.core', '-outdir', coreWrapperDir.absolutePath, "${projectDir}/src/main/jni/SeePlusPlus.i"
}
}
source to share
The example code you are using is mine, so maybe I can help :)
The library itself is statically loaded into the project in MainActivity (or MainApplication, whatever). Make sure your libraries are in the correct location.
static {
// Use the same name as defined in app.gradle (do not add the 'lib' or the '.so')
System.loadLibrary("SeePlusPlus");
}
In my example NDK that you will see after build, the .so files end up in android-ndk-swig-example / NDKExample / app / build / intermediates / ndk / debug / lib /
If you have your own .so files I believe they need to go to the app / src / main / jniLibs / [architecture] folder ... They can be placed there, then try loading the Library and if the application crashes, it is wrong.
Likewise, check out this answer - in particular talk about sources (should be jniLibs by default, but you can change it): fooobar.com/questions/12712 / ...
source to share