Android Studio、Gradle、OpenCV 和 NDK [英] Android Studio, Gradle, OpenCV and NDK

查看:33
本文介绍了Android Studio、Gradle、OpenCV 和 NDK的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Android Studio 上测试 OpenCV Android,我对如何包含 NDK 感到困惑.

我想做的是运行 OpenCV 附带的示例.在提供的 6 个样本中,我成功运行了 4 个.例外是面部检测和本地活动.

我怀疑原因是我没有正确设置我的 NDK.

谷歌搜索我发现了一堆讨论,但并不真正理解它们.这是我第一次尝试同时使用 NDK 和 OpenCV,我对 Gradle 的理解有限.

我在我的 .bash_profile 中设置了一个环境变量

导出 ANDROID_NDK=pathTo/android-ndk-r9

我不明白如何把它送到工作室.

我看到对 jniFolder 的引用,但不明白这些是什么,我现在应该关心.Stackoverflow.com/questions/17767557

tasks.withType(com.android.build.gradle.tasks.PackageApplication) { pkgTask ->pkgTask.jniFolders = new HashSet()pkgTask.jniFolders.add(new File(projectDir, 'native-libs'))

}

我应该如何处理 build.gradle 文件末尾的这个粘贴?

总而言之,我的问题是.

  1. 如何让 Android Studio 读取 NDK 变量?
  2. jniFolders 到底是什么?
  3. 仅仅粘贴到我的 build.gradle 文件的末尾就足够了吗?

关于 Gradle 和 NDK 的 Google 群组讨论><小时>

对于遇到此问题的任何人,除了 Xaviers anwser 之外,这就是我解决它的方法.首先,我在 docs 目录中阅读了 NDK 附带的 OVERVIEW.html.然后我将 .mk 和 .cpp 文件编译成一个 .so 文件.我在示例 jni 目录中就地做了这个这在 libs 文件夹中创建了 .so 文件,我将其复制到 Xavier 指定的目的地.

解决方案

如果您有使用 ndk 构建的库,并希望将它们放入支持 gradle 的 android 项目(使用插件的 0.7.+ 版),你可以把它们放进去

src/main/jniLibs//libfoo.so

例如:

src/main/jniLibs/armeabi-v7a/libfoo.sosrc/main/jniLibs/x86/libfoo.so

它们会自动打包.

如果您想将它们保存在 native-libs 文件夹中,您可以将以下内容放入您的 gradle 文件中:

android {sourceSets.main {jniLibs.srcDirs = ['native-libs']}}

所有这些实际上都是告诉 gradle 主源集的 jniLibs 文件夹在哪里(相对于项目根目录.)

您展示的代码段正在做一些不同的事情.它告诉包任务还包括一些本机库.这是一个 hack,曾经在以前的版本中使用不再支持的任务的未记录的 API 工作.

I am trying to test OpenCV Android, on Android Studio, I am confused about how to include the NDK.

What I want to do is run the samples which come with OpenCV. Of the 6 samples provided I managed to run 4 successfully. The exceptions were face-detection and native-activity.

I suspect the reason is I have not set up my NDK correctly.

Googling I found a bunch of discussions but do not really understand them. This is my first time I am trying to work with both the NDK and OpenCV, and my Gradle understanding is limited.

I set an environment variable in my .bash_profile

export ANDROID_NDK=pathTo/android-ndk-r9

I do not understand how to get this to studio.

I see reference to jniFolder but do not understand what these are and should I care right now. Stackoverflow.com/questions/17767557

tasks.withType(com.android.build.gradle.tasks.PackageApplication) { pkgTask ->
pkgTask.jniFolders = new HashSet<File>()
pkgTask.jniFolders.add(new File(projectDir, 'native-libs'))

}

What am I supposed to do with this paste at the end of my build.gradle file ?

In summation, my questions are.

  1. How do I get Android Studio to read the NDK variable ?
  2. What exactly are the jniFolders ?
  3. Is it enough just to paste at the end of my build.gradle file ?

Google Group Discussions on Gradle and NDK


For anyone coming across this this is how I resolved it apart from Xaviers anwser. First I read OVERVIEW.html which comes with the NDK, in the docs directory. I then compiled the .mk and .cpp files into an .so file. I did this inplace in the sample jni directory This created the .so file in the libs folder which I copied to the destination as given by Xavier.

解决方案

If you have libraries that you build with the ndk and want to put them in a gradle-enabled android project (using version 0.7.+ of the plugin), you can just put them in

src/main/jniLibs/<abi>/libfoo.so

for example:

src/main/jniLibs/armeabi-v7a/libfoo.so
src/main/jniLibs/x86/libfoo.so

and they'll get packaged automatically.

If you want to keep them in the native-libs folder you can put the following in your gradle file:

android {
    sourceSets.main {
        jniLibs.srcDirs = ['native-libs']
    }
}

All this does really is tell gradle where the jniLibs folder for the main source set is (relative to the project root.)

The snippet you showed is doing something different. It's telling the package task to also include some native libraries. This was a hack that used to work in a previous version using undocumented API of the task that are no longer supported.

这篇关于Android Studio、Gradle、OpenCV 和 NDK的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆