Thursday, 20 August 2009

Android JNI/NDK - notes on getting started

There are a lot of steps required to get your Android development environment set-up, to a state where you can create your first Android "Hello World" app with JNI/NDK.

This is definitely not for the faint-hearted! It is complicated enough that I thought it'd be useful to run through in a blog entry.

So, here is a summary of what to do, in order to get started with using the Android software under both Mac and Windows.

1. Get the latest Sun Java SE Development Kit

Windows: You can get this from here:
http://java.sun.com/javase/downloads/index.jsp

Mac: You should already be up-to-date, assuming you have XCode installed

2. Get Eclipse

I downloaded the latest version of the "Eclipse IDE for Java Developers" from here:
http://www.eclipse.org/downloads/

Windows: Unzip, and then put a shortcut to eclipse.exe somewhere where you can find it!

Mac: Unstuff, and put Eclipse to your Applications folder.

3. Get the base Android SDK (1.5 or later!)

As we're doing to be doing NDK development, make sure you get Android 1.5 or later from here:

http://developer.android.com/sdk/index.html

Make sure you follow the appropriate instructions for installing/upgrading (depending on what current system you might already have)

Windows: When you unzip the file, make sure that it goes under the c:\ root folder... i.e. so it looks like this (for example):

c:\android-sdk-windows-1.5_r3


Mac: Put this under your home folder, so it looks like this (for example):

~/android-sdk-windows-1.5_r3


4. Windows: Get Cygwin

The cygwin toolchain is required for NDK development if you're using Windows.

So, make sure you have cygwin installed! You can get this from here:
http://www.cygwin.org
... note that you'll need the C/C++ developer tool packages.

Mac: users already have a suitable toolchain built-in through XCode.

5. Get the Android NDK

Get the latest Android NDK, from the "Native Development Tools" in the sidebar on in the sidebar on this page:
http://developer.android.com/sdk/index.html

Windows: When you unzip the files, make sure you copy them such that they are under the C:\ root folder, i.e. so that they look like this:

c:\android-ndk-1.5_r1

... if you leave them under e.g. My Documents, then the system will fail to build your source code due to spaces in the file paths etc.

Mac: Put this under your home folder, so it looks like this (for example):

~/android-ndk-1.5_r1


Make sure you follow the instructions that are in DOCS/INSTALL.TXT ...!!!

6. Windows: My Computer - path

Remember to follow instructions on setting your path variable!

Basically, append something like the following (which you'll have to change if you ever come to upgrade)


;c:\android-sdk-windows-1.5_r3;c:\android-ndk-1.5_r1


7. Create your first AVD

You can create your first AVD to be called something like my_android1.5 ... make sure you base it on the Android 1.5 SDK!

You can do this from Eclipse; and more information is available here:
http://developer.android.com/guide/developing/tools/avd.html

Test that you set-up the NDK properly

The following steps will create the following file:

out/apps/hello-jni/android-1.5-arm/libhello-jni.so


Windows:

start cygwin
cd /cygdrive/c/android-ndk-1.5_r1
make APP=hello-jni


Mac:

Run Terminal
cd ~/android-ndk-1.5_r1
make APP=hello-jni


8. Create your first basic Android Project

Now follow the instructions here:

http://developer.android.com/guide/developing/eclipse-adt.html#CreatingAProject


... and if you're lucky, it will build and run, and you can then get on with the JNI/NDK side of things!

9. Create and build/run your first NDK project

You can now create your first NDK project using JNI, and build/run it, following the steps you'll find here:

http://developer.android.com/sdk/ndk/1.5_r1/index.html#samples

Easy!

11 comments:

wjspoel said...

Nice tutorial on how to get started! Helped me a lot.

Greetings
Willem-Jan

Pete Cole said...

Hi Willem-Jan,

I'm very pleased to hear that you found this useful; many thanks for the feedback!

Pete

John Gaby said...
This comment has been removed by the author.
John Gaby said...

Following your tutorial, I was able to get the NDK installed and was able to build the sample Hellojni application. I installed it on the phone simulator, and it seemed to work OK.

However, if I go and change the hello-jni.c file (e.g. change the text string that is returned), my changes to not show up. I rebuilt the shared 'c' library, and the Eclipse project but that didn't help.

Do you have any sage words of advice?

Thanks

Pete Cole said...

Hi John,

Yes, this one is quite easy luckily!

The problem is that Eclipse doesn't fully understand the dependency between your C code/library and your project; one of the many reasons why I don't like developing for Android :)

To work around this, I have configured my copy of Eclipse to use manual build (I have been unable to rely on the automatic rebuilding which is enabled by default...)

So, assuming you have manual rebuilding set-up through Eclipse: whenever you have rebuilt your underlying shared library, you must force your project to rebuild the package and redeploy.

To achieve this, I simply add a space to one of the Java wrapper file sources, remove the space, save the modified file (!) and then select project rebuild from the menu. Your changes should then be picked-up OK!

Hoping this helps, and with best wishes,

Pete

John Gaby said...

Yes, that seems to work, thanks.

I have a second question, if you would be so kind. How the heck do you debug your C/C++ code? I found some help with Google searches, but they seem to be mostly for a Linux environment.

Thanks.

Pete Cole said...

This is where things become almost impossible with Android. Until such time as Google properly integrate C++/C development within the development environment, we're all a bit stuffed and are stuck with trace output (you can review this using adb) and no direct debugging. Which makes things very, very hard (e.g. porting Mixtikl - which is > 250K lines of C++ ... ! ... for which I really need better tools from Google!).

I've tried out many different suggestions on the net, and none of them have allowed me to set breakpoints or see a stack trace that is of any use! If I knew how to do this, I'd certainly spread the word on this blog. :)

If you find a solution, I'd really like to hear of it!

Best wishes,

Pete

John Gaby said...

Sigh, that is what I was afraid of considering the dearth of information about it on the internet.

I have found some information that might be helpful under Linux, and I am considering setting up a virtual Linux machine to try it out. I will let you know if I have any success.

Thanks for all your help.

Ammu said...

nice tutorial...can u tell how to start ndk to get work on ubuntu platform

Ecommerce said...
This comment has been removed by the author.
Ecommerce said...

Thanks for creating this interesting blog and also this information is resourceful one for lots of android companies.

Android app developer