问题
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!
回答1:
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
来源:https://stackoverflow.com/questions/32717872/gradle-2-5-experimental-include-so-library-and-call-from-jni