Invoking JNI functions in Android package name containing underscore
Asked Answered
P

2

32

I am trying to implement MP3 encoding in Android using the LAME library following these guides: Lame MP3 Encoder compile for Android http://developer.samsung.com/android/technical-docs/Porting-and-using-LAME-MP3-on-Android-with-JNI

However I am getting a java.lang.UnsatisfiedLinkError which I believe might be due to the fact that my package name contains an underscore which it interprets as a full stop .

Looking at my code below is this likely this issue and how do I get around that. Or is there something else causing this. Thanks in advance for any help.

Record.java:

package co.uk.ing_simmons.aberdeensoundsites;

public class Record extends Activity implements OnClickListener {

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

private native void initEncoder(int numChannels, int sampleRate, int bitRate, int mode, int quality);

private native void destroyEncoder();

private native int encodeFile(String sourcePath, String targetPath);

[.....]
}

wrapper.c:

void Java_co_uk_ing_simmons_aberdeensoundsites_Record_initEncoder(JNIEnv *env,
        jobject jobj, jint in_num_channels, jint in_samplerate, jint in_brate,
        jint in_mode, jint in_quality) {
[....]

Full log cat error:

04-17 20:58:36.009: E/AndroidRuntime(26768): FATAL EXCEPTION: main
04-17 20:58:36.009: E/AndroidRuntime(26768): java.lang.UnsatisfiedLinkError: initEncoder
04-17 20:58:36.009: E/AndroidRuntime(26768):    at co.uk.ing_simmons.aberdeensoundsites.Record.initEncoder(Native Method)
04-17 20:58:36.009: E/AndroidRuntime(26768):    at co.uk.ing_simmons.aberdeensoundsites.Record.onCreate(Record.java:79)
04-17 20:58:36.009: E/AndroidRuntime(26768):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-17 20:58:36.009: E/AndroidRuntime(26768):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
04-17 20:58:36.009: E/AndroidRuntime(26768):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
04-17 20:58:36.009: E/AndroidRuntime(26768):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
04-17 20:58:36.009: E/AndroidRuntime(26768):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
04-17 20:58:36.009: E/AndroidRuntime(26768):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-17 20:58:36.009: E/AndroidRuntime(26768):    at android.os.Looper.loop(Looper.java:123)
04-17 20:58:36.009: E/AndroidRuntime(26768):    at android.app.ActivityThread.main(ActivityThread.java:3687)
04-17 20:58:36.009: E/AndroidRuntime(26768):    at java.lang.reflect.Method.invokeNative(Native Method)
04-17 20:58:36.009: E/AndroidRuntime(26768):    at java.lang.reflect.Method.invoke(Method.java:507)
04-17 20:58:36.009: E/AndroidRuntime(26768):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
04-17 20:58:36.009: E/AndroidRuntime(26768):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
04-17 20:58:36.009: E/AndroidRuntime(26768):    at dalvik.system.NativeStart.main(Native Method)
Promulgate answered 17/4, 2013 at 20:3 Comment(0)
O
59

You should follow the underscore with the number 1. So if your package name contains ing_simmons then your JNI would be formed like so.

void Java_co_uk_ing_1simmons_aberdeensoundsites_Record_initEncoder

This is true also if you have underscores in any other part of the call, such as class name or method name in the Java file.

Osmond answered 17/4, 2013 at 20:28 Comment(2)
Is this just a JNI convention? If so, do you have a link that mentions this?Lucid
qscribble.blogspot.co.uk/2012/04/… and I vaguely remember reading the target of the deadlink mentioned therein. That it's removed now, several people say simply don't do it and javah has just avowed to me that mine is "not a valid class name", means I'm tempted to drop the underscore from my next package name.Coralline
S
20

JNI spec quote on the _1 rule

The _1 rule scriptocalypse mentions is part of the JNI spec 8 Chapter 2: Design Overview - Resolving Native Method Names:

Escape Sequence     Denotes
_1                  the character “_” 
Skilling answered 26/1, 2016 at 14:1 Comment(1)
Unicode _0005F would also work, even if _1 suits better.Wench

© 2022 - 2024 — McMap. All rights reserved.