Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android ndk(cmake): 'undefined reference to `__android_log_write' when using log api in the second jni library

I use Android Studio 2.2 and cmake to build jni file.

I want to show log in jni file but get error message "undefined reference to `__android_log_write".

My CMakeLists.txt file is :

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} )

And my two jni files are the same as below without function name

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());
};

My build.gradle(Module:app) is

    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 {
    }
}

According to the android document:Add C and C++ Code to Your Project. I think I can use log api.

What's wrong in my code or my setting ?


Update:

I found it is not problem in my first jni library(Update code).

It only occurs error in the second library.

ex: target_link_libraries(test-lib, native-lib, ...), native-lib is the second library to be loaded.

So native-lib can't use log api.

Now I only can do is to remove native-lib. However I really want to know why ?

like image 552
larry lu Avatar asked Nov 28 '16 12:11

larry lu


1 Answers

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} )
like image 80
larry lu Avatar answered Nov 15 '22 16:11

larry lu