Step by Step - How to create a c++ library with NDK on Android Studio 1.5 (not experimental way)

1. Configure JDK, NDK path environments

I'm using jdk 1.8.0_65 and ndk-bundle. We can use the official ndk also.

Let's see!

2. Add javah, ndk-build as External Tools

(Menu Location is 'Settings > Tools > External Tools')

2-1. Configure javah

Let's see!

2-2. Configure ndk-build

Let's see!

2-3. Configure ndk-build clean

Let's see!

3. Create a new Android Studio Project

Let's see!

4. Add a java class for JNI

Let's see!

5. Create a folder for JNI

screenshot from 2015-12-03 15_36_30

6. Configure build.gradle

android {
  ...
  defaultConfig {
    ...
    ndk {
      moduleName "your dllname"
    }

    sourceSets.main {
      jni.srcDirs = []
      jniLibs.srcDir "src/main/libs"
    }
    ...
  }
  ...
}

7. First Build

We will encounter an error like 'Error: NDK integration is deprecated in the current plugin. blah blah'.

Because we are not using the experimental class for NDK.

Let's add one line in 'gradle.properties'.

android.useDeprecatedNdk=true

And build again, it must be succeeded.

8. Create a JNI header file

Use 'NDK external tools - javah'

screenshot from 2015-12-03 15_40_32

Then we will have a JNI header file.

screenshot from 2015-12-03 15_41_07

9. Create a C++ source file

We don't need to check "Create associated header".

screenshot from 2015-12-03 15_42_13

screenshot from 2015-12-03 15_42_26

screenshot from 2015-12-03 15_43_55

10. Create some MakeFiles

Android.mk

screenshot from 2015-12-03 15_44_24

screenshot from 2015-12-03 15_44_37

screenshot from 2015-12-03 15_45_44

Application.mk

screenshot from 2015-12-03 15_46_24

11. Build Our NDK Library

screenshot from 2015-12-03 15_51_28

ndk_build_result

12. Let's Use Our NDK Library Function

use_ndk

13. Final Build and Create APK

After build, We must have our library in the APK.

screenshot from 2015-12-03 15_54_57

screenshot from 2015-12-03 15_55_12

14. Let's Enjoy NDK :)

Written on December 3, 2015