1. Create an activity
Package com. example. mycocos2dbyjni;
Import Android. OS. Bundle;
Import Android. App. activity;
Import Android. View. Menu;
Import Android. View. view;
Import Android. View. View. onclicklistener;
Import Android. widget. Button;
Import Android. widget. textview;
Public class mainactivity extends activity implements onclicklistener {
Private button btntestccanim;
Private textview tview;
@ override
protected void oncreate (bundle savedinstancestate) {
super. oncreate (savedinstancestate);
setcontentview (R. layout. activity_main);
btntestccanim = (button) findviewbyid (R. id. button1);
btntestccanim. setonclicklistener (this);
tview = (textview) findviewbyid (R. id. textview1);
tview. settext ( stringfromjni () ); // call the native method to obtain the string
}
@ Override
Public Boolean oncreateoptionsmenu (menu ){
// Inflate the menu; this adds items to the action bar if it is present.
Getmenuinflater (). Inflate (R. Menu. activity_main, menu );
Return true;
}
@ Override
Public void onclick (view v ){
If (V = btntestccanim ){
// Run cocos2d-x Animation
}
}
Public native string stringfromjni (); // declare the method in C
// Load CC-JNI library. The CC-JNI library is compiled by ndk.
Static {
System. loadlibrary ("cc-JNI ");
}
}
2. the header file of C is generated by javah (see http://tech.ddvip.com/2012-10/1351226397184207.html)
CD to the root directory of the project
Run the following command:
Javah-classpath bin/classes-d jni com. example. mycocos2dbyjni. mainactivity
Generate a. h file (ignore the two files above. H, which will be generated later)
3. Generate a C/C ++ file from the. h file
Failed to create C ++ file in eclipse. I used the ndk exampleCodeThe C file copy of hellojni is changed
Change the method in com_example_mycocos2dbyjni_mainactivity.c to the method declared in. h.
4. Create an android. mk File
The third step is also copy to make changes.
Android. mk file content:
Local_path:=$ (Call my-DIR)
Include $ (Clear_vars)
Local_module:= Cc-JNI
Local_src_files:= Com_example_mycocos2dbyjni_mainactivity.c
Include $ (Build_shared_library)
5. CD to the JNI directory, and then run ndk-build, will generate the libcc-jni.so library file under libs, then run the android project failed again, then I put libc from the sample code. so, the copy is successful !!