Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add .so files to an android library project using gradle 0.7+

Project structure:

App project --> depends on Library project

Library Project has a folder for the compiled jni libs

jniLibs.srcDirs = ['libs'] 

And I've tried adding the following to the android element of the build.gradle as per the example app https://android.googlesource.com/platform/tools/build/+/2e1f7810edd76d92cee8d3e06bc4dec0c288adea/tests/ndkSanAngeles/build.gradle however android library projects do not support productFlavours and as such the assemble fails with "Could not find method productFlavors() for arguments [dghdhd] on project"

productFlavors {     x86 {         ndk {             abiFilter "x86"         }     }     arm {         ndk {             abiFilters "armeabi-v7a", "armeabi"         }     } } 

Is there a way to add ndk support to an android library project?

like image 305
Alec Holmes Avatar asked Jan 21 '14 10:01

Alec Holmes


People also ask

How do I add a library project to Android Studio?

Navigate to File > Project Structure > Dependencies. In the Declared Dependencies tab, click and select Library Dependency in the dropdown. In the Add Library Dependency dialog, use the search box to find the library to add.

What is Android plugin for Gradle?

The Android Gradle plugin (AGP) is the official build system for Android applications. It includes support for compiling many different types of sources and linking them together into an application that you can run on a physical Android device or an emulator.


Video Answer


2 Answers

In the end I didnt need to use product flavours.

For the library project I added the following:

android {     sourceSets {         main {             jniLibs.srcDirs = ['libs']         }     }         } 

The libs folder had a folder inside called "armeabi-v7a" and as this is my only target it worked a treat.

The ndk files (.so) are propagated into the android project that is using the android library project.

like image 157
Alec Holmes Avatar answered Sep 28 '22 02:09

Alec Holmes


Example with new android experimental gradle plugin.


Requirements:

  • Android Studio 1.5+
  • gradle-2.10
  • gradle-experimental:0.6.0-alpha5

1) You could simply put all shared native libraries in the main / jniLibs folder, by default.

Project structure

root folder / android project

root folder / android_project / app / src / main / jniLibs / x86

root folder / android_project / app / src / main / jniLibs / armeabi-v7a

root folder / android_project / app / src / main / jniLibs / ...

Gradle will automatically upload them to the device.

Then you could load the library in an application.

static {     System.loadLibrary("mylibrary"); } 

2) You could also put all shared native libraries in the custom location.

Example with a path to the bin / android / Debug directory.

In that case you should manually set the libraries location in the build.gradle file.

Project structure

root folder / android project

root folder / bin / android / Debug / jniLibs / x86

root folder / bin / android / Debug / jniLibs / armeabi-v7a

root folder / bin / android / Debug / jniLibs / ...

root folder / android_project / app / build.gradle

apply plugin: 'com.android.model.application'  model {     android {         sources {             main {                 jni {                     source {                         srcDirs = []                     }                 }                  jniLibs {                     source {                         srcDirs "/../../bin/android/Debug/jniLibs"                     }                 }             }         }     } } 
like image 37
Maks Avatar answered Sep 28 '22 03:09

Maks