2011年4月27日 星期三

[Android] Build NDK Environment

NDK是什麼?
The Android NDK is a toolset that lets you embed components that make use of native code in your Android applications.

安裝NDK

Step 1:安裝Cygwin
  • 下載點:到http://cygwin.com/install.html下載setup.exe
  • Cygwin Setup: Next
  • Cygwin Setup - Choose Installation Type: Install from Internet > Next
  • Cygwin Setup - Choose Installation Directory: All Users > Next
  • Cygwin Setup - Select Local Package Directory > Next
  • Cygwin Setup - Select Connection Type: Direct Connection > Next
  • Cygwin Setup - Choose Download Site(s): ftp:/ftp.ntu.edu.tw > Next
  • Cygwin Setup - Select Packages as below > Next


  • Launch Cygwin and run sure "gcc -v" and "make -v" to verify if it works correctly.
$ gcc -v
Reading specs from /usr/lib/gcc/i686-pc-cygwin/3.4.4/specs
Configured with: /managed/gcc-build/final-v3-bootstrap/gcc-3.4.4-999/configure --verbose --program-suffix=-3 --prefix=/usr --exec-prefix=/us
r --sysconfdir=/etc --libdir=/usr/lib --libexecdir=/usr/lib --mandir=/usr/share/man --infodir=/usr/share/info --enable-languages=c,ada,c++,d
,f77,pascal,java,objc --enable-nls --without-included-gettext --enable-version-specific-runtime-libs --without-x --enable-libgcj --disable-j
ava-awt --with-system-zlib --enable-interpreter --disable-libgcj-debug --enable-threads=posix --enable-java-gc=boehm --disable-win32-registr
y --enable-sjlj-exceptions --enable-hash-synchronization --enable-libstdcxx-debug
Thread model: posix
gcc version 3.4.4 (cygming special, gdc 0.12, using dmd 0.125)

$ make -v
GNU Make 3.81
Copyright (C) 2006  Free Software Foundation, Inc.
This is free software; see the source for copying conditions.
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.

This program built for i686-pc-cygwin

Step 2:安裝NDK

  • 下載點:到http://developer.android.com/sdk/ndk/index.html 下載Android NDK for Windows
  • Cygwin Setup: Next
  • Cygwin Setup - Choose Installation Type: Install from Internet > Next
  • Cygwin Setup - Choose Installation Directory: All Users > Next
  • Cygwin Setup - Select Local Package Directory > Next
  • Cygwin Setup - Select Connection Type: Direct Connection > Next
  • Cygwin Setup - Choose Download Site(s): ftp:/ftp.ntu.edu.tw > Next
  • Cygwin Setup - Select Packages as below > Next
  • 然後到C:\cygwin\home\"使用者名稱" 目錄下,用windows編輯器編輯.bash_profile :
在最後一行後加入下面四行,然後存檔:
PATH=/android-ndk-r5:${PATH}
NDK_ROOT=/android-ndk-r5
NDK_Sample=/android-ndk-r5/samples
export PATH NDK_ROOT NDK_Sample
(NDK_Sample是指定你的JNI程式所要放置的目錄,這邊我我指定到/android-ndk-r5/samples,這個參數會在Eclipse的C編譯中使用到。)
  • 開啟Cygwin Bash Shell到/android-ndk-r5/samples/hello-jni/目錄下,執行ndk-build
vince_huang@VinceH-DT ~
$ cd /android-ndk-r5/samples/hello-jni/

vince_huang@VinceH-DT /android-ndk-r5/samples/hello-jni
$ ndk-build
Gdbserver      : [arm-linux-androideabi-4.4.3] libs/armeabi/gdbserver
Gdbsetup       : libs/armeabi/gdb.setup
Install        : libhello-jni.so => libs/armeabi/libhello-jni.so

Step 2:安裝Eclipse CDT(C/C++ Development Tool)

  • Eclipse Help > Install New Software... > Add...
  • Name: CDT
  • Location: http://download.eclipse.org/tools/cdt/releases/galileo
  • 安裝Eclipse C/C++ Development Tools
  • 安裝CDT GNU Toolchain Build Support
  • 安裝CDT GNU Toolchain Debug Support
  • 安裝CDT Utilities
  • 安裝Eclipse C/C++ Development Platform
  • 安裝完成後,Eclipse會要求重新開啟Eclipse,重開Eclipse後便可以在Eclipse中編輯C/C++程式。

相關連結

沒有留言:

張貼留言