1. Background
Android NDK can be used to compile the native method of android. It can also compile the c and c ++ code into a. so file so that android programs can run.
2. Install NDK in linux)
(1) download the corresponding ndk version and enter the following command in shell:
Gedit ~ /. Bashrc
(2) Open the bash file and add the downloaded ndk path
NDKROOT =/home/CORPUSERS/28852262/android-ndk-r9c
Export PATH = $ NDKROOT: $ PATH
(3) enter the following command and restart shell
Source/etc/profile
(4) Input ndk-build in shell. The following status indicates that the installation is successful.
3. jni's first demon helloworld
(1) Write hello-jni.c files.
# Include <string. h>
# Include <jni. h>
/* This is a trivial JNI example where we use a native method
* To return a new VM String. See the corresponding Java source
* File located:
*
* Apps/samples/hello-jni/project/src/com/example/hellojni/HelloJni. java
*/
Jstring
Java_com_example_hellojni_HelloJni_aa (JNIEnv * env,
Jobject thiz)
{
# If defined (_ arm __)
# If defined (_ ARM_ARCH_7A __)
# If defined (_ ARM_NEON __)
# Define ABI "armeabi-v7a/NEON"
# Else
# Define ABI "armeabi-v7a"
# Endif
# Else
# Define ABI "armeabi"
# Endif
# Elif defined (_ i386 __)
# Define ABI "x86"
# Elif defined (_ mips __)
# Define ABI "mips"
# Else
# Define ABI "unknown"
# Endif
Return (* env)-> NewStringUTF (env, "Hello from JNI! Compiled with ABI "ABI ".");
}
This c file outputs a string function. Note the following:
Java_com_example_hellojni_HelloJni_aa
When we create an android Application, the package and function names must correspond to this. Otherwise, the error "native method is not found" is reported. For example
(2) Compile the android. mk file.
(3) Note that the folder name is jni; otherwise, the ndk does not recognize the folder name.
(4) Go to the relevant path in shell and execute
Ndk-build
(5) import the generated libs file and numbered jni to the project.
(6) Check the main program code and note the annotations.
Public class HelloJni extends Activity {
TextView TV;
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_hello_jni );
TV = (TextView) findViewById (R. id. textView1 );
Button bt2 = (Button) findViewById (R. id. button1 );
Bt2.setOnClickListener (new Button. OnClickListener ()
{
@ Override
Public void onClick (View v)
{
TV. setText (aa ());
}
});
}
Public native String aa (); // native method, which corresponds to aa in Java_com_example_hellojni_HelloJni_aa of the c file
Static {
System. loadLibrary ("hello-jni"); // note that the name is libhello-jni.so removing lib and suffix
}
}
(7) Effect
Click the button.
Finally, upload the project. My project is the company's sdk, so you need to replace it with your own at home. Lib and jni are both common, have fun!
Download resources. Available for test!
Free in http://linux.bkjia.com/
The username and password are both www.bkjia.com
The specific download directory is available in/June 17/June 17/Android. The NDK installation and simple jni demon implementation are described in the following figure:
For the download method, see
Recommended reading:
Build the NDK-gdb environment with hello-gl2 (with source code and without source code debugging environment)
Analysis of Android NDK-based development practices
Solve the Problem of No such file or directory in binfiles not compiled by Android NDK
Android NDK: Write a clear code structure
Android development Tutorial: NDK fails to compile the static library