Step 04: Hello World Library (C)
- 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:
- 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
- Click "Finish". A new package will appear under the "Package Explorer".
- Make a folder called "jni" in the root of the project
- Right-click the project node "02HelloWorldNative"
- Select New > Folder)
- Enter "jni" as the Folder Name
- Click Finish
- Create a Makefile for the C code
- Right click on the newly created "jni" folder
- Select New > File
- Enter File Name as "Android.mk"
- Click Finish
- "Android.mk" will be opened for editing
- Type / Copy-Paste the following contents into it:
- LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) # Here we give our module name and source file(s) LOCAL_MODULE := hwnative LOCAL_SRC_FILES := hwnative.c include $(BUILD_SHARED_LIBRARY)
- Save the file by pressing "Ctrl+S".
- Create the C 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:
- #include <string.h>#include <jni.h> jstring Java_com_lithiumhead_android_HelloWorldNative_HelloWorldNativeActivity_invokeNativeFunction(JNIEnv* env, jobject javaThis) { return (*env)->NewStringUTF(env, "Hello from native code!"); }
- Save the file by pressing "Ctrl+S".
- Compile the C file and generate a .so file
- 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.
- Write the Java Code
- In Eclipse, double click on "HelloWorldNativeActivity.java" to edit it and type / copy-paste the following contents into it:
- package com.lithiumhead.android.HelloWorldNative;
- import android.app.Activity; import android.app.AlertDialog; import android.os.Bundle;
- public class HelloWorldNativeActivity extends Activity { //Load the library - name must match the one specified in jni/Android.mk static { System.loadLibrary("hwnative"); } //Declare the native code function - must match the one in hwnative.c private native String invokeNativeFunction(); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //This is where we call the native code String hello = invokeNativeFunction(); new AlertDialog.Builder(this).setMessage(hello).show(); } }
- Save the file by pressing "Ctrl+S".
- In Eclipse, double click on "HelloWorldNativeActivity.java" to edit it and type / copy-paste the following contents into it:
- Wait 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".
References: