JNI (i)--helloworld of Android development and error resolution encountered

Source: Internet
Author: User

JNI (i)--helloworld of Android development and error resolution encountered
1.NDK Environment Construction Reference http://blog.csdn.net/xiaoliouc/article/details/8705560
2.HelloWorld Write (1) Create a new Android project Jnidemo, the name can be casually up;
(2) Create a new Helloworld.java class with the following contents:
public class HelloWorld {public        native String print ();        static {        system.loadlibrary ("Hello-jni");}    }

(3) called in the Mainactivity.java class as follows:
Private TextView Mtextview;        @Override    protected void onCreate (Bundle savedinstancestate) {        super.oncreate (savedinstancestate);        Setcontentview (r.layout.activity_main);                Mtextview = (TextView) Findviewbyid (R.id.text_view);        String HelloWorld = new HelloWorld (). print ();        Mtextview.settext (HelloWorld);    }

(4) Open cmd, enter the directory of the project Helloworld.java class, run the following command Javac Helloworld.java generated a Helloworld.class file (5) Return to the project Src directory with cmd and run the following commandJavah Com.example.jnidemo.HelloWorld,This generates acom_example_jnidemo_helloworld.hFiles can also be used with commandsjavah-classpath.-jni Com.example.jnidemo.HelloWorldJavah-classpath. Com.example.jnidemo.HelloWorldBut must be in the SRC directory
(6) Right click on the project name, the new folder, named Jni, copy this two files from the Android NDK directory,android-ndk\samples\hello-jni\jni\application.mkandroid-ndk\samples\hello-jni\jni\android.mkThen rename the com_example_jnidemo_helloworld.h to HelloWorld.h and copy it to the JNI directory at will.
(7) Write the H file implementation file C, where the implementation of the file is HELLOWORLD.C, the content is as follows: (8) Open cygdrive, go to the root directory of the project, run the following command$NDK/ndk-build, $NDK changed to their own name; refresh the project after completion, the contents of the Obj folder is more
#include  <string.h> #include <jni.h> #include "HelloWorld.h" Jniexport jstring jnicall java_com_ Example_jnidemo_helloworld_print  (jnienv* env, Jobject thiz) {return (*env)->newstringutf (env, "Hello world!");}

(9) The Android.mk file is configured as follows:
Local_path: = $ (call My-dir) include $ (clear_vars) local_module    : = hello-jnilocal_src_files: = Helloworld.cinclude $ (build_shared_library)
Local_module the values behind and Ystem.loadlibrary ("Hello-jni"), the string inside is consistent, indicating that the module library to be loaded local_src_files the value of the source file to be compiled after compiling the project, get the desired results

3. Summary of some of the issues that may be encountered during the initial preparation of the NDK: 1.Type ' * * * * * ' could not being resolvedMethod ' ****** ' could not being resolved et ceteraWorkaround: Left-properties->c/c++ general->paths and symbols->includes->add-> add D:\adt-bundle-windows\ Android-ndk\platforms\android-19\arch-arm\usr\include, the path of the NDK
2. If also reported 1 error, Window->show View->problems, clear all errors, so it's OK. Sometimes after it is done, the switch between several files, and will report such a mistake, the practice continues 2.
3.Android NDK:WARNING:APP_PLATFORM android-14 is larger than android:minsdkversion 8

This is because the version specified by the NDK is inconsistent with minsdkversion, which is a warning in itself, but it compiles an error under Eclipse, so how do I fix it?

Only needed in your project:

Application.mk

Add the following code to the file:

[CPP]View Plaincopy
    1. App_platform: = android-8
8 of which meansminsdkversion, according to their actual situation to replace!

4.androidndk:waringunsupported source file extensions in Jni/android.mk for module XXXXIt's because I'm doing it.[CPP]View Plaincopy
    1. Local_src_files: = HelloWorld.h changed to local_src_files: = HELLOWORLD.C

5.obj/local/armeabi-v7a/objs/xxxxx ****multiple target patterns. StopThis is delete obj this folder, regenerate $ndk/ndk-build ok!
If you are in the process of development also encountered other errors, please put a question in the message and attach a solution, for other people to refer to, thank you!

JNI (i)--helloworld of Android development and error resolution encountered

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.