Android NDK Development Simple Program sharing (Hello word!) _android

Source: Internet
Author: User

In the previous blog has been introduced to you, how to configure the DNK program in WIN environment, this article I will take you to achieve a simple Hello JNI program, so that we really feel the charm of NDK development. Here we choose to use C+java to develop the Android program, first you must understand the C language, the Java language and Linux operating system, which can help you get started, of course not to say you must be Daniel to carry out NDK development, below we come together to implement a simple NDK program.

The first step is to create the Android project

Open development environment Eclipse, on the left blank area, right-click to create Android project (very simple, no longer repeat).

The second step is to compile the header file

Open our main activity and define a native-type approach inside:

public class Mainactivity extends activity {public
 static native String Getstringfromc ();
 @Override
 protected void onCreate (Bundle savedinstancestate) {
 super.oncreate (savedinstancestate);
 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;
 }

After creating our Android project, and defining a good method, then create a new JNI directory in the directory, then click Run input cmd return--> through the CD command to enter the current project directory

And then through the Javah command to generate our header file, let's take a look at the Javah command:

Here we mainly use to D,-jni,-classpath, below we will generate our header file, input at the command line:

javah -classpath bin/classes -d jni com.edu.hpu.jni.MainActivity

This problem occurs because our Calsspath did not find the Android.jar file at compile time, and the error message is as follows:

This problem is well solved, the first way, we add a Android.jar file directory under the environment variable Calsspath, Android.jar file exists in our Android SDK folder, There are a lot of Android versions in the Platforms folder, we choose one at a turn, we find the Android.jar file we need, and then configure the address to the environment variable classpath; The second way we can set a path at the command line:

So our header file is generated, most of our command line window, because then we have to use, complete the above operation, we refresh the project, this is in the JNI directory will be a header file: Com_edu_hpu_jni_ MainActivity.h (file name varies by project), below we create a new. c file under the JNI directory:

#include <stdio.h>
#include <stdlib.h>
#include "com_edu_hpu_jni_mainactivity.h"
Jniexport jstring jnicall JAVA_COM_EDU_HPU_JNI_MAINACTIVITY_GETSTRINGFROMC (jnienv* env, jclass this) {return
 (* ENV)->newstringutf (env, "Hello from JNI!");
}

JAVA_COM_EDU_HPU_JNI_MAINACTIVITY_GETSTRINGFROMC simply say this name, Java_ is a fixed prefix, com_edu_hpu_jni_mainactivity: Main activity Package name + Class name, Getstringfromc: Method name

To prevent us from writing wrong, we open the header file we just generated and copy it from inside:

Here we are about to complete the task, below we will be the previous demo Project Jni directory of the ANDROID.MK copy to our project JNI directory, and then double-click to open:

Local_path: = $ (call My-dir)
include $ (clear_vars)
local_module: = Hello
local_src_files: = hello.c
Include $ (build_shared_library)

Local_module: Refers to our. c file access token; Local_src_files: the. c file that we created under the JNI directory.

Finally we need to make a call in our main activity. Class C Libraries:

public class Mainactivity extends activity {
 static{
 system.loadlibrary ("Hello");
 }
 public static native String GETSTRINGFROMC ();
 private TextView text;
 @Override
 protected void onCreate (Bundle savedinstancestate) {
 super.oncreate (savedinstancestate);
 Setcontentview (r.layout.activity_main);
 Text = (TextView) Findviewbyid (R.id.hello);
 Text.settext (GETSTRINGFROMC ());
 }
 @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;
 }

By doing the above work, we open our Command line window and enter the Ndk-build compilation build. So Class Library

After the operation was successful, we refreshed the project and found that an. So file was added to the obj directory:

Then we click to run the project, so that our first NDK program is completed, and the last one is attached to the picture:

The command line described above generates the. So file is not very troublesome, next please join me to explore how to use the compilation tool eclipse to complete the above operation.

First step: Install NDK

Installation method for details of Android NDK development introduction

Step Two: Configure Eclipse

First we need to configure the NDK environment to eclipse: click window-->perferences:

Then select Android-->ndk--> to add our NDK installation directory:

Here our configuration is basically complete, finally to our project configuration a start ndk-build path, select our project right click Select Properties:

Well, our Eclipse compile NDK program configuration even if we are done, let's go and try it, super convenient.

The above is the entire content of this article, I hope the content of this article for everyone's study or work can bring some help, but also hope that a lot of support cloud Habitat community!

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.