One of the enjoyable aspects of working with the Eclipse IDE is that you rarely have to compile. Anytime you save a file within the Eclipse IDE, your project is built automatically. That is great for the Android SDK (that is, Java) files and the Android XML files, but what about the NDK-built library? Let's find out.
As mentioned, building the native library is as simple as
ndk-build command. However,
when working with a project for anything other than a trivial
exercise, it is a hassle to jump out to a terminal or command
window and execute the
return to the Eclipse environment, and force a refresh by "touching"
one of the project files, which forces a recompile and repackaging of
the complete application. The solution is to extend the Eclipse
environment by customizing the build settings for your Android
To modify the build settings, first view the properties of the Android project and select Builders in the list. Add a new Builder and move it to the top of the list, as shown in Figure 8.
Figure 8. Modifying the build settings
Each builder has four configuration tabs. Give your builder a name, such as Build NDK Library, then populate the tabs. The first tab ("Main") specifies the executable tool location and working directory. Point the location to the ndk-build file and the working directory to your jni folder, as shown in Figure 9.
Figure 9. Setting up the Builder properties for the NDK
You only want the ndk-build to operate on this project and not others within your Eclipse workspace, so set this up on the Refresh tab, as shown in Figure 10.
Figure 10. Setting up the Refresh tab
The only time you want the library to rebuild is when either the Android.mk file or the ibmphotophun.c file is modified. To set this up, choose the jni folder under the Specify Resources button on the Build Options tab. Also, specify when you want the build tool to run by checking off the appropriate times, as shown in Figure 10.
Figure 11. Setting up the build options
After clicking OK to confirm your settings, make sure that this NDK build tool is set up as the first entry in the list by selecting the Up button until it is at the top of the list of Builders, as shown in Figure 7.
To test that your Builder is set up properly, open the ibmphotophun.c source file within Eclipse by right-clicking on the source file and choose to open it with the Text Editor. Make a simple change and save the file. You should see the NDK tool-chain output in the console window, as shown in Figure 11. If your C code has errors they are shown in red.
Figure 12. NDK output shows in the console of the Eclipse IDE
With the NDK stitched into your build process, you can focus on writing code and not concern yourself so much with the build environment. Need to make a change to the application logic? No problem, modify the Java code and save the file. Need to tweak the image processing algorithm? No worries, just modify the C routine and save the file. Eclipse and the ADT plug-in takes care of the rest for you.