Posted on 06-05-2015
Filed Under (Technology) by bigbang

In Android jni folder, we will always add some C/C++ header file folders. Basically, there are two ways to do this job.

Option 1:

Add one of the following lines to your Android.mk inside a module of your choice:

LOCAL_C_INCLUDES := /path/to/your/includes # ignore previous includes
                                           # OR
LOCAL_C_INCLUDES += /path/to/your/includes # preserve previous includes

If necessary you could create an environment variable pointing at ‘/path/to/your/includes’ and include it like this:

LOCAL_C_INCLUDES := $(MYLIB_INCLUDES_PATH) # ignore previous includes
                                           # OR
LOCAL_C_INCLUDES += $(MYLIB_INCLUDES_PATH) # preserve previous includes

If you write following code but there is no effect:

LOCAL_C_INCLUDES := ../example/include
LOCAL_C_INCLUDES += ../example/include2

please add $(LOCAL_PATH) to them, which will be:

LOCAL_C_INCLUDES := $(LOCAL_PATH)../example/include
LOCAL_C_INCLUDES += $(LOCAL_PATH)../example/include2

Option 2:

  1. Copy the complete folder with all header-files in it (mylib) into the ‘jni’ folder of your project.
  2. Add the following line to your Android.mk inside a module of your choice:
LOCAL_C_INCLUDES := $(LOCAL_PATH)/mylib

or

LOCAL_C_INCLUDES += $(LOCAL_PATH)/mylib

Depending on whether there are previous includes or not. If the include is the first one, use the first examle which use := as keyword. If has previous includes, use latter which is += as keyword.

(0) Comments    Read More   

When we put the jni folder under projects and compile it using command line tool ndk-build, we will get libs & obj folders like this:

|--jni
|--libs
|----armeabi-v7a
|--obj

But under Android Studio develop environment, android studio can’t find the so files under libs folder.

We can do it by using following folder structure under Android Studio:

|---app
|------jni
|------libs
|------obj
|------src
|---------androidTest
|---------main

We can use this folder structure to compile JNI. When we compile the JNI file, ndk-build will generate libs & obj folders.

Finally, we add following to android part in build.gradle to let Android Studio finds the so files generated by jni folder:

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

Finally, the build.gradle should look like this:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 22
    buildToolsVersion "21.1.2"

    defaultConfig {
        applicationId "org.waterlin.mediatester"
        minSdkVersion 15
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

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

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

By this way, Android Studio can let you use local jni code to generate so library and integrated into your Android project.

(0) Comments    Read More