Game Development Reference
In-Depth Information
Generate JNI header files required to cascade information back and forth
between Java and C
Build the static test program
Deploy the files to the device for testing
Let's look at the files in more detail to understand what they do. We'll start with the Java layer.
Main Activity
The 8.70 ch02.project.MainActivity.java file is created by the wizard, and it is the entry point to the
phone application. Listing 2-1 shows the code for this file. There are some remarkable things to note
about this file.
As you should know, when the application starts, the method onCreate(Bundle
savedInstanceState) will be invoked by Android. This method performs three critical steps:
It installs (simply copies) the native library ( libch02.so ) from the assets folder of
the application package to the project's files folder (located in /data/data/
PACKAGE_NAME/files ; the name of the package is ch02.project ). This step is
required before the library can be loaded by JNI.
It loads the native library using System.load(path) .
Finally, it runs the main library sub by invoking the native method
Natives.LibMain(String[] argv) .
Native libraries cannot be loaded within the application package; thus, they must be copied
somewhere in the file system before invoking System.load() . The obvious place would be the
application's file folder (located in /data/data/Package_Name/files ), but the library can be saved
anywhere where permissions allow it.
Caution Shared libraries should not be saved on the SD card, as Android does not allow you to run executable
code from the SD card. Developers that have large shared libraries might be tempted to do so to save some space.
Save yourself some trouble and don't do it.
Note the way the library is installed:
writeToStream(
getAssets().open(LIB),
openFileOutput(LIB, 0)
);
The getAssets().open() method will open a stream from the package assets folder (where the
library will be stored). The writeToStream method will then write the stream using openFileOutput(LIB,
0) , which will open an output stream to the default application folder ( /data/data/ch02.project/files ).
getAssets and openFileOutput are Android system calls, and LIB is the name of the shared library
Search WWH ::




Custom Search