Reputation: 1814
I am trying to call a native function from an android activity class, which results in a unsatisfied link error. Here is the activity.
package com.example.rockboos;
import android.os.Bundle;
import android.app.Activity;
import android.widget.Button;
import android.widget.Toast;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
public native void hello();
private void boostMe()
{
hello();
}
private VolumeController volumeController;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.volumeController = new VolumeController(this);
Button btn = (Button)findViewById(R.id.btn0);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//volumeController.boost();
boostMe();
}
});
}
static {
System.loadLibrary("hellomodule");
}
}
native.h
#include <jni.h>
JNIEXPORT void JNICALL Java_com_example_rockboost_MainActivity_hello(JNIEnv *, jobject);
native.c:
#include "mp3gain/mp3gain.h"
#include "native.h"
JNIEXPORT void JNICALL Java_com_example_rockboost_MainActivity_hello(JNIEnv *jniEnv, jobject thiz)
{
//changeGain("/sdcard0/test.mp3", 10, 10);
int x = 0;
x++;
}
catlogs
08-03 17:58:54.040: D/dalvikvm(4007): Trying to load lib /data/app-lib/com.example.rockboost-1/libhellomodule.so 0xb1c88e70
08-03 17:58:54.050: D/dalvikvm(4007): Added shared lib /data/app-lib/com.example.rockboost-1/libhellomodule.so 0xb1c88e70
08-03 17:58:54.050: D/dalvikvm(4007): No JNI_OnLoad found in /data/app-lib/com.example.rockboost-1/libhellomodule.so 0xb1c88e70, skipping init
08-03 17:58:55.700: D/gralloc_goldfish(4007): Emulator without GPU emulation detected. 08-03 17:59:15.040: W/dalvikvm(4007): No implementation found for native Lcom/example/rockboost/MainActivity;.hello:()V
08-03 17:59:15.050: D/AndroidRuntime(4007): Shutting down VM 08-03 17:59:15.050: W/dalvikvm(4007): threadid=1: thread exiting with uncaught exception (group=0xb1a4ed70)
08-03 17:59:15.130: E/AndroidRuntime(4007): FATAL EXCEPTION: main
08-03 17:59:15.130: E/AndroidRuntime(4007): Process: com.example.rockboost, PID: 4007
08-03 17:59:15.130: E/AndroidRuntime(4007): java.lang.UnsatisfiedLinkError: Native method not found: com.example.rockboost.MainActivity.hello:()V
08-03 17:59:15.130: E/AndroidRuntime(4007): at com.example.rockboost.MainActivity.hello(Native Method) 08-03 17:59:15.130: E/AndroidRuntime(4007): at com.example.rockboost.MainActivity.boostMe(MainActivity.java:17) 08-03 17:59:15.130: E/AndroidRuntime(4007): at com.example.rockboost.MainActivity.access$0(MainActivity.java:15) 08-03 17:59:15.130: E/AndroidRuntime(4007): at com.example.rockboost.MainActivity$1.onClick(MainActivity.java:35) 08-03 17:59:15.130: E/AndroidRuntime(4007): at android.view.View.performClick(View.java:4450) 08-03 17:59:15.130: E/AndroidRuntime(4007): at android.view.View$PerformClick.run(View.java:18600) 08-03 17:59:15.130: E/AndroidRuntime(4007): at android.os.Handler.handleCallback(Handler.java:733) 08-03 17:59:15.130: E/AndroidRuntime(4007): at android.os.Handler.dispatchMessage(Handler.java:95) 08-03 17:59:15.130: E/AndroidRuntime(4007): at android.os.Looper.loop(Looper.java:136) 08-03 17:59:15.130: E/AndroidRuntime(4007): at android.app.ActivityThread.main(ActivityThread.java:5026) 08-03 17:59:15.130: E/AndroidRuntime(4007): at java.lang.reflect.Method.invokeNative(Native Method) 08-03 17:59:15.130: E/AndroidRuntime(4007): at java.lang.reflect.Method.invoke(Method.java:515) 08-03 17:59:15.130: E/AndroidRuntime(4007): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777) 08-03 17:59:15.130: E/AndroidRuntime(4007): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602) 08-03 17:59:15.130: E/AndroidRuntime(4007): at dalvik.system.NativeStart.main(Native Method) 08-03 18:04:16.680: I/Process(4007): Sending signal. PID: 4007 SIG: 9 08-03 18:24:16.030: D/dalvikvm(5266): Trying to load lib /data/app-lib/com.example.rockboost-2/libhellomodule.so 0xb1c89f18 08-03 18:24:16.410: D/dalvikvm(5266): Added shared lib /data/app-lib/com.example.rockboost-2/libhellomodule.so 0xb1c89f18 08-03 18:24:16.410: D/dalvikvm(5266): No JNI_OnLoad found in /data/app-lib/com.example.rockboost-2/libhellomodule.so 0xb1c89f18, skipping init 08-03 18:24:17.330: D/gralloc_goldfish(5266): Emulator without GPU emulation detected.
08-03 17:58:54.040: D/dalvikvm(4007): Trying to load lib /data/app-lib/com.example.rockboost-1/libhellomodule.so 0xb1c88e70 08-03 17:58:54.050: D/dalvikvm(4007): Added shared lib /data/app-lib/com.example.rockboost-1/libhellomodule.so 0xb1c88e70 08-03 17:58:54.050: D/dalvikvm(4007): No JNI_OnLoad found in /data/app-lib/com.example.rockboost-1/libhellomodule.so 0xb1c88e70, skipping init 08-03 17:59:15.040: W/dalvikvm(4007): No implementation found for native Lcom/example/rockboost/MainActivity;.hello:()V
The library compiles. I can see in the logs that the shared library could be loaded. However the native call didn't work and caused a unsatified link error
The naming convention seem fine as far as i can see. I can start the application the error only happens when calling the native function. I also made sure that the library is built for the correct cpu/architecture,
I ran objdump -t to check the symbol table:
$ objdump.exe -t libhellomodule.so
libhellomodule.so: file format elf32-little
SYMBOL TABLE:
no symbols
So the function is not in the library but I don't know why. Here is my Android.mk file:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_LDLIBS := -llog
LOCAL_MODULE := hellomodule
LOCAL_SRC_FILES := mp3gain mp3gain/mpglibDBL
LOCAL_C_INCLUDE := mp3gain mp3gain/mpglibDBL native.h
APP_ABI := armeabi
include $(BUILD_SHARED_LIBRARY)
There's probably something wrong with that but i'm not sure where.
Fixed my Android.mk and used another library
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := mp3lame
LOCAL_SRC_FILES := bitstream.c fft.c id3tag.c mpglib_interface.c presets.c quantize.c reservoir.c tables.c util.c VbrTag.c encoder.c gain_analysis.c lame.c newmdct.c psymodel.c quantize_pvt.c set_get.c takehiro.c vbrquantize.c version.c native.c
include $(BUILD_SHARED_LIBRARY)
Compiles fine but with the same result, program cannot find the function in the library. Altough nm shows that the symbol is there:
$ nm -D ../libs/armeabi/libmp3lame.so | grep hello
0001c4e9 T Java_com_example_rockboost_MainActivity_hello
Upvotes: 0
Views: 495
Reputation: 57203
LOCAL_SRC_FILES
should be a list of C files. In your case, it seems to be
LOCAL_SRC_FILES = native.c
When you run ndk-build
, you will see it compiled.
There are other mistakes in your Android.mk.
APP_ABI
is irrelevant there; put it in Application.mk or on command line.
LOCAL_C_INCLUDE
is misspelled; it reads LOCAL_C_INCLUDES. This lists directories where to look for header files. In your case, it may be path from your project to mp3gain
directory.
Upvotes: 1