Tôi đang cố gắng tạo một ứng dụng Android để sử dụng thêm một số thư viện * .so (cụ thể là 'libinterface.so'). Những thư viện này được tạo ra bên ngoài, và được bao gồm như là một sự phụ thuộc bên trong một lớp trình bao bọc được gọi là từ phía Java. Thư viện được lưu vào 'src/main/jniLibs/armeabi-v7a'. Hệ thống bao gồm tất cả các tập tin .so vào ứng dụng được tạo ra.Làm thế nào để sử dụng thêm * .so thư viện trên Android Studio và NDK
Trước đây, tôi đã sử dụng Eclipse cho mục đích này và tôi đã có thể sử dụng thư viện này, nhưng tôi có vấn đề để làm điều này với Android Studio.
Các lỗi được tạo ra là:
/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
Khi lỗi được ném bởi các mối liên kết, có vẻ liên quan với các bước hòa nhập của thư viện. Trên Eclipse, tôi đã sử dụng tệp 'Android.mk' để bao gồm thư viện mới, nhưng tôi không thể tìm ra cách để làm điều này bằng 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)
Tôi cố gắng để bao gồm các thư viện với nét gradle này (Lưu ý: Tôi đã bao gồm hỗ trợ cuối cùng JNI và gradle-thực nghiệm using this tutorial):
...
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
}
...
Thư viện này được biên soạn từ bên ngoài sử dụng CMake và công cụ makefile, và nó được biên dịch chéo 'chính xác' cho nền tảng Android (được thử nghiệm với Eclipse và ADT).
tôi đã thực hiện các wrapper như thế này:
// 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
và
// 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
}
Phần header của thư viện trông như thế này:
#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__
Cảm ơn trước.
UPDATE:
Sau this example, tôi đã bao gồm một số khối vào script 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"
}
}
}
}
}
nhưng không hoạt động:
Error: org.gradle.nativeplatform.toolchain.internal.CommandLineToolInvocationFailure: Linker failed while linking libcustom_wrapper_jni.so.
Bạn đặt libinterface.so ở đâu? – shhp
Thư viện được lưu trữ trong 'src/main/jniLibs/armeabi-v7a'. Vì nó có thể là thông tin quan trọng, tôi đã cập nhật câu hỏi. – goe
Các thư viện khác có thể được tải chính xác không? – shhp