Android Ndk:② First NDK program (Create steps & some questions)

Source: Internet
Author: User

Android Ndk:② First NDK program (Create steps & some questions)

This section is: Demo to build the first NDK program

    • Android NDK First NDK program creation steps some questions
      • Create an Android Project
      • Declaring the native method in Java code
      • Create a JNI folder write Ccpp code
      • Add NDK support to your project
      • New Configuration A Builder
      • Loading libraries where the native method was called
      • All right, run the program.
      • Summarize
      • Problems encountered

1. Create an Android Project

Create a new Android application project directly

The native method is declared in the 2.Java code:
 PackageCOM.JAY.EXAMPLE.FIRSTNDK;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.Button;ImportAndroid.widget.Toast; Public  class mainactivity extends Activity {    PrivateButton btnshow;//Declaration native Method     Public nativeStringJavafromjni();@Override    protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);        Setcontentview (R.layout.activity_main);        Btnshow = (Button) Findviewbyid (r.id.btnshow); Btnshow.setonclicklistener (NewOnclicklistener () {@Override             Public void OnClick(View v) {Toast.maketext (mainactivity). This, Javafromjni (), Toast.length_short). Show ();    }        }); }}
3. Create the Jni folder and write the C/cpp code

Here are two places to note:

① return value type

Jni.h file in the pre-defined data type, we can find in the Ndk/platforms/android-xx/arch-arm/user/include directory, ~xx represents the version number
Open Jni.h We'll see a lot of data types, and here's the string:

② method Name: In accordance with the following rules:

Return value type Java_ Package Name _ Class name _native method name (jnienv* env, Jobject obj)

And the parameters inside are fixed, in the jni.h file we can also find that he is a struct called jninativeinterface, the struct defines a lot of data types, we find that return string:

OK, so then we'll finish writing our C code, and we'll create a file called first.c in the JNI directory:

#include<stdio.h>  #include<jni.h>  jstring Java_com_jay_example_firstndk_MainActivity_javaFromJNI(JNIEnv* env, jobject obj) {    return(*env)->"First Blood~");}
4. Add NDK support for your project

Project Right-click->android Tools->add Native support input so library name:

Name the so library yourself:

This time will see that the JNI directory in addition to our own written C files, there are more than two east, Xx.cpp and android.mk files, the CPP can be deleted directly, and then open android.mk pieces, modify the following things:

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

PS: In fact, this step can be done directly in 3, will directly generate a. cpp and ANDROID.MK Files ~

5. New & Configure a Builder

Right-project->properties->builders->new, create a new builder, and click Program,ok! on the popup dialog box.

Double-click to open to configure:

Click OK, OK, OK, then you can customize the compilation code ~ For example, we modify the code after saving, he will recompile.

6. The native method is called where the library is loaded:

7. Well, run the program up ~

8. Summary:

Well, the first NDK program is completed, halfway bump encountered a lot of problems, and finally the smooth run up, the following summary of the approximate steps:

    • Create a common NDK project
    • Declaring the native method
    • To create a JNI folder, you can write your own. C or. cpp file, or directly generate a. cpp and android.mk file directly in step Fourth
    • Add NDK support to your project
    • New & Configure a Builder
    • Loading library (system.loadlibrary (XXX)) where the native method was called
    • Run the program ~ ~

Well, probably the process as described above ~ There may be other quick way to create, follow-up encountered in the introduction of HA ~ finally attached to this program encountered some problems and solutions!

9. Problems encountered

1. After the operation of the program directly died--, and then found that the log was C where the method name is wrong, the direct report method can not find!
Solution: Write in strict accordance with the following provisions
Return value type Java_ Package Name _ Class name _native method name (jnienv* env, Jobject obj)

2. This prompt appears:
Android NDK:WARNING:APP_PLATFORM android-17 is larger than android:minsdkversion
Workaround:
Android-ndk-r8e/build/core/add-application.mk the 128th line to change the __ndk_warning to __ndk_info, and then rebuild the project again to eliminate the error.
Or, just write it like this ... Our company is doing this ... I feel like this is not an error, but not good ...

3. The appearance of writing in C: Method ' Newstringutf ' could not being resolved
Just can't find the method, really wonderful ...
Workaround:

For example, my path is:
E:\NDK\android-ndk-r10d\platforms\android-17\arch-arm\usr\include

4. If you're using. cpp, there's a 3 problem
Workaround: Add the extern "C" modifier before the C + + function for Java calls
Also the statement of the output string is not the same:
(*env)->method (env, XXX) can be changed to Env->method (XXX).

Android Ndk:② First NDK program (Create steps & some questions)

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.