摇篮2.5实验:包括JNI .so库和呼叫 [英] Gradle 2.5 Experimental : include .so library and call from JNI

查看:343
本文介绍了摇篮2.5实验:包括JNI .so库和呼叫的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是,我无法找到一些适当的文件/指导,以便正确地设置我的环境可循。

基本上我使用的是Android 1.3.2工作室(最新的稳定为2015年9月22日的)与实验摇篮2.5版。

我跟谷歌官方文档在这里:的http://工具.android.com /科技的文档/新制造系统/ gradle这个实验性的,但我觉得没有帮助,当谈到JNI库。我试过很多其他的解决方案,但他们没有工作,因为他们似乎是过时了。

我有一个的.so 简单的 C ++库文件我创建测试NDK支持和放大器;整合谷歌在为我宣布,应与该Android 1.3工作室更好/ O 2015

这是怎么看我的很简单 C ++库

TestCPPClass.h的内容

 的#ifndef TESTCPPCLASS_H
#定义TESTCPPCLASS_H类TestCPPClass {
上市:
    双getDouble();
};#ENDIF TESTCPPCLASS_H

和TestClass.cpp:

 的#includeTestCPPClass.h双TestCPPClass :: getDouble()
{
    返回1; //不能笨! ^^
}

使用的 Cygwin的终端 GCC 我编译使用以下行这个非常先进的库:


  

gcc.exe -shared -o TestLib.so TestCPPClass.cpp


这也正常产生 TestLib.so

在我目前的Andr​​oid项目,我有一个本地方法,其次是在JNI实现的:

公共静态双getDoubleFromJNI();

使用的 javah.exe 我产生的 .H 文件,然后实施了JNI方法如下:

 的#includejni.h
#包括my_package_header.h
#包括TestCPPClass.h// =>标记为红色的,因为没有发现?JNIEXPORT jdouble JNICALL Java_my_package_header_getDoubleFromJNI
        (JNIEnv的* ENV,jobject主叫方)
{
     //在这里,我想打电话给我TestLib.so应该包含我的TestCPPClass,并呼吁TestCPPClass :: getDouble()
     //为了返回结果。
}

那么,关于我的的build.gradle
这是我对里面的NDK 模型{}

  android.ndk {
        MODULENAME =MyAwesomeJNI
        LDLIBS + =TESTLIB=> SYNC的gradle工作,但BUILD失败!
    }

这是我的错误:


  

错误:执行失败的任务
  ':应用程序:linkArm64-v8aDebugMyAwesomeJNISharedLibrary


  
  

    

在建立操作失败。
          链接在链接libMyAwesomeJNI.so失败。看到完整的日志在:
    file:///C:/Users/Me/Documents/MyProject/app/build/tmp/linkArm64-v8aDebugMyAwesomeJNISharedLibrary/output.txt


  

我手动创建的 jniLibs 文件夹旁边的 JNI 新>目录。我还添加了 armeabi 文件夹,并把 TestLib.so 里面。我想没有 armeabi ,但没有运气。

我缺少的是在这里吗?我完全失去了!感谢您的帮助!


解决方案

有以下指南一起来看看:
http://www.shaneenishry.com/blog/ 2014年/ 08/17 / NDK与 - 机器人工作室/
我还与.so文件挣扎,但我认为你缺少以下内容:

安卓{

  // .. Android设置..sourceSets.main {
    jniLibs.srcDir'的src / main / myCppLibraries'//< - 在这里设置你的文件夹!
}

}

您是对的貌似有2.4和2.5之间变化
下面的配置工作,以我,我试着用的OpenCL和放大器工作; Andriod的:
的build.gradle(APP)
...

  android.ndk {
MODULENAME =
LDLIBS + =机器人,日志,OpenCL的]
CPPFLAGS + = - I $ {文件。(钢骨混凝土/主/ jniLibs /)}的toString()
LDFLAGS + =-L $ {文件(钢骨混凝土/主/ jniLibs /)}的toString()
}

...

应用程序结构:

 应用
-src
- 主要
--- java的
--- JNI
---- test.c的
--- jniLibs
----libOpenCL.so

使用gradle这个实验性:0.2.0

My problem is that I fail to find some proper documents/guides to follow in order to set up my environment correctly.

Basically I am using Android Studio 1.3.2 (latest stable as of 22nd september 2015) with Gradle Experimental version 2.5.

I followed Google official documentation here : http://tools.android.com/tech-docs/new-build-system/gradle-experimental but I find no help when it comes to JNI Libs. I tried many other solutions but they do not work because they seem to be obsolete.

I have a .so file of a simple C++ library I created to test the NDK support & integration that should be better with this Android Studio 1.3 as announced during Google I/O 2015.

This is how looks my very simple C++ Lib:

Content of TestCPPClass.h

#ifndef TESTCPPCLASS_H
#define TESTCPPCLASS_H

class TestCPPClass {
public:
    double getDouble();
};

#endif TESTCPPCLASS_H

And TestClass.cpp:

#include "TestCPPClass.h"  

double TestCPPClass::getDouble()
{
    return 1; // cannot be dumber ! ^^
}

Using Cygwin terminal and gcc I compiled this very advanced library using the following line :

gcc.exe -shared -o TestLib.so TestCPPClass.cpp

Which did correctly produce a TestLib.so.

In my current Android project, I have a native method that is implemented in JNI as followed :

public static double getDoubleFromJNI();

Using javah.exe I generated the .h file and then implemented the JNI method as follow :

#include "jni.h"
#include "my_package_header.h"
#include "TestCPPClass.h" // => MARKED AS RED because not found ?

JNIEXPORT jdouble JNICALL Java_my_package_header_getDoubleFromJNI
        (JNIEnv *env, jobject caller)
{
     // Here I wish to call my TestLib.so that should contain my TestCPPClass and call "TestCPPClass::getDouble()"
     // in order to return the result.
}

So what about my build.gradle ? This is what I have for NDK inside model{}

android.ndk {
        moduleName = "MyAwesomeJNI"
        ldLibs += "TestLib" => gradle SYNC works, but BUILD fails !
    }

This is the error I get :

Error:Execution failed for task ':app:linkArm64-v8aDebugMyAwesomeJNISharedLibrary'.

A build operation failed. Linker failed while linking libMyAwesomeJNI.so. See the complete log at: file:///C:/Users/Me/Documents/MyProject/app/build/tmp/linkArm64-v8aDebugMyAwesomeJNISharedLibrary/output.txt

I manually created the jniLibs folder alongside jni using New > directory. I also added a armeabi folder and put TestLib.so inside. I tried without armeabi but with no luck.

What am I missing here ? I am totally lost! Thanks for the help!

解决方案

Have a look on the following guide: http://www.shaneenishry.com/blog/2014/08/17/ndk-with-android-studio/ I also struggle with the .so files but I think you are missing the following:

android {

// .. android settings ..

sourceSets.main {
    jniLibs.srcDir 'src/main/myCppLibraries' // <-- Set your folder here!
}

}

You are right looks like there is change between 2.4 and 2.5 The following configuration works to me as I try to work with OpenCL & Andriod: build.gradle (app) ...

android.ndk{
moduleName = ""
ldLibs +=["android", "log", "OpenCL"]
cppFlags+="-I${file("src/main/jniLibs/")}".toString()
ldFlags += "-L${file("src/main/jniLibs/")}".toString()
}

...

application structure:

app
-src
--main
---java
---jni
----test.c
---jniLibs
----libOpenCL.so

Using gradle-experimental:0.2.0

这篇关于摇篮2.5实验:包括JNI .so库和呼叫的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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