A simple Demo of Calling C ++ functions in Android

Source: Internet
Author: User

I don't want to explain anything here. For any JNI and NDK content, everyone should go to Baidu or Google. I am just a beginner in Android learning.

1. Create an Android Application Project in Eclipse. The related parameters are as follows:

Application Name : HelloJni; Project Name: HelloJni; Package Name:com.example.hellojni

2. Add a Jni class. The related parameters are as follows:

Source folder: HelloJni/src; Package:com.example.hellojni

3. Add the following two method codes to the Jni. java created above. Note that the keyword native must be added.

package com.example.hellojni;public class Jni { public native String ShowString(String name); public native int Add(int x,int y);}
4. Set Jni. copy the java file to the BIN directory of the JDK installation directory. in WIN7, the path is C: \ Program Files \ Java \ jdk1.7.0 \ bin. then open the console and enter this directory to execute the command as follows. after successful execution, Jni is generated. class.

C:\Program Files\Java\jdk1.7.0\bin>javac Jni.java

5. Copy the com folder under the src file in our Android project to our current directory. set Jni. copy the class file to the com \ example \ hellojni folder. you can delete other content first. run the following command. after successful execution, a file is generated in the current directory. hfile: com_example_hellojni_Jni.h

C:\Program Files\Java\jdk1.7.0\bin>javah -jni com.example.hellojni.Jni

6. Create a folder JNI In the Android project and run. copy the H file to it. create a new. H corresponds. file C: com_example_hellojni_Jni.c. add the following code:

#include"com_example_hellojni_Jni.h"JNIEXPORT jstring JNICALL Java_com_example_hellojni_Jni_ShowString(JNIEnv *pJNIEnv, jobject thiz, jstring str){ char * sTempStr = (char *) (*pJNIEnv)->GetStringUTFChars(pJNIEnv,str,0); return (*pJNIEnv)->NewStringUTF(pJNIEnv,sTempStr);}JNIEXPORT jint JNICALL Java_com_example_hellojni_Jni_Add(JNIEnv *pJNIEnv, jobject thiz, jint x, jint y){  return x+y;}
7. Create an Android. mk file in the JNI folder of the project. The content is as follows:

LOCAL_PATH := $(call my-dir)include $(CLEAR_VARS)LOCAL_MODULE := HelloJNILOCAL_SRC_FILES := com_example_hellojni_Jni.cinclude $(BUILD_SHARED_LIBRARY)

8. Create a file Application. mk in the upper-level directory of the JNI folder of the project. The content is as follows:

APP_PROJECT_PATH := $(call my-dir)/HelloJniAPP_MOUDLES := HelloJNI

9. Switch the console to the directory where the JNI folder is located. Run the command to generate the. SO file. After successful execution, you can see the. SO file libs \ libHelloJNI. so.

C: \ AndroidWorkspace \ HelloJni> F: \ AndroidNdkR8bWindows \ ndk-build.cmd // This Is Your NDK tool.
10. Then we can call the above two function codes in Android as follows:

Package com. example. hellojni; import android. OS. bundle; import android. app. activity; import android. view. menu; import android. widget. textView; public class MainActivity extends Activity {static {System. loadLibrary ("HelloJNI"); // corresponds to libHelloJNI. so. no prefix or suffix is required for loading.} @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); Jni jni = new Jni (); TextView textView = new TextView (this); textView. setText (jni. showString ("sum of two numbers:") + Integer. toString (jni. add (); setContentView (textView );}}

Eleven, the program running results are as follows. The Demo: http://t.cn/z8egX2P

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.