0

I need to call a function which belongs to a native Android code written in C . i just need one function and the native code is huge. What would be the way to achieve this in best possible way?

Manik Mahajan
  • 1,566
  • 2
  • 13
  • 19

1 Answers1

2

Do you mind to disclose the name of the function? I am asking because native non-documented APIs in Android can be crudely divided into three categories: code that is ODM dependent, code that is relatively stable, and code that is unstable or not exported by system libraries.

Regarding the first, you have no choice but to use the device-specific library. Usually, you can download such library from one device, and, not without great care, your library that dynamically links to that system lib, may work on other devices. Typical example is the OpenMAX family of libraries (see for example Creating Android app using OpenMAX library in GB, but showing not found?).

Regarding the second, the purists will download parts of the source tree and compile them into their local shared lib, but the practice of reusing a system lib is widespread (see for example shared memory in android ndk).

For the third, you have no choice but to recompile the AOSP code yourself.

Community
  • 1
  • 1
Alex Cohn
  • 56,089
  • 9
  • 113
  • 307
  • Thanks Alex ,The function i am going to use is ODM specific , for example telephony related functions. Normally Android adds a layer and make upper layers send AT commands , which makes upper layers ODM independent. Now in my case , i need to call the function directly . The code is in C and i was wondering how to go about it .I have complete access to whole source code , including the ODM source code i am taking about.ANd i need to make the call from Java code of a new application. – Manik Mahajan Apr 17 '14 at 08:29
  • So your case falls in category #4: not that _implementation_ is ODM-specific, but the API is ODM-specific. In such case I strongly recommend to build your library or whole app as part of the system build. This way you can safely reuse any part of the system code, including vendor-specific. The big issue with AT commands and other similar functions that interact directly with device hardware, is sharing the resource with other processes and apps. Normally Android provides services that may be used via Binder IPC by different apps. Maybe you, too, want to prepare such layer of abstraction. – Alex Cohn Apr 17 '14 at 10:19
  • My application is also part of the build (apk in system folder). Now how to call the C function from there? should i create a shared lib (.so)of the whole native code and then load it and then call the one fuction through JNI ..or is there a better way. – Manik Mahajan Apr 21 '14 at 05:42
  • As you said, the native code is all there already. Your Java should load the native library that defines this top-level function, and after that it should call (through JNI) this function. You have very little reason to rebuild this source again into a separate library. Maybe, you need to "expose" this function to Java. There are two options: either you use `JNI_OnLoad()` API of Jni, and register the function there, or you use the [JNI naming convention](https://github.com/yixia/FFmpeg-Vitamio/blob/vitamio/build_android_with_librtmp.sh). – Alex Cohn Apr 21 '14 at 20:51