- Connect your cell phone (HTC Desire S) to your computer using a USB cable.
- On your cell phone go to Setting>Application>Development and enable USB debugging.
- Start Eclipse by double clicking its icon on your desktop.
- From the "Window" menu, select "Open Perspective > Other..."
- Select DDMS and click OK.
- Check if your cellphone is listed under the "Devices" view and if the logs are being shown under the "LogCat" view. (In Eclipse, various tabbed panes are called "views")
- Go back to the "Java" perspective by clicking on the "Java" button on the top right corner within Eclipse.
- Select "New > Project..." from "File" menu.
- Select "Android Project" listed under "Android" folder and click "Next >".
- Enter details as follows:
Click "Finish". A new package will appear under the "Package Explorer".
Make a folder called "jni" in the root of the project
- Project Name: 02HelloWorldNative
- Check "Android 2.3.3" as the Build Target
- Application Name: HelloWorldNative
- Package Name: com.lithiumhead.android.HelloWorldNative
- Check "Create Activity" and enter the name as: HelloWorldNativeActivity
- Leave everything else on default
Create a Makefile for the C code
Create the C file
- Right-click the project node "02HelloWorldNative"
- Select New > Folder)
- Enter "jni" as the Folder Name
- Click Finish
Compile the C file and generate a .so file
- Right click on the "jni" folder
- Select New > File
- Enter File Name as "hwnative.c"
- Click Finish
- "hwnative.c" will be opened for editing.
- Type / Copy-Paste the following contents into it:
jstring Java_com_lithiumhead_android_HelloWorldNative_HelloWorldNativeActivity_invokeNativeFunction(JNIEnv* env,
return (*env)->NewStringUTF(env, "Hello from native code!");
- Save the file by pressing "Ctrl+S".
Write the Java CodeWait for the package to be built automatically. Progress indicator will appear on the bottom right within Eclipse along side the message "Building Workspace".Right click "02HelloWorldNative" under "Package Explorer" and select "Run As > 1 Android Application".Unlock your cellphone by clicking the "Power" button on the top side and then sliding your finger over the screen.After the screen unlocks, you will see the HelloWorld App running on you phone.You can observe the installation messages under the "Console" view within Eclipse.To uninstall the "HellowWorldNative" App, on you PC start cmd.exe and issue the command "adb uninstall com.lithiumhead.android.HelloWorldNative".
- Launch Cygwin by selecting it from the Windows Start Menu (it is a shortcut to c:\cygwin\cygwin.bat")
- Use the "cd" command to change to your project folder located inside your Eclipse Workspace folder. (for eg if your project folder is "C:\WorkSpaceEclipseAndroid\02HelloWorldNative", then issue the command "cd /cygdrive/c/WorkSpaceEclipseAndroid/02HelloWorldNative")
- Issue the command to compile the C file: "/cygdrive/c/android-ndk-r6b/ndk-build" (Assuming that you have extracted the NDK here: C:\android-ndk-r6b)
- On successful compilation, a new folder called "libs" will be created in your project folder and the compiled file "libhwnative.so" will be placed in one of its sub folders.
- Switch to Eclipse and right click on the project node "02HelloWorldNative" and select "Refresh". The newly created folders will appear in Eclipse too.