Search code examples
androidopencvandroid-ndkjava-native-interfacendk-build

Basic Hello World app isn't building with ndk-build (.mk files)


I'm using Android Studio 2.2.3 with OpenCV SDK. I started with the default Hello world application (C++ option selected). The project was configured (by default) to use CMake.

OpenCV SDK uses the .mk-based build system. I modified the build.gradle for my application as follows (commented 3 lines, added 3 lines for ndk-build):

apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.3"
    defaultConfig {
        applicationId "in.ac.iitb.sc.arms.sample"
        minSdkVersion 19
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        externalNativeBuild {
            cmake {
                cppFlags "-frtti -fexceptions"
            }
        }
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    externalNativeBuild {
//        cmake {
//            path "CMakeLists.txt"
//        }
        ndkBuild{
            path 'src/main/cpp/Android.mk'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.1.1'
    testCompile 'junit:junit:4.12'
}

My current directory tree looks like:

APP
├── app.iml
├── build.gradle
├── CMakeLists.txt
├── libs
├── proguard-rules.pro
└── src
    ├── androidTest
    │   └── java
    │       └── test 
    │           └── ...
    ├── main
    │   ├── AndroidManifest.xml
    │   ├── cpp
    │   │   ├── Android.mk
    │   │   ├── Application.mk
    │   │   └── native-lib.cpp
    │   ├── java
    │   │   └── test
    │   │       └── sc
    │   │           └── arms
    │   │               └── sample
    │   │                   └── MainActivity.java

where Android.mk looks like

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

#opencv
OPENCVROOT:= /home/homer/Desktop/OpenCV-android-sdk
OPENCV_CAMERA_MODULES:=on
OPENCV_INSTALL_MODULES:=on
OPENCV_LIB_TYPE:=SHARED

include $(OPENCVROOT)/sdk/native/jni/OpenCV.mk

LOCAL_SRC_FILES := native-lib.cpp

LOCAL_LDLIBS +=  -llog -ldl
LOCAL_MODULE:=MyLibs

include $(BUILD_SHARED_LIBRARY)

Also I have placed the Application.mk file with the following content in the cpp directory

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

And also Android is not able to resolve the stringFromJNI() function as shown

android unable to resolve the function

When I build the application it shows no error. When I run the app it immediately crashes with the java.lang.UnsatisfiedLinkError: couldn't find "libnative-lib.so" error.

The libnative-lib.so is supposed to be generated by ndk-build, it is not being built. I suspect that it is a Gradle configuration related issue.

EDIT:

Here is the content of native-lib.cpp:

#include <jni.h>
#include <string>

extern "C"
jstring
Java_e2016_iitb_1projects_vvy_sample_MainActivity_stringFromJNI(
        JNIEnv* env,
        jobject /* this */) {
    std::string hello = "Hello from C++";
    return env->NewStringUTF(hello.c_str());
}

Solution

  • There are 2 potential problems I can see here:

    • in your build.gradle the defaultConfig block still uses the cmake build tool within the externalNativeBuild block. It should be ndkBuild too.
    • regarding the UnsatisfiedLinkError, you might have encountered a common mistake. One of the solutions for preventing this kind of exception is to make sure the C method signature is fully correlated to your app (Java) package name.