包括用于通过JNI交互生成渐变的头目录



我正在尝试让C++代码与react-native一起工作(有关常规步骤,请参阅此部分)。

我已经用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将目录添加到其搜索/indude路径

在常规的Android项目中,似乎可以编辑Android.mk/Application.mk文件。我的文件夹里没有这样的文件;我认为gradle实际上生成了一个Android.mk文件(在$PROJECT_ROOT/android/app/build/intermediates/ndk/debug/Android.mk中),我尝试编辑它(LOCAL_C_INCLUDES字段)来添加我的目录,但当我尝试另一个构建时,它会被覆盖。

提前谢谢。

您可以在$PROJECTROOT/android/app/build.gradle:中对此进行编辑

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

然而,你很可能会遇到其他问题(我知道这是事实,因为我知道)。我建议您使用现代且用户友好的CMake集成。

我将写下一般步骤,以便在未来的某个时候再次找到它:

  • 将您的文件添加到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 studio,只需使用本指南中提供的信息,因为它已经得到了很好的解释
    • 如果你没有,我也建议你看看这本指南,因为它解释得很好

之后,您可以在android studio或cd $(PROJECTROOT)/android && ./gradlew installDebug中构建项目。它应该起作用。

祝你好运!

最新更新