【android 開 發 】,android

來源:互聯網
上載者:User

【android 開 發 】,android

   Android 開發了一段時間,一方面 ,感覺不留下點什麼。有點對不起自己,

另一方面,好記性不如爛筆頭,為了往後可以回頭來看看,就當做是筆記,便決定開始寫部落格。廢話不多說 !

 今天想搞一搞 ndk 和jni  ,, 現在開始寫一個簡單的demo

 1. 建立一個新的工程

 


2. 建立一個新的類 JniText.java  點擊Build--Make Project  後

    選中工程 點擊F4鍵 sdk location 中 Android ndk location 選擇配置ndk 的路徑
    沒有下載的可以到這下載:http://wear.techbrood.com/tools/sdk/ndk/
 

    保險起見 點擊查看 local.properties 檔案
    ndk.dir=D\:\\ndk\\android-ndk-r10e
    sdk.dir=C\:\\Users\\Administrator\\AppData\\Local\\Android\\sdk
    ok so good 下一步   


    查看 項目目錄下的 E:\text\YipJniDemo\app\build\intermediates\classes\debug 產生了class檔案
    接下來 點擊底下的Terminal視窗 cd 到當前項目目錄
    E:\text\YipJniDemo>cd E:\text\YipJniDemo\app\build\intermediates\classes\debug
    

   使用javah 產生標頭檔

    E:\text\YipJniDemo\app\build\intermediates\classes\debug>javah -jni com.yip.yipjnidemo.JniText

    
    在 E:\text\YipJniDemo\app\build\intermediates\classes\debug
    下可以看到產生的 com_yip_yipjnidemo_JniText.h
  
 1 /* DO NOT EDIT THIS FILE - it is machine generated */ 2 #include <jni.h> 3 /* Header for class com_yip_yipjnidemo_JniText */ 4  5 #ifndef _Included_com_yip_yipjnidemo_JniText 6 #define _Included_com_yip_yipjnidemo_JniText 7 #ifdef __cplusplus 8 extern "C" { 9 #endif10 /*11  * Class:     com_yip_yipjnidemo_JniText12  * Method:    getCLangString13  * Signature: ()Ljava/lang/String;14  */15 JNIEXPORT jstring JNICALL Java_com_yip_yipjnidemo_JniText_getCLangString16   (JNIEnv *, jobject);17 18 #ifdef __cplusplus19 }20 #endif21 #endif

 

   
    在項目的src 檔案下的    建立jni檔案夾 注意 (右鍵main - new - Folder - JniFolder )
    將產生的檔案 com_yip_yipjnidemo_JniText.h 剪下到jni檔案夾下

 

   在jni 目錄下建立一個com_yip_yipjnidemo_JniText.c 檔案

  
 1 #include "com_yip_yipjnidemo_JniText.h" 2 /* 3  * Class:     io_github_yanbober_ndkapplication_NdkJniUtils 4  * Method:    getCLanguageString 5  * Signature: ()Ljava/lang/String; 6  */ 7 JNIEXPORT jstring JNICALL Java_com_yip_yipjnidemo_JniText_getCLangString(JNIEnv *env, jobject obj) 8 { 9   return (*env)->NewStringUTF(env,"This just a test for Android Studio NDK JNI developer!");10 }

 



 下一步, 在app的build.gradle;

   
1 defaultConfig下添加2     ndk{3         moduleName "jnitext"         //產生的so名字4             abiFilters "armeabi", "armeabi-v7a", "x86"  //輸出指定三種abi體繫結構下的so庫。目前可有可無。5     }

 


    
    點擊 make-project
    產生  .so 庫
    此時報了 Error:(13, 0) Error: NDK integration is deprecated in the current plugin.  Consider trying the new experimental plugin.  For details, see http://tools.android.com/tech-docs/new-build-system/gradle-experimental.  Set "android.useDeprecatedNdk=true" in gradle.properties to continue using the current NDK integration.
<a href="openFile:E:\text\MyJniDemo\app\build.gradle">Open File</a>
    沒關係 
    我們在   工程下的gradle.properties 添加屬性
   
  android.useDeprecatedNdk=true  

   此時 補全 .YipJniDemo下MainActivity的代碼
 1 package com.yip.yipjnidemo; 2  3 import android.app.Activity; 4 import android.support.v7.app.AppCompatActivity; 5 import android.os.Bundle; 6 import android.widget.TextView; 7  8 public class MainActivity extends Activity { 9 10 11     private TextView txtView;12 13     @Override14     protected void onCreate(Bundle savedInstanceState) {15         super.onCreate(savedInstanceState);16         setContentView(R.layout.activity_main);17         txtView= (TextView) findViewById(R.id.txt_jnitext);18         JniText jniText=new JniText();19         txtView.setText(jniText.getCLangString());20     }21 }

 


   JniText:
 1 package com.yip.yipjnidemo; 2  3 /** 4  * Created by Administrator on 2016/2/22. 5  */ 6 public class JniText { 7     static { 8             System.loadLibrary("jnitext");   //defaultConfig.ndk.moduleName 9         }10     public native String getCLangString();11 }

 


  
ps:  產生的. so檔案有些盆友找不到 。其實是在這裡的。





  

 

 

聯繫我們

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