문제 설명
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} )