L1. LAB 1: GETTING FAMILIAR WITH ANDROID SOFTWARE TOOLS 33
{
return (*env)->NewStringUTF(env, "Hello UTD!");
}
is code defines a method that returns a Java string object according to the JNI specifications
with the text Hello UTD! . e naming for this method is dependent on what is called fully
qualified name of the native method which is defined in the MainActivity class. ere are alternate
methods of defining native methods that will be discussed in later labs.
It is important to note that due to a bug currently present in the Gradle build system,
a dummy C source file needs to be created in the jni folder in order for the build process to
complete successfully. Simply create a new source file, named dummy.c for example, without
any code content.
Next, the native method needs to be declared within the MainActivity.java class (see Fig-
ure L1.9) according to the naming used in the C code. To do so, add this declaration below the
onCreate method already defined:
public native String getString();
Now, add the following code within public class to load the native library:
static {
System.loadLibrary("HelloWorld");
}
To use the TextView GUI object, it needs to be imported by adding the following declaration
to the top of the MainActivity.java file:
import android.widget.TextView;
e TextView defined in the GUI layout needs to be hooked to the onCreate method
by adding the following lines to the end of the onCreate method code section (after
setContentView but inside the bracket):
TextView log = (TextView)findViewById(R.id.Log);
log.setText( getString() );
is will cause the text displayed in the TextView to be changed by the second line which calls
the C getString method.
Save the changes and select the Make Project option (located under the Build category
on the main toolbar). Android Studio would display the build progress and notify if any errors
occur. Next, run the app on the Android emulator using the Run app option located in the Run