How do I wrap an existing C library for use with Android Studio, with SWIG, given I have the headers? - android-studio

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"
}
}

The example code you're using is mine, so maybe I can help :)
The library itself is statically loaded into the project in the MainActivity (or MainApplication, whatever). Make sure your libraries end up 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 NDK example, you'll see after building, 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 into the app/src/main/jniLibs/[architecture] folder... You can put them there, then try the loadLibrary, and if the app crashes, it's incorrect.
Similarly, check out this answer - specifically the talk about sourcesets (default should be jniLibs, but you can mod it): https://stackoverflow.com/a/26693354/992509

Related

Android Studio 2.3.3 using gradle component model was creating .aar automagically; Android Studio 3.4.1 now using CMake or NDK-Build does not

My wavvoxlibrary/build.gradle is not only serving the main program (app/build.gradle) but also creating the shared library .aar.
And that is working nicely with Android Studio 2.3.3 and -- for the library -- gradle-experimental 0.9.3. The gradle component model, however, does not support abifilters, say, to build the JNI / c/c++ programs in 64-bit.
So I upgraded to Android Studio 3.4.1, and my wavvoxlibrary/build.gradle moved away form said component model and got the appropriate CMakeList.txt file to list the c/c++ programs and any compiler options that used to be part of the older build.gradle. And I also added the NDK-Build option with the Application.mk and Android.mk make files.
So far so good. My app is built and running correctly -- including the shared libraries for both, armabi-v7a (32-bit) and arm64-v8a (64-bit).
What's missing: to create the .aar file, I have to explicitly call the gradle task wavvoxlibrary .. assemble.
Is there some way to generate the .aar that during build of my, say, signed apk?
I have removed the gradle component model to move from gradle:2.3.3 and gradle-experimental:0.9.3 with "apply plugin: 'com.android.model.library' "
to gradle:3.4.1 with "apply plugin: 'com.android.library' " and also called the cmake (or, optionally ndkBuild instead) to list the c/c++ files and compiler options there.
See my blog with the details: http://jurgenmenge.com/blog/computer/migrate-a-library-in-android-studio/
import java.text.SimpleDateFormat
// migrate from Android Studio with gradle:2.3.3 + gradle-experimental:0.9.3
// to Android Studio with gradle:3.4.1 + Cmake + ndkBuild
// jm*20190614
apply plugin: 'com.android.library'
//apply plugin: 'com.android.model.library'
/**
* Each subversion code should be exactly 2 numeric digits or we will get wrong versions published
* in the Google Play Store; buildType to switch between "cmake" and "ndkBuild"
*/
ext.majorVersion = "03"
ext.minorVersion = "08"
ext.patchVersion = "03"
static String buildKind() { // neat trick I created :-) // jm*20190614
//return "gradle" // model - experimental
//return "cmake"
return "ndkBuild"
}
/**
* The name of the app version, using the Semantic Versioning format (major.minor.patch)
* #return
*/
String appVersionName() {
String appVersion = removeLeadingZeros(ext.majorVersion) + "." \
+ removeLeadingZeros(ext.minorVersion) + "." \
+ ext.patchVersion
System.out.println(" ************************************************")
System.out.println(" *** WavvoxLibrary version: " + appVersion + " ")
System.out.println(" *** buildKind " + buildKind() )
System.out.println(" ************************************************")
return appVersion
}
//model {
android {
compileSdkVersion 28
buildToolsVersion "28.0.3" // "25.0.3"
defaultConfig {
minSdkVersion 16 /*** was: 8 ***/
targetSdkVersion 28
//
versionName appVersionName() + "_" + buildKind()
setProperty("archivesBaseName", "wavvoxlibrary-$versionName")
buildConfigField "String", "VERSION_NAME_WAVVOX_FORMAT", versionNameWavvoxFormat()
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
//} // defaultConfig
ndk {
moduleName "wavvox-decoder"
ldLibs "log", "android"
// these platforms cover 99% percent of all Android devices
abiFilters "arm64-v8a", "armeabi-v7a"
//, "x86", "x86_64"
} // ndk
externalNativeBuild {
switch (buildKind()) {
case "ndkBuild":
ndkBuild {
cFlags "-std=c99", "-O3", "-Ofast", "-mfpu=neon" // jm*20190616
// "-Wno-error=format-security", "-mfloat-abi=softfp:, "-g"
abiFilters "armeabi-v7a", "arm64-v8a"
}
break
case "cmake":
cmake {
cFlags "-std=c99", "-O3", "-Ofast", "-mfpu=neon" // jm*20190616
// "-Wno-error=format-security", "-mfloat-abi=softfp:, "-g"
abiFilters 'armeabi-v7a', 'arm64-v8a'
}
break
default:
println "**** unknown buildType value: " + buildType() + " ****"
}
} // externalNativeBuild
} // defaultConfig
buildTypes {
release {
minifyEnabled true
proguardFiles "proguard-rules.pro"
}
} // buildTypes
// } // android // jm*20190405
// android.ndk { // jm*20190405
// C source files to include in the build script
// android.sources.main.jni { // jm*20190405
/* if (buildKind() == "gradle") // using "model" in gradle_experimental
sourceSets {
main {
jniLibs.srcDirs = ['src/main/jni'] // for pre-built .so files
jni {
//source {
include "wavvoxNativeApp.c"
// ... and all the other c/c++ programs
srcDir "jni"
//}
} // jni
} // main
} // source
*/
externalNativeBuild {
switch (buildKind()) {
case "ndkBuild":
ndkBuild {
path 'src/main/jni/Android.mk'
}
break
case "cmake":
cmake {
version '3.10.2'
path 'src/main/jni/CMakeLists.txt'
}
break
default:
println "**** unknown buildType value: " + buildType() + " ****"
}
} // externalNativeBuild
} // android // jm*20190405
//} // model
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.android.support:support-annotations:28.0.0'
testImplementation 'junit:junit:4.12'
testImplementation 'org.hamcrest:hamcrest-junit:2.0.0.0'
}
/**
* Workaround for using ProGuard with the experimental Gradle plugin.
*
* Trouble seems to be that unless process(Debug|Release)Resources task is needed, it is not created
* and VariantOutputScope.setProcessResourcesTask in TaskManager.createProcessResTask() is not
* called so when TaskManager.applyProguardConfig() calls
* BaseVariantOutputData.processResouresTask.getProguardOutputFile() it does so on a null object.
* So, by making transformClassesAndResourcesWithProguardFor(Debug|Release) depend on
* process(Debug|Release)Resources I force the task to be created
*
* More discussions here: https://issuetracker.google.com/issues/37079003
*/
tasks.all { task ->
def match = task.name =~ /^transformClassesAndResourcesWithProguardFor(.*)$/
if (match) {
task.dependsOn "process${match.group(1)}Resources"
return
}
}
// ToDo: get automatic build of .aar libraries working;
// currently, double-click on Gradle "wavvoxlibrary / Tasks / build / assemble"
}
I wanted to get the .aar file created automagically while building the signed app (.apk) without the extra step running gradle task wavvoxlibrary assemble.
What do I miss?
Thanks, jm.
I think you might have tried this, but still adding this as answer, if it does not help, will delete this answer.
Add your library module to your settings.gradle file.
include ':app', ':my-library-module'
https://developer.android.com/studio/projects/android-library#AddDependency

Android Studio - error: linker command failed with exit code 1

I downloaded the code from this repository (its a app to control a parrot drone):
Github
with the hopes of getting it to work so i can study the code, however im getting this error that seems hard to find a solution after searching the web, i mostly found things for IOS, xcode, etc
I imported the project into android studio, when i try to execute the app i get the following error:
Error:error: linker command failed with exit code 1 (use -v to see invocation)
I am not really into NDK, but from what i saw it could be the reason, things i tried:
Downloaded NDK and added the correct path to it.
Using latest SDK.
Changes to build.gradle like setting buildToolsVersion "25.0.0", etc
Build.gradle (Project: ardrone)
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.2'
}
}
allprojects {
repositories {
jcenter()
}
}
Build.gradle (Module:app)
import org.apache.tools.ant.taskdefs.condition.Os
apply plugin: 'com.android.application'
android {
compileSdkVersion 24
buildToolsVersion "25.0.0"
defaultConfig {
multiDexEnabled true
applicationId "com.parrot.freeflight"
minSdkVersion 9
targetSdkVersion 24
versionCode 20000
versionName "2.0-SDK"
ndk {
moduleName "adfreeflight"
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
// TODO proguard-rules seem outdated and useless cause it's only Android stuff
}
}
sourceSets.main {
jni.srcDirs = [] // This prevents the auto generation of Android.mk
jniLibs.srcDir 'src/main/jniLibs'
// This is not necessary unless you have precompiled libraries in your project.
}
task buildNative(type: Exec, description: 'Compile JNI source via NDK') {
def ndkCommand = "${android.ndkDirectory}/ndk-build"
if (Os.isFamily(Os.FAMILY_WINDOWS)) {
ndkCommand += ".cmd"
}
commandLine ndkCommand,
'-C', file('src/main/jni').absolutePath,
'-j', Runtime.runtime.availableProcessors(),
'all',
'NDK_DEBUG=1'
}
task cleanNative(type: Exec, description: 'Clean JNI object files') {
def ndkCommand = "${android.ndkDirectory}/ndk-build"
if (Os.isFamily(Os.FAMILY_WINDOWS)) {
ndkCommand += ".cmd"
}
commandLine ndkCommand,
'-C', file('src/main/jni').absolutePath,
'clean'
}
clean.dependsOn 'cleanNative'
tasks.withType(JavaCompile) {
compileTask -> compileTask.dependsOn buildNative
}
}
dependencies {
compile 'com.google.android.gms:play-services:10.0.0'
compile files('libs/android-support-v13.jar')
compile files('libs/com.sony.rdis.receiver-20111206.jar')
compile files('libs/com.sony.rdis.receiver.utility-20111206.jar')
}
local.properties
ndk.dir=C\:\\Users\\BugDroid\\AppData\\Local\\Android\\Sdk\\ndk-bundle
sdk.dir=C\:\\Users\\BugDroid\\AppData\\Local\\Android\\Sdk
You need to check your log for more details as the error 'Linker command failed with exit code 1' is usually followed by the more detailed error.
So to find more details, in Xcode click on the error under Buildtime and choose Reveal in log. This should give you extra hint. Without any specific error, it's difficult to know what's the problem.

Could not find method externalNativeBuild() for arguments

i'm trying to integrate the ndkBuild functionality into an existing android studio project, using the new android studio 2.2 , in order to enable c++ debugging etc.
i have tried out one of the ndk example projects which android studio 2.2 offers, which works perfectly fine. However, when i try to run the gradle commands in my own project, i get this error message:
Error:(73, 0) Could not find method externalNativeBuild() for arguments [build_c6heui1f67l8o1c3ifgpntw6$_run_closure2$_closure9#4329c1c9] on project ':core' of type org.gradle.api.Project.
By following this description
http://tools.android.com/tech-docs/external-c-builds
i ended up with a gradle script which includes the following commands:
externalNativeBuild{
ndkBuild{
path "$projectDir/jni/Android.mk"
}
}
externalNativeBuild {
ndkBuild {
arguments "NDK_APPLICATION_MK:=$projectDir/jni/Application.mk"
abiFilters "armeabi-v7a", "armeabi","arm64-v8a","x86"
cppFlags "-frtti -fexceptions"
}
}
Did i perhaps miss out on something here with the project setup?
I have set the Android NDK location properly under
File -> Project Structure ... -> SDK Location -> Android NDK location
in my android studio.
Anything else i might have forgotton?
Has anyone run into a similar problem before?
Advice would be much appreciated =)
Just had this error myself. In your root build.gradle, make sure that gradle is set to at least version 2.2.0:
So you should have the following in buildscript {...}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.0'
}
Suggested by Kun Ming Xies answer, I have separated my cmake part in two to get rid of the annoying error:
Could not find method arguments() for arguments [-DREVISION=1.3.1] on object of type com.android.build.gradle.internal.dsl.CmakeOptions.
The first part in defaultConfig contains the configuration (command line arguments for CMake and C++ flags), and the second contains the path to CMakeLists.txt:
def revision = "1.3.1"
android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
defaultConfig {
versionCode = ...
versionName "${revision}"
externalNativeBuild {
cmake {
arguments "-DREVISION=${revision}"
cppFlags '-fexceptions', '-frtti', '-std=c++11'
}
}
}
buildTypes { }
lintOptions { }
externalNativeBuild {
cmake {
path 'CMakeLists.txt'
}
}
}
android {
defaultConfig {
externalNativeBuild {
cmake {
arguments '-DANDROID_TOOLCHAIN=clang'
}
}
}

Android: Unable to set getContext() in non-static method - requires API Level 23

Below, is some source code which is being called from a non-static method. I'm getting the error
"Call requires API level 23 (current min is 15);
android.app.Fragment#getContext"
android.content.Context context = (Context) getContext();
How can I get the context object?
Under the object explorer > Gradle Scripts > build.gradle, I see this. Looks like version 23 to me. Am I looking in the correct location?
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.3"
defaultConfig {
applicationId "com.example.test.test"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.2.1'
compile 'com.android.support:support-v4:23.2.1'
}
Looks like I have Android Studio 1.5.1. I just chose the SettingsActivity project template in Android Studio. And added one class to that default code.
You need to import import android.support.v4.app.Fragment instead of import android.support.app.Fragment;
Pass getActivity() instead of getContext() as Context.
One way to solve was to cast "this" (activity) to (Context) inside the Activity onCreate method.

Error: java.util.zip.ZipException: duplicate entry

I'm trying to add a library to my project, right now my current build.gradle is:
apply plugin: 'com.android.application'
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
repositories {
mavenCentral()
}
defaultConfig {
applicationId "com.example.guycohen.cheaters"
minSdkVersion 11
targetSdkVersion 21
versionCode 1
versionName "1.0"
// Enabling multidex support.
multiDexEnabled true
}
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.facebook.android:facebook-android-sdk:4.0.0'
compile 'com.android.support:multidex:1.0.0'
compile 'com.parse.bolts:bolts-android:1.+'
compile 'com.squareup.picasso:picasso:2.5.2'
compile 'com.whl.handytabbar:library:1.0.4'
compile 'com.nineoldandroids:library:2.4.0'
compile 'com.daimajia.easing:library:1.0.1#aar'
compile 'com.daimajia.androidanimations:library:1.1.3#aar'
}
When I add a new library
compile 'com.github.navasmdc:PhoneTutorial:1.+#aar'
I get this error:
Error:Execution failed for task ':app:packageAllDebugClassesForMultiDex'.
> java.util.zip.ZipException: duplicate entry: android/support/v4/print/PrintHelperKitkat$2$1.class
I've tried to fix this issue by adding
configurations { all*.exclude group: 'com.android.support', module: 'support-v4' }
I couldn't find a duplicate class in my project.
I'm sure whether if I could delete the duplicate entry it would run perfectly, but I'm not sure how I'd find it.
compile 'com.android.support:support-v4:22.1.1'
compile ('com.android.support:appcompat-v7:22.1.1') {
exclude module: 'support-v4'
}
compile ('com.facebook.android:facebook-android-sdk:4.2.0') {
exclude module: 'support-v4'
}
compile ('com.github.navasmdc:PhoneTutorial:1.+#aar') {
exclude module: 'support-v4'
}
I use this to replace all support libraries versions with the latest one that i use in gradle file:
configurations.all {
resolutionStrategy.eachDependency { DependencyResolveDetails details ->
def requested = details.requested
if (requested.group == 'com.android.support') {
if (!requested.name.startsWith("multidex")) {
details.useVersion "26.0.1"
}
}
}
}
Try using this versions in build.gradle file.
compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.android.support:design:23.4.0'
compile 'com.android.support:recyclerview-v7:23.4.0'
compile 'com.android.support:cardview-v7:23.4.0'
Your Error:
java.util.zip.ZipException: duplicate entry: android/support/v4/print/PrintHelperKitkat$2$1.class
Step 1:
In this case main hint is android/support/v4/print/PrintHelperKitkat$2$1.class
Step 2: Searching for the class, in your case the "PrintHelperKitkat.class" (in AndroidStudio just hit Ctrl+N on Windows or CMD-O on Mac)
Step 3: See which jar contains it - Android Studio will write it in the popup.
Step 4: Exclude it from all builds,
for example:
com.android.support:support-v4:_____
compile('your_conflicted_dependency')
{
exclude module: 'support-v4'
}
In my case my one dependency also included in my another AAR. So I
deleted that dependency
I was faced the same issue. I solved it by,
Make sure that in all imported project's build.gradle file should have same compileSdkVersion and dependencies versions like in your project's build.gradle file.
It will remove this error.

Resources