Ubuntu下編譯Android JNI執行個體全過程

來源:互聯網
上載者:User

標籤:jni   android應用   ubantu   

第一步:保證make和gcc可用

在shell中輸入make-v,不報錯就是對的。(可參考http://wenku.baidu.com/view/d87586c24028915f804dc24a.html.)

在shell中輸入gcc-v,不報錯就是對的。

第二步:安裝NDK

 下載NDK後,設定環境變數:

    將android-ndk的路勁加到環境變數PATH中:sudogedit /etc/environment

   然後再讓這個更改的環境變數立即生效:source /etc/environment

   經過了上述步驟,在命令列下敲:ndk-bulid(在個人使用者下,不要在root下)

 彈出如下的錯誤,而不是說ndk-buildnot found,就說明ndk環境已經安裝成功了。

    AndroidNDK: Could not find application project directory !    
    AndroidNDK: Please define the NDK_PROJECT_PATH variable to point toit.    
    /home/braincol/workspace/android/android-ndk-r5/build/core/build-local.mk:85:*** Android NDK: Aborting    .  Stop.

第三步:編寫Android工程

  建立一個Android應用工程HelloJni,建立HelloJni.java檔案:

  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 is implemented by the ‘hello-jni‘ native library, which is packaged with this application. */    public native String  stringFromJNI();    /* this is used to 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");    }}
第四步:用eclipse編譯該工程

   產生相應的.class檔案,這步必須在下一步之前完成,因為產生.h檔案需要用到相應的.class檔案。

首先在工程下建立一jni檔案夾,開啟終端;cd<工程名>;執行:javah-classpath bin/classes -d jni XXX. HelloJni 就可以在jni下發現.h檔案,其中xxx是包名。比如我的是cd /home/lck/workspace/HelloJni 執行:javah-classpath bin/classes -d jnicom.example.hellojni.HelloJni

第五步:編寫C代碼

在jni下建立一個c檔案,編寫相應的.c檔案:

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 !");}

第六步:編寫Android.mk檔案

在jni檔案夾下建立一個Android.mk檔案,

裡面添加如下內容:

LOCAL_PATH := $(call my-dir)include $(CLEAR_VARS)LOCAL_MODULE := hello-h3cLOCAL_SRC_FILES := hello-h3c.cinclude $(BUILD_SHARED_LIBRARY)對於=號左邊符號的意思,百度上有很多解釋,在這裡就不介紹了。
第七步:產生.so共用庫檔案

cd<工程名>;執行ndk-build,如果發現

[armeabi]Install : libhello-h3c.so => libs/armeabi/libhello-h3c.so

即產生了.so檔案,我們可以在libs/armeabi/目錄下找到該檔案

此時運行安卓工程,我們可以在模擬器上看到。

注意:第七步完成後運行,容易出現java.lang.UnsatisfiedLinkError錯誤,這種錯誤往往是由於方法名的大小寫或者引用出錯。比如,我用的別人的例子,在c檔案中jstringJava_com_example_h3cjni_HelloJni_stringFromJNI( JNIEnv* env, jobjectthiz )

應改為jstringJNICALLJava_com_example_hellojni_HelloJni_stringFromJNI,所以引用一定要正確。



參考部落格:http://blog.csdn.net/h3c4lenovo/article/details/10364679








聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.