How to link static libraries (.a files) with APK using gradle experimental plugin 0.2.0 in Android Studio

There are two things in my project.

  • I am creating a static library using ndk-build
  • Link the static library to an existing application in Android Studio.

Enjoyed many questions and many answers. But none of them work for me.

Here are my doubts

  • Where should the static libraries actually be located ..?
  • How do I specify the Gradle build system with which I link the static library ..?

Please help me with the necessary steps to follow.

+6
source share
4 answers

Finally, I found the answer.

Modify the build.gradle file of the application ... as shown below

 android.ndk { moduleName = "AEC" ldLibs += ["android", "OpenSLES", "log"] ldFlags += "/home/lone/AndroidStudioProjects/AcousticEchoCanceller/app/src/main/jni/prebuilt/libToneDetect.a" ldFlags += "/home/lone/AndroidStudioProjects/AcousticEchoCanceller/app/src/main/jni/prebuilt/libAec.a" CFlags += ['-std=c99'] } 

Note. This only works if the library and architecture are the same. And apk builds only for 1 platform

+3
source

gradle -experimental 0.2.0 is pretty old. I have a solution for 0.7.2.

put this Android VIOLATION {}

 repositories { libs(PrebuiltLibraries) { yourlib { headers.srcDir "src/main/jni/prebuilts/include" binaries.withType(SharedLibraryBinary) { sharedLibraryFile = file("src/main/jni/prebuilts/${targetPlatform.getName()}/libyourlib.so") } } } } 

and this code INSIDE Android {}

  sources { main { jni { dependencies { library "yourlib" linkage "shared" } } } } 

The code comes from: Xavier Hallade's message

+2
source

Hello-libs is one that can be useful: it creates 2 libraries and uses them in the application. Building libraries and the use of libs can be deactivated; for use, see app /build.gradle

sample for a shared library; follow the comment above gperf for static lib is that build.gradle [don't put the static library in jniLibs folder, don't need it], it will work.

Studio CMake + android is similar and simpler [they are in the master-cmake branch]

+1
source

The folder structure is relatively important when accessing the static library, I'm going to point out the structure of mine, obviously, it will be slightly different for you, but I hope this makes the situation a little easier. Firstly:

in my (C :) drive I have a Projects folder that is home to all my Android studio projects, in this folder I have my library, as well as the root directory of the application I'm working on (which uses the library)

The next step is to open the project in which you want to use the library and include these few lines of code.

Expand "Gradle Scripts" and open "build.gradle (module: app)", and in the dependencies add this project - compile (path: ": library")

Next, open 'settings.gradle (Project Settings)' and you should see this: include ': app'

Change the line above to read include ': app', ': library' and in the line below add this - the project (": library"). projectDir = new file ("../(yourlibraryfoldernamehere)/library") '

SHOULD do the trick, hope this helps, if something is unclear, give me a shout and I will try to help or explain better! Good luck

0
source

All Articles