Installation guide of Android ndk r4b development environment¶
(This article is translated from my friend Yarin's blog http://yarin.blog.51cto.com/1130898/382109. I used it as my first lesson of android, but I haven't try the last step "Integrate NDK development in Eclipse". And the recommended ndk version for cocos2d-x is ndk-r5 or higher -- Walzer)
- Installation guide of Android ndk r4b development environment
- Resources:
- Install Android SDK
- 1. JDK
- 2. Eclipse IDE
- 3. Android SDK
- 4. Android Developer Tools
- 5. Open the terminal, use "android list target" to find if there's any Android targets. Then open Eclipse->Menu bar->Window->Android SDK and AVD Manager, select the available packages, install them, as the screenshot below
- 6. Wait until the download & installation finishes. After then, you can check the list in "Install Packges"
- 7. use "android create avd --name sdk_2_2_version --target 8" to create AVD. Then you can see the created AVD in "Virtual Devices". (In fact, we used to call AVD as android simulator)
- Install Android NDK
- Comment
Resources:¶
Note: this article is talking about the installation on Mac OS X. All the softwares must be Mac version.- Andoird SDK: android-sdk-mac_86
- Android NDK: android-ndk-r4b-darwin-x86
- Eclipse
- ADT
- CDT
- ANT
Install Android SDK¶
1. JDK¶
Version 1.5 or higher is required. JDK in built-in the Mac OS. My version is JDK 1.6. You can use "java -version" command to see the version in terminal.
2. Eclipse IDE¶
Please download from Eclipse.org official. My choice is eclipse 3.5.2, unarchive to any path you like, don't need to build and install, just run eclipse.app directly.
3. Android SDK¶
The same to eclipse, just download and unarchive the SDK package. But the one more thing is you need to add the "tools" path into environment PATH variable.
Open terminal, and edit the ~/.bash_profile, add this line:
PATH=${PATH}:/{your-android-sdk-path}/tools
(cocos2d-x supports android 2.0 and higher version, 2.2 & 2.3 is most popular now at 2011 Q2)
4. Android Developer Tools¶
Open Eclipse, click the "Help" menu, find "Install New Software", and add a new location http://dl-ssl.google.com/android/eclipse/ in the "Add Site". After then, you can see "Android DDMS" & "Android Development Tools", select and install. After restart, you can open "Eclipse->Preference", and click "Android" item to set your Android SDK location, as the screenshot below

5. Open the terminal, use "android list target" to find if there's any Android targets. Then open Eclipse->Menu bar->Window->Android SDK and AVD Manager, select the available packages, install them, as the screenshot below¶

6. Wait until the download & installation finishes. After then, you can check the list in "Install Packges"¶

7. use "android create avd --name sdk_2_2_version --target 8" to create AVD. Then you can see the created AVD in "Virtual Devices". (In fact, we used to call AVD as android simulator)¶

Now we can write HelloWorld to test the development environment. In the previous steps, I located Eclipse and Android SDK in "/Developer/Android/" folder

Install Android NDK¶
In the screenshot above, we can see Andorid NDK is intalled into the same folder. The installation of NDK is very easy, just archive the downloaded package to the location. The key is to set the environment variable.
(Again, the recommended ndk version of cocos2d-x development is r5 or higher)
1. set the environment variable¶
run "pico .bash_profile" to open or create .bash_profile file, then input the sources below

click "control+x" and input "y" to check save and exit. In this step, we add sdk/tools and ndk path into environment variable, it will make us more convenient in the future usage. Meanwhile, we set ANDROID_NDK_ROOt & ANDROID_SDK_ROOt variable.
We have finished the NDK installation now. And next I will introduce how to build projects on NDK. Please note that the samples offered by NDK are not located in apps folder any more, but in samples folder instead. For the compatibility, putting project files under apps folder is still OK. Now we see how to build, run and debug
2. Build Hello-Jni¶
Enter "samples/hello-jni"/ folder in NDK, then type the command "ndk-build" to start compilation. If you see the result as the picture below means your installation of SDK & NDK is successful, otherwise you need to check the previous steps.

3. Run Hello-Jni¶
We package the .apk file now. "Ant" is required by making apk file, and it's built-in the mac os x, so we can skip this step.
now use "cd ../" command and go back to the parent folder of hello-jni, then use "android update project --path hello-jni" to update the hello-jni project, just generate a build.xml file for the finally apk package. After the update, you need to enter the root path of project, use "cd hello-jni", and then is the key step, build apk file. use "ant debug". If you see the screenshot below, you're lucky.

Now you can find an .APK file in "samples/hello-jni/bin". It's time to download and debug it on mobile device or simulator.
4. Debug Hello-Jni¶
Since Android NDK r4, we can use GDB to step-debugging JNI codes. (Yes, gdb command line only, no IDE, WTF... -- Walzer). About how to use GDB, there're many articles on the internet, we won't discuss this here.
Enter "samples/hello-jni" folder, then run "ndk-gdb" command. Wait for your mobile device or simulator to run hello-jni. Meanwhile the terminal enters the gdb interface, as the image below. Don't forget to set "debug" to "true" in the xml before you can debug the project.

5. Integrate NDK development in Eclipse¶
// I skipped this paragraph cause I prefer to type build commands in terminal 
Some guys may prefer the notepad to write codes, but why not use the IDE? We can use Eclipse to write ndk projects. At the start we need to config it.
(1) Install CDT, this will make Eclipse convenient to edit C++ sources(2) Anyedit plugin is recommended. Download from http://andrei.gmxhome.de/anyedit/ It's not required, but very useful to edit various types of files.
(3) Create a new project based on NDK. We use hello-jni as the sample.
Do this steps:
- import hello-jni into eclipse
- right-click the project, chose "properties"
- select "Builder" in the left list
- click "New..." button on the right side
- in the list box, select "Program" as the config type; click "OK" button to enter the next step
- set name as "NDK Builder"
- set the Location as "/bin/bash". If you're on windows with cygwin, then use "c:\cygwin\bin\bash.exe" instead.
- set "Working Directory" to "/bin". On cygwin set to "c:\cygwin\bin"
- set "Arguments" to:
--login -c "cd $ANDROID_NDK_ROOT/samples/hello-jni && ndk-build"
Make sure there're two hyphens before login, and the double quotation marks after -c. ANDROID_NDK_ROOT is the full path of NDK. After this steps, the interface should be like this:

check "Refresh resources upon completion"- check "Specific resources"
- click "Specify resources" button, set to your "lib" folder of your project
- check "Recursively include sub-folder"
Here's the finished screen

- check "Allocate Console"
- check "Launch in background"
- check "Run the builder After a Clean"
- check "Run the builder During manual builds"
- check "Run the builder During auto builds"
- check "Specify working set of relevant resources"
- click "Specify Resources" button, set to the JNI folder of your project, and includes all files
The result looks like
Click "OK" button to complete the config. By now, when we modify the c++ source in Eclipse, eclipse will call the ndk-build automatically, output the build info at console, and the .so binary

The integration isn't required, but just for our convenience. There maybe some better approach to fit the project's requirement. We may update this article if meet problems in the development. That's all, thanks!
If you're developing OpenGl ES programs on NDK, you can write and debug in VC on windows, then port it onto ndk. It'll be more convenient to debug c++ codes on windows.
This article is from "Yarin's blog", please retain this entry http://yarin.blog.51cto.com/1130898/382109