Use ndk for development without cygwin
Ndk comes with a compiler starting from 7, and does not need to configure the cygwin environment on Windows.
Configure the ndk path in ipvs
In eclipse, click window-perferences in the menu bar to configure the ndk path.
Add native lib to the Project
Create a blank Android project and right-click adnroid tools-add native support in the root directory...
In the displayed dialog box, enter the name of the lib to be created and click Finish. a jni folder is displayed under the project directory, which contains a blank CPP file and an android. mk file.
The content of the android. mk file is
Local_path: = $ (call my-DIR)
Include $ (clear_vars)
Local_module: = testjni
Local_src_files: = testjni. cpp
Include $ (build_shared_library)
Testjni is the name of the lib to be loaded, and local_src_files is the path of the resource file.
Hello JNI
Mainactivity. JavaCodeAs follows:
Package Com. example. testjni; Import Android. OS. Bundle; Import Android. App. activity; Import Android. widget. textview; Public Class Mainactivity Extends Activity {@ override Protected Void Oncreate (bundle savedinstancestate ){ Super . Oncreate (savedinstancestate); textview = New Textview ( This ); Textview. settext (hellofromjni (); setcontentview (textview );} Public Native String hellofromjni (); Static {System. loadlibrary ( "Testjni" );}}
The testjni. CPP Code is as follows:
# Include <JNI. h> Extern " C " Jstring java_com_example_testjni_mainactivity_hellofromjni (jnienv * ENV, jobject thiz ){ Return Env-> newstringutf ( " Hello from JNI " );}
Run directlyProgram, You can see the ndk build output in the console window
One thing to note: Because I use the x86 Android Virtual Machine to run, all of them use the x86 compiler. The compiler method is to create an application. mk file in the JNI directory and enter the following content:
# App_abi: = armeabi
App_abi: = x86
# App_abi: = armeabi armeabi-v7a x86 MIPS mips-r2 mips-r2-sf
# App_abi: = all
App_stl: = stlport_static
For Android phones with common arm processors, app_abi: = armeabi is used. For x86 processors, app_abi: = x86 is used.
If app_abi: = all, The so of all commands will be compiled.
The running result is as follows:
As for how C/C ++ compilation is implemented, let's take a look at the properties of the project.
There are two more items in builders: CDT builder and hierarchical configuration builder.
Add builder for existing projects
If the JNI directory already exists in the project, but the C/C ++ compiler is not configured, the experts can add the Builder by themselves. For me, this cute little cainiao, right-click the project menu, add a new blank so and delete it. Then, CDT builder and deployment configuration builder will be released.