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 Androidprojectjnidemo, the name can be casually up.
(2) Create a new Helloworld.java class, the contents of which are as follows:
public class HelloWorld {public        native String print ();        static {        system.loadlibrary ("Hello-jni");}    }

(3) called in the Mainactivity.java class, as seen in the following:
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 and go to the folder where the Projecthelloworld.java class is located. Execute commands such as the following command Javac Helloworld.java generated a Helloworld.class file (5) with Cmd returned to the Projectsrc folder, execute the following commandJavah Com.example.jnidemo.HelloWorld,This generates acom_example_jnidemo_helloworld.hFiles can also be used to commandjavah-classpath.-jni Com.example.jnidemo.HelloWorldJavah-classpath. Com.example.jnidemo.HelloWorldHowever, it must be under the SRC folder
(6) Right click on Project name, new folder, named Jni, copy this two files from the Android Ndk folder.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 the name can be copied to the Jni folder.
(7) Write the H file implementation file C, where the implementation of the file is HELLOWORLD.C. Content such as the following: (8) Open cygdrive, go to the root folder of the project, execute such as the following command$NDK/ndk-build, $NDK changed to a name of your own; refresh the project when finished. Folder is more than the obj folder
#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) Android.mk file configuration such as the following:
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 the project is compiled, the desired results are obtained

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-> join D:\adt-bundle-windows\ Android-ndk\platforms\android-19\arch-arm\usr\include, the path of the NDK
2. Assume also reported 1 error, Window->show View->problems, clear all errors, so it's OK. Sometimes when you're done, switch between several files. This error will also be reported, and the practice continues 2.
3.Android NDK:WARNING:APP_PLATFORM android-14 is larger than android:minsdkversion 8

This is due to the fact that the version number specified by the NDK is inconsistent with the minsdkversion, which is itself a warning only. But in eclipse will compile the error, then how to fix it?

Just need to be in your project:

Application.mk

Add the following code to the file, for example:

[CPP]View Plaincopy
    1. App_platform: = android-8
8 of them meanminsdkversion. 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 the deletion of the obj directory. Another generation of $ndk/ndk-build is ok!.
Suppose you have other mistakes in the process of development, please post a question in the message and attach a solution for other people to participate in the test. 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.