1

I have a requirement where I need to access native code(inside android os) and use it in my application through jni interface.

For eg: Accessing void NuPlayer::HTTPLiveSource::start() {..} method inside HttpLiveSource.cpp.

I have found out from the from Android - Include native StageFright features in my own project that this is possible using two options:

  1. build your project using android full source tree.
  2. you can just copy include file to your project

I can't use option 1 and so am trying to use option 2... I am new to jni and so want to know:

  • What files to include inside my project?
  • How to access native methods?
  • Is it a future proof approach?(I have seen that source files are moved to different folders depending on the OS version)

Any pointers or detailed explanation will be very helpful.

Community
  • 1
  • 1
anz
  • 1,277
  • 1
  • 11
  • 25

1 Answers1

2

What files to include inside my project?

This depends on your project. Usually, you can use incremental approach: when your compiler complains about missing includes, go to Google source and clone the relevant piece of their Git repository. But it's also OK to download the full AOSP tree.

Note that you will need the non-NDK shared libs to link your .so in the final stage. The easiest way is to pull all libraries from /system/lib from your target device. But you may use the emulator if you prefer.

How to access native methods?

You should build your native code wrapper shared library that uses JNI to communicate with your Java app. Sometimes, this is a thin layer which essentially exposes all the underlying methods in their full beauty to Java. But it's preferable to aggregate the native access (after all, every JNI traversal has its inherent overhead), and come up with a high-level easy-to-use JNI class which will take care of the native objects, threads, you-name-it.

Is it a future proof approach?(I have seen that source files are moved to different folders depending on the OS version)

No, it is not 100% guaranteed to work in the future, or even on one of the devices that you didn't explicitly test. This is true not only for use of native code, but for the undocumented Java APIs as well. OTOH, changes like moving source files to different folders (like what happened around 4.2 for stagefright), does not necessarily cause incompatibility.

I am new to jni

I would say that working with system libraries requires more than basic understanding of JNI and Android NDK. I would strongly suggest to study http://www.apress.com/9781430248279 or similar books before you dive into it.

Alex Cohn
  • 52,705
  • 8
  • 94
  • 269
  • Thanks for the reply bro...removed a lot of confusions..regarding the incremental approach, if I have the source code on my machine and I copy MediaPlayer.cpp file into my project, will it compile if I give the dependencies as absolute paths to the AOSP source present on my machine.. ...And if I dont have the AOSP source on my machine,will I have to include all the dependencies(header files, cpp files) inside my project itself... – anz Dec 27 '13 at 05:37
  • 1
    You probably don't need the system's `MediaPlayer.cpp` in your project, you probably want to use its functionality by linking `-lmedia` and using the necessary include files. It's OK to use these include files via absolute paths to AOSP source tree, but you probably need only a very _sparse_ part of this tree to build, which is essentially same as having all these files copied into your project. The cleanest approach IMHO is to use symbolic links to the AOSP **include** directories somewhere under your **jni** directory. – Alex Cohn Dec 27 '13 at 15:20
  • I am sorry if I sound a bit naive(I am new to this) when I ask if it is possible to call a method of MediaPlayer.cpp(setDataSource..) from java code using jni without including the MediaPlayer.cpp file inside my project..What is -lmedia and what are symbolic links? Confused with a lot of information overflow Thanks :) – anz Dec 29 '13 at 17:09
  • I don't really know where to start… As I said, your task requires more than basic understanding of JNI and Android NDK. Plus basic knowledge of Linux from system and programmer's perspective. These are topics that cannot be covered in a SO answer. There are thick books devoted to them, or lecture courses each few days long. – Alex Cohn Dec 29 '13 at 17:34
  • I have got a thread "https://groups.google.com/forum/#!topic/android-ndk/n5VIQONagTk"..This is what I am looking for...get Back to you with any doubts...Thanks Mate!! – anz Dec 29 '13 at 17:39