Android ndk (cmake): 'referência indefinida a `__android_log_write' ao usar a API de log na segunda biblioteca jni
Eu uso o Android Studio 2.2 e o cmake para criar o arquivo jni.
Eu quero mostrar o log no arquivo jni, mas recebo a mensagem de erro "referência indefinida a` __android_log_write ".
Meu arquivo 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} )
E meus dois arquivos jni são os mesmos que abaixo, sem o nome da função
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());
};
Meu build.gradle (Módulo: 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 {
}
}
De acordo comdocumento android: Adicione código C e C ++ ao seu projeto. Eu acho que posso usar API de log.
O que há de errado no meu código ou na minha configuração?
Atualizar:
Eu achei que não era problema na minha primeira biblioteca jni (código de atualização).
Ocorre apenas erro na segunda biblioteca.
ex: target_link_libraries (test-lib,lib nativo, ...), native-lib é a segunda biblioteca a ser carregada.
Portanto, a native-lib não pode usar a API de log.
Agora só posso remover a lib nativa. No entanto, eu realmente quero saber o porquê?