Step by step create JNI examples for Android

Source: Internet
Author: User

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 !!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.