Android NDK build in Android Studio

There are two ways to do the thing:

  1. deprecated method
      1. Download NDK for the android studio
      2. Create folder in “<Project>/src/main” named “jni”
      3. Open Settings -> Tools -> External tools -> Add following
        1. Name: “ndk-build”, Program:”/home/jiafei427/software/Android/Sdk/ndk-bundle/ndk-build”, Working Directory:”<project>/src/main/jni”
        2. Name: “javah”, Program:”/usr/bin/javah”, Parameters:”-classpath $Classpath$ -v -jni $FileClass$”, Working Directory:”<project>/src/main/jni”
      4. open build.gradle for the module
        1. in “android {}” add
          ndk{
              moduleName "ADASMsgJNI"
              ldLibs "log"
          }

          (The ldLibs “log” is used when you want to print something in terminal)

          sourceSets {
              main {
                  jniLibs.srcDirs = ['libs']
              }
          }
          (for default, Android studio will look up the folder "<PROJECT>/src/main/jniLibs" for the jni so library. this line will automatically look up the specified folder.)
      5. in “gradle.properties” add this:
    android.useDeprecatedNdk=true
    1. when you done with the Android.mk, Application.mk stuff, right click the jni folder -> External Tools -> ndk-build to build the JNI so libraries
    2. Then you will be able to see the folders “libs” and “objs” compiled in “~/src/main”
    3. And, you are ready to go.
  2. the one that android studio is still developing
    1. Read the following link before do it.
      1. http://tools.android.com/tech-docs/android-ndk-preview
      2. http://tools.android.com/tech-docs/new-build-system/gradle-experimental
    2. A typical Android Studio project may have a directory structure as follows.

      .

      ├── app/

      │   ├── app.iml

      │   ├── build.gradle

      │   └── src/

      ├── build.gradle

      ├── gradle/

      │   └── wrapper/

      │       ├── gradle-wrapper.jar

      │       └── gradle-wrapper.properties

      ├── gradle.properties

      ├── gradlew*

      ├── gradlew.bat

      ├── local.properties

      ├── MyApplication.iml

      └── settings.gradle

    3. ./gradle/wrapper/gradle-wrapper.properties

      • Each version of the new plugin supports a specific Gradle version. Consult Gradle Requirements for the Gradle version to use.


      #Wed Apr 10 15:27:10 PDT 2013
      distributionBase=GRADLE_USER_HOME
      distributionPath=wrapper/dists
      zipStoreBase=GRADLE_USER_HOME
      zipStorePath=wrapper/dists
      distributionUrl=https\://services.gradle.org/distributions/gradle-2.10-all.zip

    4. ./build.gradle

      • Classpath for the plugin is com.android.tools.build:gradle-experimental instead of com.android.tools.build:gradle.

      buildscript {
      repositories {
      jcenter()
      }
      dependencies {
      classpath “com.android.tools.build:gradle-experimental:0.7.0-alpha4
      }
      }

      allprojects {
      repositories {
      jcenter()
      }
      }

    5. for ./app/build.gradle modify following

      apply plugin: “com.android.model.application”
      model {
      android {
      compileSdkVersion 23
      buildToolsVersion “23.0.2”

      defaultConfig {

                 applicationId “com.example.user.myapplication”
      minSdkVersion.apiLevel 15
      targetSdkVersion.apiLevel 22
      versionCode 1
      versionName “1.0”

      *********************

             }

         }
      dependencies {
      compile fileTree(dir: “libs”, include: [“*.jar”])
      compile “com.android.support:appcompat-v7:22.2.0”
      }

    6. Then the Android studio will handle the rest of stuff, and you are ready to go.

 

PS.

The other link that helped a lot:

https://codelabs.developers.google.com/codelabs/android-studio-jni/index.html?index=..%2F..%2Findex#2

https://withwontae.wordpress.com/2016/03/24/android-studio-1-5-1-ndk-debugging/

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s