What if the Android application accesses files on the Android root file system and cannot access the files due to permission restrictions on the application?
At this point, we need to use NDK programming, access to the system files with the C + + code, and build the local library for Java code in Android, so that you can access the system files in Java code by calling the interfaces of the libraries written by C/C + +.
Why use the NDK?
Generally speaking, mainly divided into the following situations:
1. The protection of the Code, because APK Java layer code is very easy to be decompile, and C + + library reverse sinks difficult.
2. Call the Third-party c/s + + library in NDK, because most open source libraries are written in C + + code.
3. Easy to transplant, the use of C + + writing library can be easily used in other embedded platforms.
Android NDK Primer
The best way to get started is to learn the example of Android, by learning the Android NDK demo program--hello-jni
One, the development environment constructs
NDK development needs to be done under Linux, because the code that is written by C + + + generates a. So file that can be run on arm, which requires a cross compilation environment, and the cross compilation needs to be done under the Linux system.
Install the ANDROID-NDK Development Kit, which can be downloaded from Google's Android website, which allows you to compile the code for the Android JNI/C + + coding into a library
Android application development environment: including Eclipse, Java, Android SDK, ADT, etc., configuration installation See Windows and Ubuntu
Download ANDROID-NDK:NDK
Install Android-ndk:tar JXVF android-ndk-r8b-linux-x86.tar.bz2
Configure ANDROID-NDK:
sudo vi/etc/profile
Export java_home=/home/homer/eclipse/jdk1.7.0_05
Export JRE_HOME=/HOME/HOMER/ECLIPSE/JDK1.7.0_05/JRE
Export ndk_home=/home/homer/android-ndk-r8b
Export classpath=.: $JAVA _home/lib: $JRE _home/lib: $CLASSPATH
Export path= $JAVA _home/bin: $JRE _home/bin: $NDK _home: $PATH
Source/etc/profile
Verify that the configuration is successful:
Ndk-build-v
The following version information pops up:
GNU make 3.81
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software; The source for copying conditions.
There is NO warranty; Not even to merchantability or FITNESS for A
particular purpose.
This program built for X86_64-UNKNOWN-LINUX-GNU
Configuration Successful!
Second, code writing
1. First, write Java code
Build an Android application engineering Hellojni to create a Hellojni.java file:
Hellojni.java:
* * Copyright (C) 2009 the Android Open Source Project * Licensed under the Apache License, Version 2.0 (t
He "License");
* You could not use this file, except in compliance with the License. * Obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * unless R Equired by applicable or agreed into writing, software * Distributed under the License is distributed on a "as
Is ' basis, * without warranties or CONDITIONS of any KIND, either express OR implied.
* The License for the specific language governing permissions and * limitations under the License.
* * Package com.example.hellojni;
Import android.app.Activity;
Import Android.widget.TextView;
Import Android.os.Bundle;
The public class Hellojni extends activity {/** called the ' when ' is the ' The activity ' is the ' the '---' @Override
public void OnCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate);
/* Create a TextView and set its content.
* The text is retrieved by calling a native * function.
* * TextView TV = new TextView (this);
Tv.settext (Stringfromjni ());
Setcontentview (TV); }/* A Native method's implemented by the * ' hello-jni ' native library, which is packaged *
With this application.
*/Public native String Stringfromjni (); /* This is another native method declaration the IS *not* * implemented by ' Hello-jni '. This are simply to show so * you can declare as many native methods with your Java code * as you want, thei r implementation is searched in the * currently loaded native libraries
M. * * Trying to call this function would result in a * Java.lang.UnsatisfiedLinkError exception! */
Public native String Unimplementedstringfromjni (); /* This are used to load the ' Hello-jni ' Library on application * startup. The library has already been unpacked into */data/data/com.example.hellojni/lib/libhello-jni.so at * Inst
Allation time by the package manager.
* * Static {system.loadlibrary ("Hello-jni"); }
}
This code is very simple, the annotation is very clear, here only mention two points::
static {
System.loadlibrary ("Hello-jni");
}
Indicates that the program will load HELLO-JNI when it starts running, and the code declared by the static zone is executed before the OnCreate method. If you have multiple classes in your program, and if the Hellojni class is not the entry for your application, then Hello-jni (the complete name is libhello-jni.so) will be loaded the first time the class is used Hellojni.
Public native String Stringfromjni ();
Public native String Unimplementedstringfromjni ();
You can see the native keyword in the declaration of both methods, which means that both methods are local methods, that is, the two methods are implemented by local code (c + +), in Java code only declarations.
Compiling the project with eclipse generates the corresponding. class file, which must be done before the next step because the. h file is required to use the corresponding. class file, which is in the bin/classes/directory.