Android NDK開發步驟(r9)

來源:互聯網
上載者:User

1. 建立Android Project File -> New -> Android Application Project   2. 定義調用C函數的java類,並產生(Eclipse中是自動編譯的)。   Java代碼: package lw.example.hellondk; public class HelloJni {    public native String getStringFromJni();        public static native String getStringFromJniStatic();        static {        System.loadLibrary("HelloNdk");    }} 3. 產生h檔案 在Project建立gen_h.bat檔案,輸入內容如下: javah -classpath bin\classes -d jni lw.example.hellondk.HelloJni@pause 運行該bat檔案後,會在jni目錄產生lw_example_hellondk_HelloJni.h檔案。 4. 添加Native支援 右擊Project -> Android Tools -> Add Native Support...   5. 附加Android Native C的h檔案 右擊project -> Properties,設定如下:   6. 在cpp檔案中實現函數。 #include "lw_example_hellondk_HelloJni.h" jstringJava_lw_example_hellondk_HelloJni_getStringFromJni(JNIEnv* env, jobject thiz){    return env->NewStringUTF("Hello JNI!");} jstringJava_lw_example_hellondk_HelloJni_getStringFromJniStatic(JNIEnv* env, jclass thiz){    return env->NewStringUTF("Hello JNI!(static)");} 7. 編譯so檔案 點擊工具列,編譯成功後,會產生so檔案。   8. 在Activity中添加測試代碼。 package lw.example.hellondk; import android.app.Activity;import android.widget.TextView;import android.os.Bundle; public class MainActivity extends Activity {     @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);         /* Create a TextView and set its content.         * the text is retrieved by calling a native         * function.         */        TextView  tv = new TextView(this);        tv.setText( HelloJni.getStringFromJniStatic() );        setContentView(tv);    } }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.