Program for Android in C/C++ with the Native Development Kit (if you dare)

Program for Android in C/C++ with the Native Development Kit (if you dare)

Summary: Not a big fan of Java? Well, get over it, because that's the primary and recommended way to write applications for Android devices.


Not a big fan of Java? Well, get over it, because that's the primary and recommended way to write applications for Android devices. It's portable and... what's that? Android's Dalvik Java VM not fast enough for you? Granted, it's an interpreted engine and as of version 1.5 there's no Just-In-Time compiler. But Dan promises... oh, can't wait for the JIT to come out? Ok, ok, we'll let you program in C if you're really sure.

Introducing the Android Native Development Kit (NDK). With it, you can implement *parts* of your application using native-code languages such as C and C++. You're familiar with the Java Native Interface (JNI), right? JNI lets you load a shared library and call C code from within Java. The NDK lets you compile and build those libraries for the ARM CPU chip used in all *current* Android devices.

The NDK provides:

  • A set of tools and build files used to generate native code libraries from C and C++ sources
  • A way to embed the corresponding native libraries into application packages files (.apks) that can be deployed on Android devices
  • A set of native system headers and libraries that will be supported in all future releases of the Android platform, starting from Android 1.5
  • Documentation, samples, and tutorials

Users downloading your program from the Market will not be able to tell whether or not you used native code. In fact, some apps already on the Market use it. However, native code is not for everyone. Google engineer David Turner writes:

Keep in mind that using the NDK will not be relevant for all Android applications. As a developer, you will need to balance its benefits against its drawbacks, which are numerous! Your application will be more complicated, have reduced compatibility, have no access to framework APIs, and be harder to debug. That said, some applications that have self-contained, CPU-intensive operations that don't allocate much memory may still benefit from increased performance and the ability to reuse existing code. Some examples are signal processing, intensive physics simulations, and some kinds of data processing.

You have been warned. Stay away from the NDK unless you really need it. Try optimizing your Java code first. Try profiling. Acupuncture, cold compresses, anything but... what did you say? "Shut the...," hey, there's no need to be rude. Have at it.

Topics: Software Development, Open Source

Ed Burnette

About Ed Burnette

Ed Burnette is a software industry veteran with more than 25 years of experience as a programmer, author, and speaker. He has written numerous technical articles and books, most recently "Hello, Android: Introducing Google's Mobile Development Platform" from the Pragmatic Programmers.

Kick off your day with ZDNet's daily email newsletter. It's the freshest tech news and opinion, served hot. Get it.


Log in or register to join the discussion
  • 1975 called, wants its SDK back.... sheeesh (nt)

    Johnny Vegas
  • Pity those guys stuck w/ C/CPP

    ... aka dead man walking languages.
    • What's JRE written in again?

      • Good to see.

        It is good to see Google building on the options to program time critical
        operations (things advocates of "managed code" don't believe should
    • Well those dead man languages....

      as you call them are the ones used primarily in OS Development, So unless those other languages can create programs and run them without OS services then you better hope they do not die.
    • Dead for what?

      Android itself uses the Linux kernel, written in C. The Davlik VM, as well as the JRE, are written in C++.

      Nuff said.
      • Yes but you can't touch it.

        Android is open source but is not a open architecture, the pseudo wannabe java in android is a high-level layer, no matter the kernel.
        • You miss the point.

          C/C++ are not dead. They are needed for doing low level stuff, or interacting with hardware, writing kernels, device drivers, virtual machines/interpreters, and general systems level programming.

          Java, C#, Python, and other higher level stuff, can't do those things very well.

          It seems you're equating all programming with application level programming (end user apps, games, productivity, CRUD apps, et al). For most of those things, C/C++ are mostly dead (except for games, and desktp apps that need high speed).

          But until some other language comes along (perhaps D?) that can do the low level, high speed stuff C/C++ can, they're here to stay.
          • well said.

            in the current era of time thins are moving fast. languages comes and go ,platforms born and die ,entire companies can prosper and die in a matter of few years. it is a crazy period for tech world.
            however ,low level programming languages will always stay. they are the languages to make operating systems with and they will stay for years to come ,like forever.
    • Far from dead

      C/C++ is alive and well. There are far more applications written in C++ then any other language, and most other languages are actually written in C/C++. Most games, and pretty much all drivers are written in C/C++.

      It is not common to find many IT professionals with much knowledge of C/C++ anymore because it isn't used in enterprise programming for custom apps as much as languages such as VB and Java. In the mobile world, most phones support Java now. Because Java apps are generally work on any platform that supports Java, most mobile apps have shifted towards it. Not hard to see the advantage there. But if you need to access core services, Java is not the best choice. For that, it is better to use C/C++.

      Not saying it can't be done with Java, but C/C++ is just better suited to it. If you can't write a program in C/C++ doesn't make the language dead. I can't write very much beyond an OK button in C++, but I do respect it for what it is, and what it is for. Without C/C++ I could not program in VB.
  • Android Scripting Environment may be better

    Program and run Android apps directly on an Android handset in BeanShell, Python or Lua(Ruby support in development).
    Tony Agudo
  • RE: Program for Android in C/C with the Native Development Kit (if you dare)

    First; 'Native' sounds odd.
    What is 'Native' stands for ? If stands for C/C++ then it shouldn't be used, 'Native' was first, than came Java.
    Say, Java development, and C/C++ development, say SDK and JDK, and we will understand, which one is which.

    P.S Java does not uses pointers, only null pointers.

    Working with java is like fixing my car engine trough the muffler.
  • RE: Program for Android in C/C with the Native Development Kit (if you dare)

    There is a tool for every job, some tools are better suited to one job than another. In many cases, I use java to prototype an application, then if I need more speed, the logic has basically been done, porting to c++ is easier.
    Java is not a speed demon, in any case, c/c++ will far out pace it, but when one has the need, java can fill the void, as far as java being the language of android, I believe it is best to maintain compatibility across a wide range of uses.
    I can program either one, as necessary.
    If the phone was designed to use a modified version of java to gain inherent portability, then I would suggest that google has a lot more time and developer resources to optimize it's initial design. Other wise we would have 8000000 programmers saying we needed to do it 8000000 ways. That in itself is a moving target and can hamper overall development on android. So, it is my opinion, use what is there, stay focused on the goal of developing apps, as android matures, there will be more optimizations that come out of it's free software stack.
    Play the game use the initial boundries that are established, instead of trying to fit individual agendas into the specification.
    I like c/c++ better myself, but it will not stop me from writing an android app.
  • Thanks

    Good intro article. I had just started messing around with the SDK, and somehow didn't notice the NDK in the docs (I found it now, though). Anyhow, thanks for making it more visible!
  • mwhuaua...

    if you don't code at least your libs in c/cpp and stick with top-level operating-system specific coding, you'll have to start over all your development if you plan to move it on to FUTURE (NOT ANCIENT) systems. stick with languages that don't require interpreters if you plan to develop software that cannot be killed by os designers in a battle of fruit versus penguin. after all we'll end up with multiple open source mobile linux distributions in a couple of years.
    Chris Dankert
  • RE: Program for Android in C/C with the Native Development Kit (if you dare)

    Embedded devices is a place where Android is being use more and more and having the ability to use C/C++ for things like interfacing with custom hardware is a big benefit. C and C++ are far from dead languages, they will disappear the day we no longer use processor that only understand machine code. They will exist at a minimum in the foundation of any computer system. Each language targets a specific programming purpose. Java, C#, etc bring the language closer to the human way of thinking with higher abstractions. The language to use is a compromise on cost, knowledge, complexity, hardware, purpose and to some extend preference. There is no such thing as one size fit all in computer programming.
  • Google people are overpaid

    They took an operating system (Linux) added a GUI and announced Android as an OS developed by them. Java will never match what you can do with pure C, even C++ and Object C are made from C. They use Eclipse as the SDK, they didn't even make their own.

    So lazy that you can plug a debugger to eclipse, and the didn't even do that.

    So mediocre, and then, they expect the programmers community to follow their path.

    Just think of the millions of applications that have to be converted, and not ported. A lot of people are going to stick to apple just because of that, and compare the number of applications apple has, compared to Android.

    Somebody in Google need to be fired, their incompetence is stopping them from becoming number one.
  • RE: Program for Android in C/C with the Native Development Kit (if you dare)

    Its a great news for people as C/C++ is having very important functions library, so it is possible to make use of those functions in android development over here
    Jacob Dixon