Complete example of getting started with JNI

Source: Internet
Author: User

MainActivity: [java] package c. c; import android. OS. bundle; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. widget. toast; import android. app. activity;/*** HelloWorld example of JNI * 1 download and install cygwin. important reference: * http://www.cnblogs.com/playing/archive/2011/07/14/2106727.html * pay attention to make component installation, this step does not, but with binutils, gcc, gcc-mingw, gdb * is similar to * 2. Check whether cygwin is correctly installed * enter cygwin and enter cd $ NDKROOT through the command line, if a line related to ndk * configuration in bash_profile is output, the installation is successful. * Note that the input command here is cd $ NDKROOT, so the * Name configured in the bash_profile file should also be NDKROOT. that is, the two are consistent. * 3 compile * 3.1 to enter cygwin, then cd, then space, and then drag the android project to the completion path. * For example, run $ cd/cygdrive/d/workplace/JNITest * 3.2 and then run $ NDKROOT/ndk-build-B. so file ** write a small example of HelloWorld related to JNI, reference: * http://blog.csdn.net/zhangjie201412/article/details/7297899 * Notes: * 1 JNI function name format * Java _ complete Activity path _ method name * For example: * jstring java_c_mainactivity_getstring (JNIEnv * env, jobject jobj) * This issue is also mentioned on the above URL. Pay special attention to * 2 using System. when loadLibrary () is loaded into the native library * the parameter is. the name of the c file, for example, * System. loadLibrary ("HelloWorld") */public class MainActivity extends Activity {private Button mButton; @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); init ();} private void init () {mButton = (Button) findViewById (R. id. button); mButton. setOnClickListener (new OnClickListener () {public void onClick (View v) {String result = getString (); Toast. makeText (MainActivity. this, result, Toast. LENGTH_LONG ). show () ;}) ;}// declare the JNI function public native String getString (); // load the static {System. loadLibrary ("HelloWorld") ;}} HelloWorld. c: [cpp] // HelloWorld. c # include <string. h> # include <jni. h> jstring java_c_mainactivity_getstring (JNIEnv * env, jobject jobj) {return (* env)-> NewStringUTF (env, "HelloWorld, JNI is good");} Android. mk is as follows: [java] LOCAL_PATH: = $ (call my-dir) include $ (CLEAR_VARS) LOCAL_MODULE: = HelloWorld LOCAL_SRC_FILES: = HelloWorld. c LOCAL_LDLIBS + =-llog-ldl include $ (BUILD_SHARED_LIBRARY) Application. mk. mk is as follows: [java] APP_STL: = gnustl_static APP_CPPFLAGS: =-frtti-fexceptions APP_ABI: = armeabi-v7a main. xml is as follows: [html] <RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: tools = "http://schemas.android.com/tools" android: layout_width = "match_parent" android: layout_height = "match_parent"> <Button android: id = "@ + id/button" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_centerHorizontal = "true" android: layout_centerVertical = "true" android: text = "hello JNI"/> </RelativeLayout>

Related Article

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.