Project name: hellofromc
1. Configure the environment we use before starting JNI programming as follows: (in windows)
1. Download The android-ndk-r7b and unzip it in the C: \ android-ndk-r7b directory. (Easy to enter commands or customize Directories)
2. Download cygwin.
During installation, the maximum version is used for installation.
3. Configure the cygwin environment variable of ndk
To facilitate the direct use of ndk-build in the command line
Add the ndk directory to cygwin's environment variable.
Directory/etc/profile/
4. Run cygwin. (Enter the following command: Make-V, GCC-V)
Enter make-V
The GNU make version is displayed, indicating that the Linux compiling environment simulated by cygwin has been successfully simulated.
2. Add C and C ++ development plug-ins for eclipse
CDT plug-in
Install new software in help
Select Helios-http://download.eclipse.org/release/helios
Wait for expansion, select Programing Language
Select C/C ++ development tools
Next.
Agree to licenses
If the installation is successful, you will be prompted if you want to restart eclipse. The installation will be completed after the restart.
Iii. Simple JNI case:
Java code calls a C method to print hellofromc
Mainactivity:
Package com. csdn. Hello;
Import Android. App. activity;
Import Android. OS. Bundle;
Import Android. View. view;
Import Android. widget. Toast;
Public class mainactivity extends activity {
Static {
System. loadlibrary ("hello"); // Load
}
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
}
Public native string hellofromc ();
Public native string hello_from_c ();
Public void onclick (view ){
String result = hellofromc ();
Toast. maketext (this, result, 0). Show ();
}
}
Main. xml:
<? XML version = "1.0" encoding = "UTF-8"?>
<Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: Orientation = "vertical">
<Button
Android: onclick = "onclick"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "hellofromc"/>
</Linearlayout>
CMD Go To The hellofromc directory of the android project in Eclipse:
D: \ workspace \ hellofromc \ bin \ Classes
Input: javah com. csdn. Hello, mainactivity
Generate the header file: com_csdn_hello_mainactivity.h
Create a folder named JNI under the hellofromc root directory;
Copy it to this directory;
Com_csdn_hello_mainactivity.h:
/* Do not edit this file-it is machine generated */
# Include <JNI. h>
/* Header for class com_itheima_hello2_mainactivity */
# Ifndef _ included_com_itheima_hello2_mainactivity
# DEFINE _ included_com_itheima_hello2_mainactivity
# Ifdef _ cplusplus
Extern "C "{
# Endif
/*
* Class: com_itheima_hello2_mainactivity
* Method: hellofromc
* Signature: () ljava/lang/string;
*/
Jniexport jstring jnicall java_com_csdn_hello_mainactivity_hellofromc
(Jnienv *, jobject );
# Ifdef _ cplusplus
}
# Endif
# Endif
Add the Android. mk file to the JNI directory:
# Local_path indicates the path of the current Android. mk File
# $ () Represents a function.
Local_path: = $ (call my-DIR)
# Clear_vars clear the variable
# Clear the content of all variables starting with local _ (data in local_path is not cleared );
Include $ (clear_vars)
# Define the name of the compiled C code library.
Local_module: = Hello
# Define the source file compiled by makefile
# The dependency header file does not need to be specified.
Local_src_files: = Hello. c
# Generate a dynamic code library
Include $ (build_shared_library)
# Include $ (build_static_library); generate a static code library
# Static code library is mainly used to provide some library functions that may need to be used for compiling.
And write the hello. c file:
# Include "com_csdn_hello_mainactivity.h"
Jniexport jstring jnicall java_com_csdn_hello_mainactivity_hellofromc
(Jnienv * ENV, jobject OBJ ){
Return (* env)-> newstringutf (ENV, "hello from C ");
}
Open cygwin
Input CD ..
CD ..
Switch to the linux root directory
Then go to the application directory.
CD cydrive/D/workspace/hellofromc
Ndk-Build:
Automatically generated under D: \ workspace \ hellofromc2 \ OBJ \ Local \ armeabi
Libhello. So file
Finally, load the dynamic library in the static code block in mainactivity, that is, the value of local_module: = Hello variable in Android. mk.
Then deploy the application and click the button to use the toast to print the result hellofromc.