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

时间:2015-06-26 08:48:23

标签: java android android-ndk

我有一个静态库libtest.a(src \ main \ obj \ local \ armeabi),我想测试它是否正常工作。那么现在如何在MainActivity.java中加载一个静态库(libtest.a)(android studio(1.1.0),gradle-2.2.1,来自https://www.crystax.net/en/download的ndk-crystaXndk),以便返回一个字符串的功能在VirtualDevice(AVD)中打印?由于我使用了System.Loadlibrary(test),它会自动添加后缀.so现在我收到一条错误,说libtest.so没找到。只有libtest.a文件如何在.java文件中引用?

--------- 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;
    }
}

1 个答案:

答案 0 :(得分:2)

我认为你不能从Java文件加载静态库。

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

加载它