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 such as the following command Javac Helloworld.java generates a Helloworld.class file (5) that is returned to the Projectsrc folder with CMD. 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 the project name. The 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 their own name; After you refresh the project, the 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)
The values behind the Local_module and Ystem.loadlibrary ("Hello-jni") are consistent, indicating that the module library to be loaded local_src_files the value of the source file to be compiled and executes the project after it has been compiled. Get the results you want.

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 after it's 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, once again generated $ndk/ndk-build ok!
Suppose everyone in the process of development also encountered other errors, please put 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.