【问题标题】:JNI compile error on Android studio 2.3.2Android Studio 2.3.2 上的 JNI 编译错误
【发布时间】:2017-09-16 07:40:11
【问题描述】:

我正在申请将手机视频播放到 youtube 频道。我找到了这个链接https://github.com/youtube/yt-watchme

编译代码时出现错误

libavutil 未找到 在文件 avecode.h 中的代码 #include "libavutil/samplefmt.h

我也改成#include "../libavutil/samplefmt.h" 还是一样的错误。

也许建议任何好的 rtmp 库将手机视频广播到 youtube 频道。

错误:失败:构建失败并出现异常。 * 出了什么问题:
任务“:app:externalNativeBuildDebug”执行失败。
构建命令失败。
执行过程出错

/Users/nomankhan/Library/Android/sdk/cmake/3.6.4111459/bin/cmake 带参数 {--build /Clients/Ankur/JniDemo/app/.externalNativeBuild/cmake/debug/mips64 --target native -lib}
[1/2] 构建 CXX 对象 CMakeFiles/native-lib.dir/src/main/cpp/native-lib.cpp.o
失败:/Users/nomankhan/Library/Android/sdk/ndk-bundle/toolchains/llvm/prebuilt/darwin-x86_64/bin/clang++ --target=mips64el-none-linux-android --gcc-toolchain=/Users/ nomankhan/库/Android/sdk/ndk-bundle/toolchains/mips64el-linux-android-4.9/prebuilt/darwin-x86_64 --sysroot=/Users/nomankhan/Library/Android/sdk/ndk-bundle/sysroot -Dnative_lib_EXPORTS - I../../../../src/main/cpp/include/libavcodec -I../../../../src/main/cpp/include/libavformat -I../ ../../../src/main/cpp/include/libavutil -isystem /Users/nomankhan/Library/Android/sdk/ndk-bundle/sources/cxx-stl/gnu-libstdc++/4.9/include -isystem /Users/nomankhan/Library/Android/sdk/ndk-bundle/sources/cxx-stl/gnu-libstdc++/4.9/libs/mips64/include -isystem /Users/nomankhan/Library/Android/sdk/ndk-bundle/sources /cxx-stl/gnu-libstdc++/4.9/include/backward -isystem /Users/nomankhan/Library/Android/sdk/ndk-bundle/sysroot/usr/include/mips64el-linux-android -D__ANDROID_API__=21 -g -DANDROID -ffunction-sections -funwind-tables -fstack-protecto r-strong -no-canonical-prefixes -fintegrated-as -Wa,--noexecstack -Wformat -Werror=format-security -O0 -fno-limit-debug-info -fPIC -MD -MT CMakeFiles/native-lib.dir /src/main/cpp/native-lib.cpp.o -MF CMakeFiles/native-lib.dir/src/main/cpp/native-lib.cpp.od -o CMakeFiles/native-lib.dir/src/main /cpp/native-lib.cpp.o -c /Clients/Ankur/JniDemo/app/src/main/cpp/native-lib.cpp

在 /Clients/Ankur/JniDemo/app/src/main/cpp/native-lib.cpp:4 包含的文件中:/Clients/Ankur/JniDemo/app/src/main/cpp/libavcodec/avcodec.h :31:10: 致命错误:找不到“libavutil/samplefmt.h”文件 #include "libavutil/samplefmt.h" ^~~~~~~~~~~~~~~~~~~~~~~

我的 CMakeLists.txt

 cmake_minimum_required(VERSION 3.4.1)

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).
             src/main/cpp/native-lib.cpp )

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 )


target_link_libraries( # Specifies the target library.
                       native-lib

                       # Links the target library to the log library
                       # included in the NDK.
                       ${log-lib} )

include_directories(src/main/cpp/include/libavcodec)

include_directories(src/main/cpp/include/libavformat)

include_directories(src/main/cpp/include/libavutil)

【问题讨论】:

  • 我相信您需要include_directories(src/main/cpp) 而不是最后三个语句。在 native-lib.cpp 你应该有 #include "libavcodec/avcodec.h".
  • @Noman 我也遇到了与您相同的错误。我正在构建简单的 cpp 文件,但我无法构建它。你可以看到我的代码stackoverflow.com/questions/51100111/…
  • @AlexCohn 我还在我的 native-lib.cpp 文件中提到了标题,但仍然是我无法成功构建代码的原因。也可以看代码stackoverflow.com/questions/51100111/…

标签: android-studio compiler-errors java-native-interface


【解决方案1】:

下面的答案是假设cpp 中的文件夹包含 C++ 代码/src 文件。如果没有,那么您可能遇到了代码和库结构问题。

简单地调用include_directories不会得到CMake 来编译它们,我相信它只会帮助IDE 进行某些“语法突出显示”和编码相关的事情,但它很重要。

相反,您需要在 add_library 调用中包含代码文件。由于很明显您有很多文件,因此这样的遍历代码会有所帮助:

cmake_minimum_required(VERSION 3.4.1)

include_directories(src/main/cpp/include/libavcodec)        

# Traverses through the directories recursively 
# and append matching files to variable my_lib_SRC
file(GLOB_RECURSE my_lib_SRC
    "src/main/cpp/*.h"
    "src/main/cpp/*.cpp"
)

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).
         ${my_lib_SRC})

target_link_libraries( # Specifies the target library.
                       native-lib

                       # Links the target library to the log library
                       # included in the NDK.
                       ${log-lib} )

注意:每次添加新的源/代码文件时,您都需要清理并再次构建项目才能正确构建二进制文件。进一步的解释可以在这里找到:https://stackoverflow.com/a/17655165/2949966

【讨论】:

  • 成功了。您能否在 android 或教程示例中提供一些有关 cmake 的链接。谢谢
  • 老实说,我的大部分知识都来自关于不同问题的帖子/问题,没有一个我发现有益的真正好的教程,这取决于我正在寻找或获得什么工作,但我确信那里有一些我没见过的。但是,我可以为您提供我为 OpenCV 制作的实现,它使用 cmake 构建 OpenCV 模块并允许开发人员编写本机 C/C++ 代码并从交叉编译中受益:github.com/ahasbini/Android-OpenCV。请考虑选择答案作为将问题标记为完成的解决方案。
  • 我正在附加项目链接github.com/nomi2013/JniCompileWithCmake 从那里我可以得到这些文件夹和必要的文件 libavcodec ibavformat libavutil libavcodec 实际上当我编译时缺少一些文件。谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-05-19
  • 1970-01-01
  • 2013-05-11
  • 2013-05-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多