Android JNI Easy Development process

Source: Internet
Author: User

The steps to develop JNI HelloWorld are described in "Android jni HelloWorld", which describes the easy-to-use development process for JNI

① writing Java code native declaring local methods

② Add local support right-click Project->andorid tools->add native Surport

If you find that finish cannot be clicked, you need to configure the location of the NDK directory for the workspace

Window->preferences-> left Select Android->ndk to assign the NDK extracted directory

Right-click the project->andorid tools->add native Surport

Click "Finish" to automatically generate

③ If you are writing a. cpp file, modify the extension of the generated. cpp file first, and don't forget to modify the value Local_src_files in the Android.mk file accordingly

④javah Generate header file copy C's function name to. c file in the generated header file

Run cmd

Switch to Project SRC directory: cd/d F:\workspace_sdk4\02_ simple development process \SRC

Input: Javah com.wuyudong.simpleprocess.MainActivity

Automatically generate "com_wuyudong_simpleprocess_mainactivity.h" files in src

/*Do not EDIT this file-it are machine generated*/#include<jni.h>/*Header for Class com_wuyudong_simpleprocess_mainactivity*/#ifndef _included_com_wuyudong_simpleprocess_mainactivity#define_included_com_wuyudong_simpleprocess_mainactivity#ifdef __cplusplusextern "C" {#endif/** class:com_wuyudong_simpleprocess_mainactivity * METHOD:HELLIFROMC * Signature: () ljava/lang/string;*/ jniexport jstring jnicall JAVA_COM_WUYUDONG_SIMPLEPROCESS_MAINACTIVITY_HELLIFROMC (JNIEnv * , jobject); #ifdef __cplusplus}#endif#endif

Copy one of the code into HELLO.C

⑤ Troubleshooting the CDT plugin error

Right-click on the project selection properties, select the General->paths and Symbols->include tab, click Add. ->file System Select the NDK directory under the platforms folder corresponding platform (minimum version supported by the project)

Problems with code hints and errors will be resolved after arch-arm. Include in the USR directory

⑥ Writing C functions If you need to compile the C code separately, find the small hammer icon in the C + + view

If you want to run directly to the simulator, you don't need a hammer.

Don't forget System.loadlibrary () in ⑦java code;

The final code is as follows:

 Packagecom.wuyudong.simpleprocess;ImportAndroid.os.Bundle;Importandroid.app.Activity;ImportAndroid.view.Menu;ImportAndroid.view.View;ImportAndroid.widget.Toast; Public classMainactivityextendsActivity {Static{system.loadlibrary ("Hello"); } @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);    Setcontentview (R.layout.activity_main); }         Public voidClick (View v) {toast.maketext (Getapplicationcontext (), HELLOFROMC (),0). Show (); }         Public nativeString HELLOFROMC (); }

Android JNI Easy Development process

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.