invoke functions of android source library (libmedia)

93 views
Skip to first unread message

André Barbosa

unread,
Mar 9, 2010, 1:18:32 PM3/9/10
to android-platform
Dear All,

I'm working on a project for my BSc in Computer Science. I have to
create a module in C to be invoked by the java through jni.
The goal of this module is call functions from libmedia that aren't
directly accessible in the current Java API.

I already tried copying the code from libmedia to my ndk project, but
the dependencies are a problem! I also tried compile the shared
library in Android Souce and copy the .so to my ndk project, but the
jni can't load the .so library.

I would like know if there is any way of my code c invoke functions of
source library (libmedia) of android? my module is to be invoked by
using the java jni..

Regards,
André Barbosa.

André Barbosa

unread,
Mar 10, 2010, 9:33:24 AM3/10/10
to android-platform
Hi André,

did you correctly add the JNI_OnLoad() to your lib and add the
'static { System.loadLibrary("media") /* without lib, without .so
*/; }'
to your java class?
In the beginning I made the fault to add my lib to LOCAL_LDLIBS
in Android.mk and to AndroidManifest.xml.

_If loading the library fails, adb logcat should give you some hints._

Good luck,
Micha

-------------------------------------

thanks Micha,

I didn't define the JNI_OnLoad () in my c module, I think it isn't
necessary! (but already have in android_media_MediaPlayer.cpp file)
what I have done so far, but without success were:

- in copy the folder /frameworks/base/media/jni to /frameworks/base/
media/libmyjni and add the file c (hello-jni.c) with the following
contents:

#include <string.h>
#include <jni.h>
#include <media/mediaplayer.h>

jstring Java_com_example_hellojni_HelloJni_stringFromJNI( JNIEnv* env,
jobject thiz ) {
return (*env)->NewStringUTF(env, "Hello from JNI !");
}


jstring
Java_com_example_hellojni_AudioStreamPlayer_stringFromJNI( JNIEnv*
env, jobject thiz ) {
return (*env)->NewStringUTF(env, "Hello from JNI !");
}


jstring
Java_com_example_hellojni_AudioStreamPlayer_createPlayer( JNIEnv* env,
jobject thiz ) {
return (*env)->NewStringUTF(env, "A criar o player !");
}


I added "hello-jni.c \" in the LOCAL_SRC_FILES of the Android.mk file,
changed the name of the module libmedia_jni to libmyjni and changed
the name of the module/Soundpool/Android.mk, it already exists with
the same name in the jni folder.

Then I done "make libmyjni" the root of the source code of android and
I have errors in various files: mediaplayer.h, SortedVector.h,
IMediaPlayerService.h, etc. However if i remove the file hello-jni.c
of the module, the project compiles correctly.

The problem is when you include the file #include <media/
mediaplayer.h> that which is strange, because the module jni has the
file android_media_MediaPlayer.cpp that also include #include <media/
mediaplayer.h> and work properly.


After copying the compile file (.so) to my eclipse project , I run the
project and the logcat show me the following error:

D/dalvikvm( 799): Trying to load lib /data/data/com.example.hellojni/
lib/libmyjni.so 0x43735490
I/dalvikvm( 799): Unable to dlopen(/data/data/com.example.hellojni/
lib/libmyjni.so): Cannot find library
W/dalvikvm( 799): Exception Ljava/lang/UnsatisfiedLinkError; thrown
during Lcom/example/hellojni/AudioStreamPlayer;.<clinit>

however i have the libmyjni.so in the folder /data/data/
com.example.hellojni/lib/

Any idea what may be happening?


Regards,
André Barbosa.

Marek Pola

unread,
Mar 11, 2010, 3:28:55 AM3/11/10
to android-platform
As far as I know, adding JNI_OnLoad() is not required any more. I
don't have it in my shared library.

But logcat will surely give you the cause of the problem.

Regards
Marek

André Barbosa

unread,
Mar 11, 2010, 5:51:48 AM3/11/10
to android-platform
thanks Marek,
I solved the problem using the command "strace-v-s 256-p <ur program's
PID>", however, the problem of "include" isn't solved yet... I think
that is a loading module problem , and when i run make, the graph of
the makefile enter in a cycle.

I would like know your opinion about this situation?

PS: Is there any software to test if there is a cyclic dependencies in
makefiles?

Regards,
André Barbosa.

Marek Pola

unread,
Mar 11, 2010, 8:39:03 AM3/11/10
to android-platform
Hello André,

I hope you are aware of the fact that the back slash "\" at the end of
the line means that that line will continue to the next line?

If that's not your problem, try adding a relevant path to
LOCAL_C_INCLUDES. For example, in one of my make files, I have:

LOCAL_C_INCLUDES := $(LOCAL_PATH) \
$(LOCAL_PATH)/../../../external/sqlite/dist

If this doesn't solve it, please post your Android.mk and hopefully
somebody will find why the include doesn't work.

Regards
Marek


On Mar 10, 3:33 pm, André Barbosa <afilipebarb...@gmail.com> wrote:

Reply all
Reply to author
Forward
0 new messages