goe*_*goe 8 java-native-interface android shared-libraries android-studio gradle-experimental
我正在尝试生成一个Android应用程序来使用一些额外的*.so库(特别是'libinterface.so').这些库是在外部生成的,并作为依赖包含在从Java端调用的包装类中.该库存储在'src/main/jniLibs/armeabi-v7a'中.系统将所有.so文件包含到生成的应用程序中.
以前,我使用Eclipse就是为了这个目的而且我能够使用这个库,但是我在使用Android Studio时遇到了问题.
生成的错误是:
/home/******/Libraries/android-sdk-linux/ndk-bundle/toolchains/aarch64-linux-android-4.9/prebuilt/linux-x86_64/bin/../lib/gcc/aarch64-linux-android/4.9/../../../../aarch64-linux-android/bin/ld: cannot find -linterface
Run Code Online (Sandbox Code Playgroud)
由于链接器抛出错误,它看起来与库包含步骤有关.在Eclipse上,我使用'Android.mk'文件来包含新库,但我找不到使用Gradle执行此操作的方法.
#Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := libinterface-prebuilt
LOCAL_SRC_FILES := prebuilt/libinterface.so
include $(PREBUILT_SHARED_LIBRARY)
Run Code Online (Sandbox Code Playgroud)
我试图用这个gradle定义包含库(注意:我已经包含了最后一个JNI支持和使用本教程的 gradle实验):
...
android.buildTypes {
release {
minifyEnabled = false
proguardFiles.add(file('proguard-android.txt'))
}
}
android.ndk {
moduleName = "custom_wrapper_jni"
cppFlags.add("-I" + file("src/main/jni").absolutePath)
cppFlags.add("-I" + file("../Integration/include").absolutePath) // <- New library header include path
cppFlags.add("-L" + file("src/main/jniLibs/armeabi-v7a").absolutePath) // <- Path where the library is stored
cppFlags.add("-std=c++11")
stl = "stlport_static" // Which STL library to use: gnustl or stlport
ldLibs.add("log")
ldLibs.add("interface") //<- Library to be included
}
...
Run Code Online (Sandbox Code Playgroud)
该库是使用CMake和makefile工具在外部编译的,并且它为Android平台"正确"交叉编译(使用Eclipse和ADT进行测试).
我已经实现了这样的包装:
// custom_wrapper_jni.h
#ifndef ANDROID_JNI_H
#define ANDROID_JNI_H
#include <jni.h>
extern "C"
{
JNIEXPORT jint JNICALL
Java_com_example_goe_android_JniInterface_testFunction(JNIEnv *env,
jobject instance);
}
#endif
Run Code Online (Sandbox Code Playgroud)
和
// custom_wrapper_jni.cpp
#include <custom_wrapper_jni.h>
#include "Interface.h" // Header of the included library
Interface* mInterface = Interface::create(); // Generate the library class instance
JNIEXPORT jint JNICALL
Java_com_example_goe_android_JniInterface_testFunction(JNIEnv *env,
jobject instance)
{
LOGI("Test function called in wrapper!");
return mInterface->test(); // Use the instance
}
Run Code Online (Sandbox Code Playgroud)
该库的标题如下所示:
#ifndef INTERFACE_H__
#define INTERFACE_H__
#include <string>
class Interface
{
public:
static Interface* create();
virtual ~Interface(){}
// Testing function
virtual int test() = 0;
protected:
Interface();
};
#endif // INTERFACE_H__
Run Code Online (Sandbox Code Playgroud)
提前致谢.
在这个例子之后,我在gradle脚本中包含了一些块:
def lib_path = file("src/main/jniLibs").absolutePath
model {
repositories {
libs(PrebuiltLibraries) {
newlibs {
headers.srcDir file("../Integration/include").absolutePath
binaries.withType(SharedLibraryBinary) {
sharedLibraryFile = file("${lib_path}/${targetPlatform.getName()}/libinterface.so")
println "Included libraries: " + file("${lib_path}/${targetPlatform.getName()}/libinterface.so")
}
}
}
}
android {
...
}
android.sources {
main {
jni {
dependencies {
library "newlibs" linkage "shared"
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
但是没有用:
Error: org.gradle.nativeplatform.toolchain.internal.CommandLineToolInvocationFailure: Linker failed while linking libcustom_wrapper_jni.so.
Run Code Online (Sandbox Code Playgroud)
好的,可能有两个不同的问题.
首先,您必须确保为正确的体系结构编译库.如果您使用的是armeabi-v7a库,但编译器正在尝试加载armeabi库,则编译将失败.
其次,在第一个问题之后,您必须根据使用的体系结构包含库.在模块build.gradle脚本中使用' flavors '配置.
例如,您可以尝试执行以下操作:
android.productFlavors {
create("arm") {
ndk.with{
abiFilters.add("armeabi")
File curDir = file('./')
curDir = file(curDir.absolutePath)
String libsDir = curDir.absolutePath + "/src/main/jniLibs/armeabi/"
ldLibs.add(libsDir + "libinterface.so")
}
}
create("armv7") {
ndk.with {
abiFilters.add("armeabi-v7a")
File curDir = file('./')
curDir = file(curDir.absolutePath)
String libsDir = curDir.absolutePath + "/src/main/jniLibs/armeabi-v7a/"
ldLibs.add(libsDir + "libinterface.so")
}
}
}
Run Code Online (Sandbox Code Playgroud)
此外,我建议您使用'jniLibs'来存储库,因为它是它们的默认路径,但每个arch使用不同的文件夹.
你可以检查像其他的例子这样.
希望这可以帮助.问候.
| 归档时间: |
|
| 查看次数: |
2574 次 |
| 最近记录: |