2

As per my knowledge, there are 3 methods to compile and use C code into android.

1. NDK : by using JNI : example
2. -static : without using libc library : example
3. dynamic : with using libc library : example

I need to find out best method. Also i need to consider reliable method and require good performance.

Thanks

Nimit
  • 1,714
  • 3
  • 22
  • 33

1 Answers1

3

There are two ways to write an Android application that contains C:

  1. You write an Android application in Java and implement some part of it in C. You call then that part with JNI.

  2. You write an Android application in C/C++ (using the NativeActivity) but as mentioned in the first answer here you will still be calling stuff in Java under the hood.

As far as I know, these are the only standard two ways to have an "application", i.e. something that the normal user can play with.

Of course, you can compile binaries using some toolchain (e.g. the NDK toolchain, or the CodeSourcery toolchain, etc.), but you can only run them from some command-line terminal or from inside some other application: these are binary code running on the device, not full-fledged applications. For some stuff, like services or drivers, that's perfectly fine. For the static vs dynamic:

(a) For the case of NDK, you usually link dynamically, against the basic library of Android (bionic) or any other libs that your device may have in /system/lib. This means that your program stays small.

(b) For some other toolchain that must compile its own libc (or whatever) statically, you get a fat binary, that may load and even execute slower (but that depends on far too many factors, you should test it).

Edit: So, in the end, it depends on what you want from your application:

  • If you want to develop standard Android "applications" that can use the GUI, the WiFi/Bluetooth, etc. use the Java or the C/C++ way, according to your language of preference. Java is more popular, so you will find more documentation and examples for it.

  • If you want to develop something in C/C++ that does not need the capabilities of Android applications (such as a service or a piece of code that interfaces to the hardware), or you want to port some existing C/C++ code, then you can just compile it with an ARM toolchain. For the differences between static and dynamic linking in general, see this question.

Community
  • 1
  • 1
gfour
  • 959
  • 6
  • 9
  • what are the advantages and disadvantages of above two ways?? – Nimit Mar 29 '12 at 06:37
  • edited the answer to better describe some characteristics of the approaches - however, it all depends on what you really want from your program, if you want to do something specific, maybe there can be said more about the possible advantages/disadvantages – gfour Mar 29 '12 at 08:08