【问题标题】:Native code Android Studio 1.5 and Experimental plugin 0.4, including .so files本机代码 Android Studio 1.5 和 Experimental plugin 0.4,包括 .so 文件
【发布时间】:2016-01-06 08:01:38
【问题描述】:

我的设置:
- Android Studio 1.5
- 安卓 NDK-r10e
- 实验插件 0.4.0

我尝试了很多关于在 Android Studio 中编译本机代码的在线教程,但我遇到了一个问题:包括 .so 文件。

我需要在我的代码中使用 GMP 和 OpenSSL。我尝试将#include <gmp.h> 添加到github 提供的示例hello-jni 代码中,以测试我的C 代码是否可以读取.so 文件,但它返回错误:

gmp.h: 没有这样的文件或目录

这是我的文件夹结构(我尝试先针对 x86 进行编译)

这样做的正确方法是什么?

【问题讨论】:

    标签: java android android-studio android-ndk android-studio-import


    【解决方案1】:

    你需要分别保存.so文件和头文件.h。在jniLibs 文件夹下只需要.so 文件。将头文件单独保存在jni/headers 文件夹下,并从该文件夹访问它们。此外,您不需要在jniLibs 文件夹下包含.a 文件。

    在 gradle build 文件的jni source dir 下添加头文件路径。使用double quotes 而不是angle brackets 访问像#include "gmp.h" 这样的头文件以在本地用户路径中搜索。

    【讨论】:

    • 谢谢。已经听从了你的建议。但错误没有解决。
    【解决方案2】:

    您需要在 build.gradle 文件中指定包含路径,详情请参阅 http://tools.android.com/tech-docs/new-build-system/gradle-experimental#TOC-Ndk-Integration

    model {
        android.ndk {
            moduleName = "hello-jni"
            def jniLibs = file("app/src/main/jniLibs/x86").getAbsolutePath()
            println(file("app/src/main/jniLibs/x86/gmp.h").exists())
            CFlags.add("-I$jniLibs".toString())
        }
    }
    

    【讨论】:

    • 谢谢。我尝试了您的建议,但导致类似的错误。你试过这个吗?你用的是哪个版本的实验插件?
    • 试过你的更新代码..仍然出现同样的错误..T_T
    • 要调试这个,你添加一些 println()
    猜你喜欢
    • 2016-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多