I have an existing C library, built for Android. I now need to interface with it using JNI. SWIG seems to be a smart way to do this. But all of the SWIG examples I can find build c code into a library which is then wrapped by SWIG.
I have all the headers for the library I have, which is necessary for SWIG to do its work, but I can't figure out how to include the library in the build process of Android Studio.
The gradle build file looks like this for one example project, but I don't see how it knows to include the built .so 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's 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"
}
}