specify .so files in build.gradle file android studio - android

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')

Related

include*.so file at jnLibs to Android Studio for spatiallite libjsqlite

I want to add *.so library to my existing project. But the *.so file isn't found. Android Studio colored the data brown.
I added the Files like this:
|--app:
|--|--src:
|--|--|--main
|--|--|--|--jniLibs
|--|--|--|--|--armeabi
|--|--|--|--|--|--.so Files
|--|--|--|--|--armeabi
|--|--|--|--|--|--.so Files
|--|--|--|--|--x86
|--|--|--|--|--|--.so Files
build gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "25.0.0"
defaultConfig {
applicationId "com.xxx.xxxx"
minSdkVersion 19
targetSdkVersion 21
versionCode 1
versionName "1"
multiDexEnabled true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
dexOptions {
incremental true
javaMaxHeapSize "5g"
}
repositories {
maven { url 'https://oss.sonatype.org/content/repositories/ksoap2-android-releases' }
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar','.so'])
compile 'me.dm7.barcodescanner:zbar:1.9'
compile 'com.android.support:appcompat-v7:25.1.0'
compile 'com.android.support:multidex:1.0.1'
compile 'com.android.support:design:25.1.0'
compile 'com.qozix:tileview:2.2.6'
compile 'com.google.android.gms:play-services:10.0.1'
compile 'com.google.code.ksoap2-android:ksoap2-android:3.6.2'
testCompile 'junit:junit:4.12'
}
I get this error:
java.lang.UnsatisfiedLinkError: No implementation found for void jsqlite.Database.internal_init() (tried Java_jsqlite_Database_internal_1init and Java_jsqlite_Database_internal_1init__) at jsqlite.Database.internal_init(Native Method)
If I look into my unpacked APK, I see librarys for arm64-v8a, mips, mips64 and x84_64 where the libjsqlite.so is not available.
Update to address changes in question:
After your update to your question including the error you get and some googleing for jsqlite.Database.internal_init() I'd suggest you have a look at this question:
java.lang.UnsatisfiedLinkError in jsqlite.Database.internal_init in Android Spatialite
Original answer:
This is how I have it in my app:
*.so files in src/main/libs (in thier armeabi, x86 folder each)
|--app:
|--|--src:
|--|--|--main
|--|--|--|--libs
|--|--|--|--|--armeabi
|--|--|--|--|--|--.so Files
|--|--|--|--|--x86
|--|--|--|--|--|--.so Files
and in my build.gradle
android {
// other configuration
sourceSets {
main {
jniLibs.srcDirs = ["src/main/libs"]
}
}
}

Force Android Studio to compile ARM64 versions of external libraries

I am trying to embed CrossWalk on native android using the method explained here:
embed crosswalk in android studio
The problem that I am having is that android studio is not building the needed libraries for ARM64, even though those libraries are on the Maven repository.
Here is my build.gradle file:
apply plugin: 'com.android.application'
android {
compileSdkVersion 24
buildToolsVersion "24.0.2"
defaultConfig {
applicationId "my.app.id"
minSdkVersion 19
targetSdkVersion 24
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
repositories {
maven {
url 'https://download.01.org/crosswalk/releases/crosswalk/android/maven2'
}
flatDir{
dirs 'libs'
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:24.2.1'
compile 'com.android.support:design:24.2.1'
compile 'org.xwalk:xwalk_core_library:22.52.561.4'
compile (name: 'wikitudesdk', ext:'aar')
testCompile 'junit:junit:4.12'
}
Is there a way to tell android studio expicitly to compile those libraries?
OK I finally found the solution here:
diego.org
Basically if you need the ARM64 library you first need to download the correct library yourself:
curl -O https://download.01.org/crosswalk/releases/crosswalk/android/maven2/org/xwalk/xwalk_core_library/22.52.561.4/xwalk_core_library-22.52.561.4-arm64.aar
Then install it to the local maven repository:
mvn install:install-file -DgroupId=org.xwalk -DartifactId=xwalk_core_library \
-Dversion=22.52.561.4-arm64 -Dpackaging=aar \
-Dfile=xwalk_core_library-22.52.561.4-arm64.aar \
-DgeneratePom=true
And update you build gradle so that the repositories point to you local maven repo:
repositories {
mavenLocal()
}
and you compile the correct lib:
compile 'org.xwalk:xwalk_core_library:22.52.561.4-arm64'
Hope it helps someone as future reference.
Take a look at section Crosswalk AAR Version in https://crosswalk-project.org/documentation/android/embedding_crosswalk/crosswalk_aar.html:
Support different CPU architectures with each APK (such as for ARM, x86).
A product flavor defines a customized version of the application build by the project. We can have different flavors which generate apk for each architecture.
android {
...
productFlavors {
armv7 {
ndk {
abiFilters "armeabi-v7a", ""
}
}
x86 {
ndk {
abiFilters "x86", ""
}
}
}
}
Get the version code from the manifest. Add an extra digit to the end of the version code which implicity specifies the architecture. The x86 final digit is 4, arm is 2.
versionCode manifest.versionCode + 4
Build your project with Gradle, the following commands will build the corresponding arch apk in build/apk directory.
$ gradle assemblex86
$ gradle assemblearmv7
Use $ gradle build to build both arm and x86 APKs at once.
OpenCV uses these magic strings for arm64: digit 3 and abiFilter arm64-v8a (reference).

How to set NDK_PROJECT_PATH in Android Studio

I am very new to NDK debugging and I am trying to build the cpp code for debugging.
This is how my Application.mk file looks :
APP_STL := stlport_static
APP_MODULES := abc xyz
APP_CFLAGS += -fno-rtti -fexceptions
APP_ABI := armeabi armeabi-v7a
NDK_TOOLCHAIN := arm-linux-androideabi-4.9
and I an getting the following error , I have tried all the toolchains in ndk tools , what am I missing ?
update:
I realized that NDK_PROJECT_PATH is set to null , when built is run from Android studio. I am able to run
ndk-build -C from the terminal.
Actually my ultimate aim is to be able to debug native c++ files , by setting break points.
So current scenario:
I have native code in xyz folder with has jni folder containing android.mk and application.mk
I can run ndk-build in this folder and get the .so files, which I manually copy paste in jnilibs folder of the my android app (say appB):
This is the gradle of the appB :
apply plugin: 'com.android.library'
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
minSdkVersion 13
targetSdkVersion 13
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
//compile 'junit:junit:4.12'
// compile 'com.android.support:appcompat-v7:24.1.1'
}
Later I use this app as dependency in another app say appA.
This is the gradle of appA:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.0'
}
}
apply plugin: 'com.android.application'
android {
compileSdkVersion 24
buildToolsVersion "24.0.0"
defaultConfig {
applicationId "*****"
minSdkVersion 14
targetSdkVersion 24
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
repositories{
mavenCentral()
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:24.0.0'
compile 'com.android.support:design:24.0.0'
compile 'com.android.support:support-v4:24.0.0'
compile 'com.android.support:recyclerview-v7:24.0.0'
compile 'com.android.support:cardview-v7:24.0.0'
compile 'com.android.support:preference-v14:24.1.0'
compile project(':appB')
}
I need to be able to debug,
appA (which I can)
appB (which I can)
native code included as .so in appB (which I dont know yet how to)
aap
You can update the Android Studio to 2.2.2, and use the menu File-> Link C ++ Project with Gradle.
The official ndk samples can be found here!
There will be a local.properties files in your project folder from where the sdk & ndk paths can be configured. Normally the file has sdk directory path, you can configure your ndk path using keywork ndk.dir. For example
sdk.dir=YOUR_PATH_TO_SDK ( sdk.dir= /home/Android/Sdk)
ndk.dir=YOUR_PATH_TO_SDK ( ndk.dir= /home/Android/Sdk/ndk-bundle)

NDK module not included in final APK

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.

Create pre build event to copy files to assets folder in Android application

I have this project structure:
ProjectFolder/IosFolder,AndroidFolder,CommonFolder
Now android app uses files from it's assets folder.
But we decide to make Common folder for the same files.
Could you help me to make function witch will copy files from Common folder(this folder is under my project, so in Android Studio I don't see it) to android assets folder before app will be built?
In Common folder will be some .json files and font files.
As I understand, i need to write this function in my build.gradle file
something like that:
task copyFiles(type: Copy)
copyFiles {
description = 'Copy files'
from 'Common/'
into 'Android/{projectName}/app/src/main/assets'
}
Here is my file:
apply plugin: 'com.android.application'
repositories {
mavenCentral()
}
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
applicationId "amc.amc_mobile_promo2"
minSdkVersion 17
targetSdkVersion 23
versionCode 1
versionName "1.0"
//For Flurry
multiDexEnabled = true
}
//For Flurry
/*compileOptions {
//noinspection GroovyAssignabilityCheck
sourceCompatibility JavaVersion.VERSION_1_7
//noinspection GroovyAssignabilityCheck
targetCompatibility JavaVersion.VERSION_1_7
}*/
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:support-v4:23.1.1'
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.facebook.android:facebook-android-sdk:4.7.0'
compile 'com.mcxiaoke.volley:library:1.0.19'
compile 'joda-time:joda-time:2.8.2'
compile 'com.github.orangegangsters:swipy:1.2.0#aar'
compile 'com.squareup.picasso:picasso:2.5.2'
compile 'com.squareup.okhttp:okhttp:2.6.0'
compile 'com.squareup.okhttp:okhttp-urlconnection:2.6.0'
/*compile 'com.android.support:multidex:1.0.1'
compile 'com.google.android.gms:play-services-ads:8.3.0'
compile 'com.google.android.gms:play-services-identity:8.3.0'
compile 'com.google.android.gms:play-services-gcm:8.3.0'*/
}
And could you tell me where can i see results of executed methods in Gradle Console?
What path i need to use and where in build.gradle file situate this method?
Hope you will help me.
Can you try this configuration:
gradle.projectsEvaluated {
preBuild.dependsOn(copyFiles)
}
update:
there are many commands the copy task can do for you. from the docs here are examples:
task anotherCopyTask(type: Copy) {
// Copy everything under src/main/webapp
from 'src/main/webapp'
// Copy a single file
from 'src/staging/index.html'
// Copy the output of a task
from copyTask
// Copy the output of a task using Task outputs explicitly.
from copyTaskWithPatterns.outputs
// Copy the contents of a Zip file
from zipTree('src/main/assets.zip')
// Determine the destination directory later
into { getDestDir() }
}
if you just want to copy from one source directory to another you can do this :
task copyFiles(type: Copy) {
from 'pathToMyAssets'
into 'AndroidStudioAssetsFolderPath'
}
UPDATE do this in your app's build.gradle at the very bottom:
task copyFiles(type: Copy) {
from 'Users/kostya/repo_amc_mobile_promo/Common/'
into 'Users/kostya/repo_amc_mobile_promo/Android/AMC_Mobile_Promo2/app/src/main/assets'
}
preBuild.dependsOn(copyFiles)
According to documentation here's how to copy a file via gradle:
task myCopyTask(type: Copy) {
from file("${buildDir}/reports/my-report.pdf")
into file("${buildDir}/toArchive")
}
To do it before build you must add the following line:
preBuild.dependsOn(myCopyTask)
You can use absolute or relative path that may use some of the project properties. To ensure path of project properties do not hesitate to print it using :
println file('${buildDir}')

Categories

Resources