-1

I have .so file which i have created using a c library .Now i want to use .so file in another project for call those functions. I don't know how to use that library. How to call any function from that library? I have tried but found error as Native method not found.I am a beginner with Ndk in android.any help on this..??

I have referred many links those were not solved my problem as Native method not found,

java.lang.UnsatisfiedLinkError: Native method not found,

I am facing the same as this link but it is unanswered. link is Need NDK Help: How to call a C++ function for shared library from another project that uses C++

My Android.mk file is as follows from which i have created .so file

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE    := ShairportSample
LOCAL_SRC_FILES := ShairportSample.cpp
APP_PLATFORM := android-19
include $(BUILD_SHARED_LIBRARY)
Community
  • 1
  • 1
Manmohan Badaya
  • 2,326
  • 1
  • 22
  • 35
  • Did you add that library to your main module as LOCAL_SHARED_LIBRARIES in your Android.mk? Posting your Android.mk could really help here because there is no information about how you handle things. – eozgonul Jan 15 '14 at 08:13
  • @user2359247 mk file of project from which i have created .so file ??..as in another i don't have that file till now. – Manmohan Badaya Jan 15 '14 at 08:54
  • As I guessed, you don't add that shared library to your module. The answer should fix your problem. – eozgonul Jan 15 '14 at 09:39
  • @user2359247 Do i also need .mk file in Android Project.Calling project is not any Ndk project in my case. Is their a need to make it Native project also.? – Manmohan Badaya Jan 15 '14 at 09:52
  • @Manmohan, I guess You have Project A (JNI Project) and A.SO which you have generated by building A. And, above .mk file is of A project. Now, you want have Project B and you want to use A.SO in Project B. Is it the scenario? What is the nature of Project B than? – Kanak Sony Jan 15 '14 at 09:55
  • @KanakSony Yes this is exact scenario. Project B is is Simple Android Project instead of Native. – Manmohan Badaya Jan 15 '14 at 09:57

2 Answers2

0

you can call functions implemented inside the .so you want to reuse from another NDK lib, by setting it as prebuilt shared library your're depending on inside your Android.mk file:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)
LOCAL_MODULE := ShairportSample
LOCAL_SRC_FILES := libs/$(TARGET_CPU_ABI)/libShairportSample.so
LOCAL_EXPORT_C_INCLUDES := PATH_TO_ShairportSample.h
include $(PREBUILT_SHARED_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE    := yournewlib
LOCAL_SRC_FILES := yournewlib.cpp
LOCAL_SHARED_LIBRARIES += ShairportSample
include $(BUILD_SHARED_LIBRARY)

From your Java code you'll have to load ShairportSample lib from your Java before loading yournewlib.

Otherwise, if you want to reuse your .so directly from Java, you should export a Java library (.jar) that uses it. Then you can reuse this Java lib from your other project.

ph0b
  • 14,353
  • 4
  • 43
  • 41
  • I have Project A (JNI Project) and A.SO which i have generated by building A. And, above .mk file is of A project. Now, i have Project B and want to use A.SO in Project B.Where i should add ur .mk file code in my project. – Manmohan Badaya Jan 15 '14 at 10:20
  • please tell me something for my previous comment as i am a newbie with Native Developments. – Manmohan Badaya Jan 15 '14 at 10:51
  • You should put this code into the Android.mk file of your project B. – ph0b Jan 15 '14 at 11:01
  • For this i also need to provide native support to B project.? now Application.mk is also needed? what is the format for values like PATH_TO_ShairportSample.h – Manmohan Badaya Jan 15 '14 at 11:07
  • Yes, but if you want to use it from Java instead it's also possible. In this case you should make a Java library from your Project A as I said at the end of my answer. PATH_TO_ShairportSample can be for example projectA/jni/include – ph0b Jan 15 '14 at 12:32
  • Ok, Now I have exported jar from .so file named shair.jar. Now how i can call methods from this jar of any of .c class. I know how to use normal jar. But with this jar i am not able to see any class from jar from which method can be called. please suggest me. – Manmohan Badaya Jan 15 '14 at 12:46
  • From your ProjectB you should be able to call all the public methods declared by your ProjectA. It's like with a normal jar, it's ProjectA that calls all the C/C++. – ph0b Jan 15 '14 at 13:54
0
  1. first of all, you should make sure the .so file being compiled correctly, named it like this libYourLibName.so, put it into libs/armeabi folder.

  2. secondly, make sure you have loaded the .so file from static block

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

may this will help you

handrenliang
  • 1,047
  • 1
  • 10
  • 21