Game Development Reference
In-Depth Information
Listing 11-7. Hello-Jni.c Source Code
#include <string.h>
#include <jni.h>
// package = com.robsexample.glhelloworld;
// class = MyGLRenderer
jstring
Java_com_robsexample_glhelloworld_MyGLRenderer_RobsstringFromJNI(JNIEnv* env,
jobject thiz )
{
return (*env)->NewStringUTF(env, "Hello World from JNI and Native Code.");
}
The native C function RobsstringFromJNI() is shown in Listing 11-7. The function creates a new
Java string by calling the NewStringUTF() function and returns the string to the Java caller.
Next, the native code must be compiled using the NDK build system. In order to do this, we have to
start up the Cygwin Unix emulator, which allows you to run Unix commands on your Windows PC,
if you are using a PC for your Android development.
You must navigate using Unix commands to the jni directory that you created previously. Use the
“cd ..” command to change the directory to one directory level up and “cd foldername” to change
the current directory to the folder name. Use the “ls” command to list the files and folders in the
current directory. Use the “pwd” command to get the current directory path you are in.
You will have to go to the root directory and change the directory to the cygdrive/ folder. Then
change the directory to the drive your Android project is stored on, and go to the specific folder
your native source files are in. Once you are in the jni directory that contains your makefile and
the source code, you have to run the ndk-build script from the Android NDK you downloaded
and unzipped to your hard drive. For example, let's say your files are in the /cygdrive/c/
AndroidWorkSpaces/WorkSpace1/MainActivity/jni directory. You can execute the ndk-build
script while in the current directory by typing in the full path to the script, such as /cygdrive/c/
AndroidNDK/andoird-ndk-r9/ndk-build . The build script will then execute and produce the output
shown in Figure 11-8 .
Figure 11-8. Running the ndk-build script in the jni directory
The ndk-build script will process your native code files and package them into a shared library
called libhello-jni.so , which is placed in the libs/armeabi directory. (See Figure 11-9 .)
 
Search WWH ::




Custom Search