我有一個靜態庫libtest.a(src \ main \ obj \ local \ armeabi),我想測試它的工作或不。所以,現在如何在MainActivity.java(android studio(1.1.0),gradle-2.2.1,ndk-crystaXndk https://www.crystax.net/en/download)中加載一個靜態庫(libtest.a),以便返回一個字符串的功能被打印在VirtualDevice(AVD)?由於我已經使用System.Loadlibrary(測試),它會自動添加後綴.so現在我收到一個錯誤,說libtest.so找不到。只有libtest.a文件如何引用.java文件?如何在Android Studio中的MainActivity.java中加載靜態庫(libtest.a)
--------- beginning of crash
06-26 13:17:12.284 1948-1948/com.example.myapplication E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.myapplication, PID: 1948
java.lang.UnsatisfiedLinkError: dalvik.system.PathClassLoader[DexPathList[[zip file "/data/app/com.example.myapplication-2/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]] couldn't find "liblibtest.so"
at java.lang.Runtime.loadLibrary(Runtime.java:366)
at java.lang.System.loadLibrary(System.java:989)
at com.example.myapplication.MainActivity.<clinit>(MainActivity.java:13)
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.Class.newInstance(Class.java:1572)
at android.app.Instrumentation.newActivity(Instrumentation.java:1065)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2199)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)}
Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := test
LOCAL_SRC_FILES := test.cpp
LOCAL_MODULE_FILENAME := libtest
include $(BUILD_STATIC_LIBRARY) //to build this libtest.a and use the same
include $(CLEAR_VARS)
LOCAL_MODULE := super
LOCAL_SRC_FILES := libtest.a
include $(PREBUILT_STATIC_LIBRARY
Application.mk
APP_CFLAGS += -fexceptions
APP_STL := gnustl_static
APP_STL := stlport_static
test.cpp
//#include "test.h"
#include <string.h>
#include <jni.h>
/*char* getString12();
char* Java_com_danfoss_myapplication_Application_getString12()
{ char x[80]="Hello World!!!";
char *s1 = new char [sizeof(x)];
strcpy(s1,x);
return s1;
}*/
extern "C" {
JNIEXPORT jstring JNICALL Java_com_danfoss_myapplication_MainActivity_getString12(JNIEnv *env,
jobject obj)
// { char sk[30]="hello-world!!!";
// const char *v = (const char *)sk;
jstring x="hellllooooo!!!!";
// return (*v)->NewStringUTF(env,"Hello from C++ JNI !");
return x;
}
}
'System.loadLibrary(libtest)'不僅在Android上追加'.so',它還預裝'lib'。因此它正在搜索'liblibtest.so'。 – Robert
感謝您的信息!我知道它搜索libtest.so我想知道一些ndk-build自動搜索libtest.a文件的機制。 – Dee
不,它不是搜索'libtest.so',而是'liblibtest.so'(開頭是兩次lib)。如果你想使用libtest.so將庫的名稱改爲「test」。 – Robert