Android application Development using Java to invoke C + + JNI's graphics and text usage _android

Source: Internet
Author: User
Tags function prototype

First of all, we suggest a project Hellojni the following diagram:

Follow the default configuration next until you are done.

Click the Windows menu-"prefrence" menu below:

Pop up the following figure: Select Andriod->ndk:

After the configuration is complete, click the Project Properties menu:

The purpose of this step is to increase support for C + + code, he will automatically generate something that you will see more than one Jni folder.

The name can be used by default, with the name of the project, which is actually the resulting C + + code that generates the name of the. so file (DLL file on Windows).

After the completion. You can look at the code and generate a hellojni. cpp file.

We can write C + + code in this CPP, of course you can add other cpp,.h files, etc.

Assuming we're going to implement a function that returns a string, we first define a class in Java. Clicking the red circle button pops up the Add Class dialog box,

My name is Hellojni.

Package Com.example.hellojni;public class Hellojni {//! Adds a function, such as the following, public native String getString ();}

In fact, in Java to call C + +, we do a class, this class is implemented in C + +. The implementation code is not written in Java, but written in C + +.

Below we need to generate the next. \

You can not run, and then go to the directory of the generated class:

We can see the generated. class file in the entry, as shown below: But we need to go back to the classes directory:

Open the Command Line window via the cmd command line: Write the following command:

In this folder, you can see the generation of an. h file.

This is the corresponding C + + function prototype generated by the Hellojni class above, and we're going to implement this function next. Copy this header file to JNI:

Copy Code code as follows:

#include <jni.h>

#include "com_example_hellojni_hellojni.h"

#ifdef __cplusplus
extern "C" {
#endif
/*
* Class:com_example_hellojni_hellojni
* method:getstring
* Signature: () ljava/lang/string;
*/
Jniexport jstring Jnicall java_com_example_hellojni_hellojni_getstring
(JNIEnv *env, Jobject)
{
Return Env->newstringutf ("Hello from JNI!");
}
#ifdef __cplusplus
}
#endif

Methods to invoke the Hellojni class in Mainactivity.java:

The code is as follows:

Copy Code code as follows:

Package com.example.hellojni;

Import Android.os.Bundle;
Import android.app.Activity;
Import Android.view.Menu;

public class Mainactivity extends activity {

@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);


Hellojni JNI = new Hellojni ();
String sss = jni.getstring ();
SSS = "AAA";


Setcontentview (R.layout.activity_main);
}

@Override
public boolean Oncreateoptionsmenu (Menu menu) {
Inflate the menu; This adds items to the action bar if it is present.
Getmenuinflater (). Inflate (R.menu.main, menu);
return true;
}

}

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.