I am building external native build library with multiple ABIs armeabi-v7a and x86 with Android Studio. The problem is that when I build the project the libraries are in these folders:
App/.externalNativeBuild/cmake/debug/armeabi-v7a/libs/armeabi-v7a/libApp.so
App/.externalNativeBuild/cmake/debug/x86/libs/x86/libApp.so
Where they should be in inside these locations in order to be found correctly on the device:
App/src/main/jniLibs/armeabi-v7a/libApp.so
App/src/main/jniLibs/x86/libApp.so
Is there a way that they can be generated or build directly into jniLibs? At the moment I have done symlinks by hand but I hope there is automated way of handling different ABIs and different configurations.
This is my current gradle script:
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "25.0.2"
defaultConfig {
applicationId "com.org.app"
minSdkVersion 19
targetSdkVersion 19
ndk {
abiFilters 'armeabi-v7a', 'x86'
stl = "c++_static"
}
externalNativeBuild {
cmake {
arguments "-DCMAKE_BUILD_TYPE=Release"
}
}
}
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt')
}
}
externalNativeBuild {
cmake {
path '../../../CMakeLists.txt'
}
}
sourceSets.main {
jni.srcDirs = []
jniLibs.srcDir 'src/main/jniLibs'
}
}
Use
sourceSets.main {
jni.srcDirs = []
jniLibs.srcDirs 'src/main/jniLibs'
}
Related
I am not so much familiar with Cmake of the android studio. Recently I decided to use it for the native interface. Every time I build the project from build tool, I am getting the aforesaid error in gradle sync message.
My CMakeLists.txt file
cmake_minimum_required(VERSION 3.4.1)
add_library(native-lib
SHARED
src/main/cpp/native-lib.cpp)
My build.gradle (app level)
apply plugin: 'com.android.application'
android {
compileSdkVersion 24
buildToolsVersion "25.0.2"
defaultConfig {
applicationId="com.example.user.MyProject"
minSdkVersion 17
targetSdkVersion 24
multiDexEnabled true
versionCode 1
versionName "1.0"
vectorDrawables.useSupportLibrary true
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
externalNativeBuild {
cmake {
cppFlags '-frtti'
arguments '-DANDROID_TOOLCHAIN=clang'
abiFilters 'armeabi-v7a'
}
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
// proguardFiles.add(file("proguard-rules.txt"))
}
}
externalNativeBuild {
cmake {
path 'src/main/cpp/CMakeLists.txt'
}
}
}
dependencies {
What am I doing wrong here?? Any help will be appreciated.
Previously I had two Android Studio projects. One is using some shared library placed in /src/main/jniLibs (lets call that project bmf). The second project is my actual library which is using that other project (bmf) as an .aar.
If some application is now using the library I had to add the project with the shared library (bmf) as well because the .so files were not packed. Because I want to merge the two projects to one library project I have imported the bmf project as a module. Now if I assemble the library, the .so file of the module is not included. How can I fix that?
My app-module build script looks like:
apply plugin: 'com.android.library'
android {
compileSdkVersion 15
buildToolsVersion "26.0.2"
lintOptions {
abortOnError false
}
defaultConfig {
minSdkVersion 15
targetSdkVersion 15
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
}
dependencies {
compile 'com.android.support:support-v4:18.0.0'
implementation project(':pbJavaBmf')
}
My bmf-module build script looks like:
apply plugin: 'com.android.library'
android {
compileSdkVersion 15
buildToolsVersion "26.0.2"
lintOptions {
abortOnError false
}
defaultConfig {
minSdkVersion 15
targetSdkVersion 15
}
externalNativeBuild {
ndkBuild {
path "src/main/jni/Android.mk"
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
task buildNative(type: Exec, description: 'Pack .so with module') {
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(),
'V=1',
'NDK_DEBUG=1',
'NDK_LIBS_OUT=../jniLibs'
}
}
dependencies {
testCompile 'junit:junit:4.12'
compile 'com.android.support:support-v4:18.0.0'
}
I'm working on a PDF viewer application using ebookdroid & MuPDF CPP files. I am having lots of problem with NDK integration in Gradle. I've gone through many answers but they have not fixed my problem.
Gradle is giving me the following error message:
Error:Execution failed for task ':app:compileDebugNdk'.
Error: Your project contains C++ files but it is not using a supported native build system.
Consider using CMake or ndk-build integration with the stable Android Gradle plugin:
https://developer.android.com/studio/projects/add-native-code.html
or use the experimental plugin:
http://tools.android.com/tech-docs/new-build-system/gradle-experimental.
Edit your build.gradle, add defaultConfig.externalNativeBuild.ndkBuild, externalNativeBuild.ndkBuild and sourceSet.main.jni.srcDir options. See the comments below.
android {
compileSdkVersion 22
buildToolsVersion "27.0.0"
defaultConfig {
minSdkVersion 18
targetSdkVersion 22
versionCode 1
versionName "1.0"
//add arguments passed to ndkBuild
externalNativeBuild {
ndkBuild {
arguments "NDK_TOOLCHAIN_VERSION=clang", "APP_SHORT_COMMANDS=true", "APP_ALLOW_MISSING_DEPS=true"
arguments "-j" + Runtime.runtime.availableProcessors()
cFlags "-fexceptions"
}
}
ndk {
abiFilters "armeabi-v7a"
}
}
//specify jni source file path
sourceSets.main {
java.srcDir "src"
res.srcDir "res"
jni.srcDir "jni"
}
buildTypes {
debug {
debuggable true
jniDebuggable true
}
}
//specify makefile / CMake file
externalNativeBuild {
ndkBuild {
path 'jni/Android.mk'
}
}
}
We're trying to publish our Android wearable app via mobile apk, so the wearable app gets synced automatically to the watch. This works fine if the app does not contain the NDK part.
However if we include the NDK/jni to our sources, the app will not install via the mobile.apk. The app installs via building in android studio and adb (adb -e install wearable.apk).
This is our build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.0"
defaultConfig {
applicationId "com.high_mobility.digitalkey"
minSdkVersion 21
targetSdkVersion 23
versionCode 1
versionName "1.0"
ndk {
moduleName = "hmbtcore"
ldLibs "log"
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
splits {
abi {
enable true
reset()
include 'x86', 'armeabi-v7a'
universalApk true
}
}
sourceSets.main {
jni.srcDirs = [] // This prevents the auto generation of Android.mk
jniLibs.srcDir 'src/main/libs' // This is not necessary unless you have precompiled libraries in your project.
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.google.android.support:wearable:1.4.0'
compile 'com.google.android.gms:play-services-wearable:8.4.0'
}
Here is the adb from the mobile side when installing com.high_mobility.digitalkey
What might be the issue here that we cannot publish our app via the mobile apk?
I'm trying to include mupdf as an android library module with precompiled .so binaries into my project.
I have an application module which includes my library module like so.
dependencies {
compile project(':mupdf')
}
In my mupdf library module I have my compiled binaries like so.
mupdf
- jniLibs
- arm64-v8a
libmupdf.so
- armeabi
libmupdf.so
- armeabi-v7a
libmupdf.so
- x86
libmupdf.so
- x86_64
libmupdf.so
My build.gradle for the mupdf library project:
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
android {
compileSdkVersion 23
buildToolsVersion "23.0.1"
defaultConfig {
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
publishNonDefault true
productFlavors {
x86 {
ndk {
abiFilter "x86"
}
}
x64 {
ndk {
abiFilter "x86_64"
}
}
arm {
ndk {
abiFilter "armeabi"
}
}
arm_v7a {
ndk {
abiFilter "armeabi-v7a"
}
}
arm64_v8a {
ndk {
abiFilter "arm64-v8a"
}
}
fat
}
}
dependencies{
compile project(':core')
}
The problem is soon as I add the product flavors my app doesn't compile because it doesn't find any of the java files from the mupdf module.
If I delete the product flavors part from the gradle file it compiles. But it crashes at runtime because it can't resolve/load the libmupdf library.
You can do this without product flavors. Just remove them and call System.loadLibrary("mupdf") on startup/at some point before the library is used. This call will figure out which native lib to use for the current devices architecture.
Indeed I have to build a fat module for the .aar library and apply the productflavors in my app.gradle.
I was missing this in my library's build.gradle
sourceSets.main {
jni.srcDirs = [] // This prevents the auto generation of Android.mk
jniLibs.srcDir 'src/main/jni'
}
Without this no methods were able to be resolved.