调用移植库的本机函数

时间:2011-05-05 10:47:22

标签: android java-native-interface android-ndk

我一直在关注如何使用Android NDK移植LAME库。 http://blog.libertadtech.com/2011/02/porting-lame-encoder-to-android-arm.html

我得到了libmp3lame.so文件。

现在进行第一次测试我想调用简单函数:

 void get_lame_version(char *strbuf, size_t buflen, const char *prefix);

我为LAME库编写了一个包装器:

package de.goddchen.android.youtubeconverter;

public class LameWrapper {

public static native void get_1lame_1version(char[] buf, int len,
        char[] prefix);
}

我这样说:

public class MainActivity extends Activity {

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

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        char[] buffer = new char[100];
        LameWrapper.get_1lame_1version(buffer, 100, "".toCharArray());
        ((TextView) findViewById(R.id.text)).setText(buffer.toString());
    }

}

但我收到以下错误:

05-05 10:36:11.973: DEBUG/dalvikvm(199): Trying to load lib /data/data/de.goddchen.android.youtubeconverter/lib/libmp3lame.so 0x43757f78
05-05 10:36:12.053: DEBUG/dalvikvm(199): Added shared lib /data/data/de.goddchen.android.youtubeconverter/lib/libmp3lame.so 0x43757f78
05-05 10:36:12.123: DEBUG/dalvikvm(199): No JNI_OnLoad found in /data/data/de.goddchen.android.youtubeconverter/lib/libmp3lame.so 0x43757f78
05-05 10:36:12.573: DEBUG/dalvikvm(199): +++ not scanning '/system/lib/libwebcore.so' for 'get_1lame_1version' (wrong CL)
05-05 10:36:12.693: DEBUG/dalvikvm(199): +++ not scanning '/system/lib/libexif.so' for 'get_1lame_1version' (wrong CL)
05-05 10:36:12.693: DEBUG/dalvikvm(199): +++ not scanning '/system/lib/libFFTEm.so' for 'get_1lame_1version' (wrong CL)
05-05 10:36:12.703: DEBUG/dalvikvm(199): +++ not scanning '/system/lib/libmedia_jni.so' for 'get_1lame_1version' (wrong CL)
05-05 10:36:12.703: DEBUG/dalvikvm(199): +++ not scanning '/system/lib/libsrec_jni.so' for 'get_1lame_1version' (wrong CL)
05-05 10:36:12.703: WARN/dalvikvm(199): No implementation found for native Lde/goddchen/android/youtubeconverter/LameWrapper;.get_1lame_1version ([CI[C)V
05-05 10:36:12.703: DEBUG/AndroidRuntime(199): Shutting down VM
05-05 10:36:12.753: INFO/ActivityManager(52): Start proc com.android.alarmclock for broadcast com.android.alarmclock/.AlarmInitReceiver: pid=213 uid=10017 gids={}
05-05 10:36:12.773: WARN/dalvikvm(199): threadid=3: thread exiting with uncaught exception (group=0x4001aa28)
05-05 10:36:12.783: ERROR/AndroidRuntime(199): Uncaught handler: thread main exiting due to uncaught exception
05-05 10:36:12.883: ERROR/AndroidRuntime(199): java.lang.UnsatisfiedLinkError: get_1lame_1version
05-05 10:36:12.883: ERROR/AndroidRuntime(199):     at de.goddchen.android.youtubeconverter.LameWrapper.get_1lame_1version(Native Method)
05-05 10:36:12.883: ERROR/AndroidRuntime(199):     at de.goddchen.android.youtubeconverter.MainActivity.onCreate(MainActivity.java:19)
05-05 10:36:12.883: ERROR/AndroidRuntime(199):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
05-05 10:36:12.883: ERROR/AndroidRuntime(199):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)
05-05 10:36:12.883: ERROR/AndroidRuntime(199):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2417)
05-05 10:36:12.883: ERROR/AndroidRuntime(199):     at android.app.ActivityThread.access$2100(ActivityThread.java:116)
05-05 10:36:12.883: ERROR/AndroidRuntime(199):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
05-05 10:36:12.883: ERROR/AndroidRuntime(199):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-05 10:36:12.883: ERROR/AndroidRuntime(199):     at android.os.Looper.loop(Looper.java:123)
05-05 10:36:12.883: ERROR/AndroidRuntime(199):     at android.app.ActivityThread.main(ActivityThread.java:4203)
05-05 10:36:12.883: ERROR/AndroidRuntime(199):     at java.lang.reflect.Method.invokeNative(Native Method)
05-05 10:36:12.883: ERROR/AndroidRuntime(199):     at java.lang.reflect.Method.invoke(Method.java:521)
05-05 10:36:12.883: ERROR/AndroidRuntime(199):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
05-05 10:36:12.883: ERROR/AndroidRuntime(199):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
05-05 10:36:12.883: ERROR/AndroidRuntime(199):     at dalvik.system.NativeStart.main(Native Method)

有人可以指出我正确的方法来调用libmp3lame.so的功能吗?

1 个答案:

答案 0 :(得分:5)

遗憾的是,您无法从Java调用任何旧的C函数。你必须使用一个带有" mangled"的函数。命名或使用JNI_OnLoad机制。无论哪种方式,Dalvik VM都会为常规跛脚get_version_number函数不期望的函数调用添加额外的参数。

这个example of using JNI解释了名称错误。对于您的Java类,您需要具有此原型的函数:

void Java_de_goddchen_android_youtubeconverter_LameWrapper_get_11lame_11version
  (JNIEnv *, jclass, jcharArray, jint, jcharArray);

JNIEnv和jclass参数是调用接口的一部分。如果您正在使用C ++,请务必指定extern "C"。 javah工具将为您生成正确的头文件。

如果我这样做,我可能会为我需要的蹩脚电话编写JNI C包装函数。在这种特殊情况下,我不是使用带有长度参数的char *,而是将空char *作为最后一个参数传递,而是创建一个更简单的Java to C接口,它只返回一个Java { {1}}。所以在Java中:

String

然后在C中调用实际的lame API:

package de.goddchen.android.youtubeconverter;

public class LameWrapper {
    public static native String getVersion();
}

然后在Java / Dalvik端控制此处创建的新String的生命周期。