2017-10-17 91 views
0

我試圖讓C++代碼與react-native一起工作(有關一般步驟,請參閱this)。包含用於與JNI交互的gradle生成的頭文件目錄

我使用react-native init生成了我的項目,使用Djinni生成了JNI綁定。我現在試圖構建應用程序並在我的android模擬器上測試它(cd $PROJECT_ROOT/android && ./gradlew installDebug)。好像都找不到頭文件,不包含他們的目錄:

> ./gradlew installDebug 
(...) 
:app:compileDebugJavaWithJavac UP-TO-DATE 
:app:compileDebugNdk 
Warning: Deprecated NDK integration enabled by useDeprecatedNdk flag in gradle.properties will be removed from Android Gradle plugin soon. 
Consider using CMake or ndk-build integration with the stable Android Gradle plugin: 
https://developer.android.com/studio/projects/add-native-code.html 
or use the experimental plugin: 
http://tools.android.com/tech-docs/new-build-system/gradle-experimental. 

In file included from $PROJECT_ROOT/android/app/src/main/jni/NativeCppBridgeText.cpp:4: 
$PROJECT_ROOT/android/app/src/main/jni/NativeCppBridgeText.hpp:6:10: fatal error: 'cpp_bridge_text.hpp' file not found 
#include "cpp_bridge_text.hpp" 
     ^~~~~~~~~~~~~~~~~~~~~ 
1 error generated. 
make: *** [$PROJECT_ROOT/android/app/build/intermediates/ndk/debug/obj/local/armeabi-v7a/objs/app/$PROJECT_ROOT/android/app/src/main/jni/NativeCppBridgeText.o] Error 1 

:app:compileDebugNdk FAILED 

FAILURE: Build failed with an exception. 

我設法通過創建硬鏈接導致問題的頭,讓過去這個小問題。這導致我:

> ./gradlew installDebug 
(...) 
:app:compileDebugJavaWithJavac UP-TO-DATE 
:app:compileDebugNdk 
Warning: Deprecated NDK integration enabled by useDeprecatedNdk flag in gradle.properties will be removed from Android Gradle plugin soon. 
Consider using CMake or ndk-build integration with the stable Android Gradle plugin: 
https://developer.android.com/studio/projects/add-native-code.html 
or use the experimental plugin: 
http://tools.android.com/tech-docs/new-build-system/gradle-experimental. 

In file included from $PROJECT_ROOT/android/app/src/main/jni/NativeCppBridgeText.cpp:4: 
In file included from $PROJECT_ROOT/android/app/src/main/jni/NativeCppBridgeText.hpp:7: 
$PROJECT_ROOT/app/src/main/jni/djinni_support.hpp:20:10: fatal error: 'exception' file not found 
#include <exception> 
     ^~~~~~~~~~~ 
1 error generated. 
make: *** [$PROJECT_ROOT/android/app/build/intermediates/ndk/debug/obj/local/armeabi-v7a/objs/app/$PROJECT_ROOT/android/app/src/main/jni/NativeCppBridgeText.o] Error 1 

:app:compileDebugNdk FAILED 

FAILURE: Build failed with an exception. 

在這種情況下,似乎甚至標準庫不包括在內。

我的問題是:如何明確指定gradle將目錄添加到其搜索/ indlude路徑?

在常規的Android項目中,似乎您可以編輯Android.mk/Application.mk文件。我的文件夾中沒有這些文件;我認爲gradle實際上生成一個Android.mk文件(在$PROJECT_ROOT/android/app/build/intermediates/ndk/debug/Android.mk),我試着編輯它(LOCAL_C_INCLUDES字段)來添加我的目錄,但是當我嘗試另一個生成時它會被覆蓋。

在此先感謝。

回答

0

您可以在$PROJECTROOT/android/app/build.gradle編輯本:

android { 
    defaultConfig { 
     (...) 
     ndk { 
      (...) 
      cFlags = "-Ipath/to/directory/" 
     } 
    } 
} 

但是,你很可能會遇到的其他問題(我知道一個事實,因爲我做了)。我建議你使用現代化和人性化的CMake integration.

我會寫下來的一般步驟,這樣我可以在將來某個時候再次找到它:

  • 您的文件添加到android項目(在android studio或其他IDE下)。您的java/文件夾中應包含文件,jni/文件夾中的文件(JNI橋文件)以及cpp/文件夾(本機C++)中的文件。
  • 將一個CMakeLists.txt文件添加到您的模塊(如果您使用的是react-native,通常命名爲app)。
  • 在這個文件中,遵循以下結構:

    cmake_minimum_required(VERSION 3.4.1) 
    
    add_library(# Name of the library 
          $(YOUR_LIBRARY_NAME) 
          # Sets it as a shared library 
          SHARED 
          # Relative path to the source file(s) 
          path/to/your/file.cpp path/to/other/file.cpp) 
    
    # Allows you to add folders to the search path : similar to -Ipath/to/your/headerfolder/ 
    include_directories(path/to/your/headerfolder/) 
    
    # Allows you to have special compilation flags for the specified library's compilation 
    target_compile_options($(YOUR_LIBRARY_NAME) 
             PRIVATE # You can also use PUBLIC/INTERFACE 
             -std=gnu++11) # Your option ; I needed this in my case 
    
  • 一旦完成,你需要鏈接gradle這個以您的本機庫:

    • 如果您使用的Android工作室,只是請使用this guide中提供的信息,因爲它有很好的解釋。
    • 如果你不是,我還建議你看看this guide,因爲它很好的解釋。

之後,你可以建立在Android Studio中,或cd $(PROJECTROOT)/android && ./gradlew installDebug項目。它應該工作。

祝你好運!