Android NDK開發初步

來源:互聯網
上載者:User

Android NDK開發初步
在配置好NDK開發之後就可以使用C/C++開發android了。下面以一個HelloWorld項目來說明
1.建立一個Android工程
建立一個HelloWorld工程
代碼如下

  1. package com.zhycheng;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.widget.TextView;
  5.  
  6. public class HelloWorldActivity extends Activity {
  7. /** Called when the activity is first created. */
  8. @Override
  9. public void onCreate(Bundle savedInstanceState) {
  10. super.onCreate(savedInstanceState);
  11. TextView tv=new TextView(this);
  12. tv.setText(srringFromCpp());
  13. setContentView(tv); }
  14. public native String srringFromCpp();
  15. static { System.loadLibrary(HelloWorld); }}


    System.loadLibrary(HelloWorld);中的HelloWorld是動態連結程式庫libHelloWorld.so
    2 產生標頭檔和源檔案
    先在工程目錄下建立一個檔案夾叫做jni
    現在我要使用javah這個工具產生標頭檔
    在工程的目錄下執行命令
    zhycheng@zhycheng-Lenovo-B460:~/androidworkspace/HelloWorld$ javah -classpath bin/classes -d jni com.zhycheng.HelloWorldActivity
    這時在jni檔案夾下就產生了com_zhycheng_HelloWorldActivity.h標頭檔,內容如下
    1. /* DO NOT EDIT THIS FILE - it is machine generated */
    2. #include
    3. /* Header for class com_zhycheng_HelloWorldActivity */
    4.  
    5. #ifndef _Included_com_zhycheng_HelloWorldActivity
    6. #define _Included_com_zhycheng_HelloWorldActivity
    7. #ifdef __cplusplus
    8. extern C {
    9. #endif
    10. /*
    11. * Class: com_zhycheng_HelloWorldActivity
    12. * Method: srringFromCpp
    13. * Signature: ()Ljava/lang/String;
    14. */
    15. JNIEXPORT jstring JNICALL Java_com_zhycheng_HelloWorldActivity_srringFromCpp
    16. (JNIEnv *, jobject);
    17.  
    18. #ifdef __cplusplus
    19. }
    20. #endif
    21. #endif
    22. 在這個檔案裡,我們可以得到srringFromCpp的函數名。
    23. 下面我在jni檔案夾裡建立一個HelloWord.c檔案內容如下
    24. #include
    25. #include
    26. JNIEXPORT jstring JNICALL Java_com_zhycheng_HelloWorldActivity_srringFromCpp
    27. (JNIEnv *env, jobject obj)
    28. {
    29. return (*env)->NewStringUTF(env,張譯成);
    30. }


      這裡還要在jni目錄裡建立一個Android.mk檔案,內容如下
      1. LOCAL_PATH:= $(call my-dir)
      2.  
      3. include $(CLEAR_VARS)
      4.  
      5. LOCAL_MODULE := HelloWorld
      6.  
      7. LOCAL_SRC_FILES:= HelloWorld.c
      8.  
      9. include $(BUILD_SHARED_LIBRARY)




        4 產生動態連結程式庫
        在工程目錄裡執行下面的命令
        1. zhycheng@zhycheng-Lenovo-B460:~/androidworkspace/HelloWorld$ ndk-build
        2. Compile thumb : HelloWorld <= HelloWorld.c
        3. SharedLibrary : libHelloWorld.so
        4. Install : libHelloWorld.so => libs/armeabi/libHelloWorld.so


          這樣就得到了libHelloWorld.so檔案。這個檔案在libs/armeabi檔案夾裡。

          5 運行整個工程


          可以看到,在介面上顯示了從動態連結程式庫返回的字串。

           

聯繫我們

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