Step-by-step learning ANDROIDNDK Programming (Hello World)

Source: Internet
Author: User

The previous blog, has built the Linux environment under Windows (Cygwine), this time we try to write a Hello world. First need to go to the official website of Android Download ANDROID-NDK compressed package, after decompression, into the extracted directory, we found that there is a Ndk-build script file, this script file is we use cross-compiling files. We run the command through "./ndk-build", such as:


Because we do not compile any C code at this time, we will prompt the error "could not find application project directory", which indicates that our NDK environment is already OK. But if we execute the command "Ndk-build" every time, do we have to go into the NDK's decompression directory and run it? The answer is "no", and we can configure the NDK's environment variables just like configuring Java environment variables.

We open the Cygwine installation directory, my is in the C disk under the Cygwine directory, is also the default directory, in this directory has a etc folder, under the ETC folder has a profile file, we open it

Route the path: "Path="/usr/local/bin:/usr/bin: "We add the NDK's unpacked directory after" Bin: "and mine is under the F: disk, such as: Red is the path of my newly added NDK:


When you are finished, save and then reopen Cygwine, enter "Ndk-build" in any directory to see if the "ndk-build" environment variable is configured.

===============================================================================================


Next, let's write a Hello world, first create a new Android project under Eclipse, here I named "HelloWorld", after creating the project, we need to declare a native method in Java code, as follows:

Public native String Helloworldndk ();
Note here that curly braces cannot be added behind parentheses, because we simply declare and do not implement the method, native the keyword, indicating that the implementation of the method is implemented at the C language level.

Next we need to create a JNI directory under the HelloWorld project. To create a new hello.c file in this directory, before writing the hello.c file, we need to generate the public native String Helloworldndk () with Javah; method corresponding to the header file, because we declare the method in Mainactivity, when we compile with Javac, because of the use of R file, resulting in the generation of bytecode failure, because we just need the. h header file, so we can create a new Java project to generate. h files, such as:


The content in Javahtest.java is the native method we declared in Mainactivty, as follows:

Package Com.test.example;public class Javahtest {public native String helloworldndk ();}
Next, we generate the. h file, and first run the Javac to generate the. Class byte code.

Javac Javahtest.java

Running the above command generates. class bytecode, and next generates. h files, such as:


At this point, a com_test_example_javahtest.h file is generated that reads as follows:

#include <jni.h>/* Header for class Com_test_example_javahtest */#ifndef _included_com_test_example_javahtest# Define _included_com_test_example_javahtest#ifdef __cplusplusextern "C" {#endif/* * Class:     com_test_example_ Javahtest * Method:    helloworldndk * Signature: () ljava/lang/string; */jniexport jstring Jnicall Java_com_test_ EXAMPLE_JAVAHTEST_HELLOWORLDNDK  (jnienv *, jobject); #ifdef __cplusplus} #endif #endif
Among them jstring Jnicall java_com_test_example_javahtest_helloworldndk (jnienv *, jobject);

Is the method we need to declare in hello.c, it is important to note that since our native method is declared in mainactivity, we need to jstring Jnicall Java_com_test_example_ JAVAHTEST_HELLOWORLDNDK (jnienv *, jobject); Javahtest in the Replace with Mainactivity, as follows:

Jstring JAVA_COM_EXAMPLE_HELLOWORLD_MAINACTIVITY_HELLOWORLDNDK (jnienv* env, Jobject obj)

The following is the complete implementation of the HELLO.C code, as follows:

As you can see here we return to the "Hello World" string.


Remember that you need to compile the C file in the Android project, we also need to write the Android.mk file, also in the JNI directory, create a new android.mk file, the contents are as follows:

   Local_path: = $ (call My-dir)   include $ (clear_vars)   local_module    : = Hello   local_src_files: = hello.c   include $ (build_shared_library)
Where Local_module is the name of the module we need to compile, the name is casually named, Local_src_files is the source file we need to compile

When both the hello.c and android.mk files are created, we can compile the hello.c file, open the Cygwine, enter the Android project, and run the "ndk-build" command to generate the libhello.so file, such as:

At the same time, we found that in HelloWorld's Android project, the following files were generated:


The libhello.so file generated under the Libs directory is a binary file that can be executed. We will use the binary in Java code. We are loading the binaries via a static block of code. As follows:

Static{system.loadlibrary ("Hello");}
Note here that the "Hello" here is the value of the Local_module in the Android.mk file.

The next step is to move the binaries generated before the move, and we just need to write this in Mainactivity.java.

Toast.maketext (THIS,HELLOWORLDNDK (), Toast.length_long). Show ();
At this point, when running our HelloWorld project, a toast will pop up showing the string we returned in hello.c.

Here, one of the most basic NDK implementations is done, and now I'm adding a declarative approach:

Public native String Hello_world_ndk ();
So, how should we write about the methods in our hello.c? Refer to the previous wording:

Jstring JAVA_COM_EXAMPLE_HELLOWORLD_MAINACTIVITY_HELLO_WORLD_NDK

It is important to note that this is wrong, and the NDK will assume that we are the inner class hello in Mainactivity and the method NDK in the inner class world in Hello, which is obviously wrong, and the NDK provides a standard for this situation, We need to add the number 1 after each underscore in the method, as follows:

This, we can build through Javah, I create a new Mainactivity.java under the D: disk, the contents are as follows:

public class Mainactivity {public native String hello_world_ndk ();}
Then we go to the D: disk and run the following command to generate the. h file, such as:


At this point in the D: disk generated a MainActivity.h file, the contents are as follows:

Jniexport jstring jnicall java_mainactivity_hello_1world_1ndk  (jnienv *, jobject);
You can see that it is named in this way.

One thing to note here is that if our class has a package name, when using Javah to generate the. h file, first copy the generated. class file to the corresponding package, and then run the following command:

Javah package name. Class name so that you can generate. h files.


So now, with these basics in place, you can generate directly for the native method declared in Mainactivity.java. h header file, cmd into the command line, first into the bin/classes directory of the HelloWorld project, Execute the following command:

Javah com.example.helloworld.MainActivity

At this point, a com_example_helloworld_mainactivity.h file is reborn under the Bin/classes folder, and we copy the file to the JNI directory, such as:


This time, our hello.c can write this:

#include <stdio.h> #include <jni.h> #include "com_example_helloworld_mainactivity.h"/*jstring java_com_ EXAMPLE_HELLOWORLD_MAINACTIVITY_HELLOWORLDNDK (jnienv* env, Jobject obj) {return (*env)->newstringutf (env, "Hello World "); Jstring JAVA_COM_EXAMPLE_HELLOWORLD_MAINACTIVITY_HELLO_1WORLD_1NDK (jnienv* env, Jobject obj) {return (*ENV) Newstringutf (env, "I am from China"); */jniexport jstring jnicall java_com_example_helloworld_mainactivity_helloworldndk  (JNIENV * env, Jobject obj) {  return (*env)->newstringutf (env, "Hello World");} Jniexport jstring jnicall java_com_example_helloworld_mainactivity_hello_1world_1ndk  (JNIENV * env, Jobject obj) {  return (*env)->newstringutf (env, "I am from China");  }

When you run the HelloWorld project, the toast will pop up "Hello World" and "I am from China" one time


To summarize:

1. First you need to declare the native method:

Public native string Helloworldndk ();p ublic native string Hello_world_ndk ();
2. Then use Javah to generate the corresponding. h header file

3. Write the HELLO.C code according to the. h header File

4. Writing the Android.mk file

#交叉编译编译c/c++ code depends on the configuration file # Gets the path of the current android.mk  local_path: = $ (call My-dir) #变量初始化操作include $ (clear_vars) #libhello. In fact, the generated libhello.so is in front of our module name plus lib behind plus. Solocal_module    : = hellolocal_src_files: = Hello.cinclude $ (build_ Shared_library)

5. Introduce binaries in Java by static speed:

static{       system.loadlibrary ("Hello");}

Source Download









Step-by-step learning ANDROIDNDK Programming (Hello World)

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.