Android111 calling C code in Java

Source: Internet
Author: User

Mainactivity:

Package Com.itheima.helloworld1;import android.os.bundle;import android.app.activity;import Android.view.View; Import Android.widget.Toast; Public classMainactivity extends Activity {Static{        //load the packaged so class librarySystem.loadlibrary ("Hello");//Hello is the name specified in Abdroid.mk. } @Overrideprotected voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);            Setcontentview (R.layout.activity_main); }    /*# #使用jni 1. Create a JNI folder under the project root directory 2. Create a C file in the Jni file (src is write Java code) 3. In Java code, create a local method HELLOFROMC public n    ative String HELLOFROMC (); 4. In Jni, define the function to implement this method, the function name must be jstring JAVA_COM_ITHEIMA_HELLOWORLD1_MAINACTIVITY_HELLOFROMC (jnienv* env, Jobject obj ) 5.    Returns a string using C to define a string char* CStr = "Hello from c";            6. Convert c string to Java string jstring jstr = (*env)->newstringutf (env, CSTR); return jstr; 7. Create the Android.mk file in Jni 8. Add the <jni.h> header file 9 to the C file. Execute the ndk-build.cmd instruction under the Jni folder (compile with cmd command under the disk Jni folder, after compiling, there is a libhello.so file under the Armeabi folder under Libs, this is the compiled C code) 10. Loading so class libraries in Java code, calling local methods*/         Public voidClick (View v) {Toast.maketext ( This, HELLOFROMC (),0). Show (); }        //defines a local method, the method body is implemented by the C language     Publicnative String Hellofromc (); }

Android.mk

# MK is what make means Local_path:= $ (call my-dir)    include $ (clear_vars)    #编译后生成的文件的类库叫什么名字    Local_ MODULE    := Hello    #要编译的c文件    local_src_files:= hello.c    include $ (build_ Shared_library)

hello.c

#include <stdio.h>#include<stdlib.h>#include<jni.h>//jstring indicates that STRING,JAVA_COM_ITHEIMA_HELLOWORLD1_MAINACTIVITY_HELLOFROMC is the package name Class name Method name//jnienv* env, Jobject obj is a must-write parameter, env is the memory address of the Java virtual machine, the Java code is running on the virtual machine and the C code is not running on the virtual machine,//env is a struct pointer, here * ENV is a two-level pointer to the Java runtime Environment, obj is a Mainactivity object, and C Jobject represents the object. Jstring JAVA_COM_ITHEIMA_HELLOWORLD1_MAINACTIVITY_HELLOFROMC (jnienv*env, Jobject obj) {    //c is not a string, is a character array, char* CStr or char cstr[], the name of the array is the address of the No. 0 element,    Char* CStr ="Hello from C"; //converts a C string into a Java string//jstring (*newstringutf) (jnienv*, const char*);//jstring jstr = (* (*env)). Newstringutf (env, CSTR);Jstring jstr = (*env)Newstringutf (env, CSTR); returnjstr;}

Android111 calling C code in Java

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.