Android NDK JNI 的環境搭建以及簡單的代碼

來源:互聯網
上載者:User

   今天下午蛋疼地開始研究ndk和jni。

    首先去下載一個Android ndk

http://dl.google.com/android/ndk/android-ndk-1.6_r1-windows.zip

  我下載的是1.6版本的

 

 

    然後去下載安裝一個Cygwin 這個安裝和下載方法自己google一下很多

 

 

 

這些都弄好了之後 進入我們的代碼環節

 

首先建立一個工程 如

其中JNI.java

package zy.myTestJni;<br />public class JNI {<br /> public native void write();<br />}<br /> 

 

JniTest.java

package zy.myTestJni;<br />import android.app.Activity;<br />import android.os.Bundle;<br />public class JniTest extends Activity {<br /> /** Called when the activity is first created. */<br />static {<br />System.loadLibrary("myjni");<br />}<br /> @Override<br /> public void onCreate(Bundle savedInstanceState) {<br /> super.onCreate(savedInstanceState);<br /> setContentView(R.layout.main);<br /> JNI j=new JNI();<br /> j.write();<br /> }<br />} 

 

然後用JNI.java來產生我們的標頭檔

進入你的工程的bin目錄

執行下述代碼

D:/Users/ZY/workspace/myTestJni/bin>javah -classpath .  -jni zy.myTestJni.JNI

 

然後產生了一個

zy_myTestJni_JNI.h檔案

這個檔案不用改

然後建立一個zy_myTestJni_JNI.c的檔案

代碼為

#include<stdio.h><br />#include <stdlib.h><br />#include "zy_myTestJni_JNI.h"<br />JNIEXPORT void JNICALL Java_zy_myTestJni_JNI_write<br /> (JNIEnv *e, jobject j)<br />{<br /> FILE * v=fopen("/sdcard/zy.txt","w+");<br /> fprintf(v,"aaaa");<br /> fclose(v);<br />}<br />

然後把這兩個檔案拷貝到ndk路徑的app路徑下 建立一個myjni檔案夾在建一個jni放進去

 

 

然後在myjni下建立一個Application.mk

APP_PROJECT_PATH := $(call my-dir)<br />APP_MODULES := myjni 

在jni目錄下建立一個Android.mk

# Copyright (C) 2009 The Android Open Source Project<br />#<br /># Licensed under the Apache License, Version 2.0 (the "License");<br /># you may not use this file except in compliance with the License.<br /># You may obtain a copy of the License at<br />#<br /># http://www.apache.org/licenses/LICENSE-2.0<br />#<br /># Unless required by applicable law or agreed to in writing, software<br /># distributed under the License is distributed on an "AS IS" BASIS,<br /># WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.<br /># See the License for the specific language governing permissions and<br /># limitations under the License.<br />#<br />LOCAL_PATH := $(call my-dir)<br />include $(CLEAR_VARS)<br />LOCAL_MODULE := myjni<br />LOCAL_SRC_FILES := zy_myTestJni_JNI.c<br />include $(BUILD_SHARED_LIBRARY)<br /> 

 

 

 

接下來 我們來編譯我們的.so庫

 

 

 

   然後進入到你的ndk目錄 我的目錄是這個

 

 

在cygwin裡用cd命令進入到這個ndk目錄

然後執行

build/host-setup.sh

  

 

執行 make APP=myjni -B

 

此時可以看到我們的myjni目錄下多了一個libs目錄 裡面有我們的.so檔案

 

然後將整個libs目錄放到我們的Android工程下

在AndroidManifest.xml裡加上

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"><br /></uses-permission> 

運行我們的代碼

 

可以看到 在sdcard下面建立了一個zy.txt 裡面的內容是aaaa

 

 

本部落格由博主曾陽原創

轉載請註明出處http://blog.csdn.net/Zengyangtech/archive/2010/11/30/6046170.aspx

謝謝大家

 

聯繫我們

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