I have been working on an app which uses an external library packaged in a .aar file. In the gradle file I only had to add
compile project(':empalink-2.0')
And it worked ok so far. Now I wanted to add a port of the libSVM library, which makes me to copy the jni directory into my app/src/main and then add some code from their gradle file, which ends up like this:
apply plugin: 'com.android.application'
android {
compileSdkVersion 22
buildToolsVersion "23.0.0"
defaultConfig {
applicationId "com.csic.iiia.ActivityRecognition"
minSdkVersion 19
targetSdkVersion 22
versionCode 1
versionName "1.0"
ndk {
moduleName "jnilibsvm" // <-- This is the name of my C++ module!
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
sourceSets.main {
jniLibs.srcDir 'src\\main\\libs'
jni.srcDirs = []
}
task buildNative(type: Exec, description: 'Compile JNI source via NDK') {
def ndkDir = android.ndkDirectory
commandLine "$ndkDir\\ndk-build.cmd",
'-C', file('src\\main\\jni').absolutePath, // Change src/main/jni the relative path to your jni source
'-j', Runtime.runtime.availableProcessors(),
'all',
'NDK_DEBUG=1'
}
task cleanNative(type: Exec, description: 'Clean JNI object files') {
def ndkDir = android.ndkDirectory
commandLine "$ndkDir\\ndk-build.cmd",
'-C', file('src\\main\\jni').absolutePath, // Change src/main/jni the relative path to your jni source
'clean'
}
clean.dependsOn 'cleanNative'
tasks.withType(JavaCompile) {
compileTask -> compileTask.dependsOn buildNative
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.2.0'
compile 'com.loopj.android:android-async-http:1.4.6'
compile project(':empalink-2.0')
compile 'com.google.android.gms:play-services-appindexing:8.1.0'
compile 'org.apache.commons:commons-math3:3.6.1'
}
The added parts are the defaultConfig.ndk and the build/cleanNative stuff.
Now when trying to execute the application, I receive the following error:
java.lang.UnsatisfiedLinkError: dalvik.system.PathClassLoader[DexPathList[[zip file "/data/app/com.csic.iiia.ActivityRecognition-1/base.apk"],nativeLibraryDirectories=[/data/app/com.csic.iiia.ActivityRecognition-1/lib/arm64, /data/app/com.csic.iiia.ActivityRecognition-1/base.apk!/lib/arm64-v8a, /vendor/lib64, /system/lib64]]] couldn't find "libempac.so"
That lilbempac.so is the .so file that comes in the .aar file that was working ok until I added the libSVM to the project. I guess that enabling NDK compilation somehow affected the linkage with the external dependency :empalink-2.0.
This is maybe a late answer but ...
Empatica only provides the armeabi version of libempac.so. But if you add other native libraries, gradle will compute other architectures like armeabi-v7a, mips, mips64, etc. And so the libempac will be available only for one this architectures.
So you need to disable compilation for other architecture than armeabi. You can do this by adding this in app build.gradle file :
android {
[...]
splits {
abi {
enable true
reset()
include 'armeabi'
universalApk false
}
}
}
Related
This is my Directory Structure
src -main
-java
-jniLibs
-armeabi
-lib1.so
-lib2.so
Need to understand what piece of code should be written in build gradle file for these files to be included in the build.
Right now, i am getting the error of not finding the .so file.
java.lang.UnsatisfiedLinkError: nativeLibraryDirectories=[/vendor/lib, /system/lib]]] couldn't find "libFPEComponent.so"
Following solution by H.Brooks
I have got to add here
My Git repository:
My bUild.gradle file
apply plugin: 'com.android.application'
android {
productFlavors {
arm {
ndk {
abiFilters "armeabi-v7a", "armeabi"
}
}
}
sourceSets
{
main
{
jniLibs.srcDirs = ['src/main/jnilibs']
}
//Another code
}
compileSdkVersion 26
buildToolsVersion '26.0.0'
defaultConfig {
applicationId "com.goambee.biometricziqitza"
minSdkVersion 15
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
splits {
// Configures multiple APKs based on ABI.
abi {
// Enables building multiple APKs per ABI.
enable true
// By default all ABIs are included, so use reset() and include to specify that we only
// want APKs for x86, armeabi-v7a, and mips.
// Resets the list of ABIs that Gradle should create APKs for to none.
reset()
// Specifies a list of ABIs that Gradle should create APKs for.
include "armeabi-v7a"
// Specifies that we do not want to also generate a universal APK that includes all ABIs.
universalApk false
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
// compile fileTree(dir: "$buildDir/native-libs", include: 'native-libs.jar')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support.constraint:constraint-layout:1.0.2'
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:26.0.0-alpha1'
}
//task native-libstiveLibsToJar(type: Jar, description: 'create a jar archive of the native libs') {
// destinationDir file("$buildDir/native-libs")
// baseName 'native-libs'
// from fileTree(dir: 'src/main/jnilibs', include: '**/*.so')
// into 'lib/'
//}
//
//tasks.withType(JavaCompile)
// {
// compileTask -> compileTask.dependsOn(nativeLibsToJar)
// }
Here i have included source set block, i also had tested without that block.
So still build isn't able to access .so files.
If you want to use it you can call it in your class like this:
static {
System.loadLibrary("libSONAME");
}
Add the following in your gradle:
jniLibs.srcDirs = ['libs']
Edit:
Adding .so Library in Android Studio
Create Folder "jniLibs" inside "src/main/"
Put all your .so libraries inside "src/main/jniLibs" folder
Folder structure looks like,
|--app:
|--|--src:
|--|--|--main
|--|--|--|--jniLibs
|--|--|--|--|--armeabi
|--|--|--|--|--|--.so Files
|--|--|--|--|--x86
|--|--|--|--|--|--.so Files
No extra code requires just sync your project and run your
application.
Reference
https://github.com/commonsguy/sqlcipher-gradle/tree/master/src/main
Not sure what files you exactly have, if you only have .so?
Have a look at the image below:
This is a 'project/library' within my project and this is how the load the .so files get loaded/added:
I compile the project like this:
compile project(':videokit')
I have been stuck on this problem for 2 days and have tried out all possible solutions given on stackoverflow. Below is my build.gradle file:
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "25.0.3"
sourceSets.main.jni.srcDirs = []
sourceSets.main.jniLibs.srcDir 'src/main/libs'
defaultConfig {
applicationId "com.example.anannyauberoi.testingcam"
minSdkVersion 15
targetSdkVersion 25
versionCode 1
versionName "1.0"
ndk {
moduleName "app"
cFlags "-std=c++11 -fexceptions"
ldLibs "log"
stl "gnustl_shared"
abiFilter "armeabi-v7a"
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
sourceSets { main { jni.srcDirs = []
res.srcDirs = ['src/main/res']
jniLibs.srcDirs=['src/main/libs']
} }
//sourceSets.main.jni.srcDirs = []
// disable automatic ndk-build call, which ignore our Android.mk
task ndkBuild(type: Exec, description: 'Compile JNI source via NDK') {
commandLine "C:/Users/Anannya-Uberoi/AppData/Local/Android/sdk/ndk-bundle/ndk-build.cmd",
'NDK_PROJECT_PATH=build/intermediates/ndk',
'NDK_LIBS_OUT=src/main/jniLibs',
'APP_BUILD_SCRIPT=src/main/jni/Android.mk',
'NDK_APPLICATION_MK=src/main/jni/Application.mk'
}
tasks.withType(JavaCompile) {
compileTask -> compileTask.dependsOn ndkBuild
}
tasks.all { task ->
if (task.name.startsWith('compile') && task.name.endsWith('Ndk')) {
task.enabled = false
}
}
// call regular ndk-build(.cmd) script from app directory
}
//Modify the below set of code to the ndk-build.cmd location in your computer.
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:25.3.1'
compile project(':openCVLibrary249')
}
I have already tried all possible solutions- deleting the obj folder in the build folder, trying to avoid automatic Android.mk call by setting the sourceSets.main, trying to avoid the compileDebugNdk task from getting called. I also do not have any cmake.txt files. I cannot seem to get over the problem.
I have used Android Studio 2.3.2 and 2.1.1 and the problem has persisted in both of them.
Any help would be appreciated.
You should use the latest Android Studio, 2.3.2 is OK. It has integrated externalNativeBuild in android gradle plugin, so you don't need the tricks with custom gradle task.
I could not actually test the build.gradle script below, so please forgive me any typos:
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "25.0.3"
defaultConfig {
applicationId "com.example.anannyauberoi.testingcam"
minSdkVersion 15
targetSdkVersion 25
versionCode 1
versionName "1.0"
externalNativeBuild {
ndkBuild {
targets "app"
cppFlags "-std=c++11 -fexceptions"
arguments "APP_STL=gnustl_shared"
abiFilters "armeabi-v7a"
}
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
sourceSets { main {
res.srcDirs = ['src/main/res']
} }
externalNativeBuild {
ndkBuild {
path "src/main/jni/Android.mk"
}
}
//Modify the below set of code to the ndk-build.cmd location in your computer.
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:25.3.1'
compile project(':openCVLibrary249')
}
I went to:
C:\Users\Dev\AppData\Local\Android\Sdk\ndk-bundle\ndk-build.cmd
directory and for the ndk-build.cmd i press
Right-Click> Edit and change the cmd file from:
#echo off
%~dp0\build\ndk-build.cmd %*
to
#echo off
THAT works for me
I've inherited a really messy android project with a lot of NDK dependencies and having a lot of problems with getting gradle to correctly link and include all .so and .a files into the resulting apk.
The project consists of some java code that sets up some activities and call into a big NDK library, built from C++ which in turn links with a dosens of 3rd party library (prebuilt or built from source).
I have managed to make it build with the latest gradle experimental plugin, but for some reason, my module isn't included in the apk while my 3rd party .so files are even though I can see that gradle have built my module into a .so file which it have placed in the build directory.
My build.gradle looks like this:
apply plugin: 'com.android.model.application'
model {
android {
compileSdkVersion = 23
buildToolsVersion = "22.0.1"
defaultConfig.with {
applicationId = "<removed>"
minSdkVersion.apiLevel = 7
targetSdkVersion.apiLevel = 23
versionCode = 1
versionName = "1.0"
}
ndk {
moduleName = "XM"
CFlags.add("-I${file("src/main/jni")}".toString())
cppFlags.addAll(["-I${file("../../3rd_part/android/osg")}".toString()])
cppFlags.addAll(["-I${file("../../3rd_part/android/opus")}".toString()])
ldFlags.add("-L${file("src/main/jniLibs/armeabi-v7a")}".toString())
ldLibs.addAll(["osgdb_jpeg", "osgdb_freetype", "jpeg","argsub_es", "jnigraphics", "android", "log"])
stl = "gnustl_shared"
abiFilters.add("armeabi-v7a")
}
sources {
main {
jni {
source {
srcDir "../../core"
srcDir "src/main/jni"
}
}
}
}
lintOptions.abortOnError = false
}
android.buildTypes {
release {
minifyEnabled = false
proguardFiles.add(file('proguard-rules.txt'))
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:support-v4:23.0.0'
compile 'com.android.support:appcompat-v7:23.0.0'
compile 'com.android.support:design:23.0.0'
compile 'com.wunderlist:sliding-layer:1.1.1'
}
So to sum up my question: why does gradle build my module (libXM.so), place it into build/libs/xM/shared/armeabi-v7a/debug, but not include it into my final apk file?
I didn't get several things: if you want to build a shared library with gradle, then you have to use apply plugin: 'com.android.model.library' and not application
If you want to build an application and use prebuilt library, then you have to write something like this:
sources {
main {
jni {
source {
srcDir "../../core"
srcDir "src/main/jni"
}
dependencies{
library "XM" linkage "shared"
}
And, of course, to set ldFlags previously.
I pretty much read through almost every question in stackoverflow about the subject.
I browsed documentation and work-notes of other people who are using AS + NDK + gradle to build an aar that would be included by other app.
I was able to build the .so in a different multi-project setup where the structure was different from the one shown in one aspect: it didn't have the first jni/ layer.
I added that extra jni layer so that I'd have a sharedObjectLib#2/ hierarchy. In that jni/ dir, all i have is a single Android.mk whose sole purpose is to include $(call all-subdir-makefiles). After I did that, gradle build reports the NDK failure:
"Error:(89) Android NDK: WARNING: There are no modules to build in this project!"
What I can't seem to be able to do is build multiple shared objects '.so' as part of the aar.
I would really like to know if (a) it is doable; and (b) some pointers to links and/or examples of gradle.build files that actually do that.
Here is the structure I currently have - skipping the usual directories created by Android Studio (v. 1.2.2, btw).
--rootProject/
--build.gradle
--gradle.properties
--local.properties
--settings.gradle
--rootProject.iml
--app/
--moduleProjectThatBuildsAAR/
--build.gradle
--build/
--libs
--src/
--main/
--res/
--AndroidManifest.xml
--jni/
--Android.mk (does include $(call all-subdir-makefiles))
--Application.mk
--sharedObjectLib#1/
--build.gradle
--src/
-- androidTest/
-- main/
--java/
--jni/
-- Android.mk
-- Application.mk
-- *.c and *.h files
--libs/
--obj/
-- build.gradle
It's pretty convoluted and I am hoping the experts would help with simplification.
thanks!
I am using 1.2.2 and found it easy to build the simple NDK projects
from the many tutorials floating around, but frustratingly difficult
to build an NDK project of any complexity. I will summarize what I
found, but I highly suggest reading
this blog
and this StackOverflow.
I found that Android Studio would completely ignore the
Android.mk file I created, and instead auto-generate its own.
To correct this, I had to first hack the build.gradle
script for my project, located at project/app/build.gradle.
You could probably hack the top-level build.gradle, if desired.
This may be what is happening in your case. The auto-generated
Android.mk looks in jni/ for .c source files and finds nothing
there.
This is my build.gradle. I build on a Windows box, so I hacked it for
Windows only. Uncomment the lines if you are using OSX or Linux.
project/app/build.gradle:
//import org.apache.tools.ant.taskdefs.condition.Os
apply plugin: 'com.android.application'
android {
compileSdkVersion 22
buildToolsVersion "22.0.1"
defaultConfig {
applicationId "com.sample.app"
minSdkVersion 15
targetSdkVersion 22
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
//ENABLE CUSTOM ANDROID.MK >>
sourceSets.main.jni.srcDirs= [] //Disable automatic ndk-build.
sourceSets.main.jniLibs.srcDir 'src/main/libs'
//Call regular ndk-build script from app directory
task ndkBuild(type: Exec) {
workingDir file('src/main')
commandLine getNdkBuildCmd()
}
tasks.withType(JavaCompile) {
compileTask -> compileTask.dependsOn { ndkBuild }
}
task cleanNative(type: Exec) {
workingDir file('src/main')
commandLine getNdkBuildCmd(), 'clean'
}
clean.dependsOn cleanNative
}
//ENABLE CUSTOM ANDROID.MK <<
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.2.0'
compile 'com.google.android.gms:play-services:7.5.0'
}
//ENABLE CUSTOM ANDROID.MK >>
def getNdkDir() {
if (System.env.ANDROID_NDK_ROOT != null)
return System.env.ANDROID_NDK_ROOT
Properties properties = new Properties()
properties.load(project.rootProject.file('local.properties').newDataInputStream())
def ndkdir = properties.getProperty('ndk.dir', null)
if (ndkdir == null)
throw new GradleException("NDK location not found. Define location with ndk.dir in the local.properties file")
return (ndkdir)
}
def getNdkBuildCmd() {
def ndkbuild = getNdkDir() + "/ndk-build.cmd"
// def ndkbuild = getNdkDir() + "/ndk-build"
// if (Os.isFamily(Os.FAMILY_WINDOWS))
// ndkbuild += ".cmd"
return ndkbuild
}
//ENABLE CUSTOM ANDROID.MK <<
I am trying to build the tutorials that are bundled with gstreamer-sdk-android-arm-debug-2013.6. The Android.mk file in the src/jni directory (tutorial 1 project) references environment variables such as GSTREAMER_SDK_ROOT. From what I have read, Android Studio does not use/pass environment variables to the build scripts. Is there a best practice for modifying makefiles and for defining/retrieving the key/value pairs required by the build scripts?
Ok, I have a working solution. You CAN pass environment variables to ndk-build (or any other process spawned by gradle Exec). In my case, I wanted to set these for both the clean and build tasks. This is is done using tasks.withType(Exec). The environment parameter is set here for all Exec tasks.
For GSTREAMER_SDK_ROOT, I added an entry to local.properties:
gst.dir=/Users/svenyonson/sdk/gstreamer-sdk-android-arm-debug-2013.6
For PATH, I used the default for the spawned process and added in what I needed.
Here is a working version of build.gradle:
apply plugin: 'com.android.application'
android {
compileSdkVersion 19
buildToolsVersion "20.0.0"
defaultConfig {
applicationId "com.gst_sdk_tutorials.tutorial_1"
minSdkVersion 19
targetSdkVersion 19
}
sourceSets.main {
jni.srcDirs = []
jniLibs.srcDir 'src/main/libs'
java.srcDirs += 'src/main/jni/src'
}
tasks.withType(Exec) {
def localProperties = new Properties()
localProperties.load(project.rootProject.file('local.properties').newDataInputStream())
def gstDir = localProperties.getProperty('gst.dir')
environment = [:]
environment['PATH'] = System.getenv("PATH")+ ":/usr/local/bin"
environment['GSTREAMER_SDK_ROOT'] = gstDir
}
task buildNative(type: Exec, description: 'Compile JNI source via NDK') {
def ndkDir = project.plugins.findPlugin('com.android.application').getNdkFolder()
commandLine "$ndkDir/ndk-build",
'-C', file('src/main/jni').absolutePath,
'-j', Runtime.runtime.availableProcessors(),
'all',
'NDK_DEBUG=1',
'V=1',
'APP_PLATFORM=android-19'
}
task cleanNative(type: Exec, description: 'Clean JNI object files') {
def ndkDir = project.plugins.findPlugin('com.android.application').getNdkFolder()
commandLine "$ndkDir/ndk-build",
'-C', file('src/main/jni').absolutePath,
'clean'
}
clean.dependsOn 'cleanNative'
tasks.withType(JavaCompile) {
compileTask -> compileTask.dependsOn buildNative
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
}
The project now builds and runs. The only other things you will need to do is add ndk.dir to local.properties:
sdk.dir=/Users/svenyonson/sdk/android-sdk
ndk.dir=/Users/svenyonson/sdk/android-ndk-r9d
gst.dir=/Users/svenyonson/sdk/gstreamer-sdk-android-arm-debug-2013.6
One more thing: These examples will not build using android-ndk-r10d. Be sure to use android-ndk-r9d.