본문 바로가기

개발 Note/Android, Java,Kotlin

JNI, Native code build 시 유의점

반응형

android 프로그램을 개발하다 보면, 종종 C++ 또는 C로 작성된 코드를 사용해야 하거나, xxx.a 또는 yyy.so 와 같은 static library 또는 shared library 를 사용해야 하는 상황들이 발생합니다.

 

static library 

static library는 소스(source) 빌드시  링킹 시점에 심볼(symbol)이 연결됩니다.

주로  xxx.a 처럼 .a 확장자를 사용합니다.

shared library 

shared library는 런타임(runtime, 실행시점)때 필요시점에 library를 로딩해서 symbol이 연결됩니다.

주로 .so 또는 .dll 확장자로 사용됩니다.

 

이런 경우를 위해서 java에서는 JNI(java native interface) 를 제공하고 있습니다.

 

Android Studio 에서 기본적으로 CMake와 make file 을 통해서 구현이 가능합니다.

 

    static {
        System.loadLibrary("native-lib");
    }
    private native String stringFromJNI();

 

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

 

그러나 android 가 vm 위에서 동작하는 것과 달리 native code는 platform에 종속성(dependency)를 가지게 되니, 빌드 환경이나 플랫폼에 영향을 받게 됩니다.

 

 

C++ 관련 link error

ld: error: undefined symbol: std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::operator=(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)
  >>> referenced by string:1078 (external/libcxx/include/string:1078)

 

때문에 ndk build 시 에러가 발생하는 경우들이 나옵니다.

android jni 의 native build error 는 주로 아래 so들 때문에 발생하는 경우들이 많습니다.

c++관련 toolchain file 인데,  android 에서는 어떤 이유에선지 해당 so file들을 제공하지 않습니다.

build 시 또는 runtime에 에러가 발생하게 됩니다.

 

libc++.so, libbase.so, libcutils.so

 

https://developer.android.com/about/versions/nougat/android-7.0-changes?hl=ko 

 

Android 7.0 동작 변경 사항  |  Android 개발자  |  Android Developers

Along with new features and capabilities, Android 7.0 includes a variety of system and API behavior changes. This document highlights some of the key changes that you should understand and account for in your apps. If you have previously published an …

developer.android.com

 

만들고자 하는 native lib을 static으로 만들면 build는 해결할 수 있지만, java에서 c++ native  symbol link 과정은 컴파일(compile)시에 결정되는 것이 아니라 런타임(runtime linking 또는 dynamic linking )을 하기 때문에 so file을 사용해야 합니다.

 

즉 해결 방법은, libc++.so, libbase.so, libcutils.so 파일들을 모두 application library로 포함 시켜야 합니다.

 

 

 

'개발 Note > Android, Java,Kotlin' 카테고리의 다른 글

[Copy&Paste]String 다루기  (0) 2020.11.11
[Copy&Paste] Array를 List로 바꾸기  (0) 2020.11.04
[Android] JavaDoc 사용법 링크.  (0) 2020.10.28