Android ndk (cmake): 'referencia indefinida a `__android_log_write' cuando se usa log api en la segunda biblioteca jni
Uso Android Studio 2.2 y cmake para construir el archivo jni.
Quiero mostrar el registro en el archivo jni pero obtener el mensaje de error "referencia indefinida a` __android_log_write ".
- Detenga el subproceso Java que llama a la función JNI
- ¿Cómo puedo cargar mi propia clase Java en C en Android?
- Android JNI - Llamar AttachCurrentThread sin DetachCurrentThread
- Error de UnsatisfiedLink: Método nativo no encontrado findLibrary devuelto null
- Imprimir el seguimiento de la pila C en Android 5 / Lolipop
Mi archivo CMakeLists.txt es:
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} )
Y mis dos archivos jni son los mismos que a continuación sin nombre de función
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()); };
Mi build.gradle (módulo: app) es
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 acuerdo con el documento de Android: Agregar código C y C ++ a su proyecto . Creo que puedo usar log api.
¿Qué hay de malo en mi código o mi configuración?
Actualizar:
He encontrado que no es problema en mi primera biblioteca jni (código de actualización).
Sólo ocurre error en la segunda biblioteca.
Ex: target_link_libraries (test-lib, native-lib , …), native-lib es la segunda librería que se va a cargar.
Así que native-lib no puede usar log api.
Ahora sólo puedo hacer es quitar lib nativo. Sin embargo, realmente quiero saber por qué?
- Dalvik está buscando el archivo .so con extensión '.0' - ¿por qué?
- Creación de un archivo de encabezado para la clase de actividad de Android
- ¿Cómo crear un socket Android RFCOMM sin ninguna entrada del usuario?
- Temporizadores Android NDK
- Cómo almacenar en caché los mapas de bits en la memoria nativa
- ¿Cómo obtengo el búfer de la cámara Android en bruto en C con JNI?
- ¿Cómo empaquete los archivos .so en mi archivo .aar?
- ¿Cómo utilizar la biblioteca JNI compilada por NDK en una aplicación Java normal no Android?
Finalmente encontré que debería separarme para hacer el enlace.
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} )
- ¿Cómo obtener el código de la versión del paquete de dependencia?
- Cómo obtener informes de cobertura de código de google Firebase para Android Espresso tests