Ubuntu compiled Android JNI instance entire process

Source: Internet
Author: User

first step: Make and gcc available

Enter make-vin the shell , no error is right. (Refer to http://wenku.baidu.com/view/d87586c24028915f804dc24a.html. ) )

Enter gcc-vin the shell , no error is right.

Step Two: Install the NDK

After you download the NDK , set the environment variables:

     android-ndk Span style= "font-family:arial" > add to environment variable path sudogedit/etc/environment

then let the changed environment variable take effect immediately: source/etc/environment

after the above steps, knock at the command line: Ndk-bulid (under individual user, not under root)

pop up the following error instead of saying Ndk-buildnot found , it explains NDK the environment has been installed successfully.

    androidndk:could not find application project directory!    &NBSP
AndroidNDK: Define the Ndk_project_path variable to point toit.    
Span style= "font-family:arial" >
/home/braincol/workspace/ android/android-ndk-r5/build/core/build-local.mk:85:*** android ndk:aborting    .  Stop.

Step Three: Write Android Engineering

Create a Android app Engineering hellojni, create hellojni.java file:

  Hellojni.java:

 import android.app.Activity; import android.widget.TextView; import android.os.Bundle;Public   class Hellojni extends Activity{    /** Called when the activity is first created. * /    @Override    public void   onCreate (Bundle savedinstancestate)       {         Super. OnCreate (savedinstancestate);          TextView TV =  new  TextView (this);         Tv.settext (Stringfromjni ());        Setcontentview (TV);    }    / * A Native method that's implemented by the ' hello-jni ' native library, which are packaged with this application. */     public native   String stringfromjni ();       /* This is used-load the ' Hello-jni ' Library on application startup. The library has already been unpacked into      /data/data/com.example.hellojni/lib/libhello-jni.so at installation time by the package Manager. * /     static  {         system.loadlibrary ("hello-h3c");    }}
Fourth step: UseEclipseCompile the project

generate the appropriate . Class file, which must be completed before the next step because the build . h the file needs to use the corresponding . Class file.

first, under the construction of a newJNIfolder, open terminal;cd<Project Name>; execution:javah-classpath bin/classes-d jni XXX. Hellojni you will be able toJNIfound under. hfile, whereXXXis the package name. Like mine is.Cd/home/lck/workspace/hellojniExecution: javah-classpath bin/classes-d jnicom.example.hellojni. Hellojni

Fifth Step: Write C code

in the JNI under Create a new C file, write the appropriate . C files:

HELLO-H3C.C:

#include <string.h>#include <jni.h>/* This is a trivial JNI example where we use a native method * To return a new VM String. See the corresponding Java source  * file located at:  * Apps/samples/hello-jni/project/src/com/example/hellojni/hellojni.java  */jstring Java_com_example_h3cjni_hellojni_stringfromjni (jnienv* env, jobject thiz){     return  (*env)->newstringutf (env,  "Hello from H3C!" ) );}

Sixth step: Write android.mk file

in the Create a new android.mk file under the Jni folder,

Add the following content:

Local_path: = $ (call My-dir) include $ (clear_vars) local_module: = hello-h3c local_src_files: = hello-h3c.c include $ (build_shared_library) for the symbol on the left of the = sign, there are many explanations on Baidu, here is not introduced.
Seventh step: Generate . so shared library files

cd< project name >execution ndk-build, if found

[Armeabi] install:libhello-h3c.so = libs/armeabi/libhello-h3c.so

The . so file is generated and we can find it in the libs/armeabi/ directory

At this point the Android project is running and we can see it on the simulator.

note: When the seventh step is completed, it is easy to appear java.lang.unsatisfiedlinkerror Span style= "COLOR: #ff0000" > error , this error is often due to the case of the method name or the reference error. For example, I use someone else's example in c file jstringjava_com_example_h3cjni_hellojni_stringfromjni (JNIEnv* env, Jobjectthiz)

Should be changed to jstringjnicalljava_com_example_hellojni_hellojni_stringfromjni, so the reference must be correct.



Reference blog:http://blog.csdn.net/h3c4lenovo/article/details/10364679








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.