JNI + ANDROID:静态方法调用中的UnsatisfiedLinkError

时间:2014-03-15 15:11:38

标签: java android c++ java-native-interface sdl

我收到了UnsatisfiedLinkError,我知道这意味着我的原生方法无法找到要链接的c方法,但我不明白为什么。我这样做的方式与hello-jni样本相同。

以下是我试图调用的Java代码:

static {
       System.loadLibrary("main");
    }

private static DisplayMetrics metrics;
public static native void callMetricDensity();
public static float getMetricDensity() { return metrics.density; }
public static native void callMetricDPIX();
public static float getMetricDPIX() { return metrics.xdpi; }
public static native void callMetricDPIY();
public static float getMetricDPIY() { return metrics.ydpi; }

@Override
    protected void onCreate(Bundle savedInstanceState) {
    ....
    ....
    ....
    metrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);

    callMetricDensity();
    callMetricDPIX();
    callMetricDPIY();
}

这是我的主要来源:

#include <stdio.h>
#include <SDL.h>
#include <jni.h>
#include <android/log.h>

float density, x, y = 0.f;

#define  LOG_TAG    "Logcat"

#define  LOGD(...)  __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)
#define  LOGE(...)  __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)

void
Java_org_libsdl_app_SDLActivity_callMetricDensity(JNIEnv * env, jobject obj)
{
    jclass cls = env->FindClass("org.libsdl.app.SDLActivity");
    jmethodID mid = env->GetStaticMethodID(cls, "getMetricDensity", "()F");
    if(mid == 0) return;
    density = env->CallStaticFloatMethod(cls, mid);
}

void
Java_org_libsdl_app_SDLActivity_callMetricDPIX(JNIEnv * env, jobject obj)
{
    jclass cls = env->FindClass("org.libsdl.app.SDLActivity");
    jmethodID mid = env->GetStaticMethodID(cls, "getMetricDPIX", "()F");
    if(mid == 0) return;
    x = env->CallStaticFloatMethod(cls, mid);
}

void
Java_org_libsdl_app_SDLActivity_callMetricDPIY(JNIEnv * env, jobject obj)
{
    jclass cls = env->FindClass("org.libsdl.app.SDLActivity");
    jmethodID mid = env->GetStaticMethodID(cls, "getMetricDPIY", "()F");
    if(mid == 0) return;
    y = env->CallStaticFloatMethod(cls, mid);
}

int main(int argc, char * argv[])
{
    ....
    ....

    LOGD("(%f, %f, %f)", density, x, y);

    SDL_Delay(10000);

    ....
    ....

    return 0;
}

编辑:错误时添加了Logcat输出。

03-16 14:09:40.601: D/AndroidRuntime(1164): >>>>>> AndroidRuntime START com.android.internal.os.RuntimeInit <<<<<<
03-16 14:09:40.621: D/AndroidRuntime(1164): CheckJNI is ON
03-16 14:09:40.731: D/dalvikvm(1164): Trying to load lib libjavacore.so 0x0
03-16 14:09:40.741: D/dalvikvm(1164): Added shared lib libjavacore.so 0x0
03-16 14:09:40.781: D/dalvikvm(1164): Trying to load lib libnativehelper.so 0x0
03-16 14:09:40.781: D/dalvikvm(1164): Added shared lib libnativehelper.so 0x0
03-16 14:09:40.781: D/dalvikvm(1164): No JNI_OnLoad found in libnativehelper.so 0x0, skipping init
03-16 14:09:41.051: D/dalvikvm(1164): Note: class Landroid/app/ActivityManagerNative; has 179 unimplemented (abstract) methods
03-16 14:09:41.771: E/memtrack(1164): Couldn't load memtrack module (No such file or directory)
03-16 14:09:41.771: E/android.os.Debug(1164): failed to load memtrack module: -2
03-16 14:09:42.151: D/AndroidRuntime(1164): Calling main entry com.android.commands.pm.Pm
03-16 14:09:42.231: D/AndroidRuntime(1164): Shutting down VM
03-16 14:09:42.241: D/dalvikvm(1164): Debugger has detached; object registry had 1 entries
03-16 14:09:43.141: D/AndroidRuntime(1175): >>>>>> AndroidRuntime START com.android.internal.os.RuntimeInit <<<<<<
03-16 14:09:43.151: D/AndroidRuntime(1175): CheckJNI is ON
03-16 14:09:43.251: D/dalvikvm(1175): Trying to load lib libjavacore.so 0x0
03-16 14:09:43.271: D/dalvikvm(1175): Added shared lib libjavacore.so 0x0
03-16 14:09:43.311: D/dalvikvm(1175): Trying to load lib libnativehelper.so 0x0
03-16 14:09:43.311: D/dalvikvm(1175): Added shared lib libnativehelper.so 0x0
03-16 14:09:43.311: D/dalvikvm(1175): No JNI_OnLoad found in libnativehelper.so 0x0, skipping init
03-16 14:09:43.591: D/dalvikvm(1175): Note: class Landroid/app/ActivityManagerNative; has 179 unimplemented (abstract) methods
03-16 14:09:44.351: E/memtrack(1175): Couldn't load memtrack module (No such file or directory)
03-16 14:09:44.351: E/android.os.Debug(1175): failed to load memtrack module: -2
03-16 14:09:44.711: D/AndroidRuntime(1175): Calling main entry com.android.commands.am.Am
03-16 14:09:44.811: I/ActivityManager(383): START u0 {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=org.libsdl.app/.SDLActivity} from pid 1175
03-16 14:09:44.881: E/gralloc_goldfish(52): gralloc_alloc: Mismatched usage flags: 268 x 429, usage 333
03-16 14:09:44.881: W/GraphicBufferAllocator(52): alloc(268, 429, 1, 00000333, ...) failed -22 (Invalid argument)
03-16 14:09:44.881: E/(52): GraphicBufferAlloc::createGraphicBuffer(w=268, h=429) failed (Invalid argument), handle=0x0
03-16 14:09:44.891: E/BufferQueue(383): [ScreenshotClient] dequeueBuffer: SurfaceComposer::createGraphicBuffer failed
03-16 14:09:44.891: W/WindowManager(383): Screenshot failure taking screenshot for (268x429) to layer 21005
03-16 14:09:45.051: D/AndroidRuntime(1175): Shutting down VM
03-16 14:09:45.071: D/dalvikvm(1175): Debugger has detached; object registry had 1 entries
03-16 14:09:45.091: I/ActivityManager(383): Start proc org.libsdl.app for activity org.libsdl.app/.SDLActivity: pid=1186 uid=10051 gids={50051}
03-16 14:09:45.101: D/dalvikvm(1186): Not late-enabling CheckJNI (already on)
03-16 14:09:45.331: I/dalvikvm(1186): CheckJNI enabled: not enabling JNI app bug workarounds.
03-16 14:09:45.791: D/dalvikvm(1186): Trying to load lib /data/app-lib/org.libsdl.app-1/libSDL2.so 0xb3cef250
03-16 14:09:45.831: D/dalvikvm(1186): Added shared lib /data/app-lib/org.libsdl.app-1/libSDL2.so 0xb3cef250
03-16 14:09:45.831: D/dalvikvm(1186): Trying to load lib /data/app-lib/org.libsdl.app-1/libmain.so 0xb3cef250
03-16 14:09:45.841: D/dalvikvm(1186): Added shared lib /data/app-lib/org.libsdl.app-1/libmain.so 0xb3cef250
03-16 14:09:45.841: D/dalvikvm(1186): No JNI_OnLoad found in /data/app-lib/org.libsdl.app-1/libmain.so 0xb3cef250, skipping init
03-16 14:09:45.931: V/SDL(1186): onCreate():null
03-16 14:09:46.211: D/dalvikvm(1186): GC_FOR_ALLOC freed 47K, 4% free 2823K/2940K, paused 38ms, total 39ms
03-16 14:09:46.211: I/dalvikvm-heap(1186): Grow heap (frag case) to 3.298MB for 500416-byte allocation
03-16 14:09:46.251: D/dalvikvm(1186): GC_FOR_ALLOC freed <1K, 4% free 3312K/3432K, paused 32ms, total 32ms
03-16 14:09:46.351: W/dalvikvm(1186): No implementation found for native Lorg/libsdl/app/SDLActivity;.callMetricDensity:()V
03-16 14:09:46.351: D/AndroidRuntime(1186): Shutting down VM
03-16 14:09:46.361: W/dalvikvm(1186): threadid=1: thread exiting with uncaught exception (group=0xb3a33ba8)
03-16 14:09:46.361: E/AndroidRuntime(1186): FATAL EXCEPTION: main
03-16 14:09:46.361: E/AndroidRuntime(1186): Process: org.libsdl.app, PID: 1186
03-16 14:09:46.361: E/AndroidRuntime(1186): java.lang.UnsatisfiedLinkError: Native method not found: org.libsdl.app.SDLActivity.callMetricDensity:()V
03-16 14:09:46.361: E/AndroidRuntime(1186):     at org.libsdl.app.SDLActivity.callMetricDensity(Native Method)
03-16 14:09:46.361: E/AndroidRuntime(1186):     at org.libsdl.app.SDLActivity.onCreate(SDLActivity.java:114)
03-16 14:09:46.361: E/AndroidRuntime(1186):     at android.app.Activity.performCreate(Activity.java:5231)
03-16 14:09:46.361: E/AndroidRuntime(1186):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
03-16 14:09:46.361: E/AndroidRuntime(1186):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
03-16 14:09:46.361: E/AndroidRuntime(1186):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
03-16 14:09:46.361: E/AndroidRuntime(1186):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
03-16 14:09:46.361: E/AndroidRuntime(1186):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
03-16 14:09:46.361: E/AndroidRuntime(1186):     at android.os.Handler.dispatchMessage(Handler.java:102)
03-16 14:09:46.361: E/AndroidRuntime(1186):     at android.os.Looper.loop(Looper.java:136)
03-16 14:09:46.361: E/AndroidRuntime(1186):     at android.app.ActivityThread.main(ActivityThread.java:5017)
03-16 14:09:46.361: E/AndroidRuntime(1186):     at java.lang.reflect.Method.invokeNative(Native Method)
03-16 14:09:46.361: E/AndroidRuntime(1186):     at java.lang.reflect.Method.invoke(Method.java:515)
03-16 14:09:46.361: E/AndroidRuntime(1186):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
03-16 14:09:46.361: E/AndroidRuntime(1186):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
03-16 14:09:46.361: E/AndroidRuntime(1186):     at dalvik.system.NativeStart.main(Native Method)
03-16 14:09:46.381: W/ActivityManager(383):   Force finishing activity org.libsdl.app/.SDLActivity
03-16 14:09:46.691: I/WindowManager(383): Screenshot max retries 4 of Token{b403e0b8 ActivityRecord{b403df58 u0 org.libsdl.app/.SDLActivity t4 f}} appWin=Window{b4041b70 u0 Starting org.libsdl.app} drawState=4
03-16 14:09:46.691: W/WindowManager(383): Screenshot failure taking screenshot for (1280x800) to layer 21010
03-16 14:09:47.251: W/ActivityManager(383): Activity pause timeout for ActivityRecord{b403df58 u0 org.libsdl.app/.SDLActivity t4 f}
03-16 14:09:47.631: W/EGL_emulation(546): eglSurfaceAttrib not implemented
03-16 14:09:48.891: I/Choreographer(546): Skipped 67 frames!  The application may be doing too much work on its main thread.
03-16 14:09:54.881: E/WindowManager(383): Starting window AppWindowToken{b40429a8 token=Token{b403e0b8 ActivityRecord{b403df58 u0 org.libsdl.app/.SDLActivity t4}}} timed out
03-16 14:09:59.131: W/ActivityManager(383): Activity destroy timeout for ActivityRecord{b403df58 u0 org.libsdl.app/.SDLActivity t4 f}

JNI对我来说是全新的,到目前为止它给我带来了很多麻烦。非常感谢任何有关此问题的帮助。

0 个答案:

没有答案