如何在 android studio 的 MainActivity.java 中加载静态库 (libtest.a)

how to load a static library(libtest.a) in MainActivity.java in android studio

我有一个静态库 libtest.a (src\main\obj\local\armeabi),我想测试它是否有效。那么现在如何从 android studio(1.1.0),gradle-2.2.1,ndk-crystaXndk 中加载 MainActivity.java 中的静态库 (libtest.a) https://www.crystax.net/en/download) 所以它返回字符串的功能被打印在 VirtualDevice(AVD) 中?因为我使用了 System.Loadlibrary(test) 自动添加后缀 .so 现在我收到一条错误消息说 libtest.so not found。 .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.access0(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;
    }
}

我认为您无法从 Java 文件加载静态库。

Hoewer 将 include $(BUILD_STATIC_LIBRARY) 替换为 include $(BUILD_SHARED_LIBRARY) 并删除了 Android.mk 的末尾。这样您将构建一个共享库 .so,您将能够从 java 文件

加载它