Android ndk(cmake): '두 번째 jni 라이브러리에서 로그 API를 사용할 때 '__android_log_write'에 대한 정의되지 않은 참조 (Android ndk(cmake): 'undefined reference to `__android_log_write' when using log api in the second jni library)


문제 설명

Android ndk(cmake): '두 번째 jni 라이브러리에서 로그 API를 사용할 때 'android_log_write'에 대한 정의되지 않은 참조 (Android ndk(cmake): 'undefined reference to `android_log_write' when using log api in the second jni library)

Android Studio 2.2 및 cmake를 사용하여 jni 파일을 빌드합니다.

jni 파일에 로그를 표시하고 싶지만 "undefined reference to `__android_log_write"라는 오류 메시지가 표시됩니다.

내 CMakeLists.txt 파일은 다음과 같습니다.

add_library( # Sets the name of the library.
         native‑lib

         # Sets the library as a shared library.
         SHARED

         # Provides a relative path to your source file(s).
         # Associated headers in the same location as their source
         # file are automatically included.
         src/main/cpp/native‑lib.cpp )

add_library( # Sets the name of the library.
         test‑lib

         # Sets the library as a shared library.
         SHARED

         # Provides a relative path to your source file(s).
         # Associated headers in the same location as their source
         # file are automatically included.
         src/main/cpp/test‑lib.cpp )

include_directories( src/main/jni/ )

# Searches for a specified prebuilt library and stores the path as a
# variable. Because system libraries are included in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.

find_library( # Sets the name of the path variable.
          log‑lib

          # Specifies the name of the NDK library that
          # you want CMake to locate.
          log )

# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in the
# build script, prebuilt third‑party libraries, or system libraries.

target_link_libraries( # Specifies the target library.
                   test‑lib
                   native‑lib
                   # Links the target library to the log library
                   # included in the NDK.
                   ${log‑lib} )

내 두 개의 jni 파일은 함수 이름 없이 아래와 같습니다.

JNIEXPORT jstring JNICALL Java_com_cyweemotion_www_jnitest_MainActivity_stringFromJNI
    (JNIEnv *env, jobject){
    __android_log_write(ANDROID_LOG_ERROR, "Tag", "Error here");
    std::string hello = "Hello from C++";
    return env‑>NewStringUTF(hello.c_str());
};

내 build.gradle(Module:app)은

    android {
    compileSdkVersion 23
    buildToolsVersion "24.0.3"
    defaultConfig {
        minSdkVersion 19
        targetSdkVersion 24
        versionCode 2
        versionName '1.02'
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        externalNativeBuild {
            cmake {
                cppFlags ""
            }
        }
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard‑android.txt'), 'proguard‑rules.pro'
            signingConfig signingConfigs.config
        }
        debug {
            jniDebuggable false
        }
    }
    externalNativeBuild {
        cmake {
            path "CMakeLists.txt"
        }
    }
    productFlavors {
    }
}

android 문서에 따르면: 프로젝트에 C 및 C++ 코드 추가 . 로그 API를 사용할 수 있다고 생각합니다.

내 코드 또는 설정에 문제가 있습니까?


업데이트:

첫 번째 jni 라이브러리(코드 업데이트)에서 문제가 아님을 발견했습니다. .

두 번째 라이브러리에서만 오류가 발생합니다.

예: target_link_libraries(test‑lib, native‑lib, ...), native‑lib는 로드할 두 번째 라이브러리입니다.

따라서 native‑lib는 로그 API를 사용할 수 없습니다.

이제 할 수 있는 것은 native‑lib를 제거하는 것뿐입니다. 하지만 그 이유를 알고 싶습니다.


참조 솔루션

방법 1:

I finally found I should separated to do the link.

target_link_libraries( # Specifies the target library.
                   test‑lib
                   native‑lib
                   # Links the target library to the log library
                   # included in the NDK.
                   ${log‑lib} )

target_link_libraries( # Specifies the target library.
                   native‑lib
                   # Links the target library to the log library
                   # included in the NDK.
                   ${log‑lib} )

(by larry lularry lu)

참조 문서

  1. Android ndk(cmake): 'undefined reference to `__android_log_write' when using log api in the second jni library (CC BY‑SA 2.5/3.0/4.0)

#android-ndk #java-native-interface #Android #cmake






관련 질문

FFmpeg 1.0 < 오디오 재생 문제 유발 (FFmpeg 1.0 < causing audio playback issues)

Android ImageView가 업데이트되지 않습니다. (Android ImageView is not being updated)

나중에 Android에서 실행해야 하는 데스크탑용 OpenCV 애플리케이션 개발 및 테스트 (Developing and testing an OpenCV application on desktop which should later run on Android)

Android 스튜디오 NDK 해결 방법 (Android Studio NDK workaround)

64비트 Android 장치에서 .so 파일을 찾을 수 없습니다. (Can not find .so file on 64 bit android device)

RUBYMOTION_ANDROID_SDK가 잘못되었습니다. (RUBYMOTION_ANDROID_SDK is incorrect)

NDK: 자바 클래스에서 .c 상수를 사용하는 방법 (NDK: How to use .c constants in java class)

Android ndk(cmake): '두 번째 jni 라이브러리에서 로그 API를 사용할 때 '__android_log_write'에 대한 정의되지 않은 참조 (Android ndk(cmake): 'undefined reference to `__android_log_write' when using log api in the second jni library)

~SkSpriteBlitter()를 반환할 때 스키어 네이티브 충돌이 발생했습니다. (I got skia native crash when return ~SkSpriteBlitter())

JNI를 사용하는 JAR에 Android 라이브러리 패키징 (Packing an android library on a JAR that uses JNI)

CMake가 서버에 통신 오류가 발생했습니다. (CMake communicate to server error occurred)

armv7-linux-androideabi 대상에서 Rust 라이브러리 충돌 (Rust library crashing on target armv7-linux-androideabi)







코멘트