lib folder missing when installing APK - android

I would like to know how to build my APK so that my libraries are installed/copied under /data/data/<package name>/lib when installing the APK.
I am using Android Studio 4.0.1
I build an APK and confirm when I extract it that I have a
lib/arm64-v8a folder that contains my jni libs.
I want that after I install my APK, some other applications on the same device can access the libs that I have in the lib/arm64-v8a. The /data/data/<package name> folder seems to be the right place for this, hence my question; if there is another approach for making my libraries accessible on device, I am open to it.
I set android.bundle.enableUncompressedNativeLibs=false
in my gradle.properties file based on this discussion even
though I didn't see any effect using it with true or false.
I set android:extractNativeLibs="true" in my Manifest
However, when I adb install my apk, the /data/data/<package name>/ folder on my device only contains a cache and code_cache folder, both of which are empty.
I do see under /data/app/ a folder with my libraries but they include strings that are randomly set each time I install the APK:
/data/app/~~sFE8-eNknFAkhKJ-1S03lg==/<package name>-rsDLNgpi4vult7yVBrPNOQ==/
base.apk
lib/arm64/
<my .so in there>
These random strings prevent me from accessing reliably my libraries from any other application. (If there is a way to not have these random strings as part of the name and /data/app is the recommended location for my libraries to be shared with other applications, I am interested in that alternate approach.)
I have also installed other prebuilt APK and with these, I do see the libraries under /data/data/<package name>/lib like I want. This to me indicates that my issue is related to how I build my APK with AS 4.0.1, not to my device, its Android version, or how I install the APK.
Here is my gradle file:
import java.util.regex.Matcher
import java.util.regex.Pattern
apply plugin: 'com.android.application'
android {
compileSdkVersion 30
buildToolsVersion "30.0.2"
lintOptions {
checkReleaseBuilds false
abortOnError false
}
sourceSets.main {
jniLibs.srcDirs = ['libs']
jni.srcDirs = []
}
defaultConfig {
applicationId "com.example.myexample"
minSdkVersion 16
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
externalNativeBuild {
cmake {
version "3.17.0"
cppFlags ""
arguments "-DANDROID_STL=c++_shared"
}
}
ndk {
moduleName "myexample"
abiFilter "arm64-v8a"
ldLibs "log"
}
packagingOptions {
exclude "lib/arm64-v8a/libassets.so"
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
externalNativeBuild {
cmake {
version "3.17.0"
path "src/main/cpp/CMakeLists.txt"
// There can be only one CMAKE per project: https://developer.android.com/studio/projects/gradle-external-native-builds
}
}
}
task copyFiles(type: Copy) {
description = 'Testing a manual copy operation (confirmed it works)...'
from file("../dsp/external/ship/libmyexample_skel.so")
into file("$buildDir/res/raw/")
}
project.afterEvaluate {
preBuild.dependsOn copyFiles
}
dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.1'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}
Something else I tried: I created manually a src/main/jniLibs.arm64-v8a folder in which I place a .so. That .so appears in the my apk when I extract it, but my /data/data/<package name>/ folder doesn't have a lib nevertheless.

I think you need to add this code to add libs in the appropriate folder like /data/data/<package name>/lib
add this to app's build.gradle file into android{} tag
sourceSets.main {
jniLibs.srcDirs = ['libs']
jni.srcDirs = []
}
this to CMakeList.txt file
cmake_minimum_required(VERSION 3.4.1)
find_library( # Sets the name of the path variable.
log-lib
log)
set(distribution_DIR ${CMAKE_SOURCE_DIR}/libs)
add_library(yourLibName
SHARED
...//list of your c/c++ files here from jni folder
)
target_link_libraries(yourLibName ${log-lib})
as per developer.android.com
Note: When making changes to the Gradle configuration, make sure to
apply your changes by clicking Sync Project in the toolbar.
Additionally, when making changes to your CMake or ndk-build script
file after you have already linked it to Gradle, you should sync
Android Studio with your changes by selecting Build > Refresh Linked
C++ Projects from the menu bar.

Related

Android Tensorflow Lite C++ .SO libraries not linked on runtime

I'm trying to build Tensorflow lite with C++ natively on Android device. I've built a .So file of tensorflow for every architecture and placed into the jniLibs folder. This is my Cmake file:
set(pathToProject /ih/user/project/NativeTfLite/app)
set(libs ${pathToProject}/src/main/jniLibs)
add_library(libtensorflowLite SHARED IMPORTED
)
set_target_properties(libtensorflowLite PROPERTIES IMPORTED_LOCATION
${libs}/${ANDROID_ABI}/libtensorflowLite.so)
find_library(
log-lib
log )
add_library(
native-lib
SHARED
native-lib.cpp)
target_include_directories(native-lib PRIVATE
${lib}/include)
target_link_libraries( # Specifies the target library.
native-lib. #Problem is here when linking native-lib with libtensorflowlite
libtensorflowLite
${log-lib} )
During compile time these files are found. However in target_link_libraries line, the linking of the 2 libraries libtensorflowlite and native-lib there is a runtime crash with the following error:
java.lang.UnsatisfiedLinkError: dlopen failed: library "/Users/ih/project/cameraFrames/NativeTfLite/app/src/main/jniLibs/arm64-v8a/libtensorflowLite.so" not found
Additionally the .SO files are not linked to the APK.
Here is my build.gradle file:
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 29
buildToolsVersion "29.0.3"
defaultConfig {
applicationId "com.proj.nativetflite"
minSdkVersion 24
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
sourceSets {
main {
jniLibs.srcDir 'src/main/jniLibs'
jniLibs.srcDirs = ["src/main/jniLibs"]
}
}
externalNativeBuild {
cmake {
cppFlags "-std=c++11"
arguments '-DCMAKE_VERBOSE_MAKEFILE=ON'
}
}
ndk {
abiFilters "armeabi-v7a", "x86" , "arm64-v8a"
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
externalNativeBuild {
cmake {
path "src/main/cpp/CMakeLists.txt"
version "3.10.2"
}
}
}
dependencies {
implementation fileTree(dir: 'jniLibs', include: '**/*.so')
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.core:core-ktx:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
What is causing this?
Prior to AGP 4.0 you need to explicitly package prebuilt libraries with jniLibs: https://developer.android.com/studio/projects/gradle-external-native-builds#jniLibs. Note that the docs say you don't need to do this for anything declared by CMake, but I'm fairly certain the docs are wrong here (I've reached out to the doc owner to see if we need to fix that or not, since it is correct for 4.0).
This should be unnecessary in 4.0, but that hasn't reached stable yet.
Although I think Dan Albert's answer should be accepted, I'd like to add that I got the exact same error because I had not set the correct path to my tflite lib.
I kept my .so files in src/main/cpp/tflite/lib/${ANDROID_ABI}/, so in build.gradle I needed to add:
main {
jniLibs.srcDirs = ['src/main/cpp/tflite/lib']
}
also, it could be worth checking out this answer (also from Dan) regarding making sure the SONAME of the .so is correct.

Can't load .so file stored in a library (aar) from an application project

0. Problem
I've create an Android library (aar) which contains some so files.
After successfully loaded the aar file in my Android application project, I try to load the so files (from the aar classes) but failed with a java.lang.UnsatisfiedLinkError.
I think the Android application is not looking inside the aar in order to load the so files, but looking at the lib folder which is inside the Android application.
(I know I can copy those shared-libraries directly in the android application, but I don't want to do that)
1. Create the Android library (aar)
build.gradle
apply plugin: 'com.android.library'
android {
compileSdkVersion 29
defaultConfig {
minSdkVersion 21
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles 'consumer-rules.pro'
externalNativeBuild {
cmake {
cppFlags "-std=c++11"
abiFilters "armeabi-v7a", "arm64-v8a", "x86", "x86_64"
}
}
packagingOptions {
pickFirst 'lib/x86_64/libnanolcms.so'
pickFirst 'lib/x86/libnanolcms.so'
pickFirst 'lib/armeabi-v7a/libnanolcms.so'
pickFirst 'lib/arm64-v8a/libnanolcms.so'
pickFirst 'lib/x86_64/liblcms2.so'
pickFirst 'lib/x86/liblcms2.so'
pickFirst 'lib/armeabi-v7a/liblcms2.so'
pickFirst 'lib/arm64-v8a/liblcms2.so'
}
}
buildTypes {
release {
minifyEnabled false
debuggable false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
jniDebuggable = true
}
debug {
minifyEnabled false
debuggable false
jniDebuggable = true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
externalNativeBuild {
cmake {
version '3.6.0'
path "src/main/jni/CMakeLists.txt"
}
}
sourceSets {
main {
jniLibs.srcDirs = ['src/main/jnilibs']
jni.srcDirs = [] //disable automatic ndk-build call
}
}
}
repositories {
flatDir {
dirs 'libs'
}
// to be able to compile opencv
maven { url "https://jitpack.io" }
}
dependencies {
implementation fileTree(include: ['*.jar','*.so'], dir: 'libs')
implementation 'androidx.appcompat:appcompat:1.1.0'
testImplementation 'junit:junit:4.13'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation(name: 'openCV-3.3.1', ext: 'aar')
implementation "com.getkeepsafe.relinker:relinker:1.4.0"
}
code from the android library project
if (useReLinker) {
ReLinker.recursively().log(logcatLogger).loadLibrary(CXColorcatchManager.getInstance().getWeakContext().get(), libraryName, new ReLinker.LoadListener() {
#Override
public void success() {
CXLogger.verbose("Library " + libraryName + " successfully loaded");
libraryLoaderListener.onLibraryLoaded(true);
}
#Override
public void failure(Throwable t) {
CXLogger.verbose("Unable to load " + libraryName + " library \n");
libraryLoaderListener.onLibraryLoaded(false);
}
});
} else {
try {
System.loadLibrary(libraryName);
libraryLoaderListener.onLibraryLoaded(true);
} catch (Exception e) {
CXLogger.verbose("Unable to load " + libraryName + " library \n " + e);
libraryLoaderListener.onLibraryLoaded(false);
}
}
}
As you can see on the following picture, the aar file contains the so files. They are in the jni folder which is the correct folder following the documentation
my-library.aar
2. Android application project
I created an Android application project, added the aar file as a dependency (it works fine).
I call the Singleton class I created from the aar file, then I try to load the so file from library class using System.loadlibrary() or Relinker.
build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 29
defaultConfig {
applicationId "com.colorix.spike.nano"
minSdkVersion 21
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
sourceSets {
main {
jniLibs.srcDirs = ['src/main/jnilibs']
}
}
}
dependencies {
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'com.google.android.material:material:1.0.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.13'
androidTestImplementation 'androidx.test:runner:1.3.0-alpha03'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0-alpha03'
// my library
implementation files('libs/my_library.aar')
implementation "com.getkeepsafe.relinker:relinker:1.4.0"
}
Unfortunately, with System.loadlibrary() it doesn't find the library : java.lang.UnsatisfiedLinkError
Using Relinker, I use recursive() method in order to check every folder :
V/ReLinkerInstance: [LibraryLoadingTask.log()-32]: Loading the library normally failed: java.lang.UnsatisfiedLinkError: dalvik.system.PathClassLoader[DexPathList[[zip file "/data/app/com.colorix.spike.nano-YmXglPRjs_msSVzvsySnZA==/base.apk"],nativeLibraryDirectories=[/data/app/com.colorix.spike.nano-YmXglPRjs_msSVzvsySnZA==/lib/x86, /system/lib]]] couldn't find "libnanolcms.so"
at java.lang.Runtime.loadLibrary0(Runtime.java:1012)
at java.lang.System.loadLibrary(System.java:1669)
at com.getkeepsafe.relinker.SystemLibraryLoader.loadLibrary(SystemLibraryLoader.java:24)
at com.getkeepsafe.relinker.ReLinkerInstance.loadLibraryInternal(ReLinkerInstance.java:163)
at com.getkeepsafe.relinker.ReLinkerInstance.access$000(ReLinkerInstance.java:31)
at com.getkeepsafe.relinker.ReLinkerInstance$1.run(ReLinkerInstance.java:142)
at java.lang.Thread.run(Thread.java:764)
V/ReLinkerInstance: [LibraryLoadingTask.log()-32]: nanolcms (null) was not loaded normally, re-linking...
V/ReLinkerInstance: [LibraryLoadingTask.log()-32]: Looking for lib/x86/libnanolcms.so in APK /data/app/com.colorix.spike.nano-YmXglPRjs_msSVzvsySnZA==/base.apk...
V/ReLinkerInstance: [LibraryLoadingTask.log()-32]: Looking for lib/armeabi-v7a/libnanolcms.so in APK /data/app/com.colorix.spike.nano-YmXglPRjs_msSVzvsySnZA==/base.apk...
[LibraryLoadingTask.log()-32]: Looking for lib/armeabi/libnanolcms.so in APK /data/app /com.colorix.spike.nano-YmXglPRjs_msSVzvsySnZA==/base.apk...
[LibraryLoadingTask.log()-32]: Looking for lib/x86/libnanolcms.so in APK /data/app /com.colorix.spike.nano-YmXglPRjs_msSVzvsySnZA==/base.apk...
[LibraryLoadingTask.log()-32]: Looking for lib/armeabi-v7a/libnanolcms.so in APK /data/app/com.colorix.spike.nano-YmXglPRjs_msSVzvsySnZA==/base.apk...
V/ReLinkerInstance: [LibraryLoadingTask.log()-32]: Looking for lib/armeabi/libnanolcms.so in APK /data/app/com.colorix.spike.nano-YmXglPRjs_msSVzvsySnZA==/base.apk...
[LibraryLoadingTask.log()-32]: Looking for lib/x86/libnanolcms.so in APK /data/app/com.colorix.spike.nano-YmXglPRjs_msSVzvsySnZA==/base.apk...
[LibraryLoadingTask.log()-32]: Looking for lib/armeabi-v7a/libnanolcms.so in APK /data/app/com.colorix.spike.nano-YmXglPRjs_msSVzvsySnZA==/base.apk...
V/ReLinkerInstance: [LibraryLoadingTask.log()-32]: Looking for lib/armeabi/libnanolcms.so in APK /data/app/com.colorix.spike.nano-YmXglPRjs_msSVzvsySnZA==/base.apk...
V/ReLinkerInstance: [LibraryLoadingTask.log()-32]: Looking for lib/x86/libnanolcms.so in APK /data/app/com.colorix.spike.nano-YmXglPRjs_msSVzvsySnZA==/base.apk...
[LibraryLoadingTask.log()-32]: Looking for lib/armeabi-v7a/libnanolcms.so in APK /data/app/com.colorix.spike.nano-YmXglPRjs_msSVzvsySnZA==/base.apk...
[LibraryLoadingTask.log()-32]: Looking for lib/armeabi/libnanolcms.so in APK /data/app/com.colorix.spike.nano-YmXglPRjs_msSVzvsySnZA==/base.apk...
V/ReLinkerInstance: [LibraryLoadingTask.log()-32]: Looking for lib/x86/libnanolcms.so in APK /data/app/com.colorix.spike.nano-YmXglPRjs_msSVzvsySnZA==/base.apk...
V/ReLinkerInstance: [LibraryLoadingTask.log()-32]: Looking for lib/armeabi-v7a/libnanolcms.so in APK /data/app/com.colorix.spike.nano-YmXglPRjs_msSVzvsySnZA==/base.apk...
V/ReLinkerInstance: [LibraryLoadingTask.log()-32]: Looking for lib/armeabi/libnanolcms.so in APK /data/app/com.colorix.spike.nano-YmXglPRjs_msSVzvsySnZA==/base.apk...
W/orix.spike.nan: Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (light greylist, reflection)
W/orix.spike.nan: Accessing hidden method Landroid/view /ViewGroup;->makeOptionalFitsSystemWindows()V (light greylist, reflection)
V/ReLinkerInstance: [LibraryLoadingTask.failure()-54]: Unable to load nanolcms library
As you can see, the library is not in lib/<architecture/... but inside the aar, which is in jni folder.
If I understand the android documentation, lib folder is the folder of the android application and not the folder of the android library (aar).
If someone knows how to load the so file which are inside the aar file without, please let me know. If you need more information, let me know.
Solution (but I don't want that)
I can copy the .so files in the jniLibs folder of the application. It will works properly. But that's no what I want.
I finally figure it out by myself.
The first problem comes from my cmakelists.txt
I create my-lib-1 from c sources files then I created my-lib-2 in order to link my-lib-1 with some cpp files for jniexport.
But the link to my-lib-1 was correctly set for the compilation time, but when I created my-lib-2 from my-lib-1 the reference to my-lib-1 was broken.
Using System.loadlibrary() method to load my-lib-2 raised an error : java.lang.UnsatisfiedLinkError saying that my-lib-1 (not my-lib-2) wasn't found. That's how I fix the first problem.
Solution :
# mylib1 - source files
file(GLOB SOURCES ${mylib1_source_DIR}/src/*.c)
# mylib1 - include header files
include_directories(${mylib1_source_DIR}/include/)
# compile and add our cpp files - Shared library
add_library(mylib2 SHARED ${SOURCES} mylib2.cpp)
# logger
find_library(log-lib log)
# include our header file
target_include_directories( mylib2 PRIVATE ${mylib1_source_DIR}/include )
# link our library (mylib2) with android and log libraries
target_link_libraries( mylib2 android ${log-lib})
The second problem comes from relinker
The library is compiled. I use Relinker to load my library, but Relinker is unable to find my-lib-2. Relinker is looking for the library in the wrong folder (lib folder instead of jni folder).
fyi : lib folder is the library folder for an android app
jni folder is the library folder for an aar library
The reason is probably because Relinker is called from an android application, which mean it's going to look at the application native library folder and not inside the aar folder (which contains my-lib-2)
Solution :
1. Remove relinker
2. load library from my aar classes :
System.loadLibrary(my_lib_2);
That's it
Thanks #bruno for your help
To my mind, you can't load a so file inside an aar lib.
What you have to do is a wrapper in your aar to access .so features because your .so, I suppose, is loaded when aar is loaded.
for me the problem was that I didnt use #aar suffix. So I hade to use:
implementation('lib:2.0.3#aar') {
transitive = true
}

How to add a shared library to my APK with Android Studio? [duplicate]

I read many threads how to add a *.so library to Android Studio, but none of them works, especially when it comes to the point of text: This does not work with the newer xxx (Android Studio, gradle, ...)
Can we make a fresh start please. I got:
Android Studio 0.6.0
From Project Structure I see:
SDK Location:
/usr/share/android-studio/data/sdk
/usr/lib/jvm/default-java
Project:
Gradle version 1.10
Android Plugin Version 0.11.+
Modules/app:
Properties:
Compile Sdk Version 19
Build Tools Version 19.1.0
Dependencies:
{dir=libs, include=[*.jar]} Compile
{dir=libs, include=[*.so]} Provided
m com.android.support: appcompat -v7:19.+ Compile
I got the *.so files pre-compiled and at the demo app they are working. I have to change the source code of the app, so I need to rebuild with the same *.so files.
Adding .so Library in Android Studio 1.0.2
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
Current Solution
Create the folder project/app/src/main/jniLibs, and then put your *.so files within their abi folders in that location. E.g.,
project/
├──libs/
| └── *.jar <-- if your library has jar files, they go here
├──src/
└── main/
├── AndroidManifest.xml
├── java/
└── jniLibs/
├── arm64-v8a/ <-- ARM 64bit
│ └── yourlib.so
├── armeabi-v7a/ <-- ARM 32bit
│ └── yourlib.so
└── x86/ <-- Intel 32bit
└── yourlib.so
Deprecated solution
Add both code snippets in your module gradle.build file as a dependency:
compile fileTree(dir: "$buildDir/native-libs", include: 'native-libs.jar')
How to create this custom jar:
task nativeLibsToJar(type: Jar, description: 'create a jar archive of the native libs') {
destinationDir file("$buildDir/native-libs")
baseName 'native-libs'
from fileTree(dir: 'libs', include: '**/*.so')
into 'lib/'
}
tasks.withType(JavaCompile) {
compileTask -> compileTask.dependsOn(nativeLibsToJar)
}
Same answer can also be found in related question: Include .so library in apk in android studio
Solution 1 : Creation of a JniLibs folder
Create a folder called “jniLibs” into your app and the folders containing your *.so inside.
The "jniLibs" folder needs to be created in the same folder as your "Java" or "Assets" folders.
Solution 2 : Modification of the build.gradle file
If you don’t want to create a new folder and keep your *.so files into the libs folder, it is possible !
In that case, just add your *.so files into the libs folder (please respect the same architecture as the solution 1 : libs/armeabi/.so for instance) and modify the build.gradle file of your app to add the source directory of the jniLibs.
sourceSets {
main {
jniLibs.srcDirs = ["libs"]
}
}
You will have more explanations, with screenshots to help you here ( Step 6 ):
http://blog.guillaumeagis.eu/setup-andengine-with-android-studio/
EDIT It had to be jniLibs.srcDirs, not jni.srcDirs - edited the code. The directory can be a [relative] path that points outside of the project directory.
*.so library in Android Studio
You have to generate jniLibs folder inside main in android Studio projects and put your all .so files inside. You can also integrate this line in build.gradle
compile fileTree(dir: 'libs', include: ['.jar','.so'])
It's work perfectly
|--app:
|--|--src:
|--|--|--main
|--|--|--|--jniLibs
|--|--|--|--|--armeabi
|--|--|--|--|--|--.so Files
This is the project structure.
This is my build.gradle file, Please note the line
jniLibs.srcDirs = ['libs']
This will include libs's *.so file to apk.
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
aidl.srcDirs = ['src']
renderscript.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
jniLibs.srcDirs = ['libs']
}
// Move the tests to tests/java, tests/res, etc...
instrumentTest.setRoot('tests')
// Move the build types to build-types/<type>
// For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ...
// This moves them out of them default location under src/<type>/... which would
// conflict with src/ being used by the main source set.
// Adding new build types or product flavors should be accompanied
// by a similar customization.
debug.setRoot('build-types/debug')
release.setRoot('build-types/release')
}
Android NDK official hello-libs CMake example
https://github.com/googlesamples/android-ndk/tree/840858984e1bb8a7fab37c1b7c571efbe7d6eb75/hello-libs
Just worked for me on Ubuntu 17.10 host, Android Studio 3, Android SDK 26, so I strongly recommend that you base your project on it.
The shared library is called libgperf, the key code parts are:
hello-libs/app/src/main/cpp/CMakeLists.txt:
// -L
add_library(lib_gperf SHARED IMPORTED)
set_target_properties(lib_gperf PROPERTIES IMPORTED_LOCATION
${distribution_DIR}/gperf/lib/${ANDROID_ABI}/libgperf.so)
// -I
target_include_directories(hello-libs PRIVATE
${distribution_DIR}/gperf/include)
// -lgperf
target_link_libraries(hello-libs
lib_gperf)
app/build.gradle:
android {
sourceSets {
main {
// let gradle pack the shared library into apk
jniLibs.srcDirs = ['../distribution/gperf/lib']
Then, if you look under /data/app on the device, libgperf.so will be there as well.
on C++ code, use: #include <gperf.h>
header location: hello-libs/distribution/gperf/include/gperf.h
lib location: distribution/gperf/lib/arm64-v8a/libgperf.so
If you only support some architectures, see: Gradle Build NDK target only ARM
The example git tracks the prebuilt shared libraries, but it also contains the build system to actually build them as well: https://github.com/googlesamples/android-ndk/tree/840858984e1bb8a7fab37c1b7c571efbe7d6eb75/hello-libs/gen-libs
I have solved a similar problem using external native lib dependencies that are packaged inside of jar files. Sometimes these architecture dependend libraries are packaged alltogether inside one jar, sometimes they are split up into several jar files. so i wrote some buildscript to scan the jar dependencies for native libs and sort them into the correct android lib folders. Additionally this also provides a way to download dependencies that not found in maven repos which is currently usefull to get JNA working on android because not all native jars are published in public maven repos.
android {
compileSdkVersion 23
buildToolsVersion '24.0.0'
lintOptions {
abortOnError false
}
defaultConfig {
applicationId "myappid"
minSdkVersion 17
targetSdkVersion 23
versionCode 1
versionName "1.0.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
sourceSets {
main {
jniLibs.srcDirs = ["src/main/jniLibs", "$buildDir/native-libs"]
}
}
}
def urlFile = { url, name ->
File file = new File("$buildDir/download/${name}.jar")
file.parentFile.mkdirs()
if (!file.exists()) {
new URL(url).withInputStream { downloadStream ->
file.withOutputStream { fileOut ->
fileOut << downloadStream
}
}
}
files(file.absolutePath)
}
dependencies {
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.3.0'
compile 'com.android.support:design:23.3.0'
compile 'net.java.dev.jna:jna:4.2.0'
compile urlFile('https://github.com/java-native-access/jna/blob/4.2.2/lib/native/android-arm.jar?raw=true', 'jna-android-arm')
compile urlFile('https://github.com/java-native-access/jna/blob/4.2.2/lib/native/android-armv7.jar?raw=true', 'jna-android-armv7')
compile urlFile('https://github.com/java-native-access/jna/blob/4.2.2/lib/native/android-aarch64.jar?raw=true', 'jna-android-aarch64')
compile urlFile('https://github.com/java-native-access/jna/blob/4.2.2/lib/native/android-x86.jar?raw=true', 'jna-android-x86')
compile urlFile('https://github.com/java-native-access/jna/blob/4.2.2/lib/native/android-x86-64.jar?raw=true', 'jna-android-x86_64')
compile urlFile('https://github.com/java-native-access/jna/blob/4.2.2/lib/native/android-mips.jar?raw=true', 'jna-android-mips')
compile urlFile('https://github.com/java-native-access/jna/blob/4.2.2/lib/native/android-mips64.jar?raw=true', 'jna-android-mips64')
}
def safeCopy = { src, dst ->
File fdst = new File(dst)
fdst.parentFile.mkdirs()
fdst.bytes = new File(src).bytes
}
def archFromName = { name ->
switch (name) {
case ~/.*android-(x86-64|x86_64|amd64).*/:
return "x86_64"
case ~/.*android-(i386|i686|x86).*/:
return "x86"
case ~/.*android-(arm64|aarch64).*/:
return "arm64-v8a"
case ~/.*android-(armhf|armv7|arm-v7|armeabi-v7).*/:
return "armeabi-v7a"
case ~/.*android-(arm).*/:
return "armeabi"
case ~/.*android-(mips).*/:
return "mips"
case ~/.*android-(mips64).*/:
return "mips64"
default:
return null
}
}
task extractNatives << {
project.configurations.compile.each { dep ->
println "Scanning ${dep.name} for native libs"
if (!dep.name.endsWith(".jar"))
return
zipTree(dep).visit { zDetail ->
if (!zDetail.name.endsWith(".so"))
return
print "\tFound ${zDetail.name}"
String arch = archFromName(zDetail.toString())
if(arch != null){
println " -> $arch"
safeCopy(zDetail.file.absolutePath,
"$buildDir/native-libs/$arch/${zDetail.file.name}")
} else {
println " -> No valid arch"
}
}
}
}
preBuild.dependsOn(['extractNatives'])
I have android studio 4.1.2 and when I tried to set the ndk path in File -> Project Structure -> Sdk Location it would ask me to download the ndk even though it was already installed, I set the NDK_HOME, ANDROID_NDK_HOME and PATH environmental variables and nothing. The only thing that worked for me was manually setting the ndk version in the local.properties, creating a jniLibs (yes with this exact name) folder with the ABIs and their respective .so files and specifying the abi filters and ndk version in the build.gradle. Hope this saves someone else from a headache.
local.properties
ndk.dir=C\:\\Users\\<user>\\AppData\\Local\\Android\\Sdk\\ndk\\22.0.7026061
build.gradle
plugins {
id 'com.android.application'
}
android {
compileSdkVersion 30
buildToolsVersion "30.0.3"
defaultConfig {
applicationId "com.example.myapplication"
minSdkVersion 22
targetSdkVersion 30
versionCode 1
versionName "1.0"
ndk.abiFilters 'armeabi-v7a','arm64-v8a','x86','x86_64'
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
sourceSets {
main {
jniLibs.srcDir 'jniLibs'
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
ndkVersion '22.0.7026061'
}
...\app\src\main\
Inside the jniLibs folder
To use native-library (so files)
You need to add some codes in the "build.gradle" file.
This code is for cleaing "armeabi" directory and copying 'so' files into "armeabi" while 'clean project'.
task copyJniLibs(type: Copy) {
from 'libs/armeabi'
into 'src/main/jniLibs/armeabi'
}
tasks.withType(JavaCompile) {
compileTask -> compileTask.dependsOn(copyJniLibs)
}
clean.dependsOn 'cleanCopyJniLibs'
I've been referred from the below.
https://gist.github.com/pocmo/6461138

Android gradle, native libs for different architectures

I am trying to build universal apk for all architectures. Here is my project structure:
-App
-appModule
-libraryModule
-libs
-armeabi
-lib.so
-src
-java
-jni
Here is my gradle file for libraryModule:
apply plugin: 'com.android.library'
// http://stackoverflow.com/questions/28485309/how-to-build-single-apk-with-andoid-ndk-and-gradle
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
ndk {
moduleName "ProxyResolver" // <-- This is the name of AndroidProxy native module
stl "gnustl_shared"
cFlags "-std=c++11"
abiFilters = ['armeabi']
ldLibs (projectDir.absolutePath + "/libs/armeabi/libresolver.so")
}
}
sourceSets {
main {
jniLibs.srcDirs = ['libs']
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
}
As you can see the path for library is hardcoded. And right now it works on arm-v7 processors. But i need to add support of x86 without adding a flavor
I guess:
ndk {
moduleName "resolver"
stl "gnustl_shared"
cFlags "-std=c++11"
abiFilters = ['armeabi','arm-v7']
}
And remove
sourceSets {
main {
jniLibs.srcDirs = ['libs']
}
}
Setting abiFilters in your defaultConfig only adds information to the app's capabilities (only useful for the Installer, i.e. the Installer gives an error when the device you try to install to is not compatible) and does not change which binaries are added to the final apk, see also https://stackoverflow.com/a/39445685/1827156.
I guess what you want to accomplish is to have different apk for different architectures. This is a common usecase, i.e. to reduce build sizes. You can accomplish this by using splits in your app's build.gradle. Please refer to https://developer.android.com/studio/build/configure-apk-splits.html.
I highly recommend https://www.neotechsoftware.com/blog/native-libraries-part-i-common-pitfalls (I'm not the author).
Note on working with libraries/aars:
However, there's one major pitfall when you work with libraries (aar instead of apk). Imho, you can't (automagically) create different ABI-specific aars. Instead, you create aars containing the binaries for all ABIs. When adding the aar to an android app, you specify splits in the apps build.gradle as mentioned above.

Android Studio adds libapp.so to the resulting apk, what is it and how to turn it off?

Android Studio 1.1 generated apk file (located # app/build/outputs/apk folder) contains the lib directory, and for every cpu type there exists a non-empty folder, like /x86. Each of these folder contain a libapp.so shared library that is around 5Kb in size per cpu.
I've searched the net and the only thing I found so far is this link from Intel https://software.intel.com/en-us/articles/building-native-android-apps-using-intelr-c-compiler-in-android-studio that shows how to change the default libapp.so to user-provided library.
So, I guess that this library (libapp.so) is somehow built by gradle.
In fact I do my own native library building, using ndk-build command line tool, and my libs are placed alongside that libapp.so. It's not causing any issues btw, but I feel that I'm losing the control over what is built and why.
Here's my humble build.gradle file:
apply plugin: 'com.android.application'
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
/**
* Path to *.so files
*/
sourceSets {
main {
jniLibs.srcDirs = ['src/main/libs']
jni.srcDirs = [] //disable automatic ndk-build
}
}
defaultConfig {
applicationId "com.sample.android"
minSdkVersion 9
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
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:22.0.0'
}
How can I disable this libapp.so being built?
defaultConfig {
ndk {
moduleName "yourSelfLib"
}
}

Categories

Resources