2017-10-17 29 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项目。它应该工作。

祝你好运!