2016-11-28 3 views
5

私はAndroidスタジオ2.2とcmakeを使ってjniファイルを作成しています。Android ndk(cmake):2番目のjniライブラリでログapiを使用しているときに '__android_log_write'への未定義

ログインしてjniファイルを表示したいのですが、エラーメッセージ "__android_log_write"への未定義の参照が表示されます。

マイCMakeLists.txtファイルです:

です:私のbuild.gradle(アプリモジュール)が

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

そして、私の2つの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()); 
}; 

関数名なしで以下と同じです

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 document:Add C and C++ Code to Your Projectによると、私はログAPIを使用できると思う。

私のコードや私の設定で何が間違っていますか?


更新:

私はそれが私の最初のJNIライブラリ(更新コード)での問題ではありません発見しました。

2番目のライブラリでのみエラーが発生します。

例:target_link_libraries(test-lib、native-lib、...)、native-libはロードする2番目のライブラリです。

したがって、native-libはlog apiを使用できません。

これで、native-libを削除できます。しかし、私は本当に理由を知りたいですか?

+0

私はドキュメントに誤字があると思います。文は単に 'target_link_libraries(log)'のように見えるかもしれません –

+0

私は2つのjniライブラリをロードしたことを忘れています。今、私はログapiが最初のもので使用できることを発見しました。しかし、2番目に失敗する。 –

+0

はい、これは重要な詳細です –

答えて

4

私は結局リンクを行うために分かなければなりませんでした。

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}) 
関連する問題