Nav Nav
Nav Nav

Reputation: 167

NDK-build vs Android Studio NDK Build

To run some Native code you can execute ndk-build command but I am confused if the Eclipse or Android Studio IDEs execute this command automatically on the Compile-time or how ndk-build command is related to the mentioned IDEs when I compile my code by eclipse or droid studio?

Upvotes: 2

Views: 1401

Answers (3)

Vitt Volt
Vitt Volt

Reputation: 337

For eclipse, there are already too many online sources related to it, so I will talk about Android Studio here.

If you don't have your onw native files (C++,C) in your project, just put the .a or .so native libraries in the src/main/jniLibs folders and you don't have to modify anything in the gradle file. The system will do everything for you automatically.

If you have your own native files and have put them in the src/main/jni folder, then you have to create your own makefiles and put them in the jni folder as well. You also have to modify the gradle file in your app module.

Here is what I did for a face detection sample of opencv, and I got the original code from this wonderful post(Here), which was actually modified from another one's method for simpler execution. The philosophy is rather simple: compile the native source codes and put the generated .so/.a library into the jniLibs folder:

My project structure: Project Structure

My makefiles (make sure you have the original sdk of the library for reference in the makefile):

Android.mk:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

OPENCV_CAMERA_MODULES:=on
OPENCV_INSTALL_MODULES:=on
OPENCV_LIB_TYPE:=SHARED
include /home/ng/Desktop/OpenCV-android-sdk/sdk/native/jni/OpenCV.mk

LOCAL_SRC_FILES  := DetectionBasedTracker_jni.cpp
LOCAL_C_INCLUDES += $(LOCAL_PATH)
LOCAL_LDLIBS     += -llog -ldl

LOCAL_MODULE     := detection_based_tracker

include $(BUILD_SHARED_LIBRARY)

Application.mk:

    APP_STL := gnustl_static
    APP_CPPFLAGS := -frtti -fexceptions
    APP_ABI := armeabi-v7a armeabi
    APP_PLATFORM := android-19

My gradle.build file in the app module:

apply plugin: 'com.android.application'
android {
    compileSdkVersion 23
    buildToolsVersion "23.0.1"

    defaultConfig {
        applicationId "org.opencv.samples.facedetect"
        minSdkVersion 14
        targetSdkVersion 19
        versionCode 1
        versionName "1.0"
    }
    sourceSets.main.jni.srcDirs = []

    task ndkBuild(type: Exec, description: 'Compile JNI source via NDK') {
       // ndkDir = project.plugins.findPlugin('com.android.application').getNdkFolder()
        commandLine "$ndkDir/ndk-build",
                'NDK_PROJECT_PATH=build/intermediates/ndk',
                'NDK_LIBS_OUT=src/main/jniLibs',
                'APP_BUILD_SCRIPT=src/main/jni/Android.mk',
                'NDK_APPLICATION_MK=src/main/jni/Application.mk'
    }

    tasks.withType(JavaCompile) {
        compileTask -> compileTask.dependsOn ndkBuild
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.1.0'
    compile project(':openCVLibrary300')
}

You don't actually have to change other gradle files for compiling the native part of the project. You could also compile the native sources codes by command line and put the .so files back to the jniLibs folder. This also works. I hope this could help in your problem.

Upvotes: 3

fastr.de
fastr.de

Reputation: 1523

Today Android-Studio (Canary) Preview 1.3 RC1 got released with jni/ndk support out of the box. No need to call commandline tools anymore.

https://sites.google.com/a/android.com/tools/download/studio/canary/latest

Release Notes: https://sites.google.com/a/android.com/tools/recent/androidstudiowithandroidndkpreviewsupportavailable

Upvotes: 0

Shajo
Shajo

Reputation: 203

If you want you could compile it using Terminal commands or else you could configure both the IDEs to do automatically

Upvotes: 0

Related Questions