I am very new to this whole gradle and Android Studio support. I have managed to convert my android project to gradle using the export option.
But I am looking for some documentation or start point how to integrate the NDK build into the gradle build process.
If possible I also need some sort of "after" stage that copies the build binaries (.so files) to the asset directory.
We have released a first version of the integration as a preview in 1.3: http://tools.android.com/tech-docs/android-ndk-preview
The integration will stay a preview even after 1.3 becomes final. No current ETA as to when it'll be final (as of 2015/07/10).
More information here: http://tools.android.com/tech-docs/android-ndk-preview
As Xavier said, you can put your prebuilts in /src/main/jniLibs/ if you are using gradle 0.7.2+
taken from: https://groups.google.com/d/msg/adt-dev/nQobKd2Gl_8/ctDp9viWaxoJ
I found "gradle 1.11 com.android.tools.build:gradle:0.9.+" supports pre-build ndk now, you can just put the *.so in the dir src/main/jniLibs. when building gradle will package the ndk to the right place.
here is my project
As of now (Android Studio v0.8.6) it's quite simple. Here are the steps to create a "Hello world" type app:
Download the Android NDK and put the root folder somewhere sane -- in the same location as the SDK folder, perhaps.
Add the following to your
local.properties
file:ndk.dir=<path-to-ndk>
Add the following to your build.gradle file inside of the
defaultConfig
closure, right after theversionName
line:ndk { moduleName="hello-world" }
In your app module's
main
directory, create a new folder calledjni
.In that folder, create a file called
hello-world.c
, which you'll see below.See the example
Activity
code below for an example of how to call a method (or is it a function?) inhello-world.c
.hello-world.c
MainActivity.java
build.gradle
Find the full source code of a very similar app here (minus the NDK).
NDK Builds and gradle (basic)
Generally building with the NDK is as simple as correctly specifying an ndkBuild path to Android.mk or cmake path to CMakeLists.txt. I recommend CMake over the older Android.mk because Android Studio's C/C++ support is based upon CLion and it uses CMake as its project format. This in my experience has tended to make the IDE more responsive on larger projects. Everything compiled in your project will be built and copied into the APK automatically.
Adding prebuilt libraries to the project (advanced)
Static libraries (.a) in your NDK build will automatically be included, but prebuilt dynamic libraries (.so) will need to be placed in
jniLibs
. This can be configured usingsourceSets
, but you should adopt the standard. You DO NOT NEED any additional commands inbuild.gradle
when including prebuilt libraries.The layout of
jniLibs
You can find more information about the structure in the Android Gradle Plugin User Guide.
You can then validate the resulting APK contains your .so files, typically under
build/outputs/apk/
, usingunzip -l myApp.apk
to list the contents.Building shared libraries
If you're building a shared library in the NDK you do not need to do anything further. It will be correctly bundled in the APK.
I have used the following code to compile native dropbox libraries, I am using Android Studio v1.1.