Android NDK Windows setup

|
http://fixnum.org/blog/2010/android_ndk/
 

http://www.pocketmagic.net/?p=1332


Android NDK for Windows setup guide

By Radu Motisan Posted on August 24th, 2010

Introduction

Since most of the NDK tools are coming from the Linux environment, that OS was my first approach in building native C applications for Android. See this tutorial for a start.

However, I also needed to be able to compile JNI Android apps using the Windows environment. It proved an easy task, but there were some issues, so I decided to write this guide.

Install Android NDK on Windows and use it to compile a JAVA/JNI application

You will need an internet connection to install Cygwin and the Android SDK automatically from the Internet.

  • 1) Download the Android NDK for Windows
  • Download link.
    Current version of the file is: android-ndk-r4b-windows.zip 45MB

  • 2) Download Cygwin
  • Download link. (Direct Link)
    Select the mirror you need to download from then follow the installation guide.
    On package selection, Open Devel category and check "make":
    2.1
    I've installed it to C:\cygwin

  • 3) Download the latest Android SDK for Windows
  • Download link.
    Current version is android-sdk_r06-windows.zip 23MB

  • 4) Install the SDK
  • 4.1) Unzip the zip content to a location on your harddisk. I choose:
    D:\work_code\android\android-sdk-windows\
    You will find SDK Setup.exe . Run it. I get a warning to install JAVA JDK
    4.2) Download Java JDK
    Download link.
    Select platform "Windows". Current file is: jdk-6u21-windows-i586.exe (Direct link)
    4.3) Install the Java JDK: simply run the exe . It takes a few minutes to complete.
    4.4) Re-run the SDK Setup.exe from the Android SDK.
    This opens the Android SDK and AVD Manager. If you get a HTTPS Error:
    4.5.1
    go to Settings and check the "Force https:// ..." checkbox:
    4.5.2
    4.5) Go to Available Packages, select all, and press Install Selected:
    4.6
    The installer will download the packages from the internet, and might prompt you to accept terms, which you must do if you want to continue with the installation. This step takes a few minutes.

  • 5) Install the NDK
  • 5.1) Unzip the NDK zip file. My choice is: D:\work_code\android\android-ndk-r4b\
    5.2) Configure Cygwin parameters
    Go to the Cygwin folder and open Cygwin.bat. It contains something like this:

     
    @echo off
     
    C:
    chdir C:\cygwin\bin
     
    bash --login -i
     

    Using any text editor change it to:

     
    @echo off
     
    set IS_UNIX=
    set DEV_ROOT=d:/work_code/android/BlueInput
    set JAVA_HOME=c:/Progra~1/Java/jdk1.6.0_21
    set CLASSPATH=d:/work_code/android/BlueInput/obj
    set PATH=D:/work_code/android/android-sdk-windows/tools;D:/work_code/android/android-ndk-r4b
    set ANDROID_NDK_ROOT=/cygdrive/d/work_code/android-ndk-r4b
    set NDK_PROJECT_PATH=/cygdrive/d/work_code/android/BlueInput
     
    C:
    chdir C:\cygwin\bin
     
    bash --login -i
     

    The path in the config, d:/work_code/android/BlueInput, is something I'm currently working on. Change to your Own project name. The project must be organized as follows:
    A) The JNI part (the .c and .h and Android.mk files) in:
    d:/work_code/android/BlueInput/jni
    B) The JAVA part (assets,bin,gen,jni,libs,obj,res,src with the .java files) in:
    d:/work_code/android/BlueInput
    For these paths always use forward slashes.
    5.3) Open Cygwin and browse to the NDK folder, using cd [enter] :
    5.4
    cd d:/work_code/android-ndk-r4b
    5.4) Type ./ndk-build to compile the JNI project
    5.5
    The C files have been successfully built. Now using Eclipse, you can build the Java android project, and it will include the JNI part automatically. Easy.


    -------------------------------------

    Android 에서 디바이스 드라이버를 테스트 한다거나, 리눅스 라이브러리를 빌드하는 것과 같은, 여러가지 이유로 간단하게 C 로 작성된 테스트 어플 및 Dynamic Linkage 라이브러리와 같은 Android C Native Application 을 개발하기 위해서 해야 할 일들을 정리해 보았습니다. 
    다들 아시겠지만, Android 에서는 BSD 계열의 Bionic-C 의 라이브러리를 사용하고 있습니다. 그래서 GNU Toolchain 을 사용해서 작업하려면 Static 으로 빌드를 해야 하지만, 바이너리 사이즈가 커지는 단점이 있습니다. 여기에서는 Android NDK 에 포함된 bionic-C 라이브러리를 사용해서 Native 바이너리를 생성하는 방법을 설명합니다.
    그리고, 여기에서는 Windows 에서 작업하는 것을 기준으로 설명합니다. 이유는 리눅스에서 작업하기 위해서는 별도의 PC나 Vmware 와 같은 프로그램이 필요하고, Windows 에서 개발하는 것이 더 편리하기 때문입니다. 또한, Windows 에서 작업하는 방법을 안다면, 리눅스에서 개발하시던 분은 리눅스에서도 동일하게 작업이 가능하실 것으로 생각됩니다.

    1. Andorid NDK 설치 

    - 먼저 http://dl.google.com/android/ndk/android-ndk-r4b-windows.zip 에서 Android NDK 를 다운로드 받습니다.  
    - 적당한 위치에 android-ndk-r4b-windows.zip 파일의 압축을 풀면 됩니다.
    - 저는, “D:\android-ndk-r4b” 에 설치하였습니다.

    2. MinGW, Msys 설치

    - Android 개발 환경이 Linux 기반으로 되어 있기 때문에, Windows 에서 리눅스 개발 환경을 제공해 주는 것입니다. MinGW 와 Msys 에 대해서는 이전 포스트의 설치 방법을 참조하시면 될 것 같습니다(http://ohadev.tistory.com/6)
    - Android NDK 에서는 Cygwin(MinGW 와 같이 Linux 개발 환경을 제공) 을 사용해서 컴파일 하도록 하고 있으나, 개인적으로 MinGW 를 더 선호하기 때문에 MinGW 를 기준으로 설명합니다. Cygwin 을 사용하시는 분들은 Cygwin 을 사용하셔서 진행하셔도 됩니다.

    3. Arm Cross-compiler Toolchain 설치

    - CodeSourcery 에서 Windows 설치용 Arm-Linux Toolchain  을 다운로드 해서 설치합니다. 
    (http://www.codesourcery.com/sgpp/lite/arm/portal/package6491/public/arm-none-linux-gnueabi/arm-2010q1-202-arm-none-linux-gnueabi.exe)
    - 저는, “D:\Android\arm-2010q1” 에 설치하였습니다.
    - CodeSourcery Toolchain 은 Cygwin 용 dll 을 사용하지 않기 때문에, Cygwin 을 설치하지 않으셔도 컴파일을 하실 수 있습니다. (제가 MinGW 를 사용하는 이유가 MinGW 로 Windows Application 을 만들면 Cygwin 과 같이 별도의 dll 을 포함 시키지 않아도 되기 때문입니다.)
    - Cygwin 을 사용하면 Android NDK 에 포함되어 있는 Toolchain (android-ndk-r4b\build\prebuilt\windows\arm-eabi-4.4.0) 을 사용해서 컴파일할 수 있을 것 같은데 저는 잘 안되는군요. 

    4. Makefile 작성

    예제파일 : 


    - 본 문서를 작성하기 위해서 http://dustinlee.egloos.com/1973505 를 참고하였습니다. 
    - Makefile 에서 주의하실 점은, Android NDK 에 포함되어 있는 Bionic-C 라이브러리를 사용하기 위해서, GNU 에 포함된 라이브러리를 사용하지 않도록, “-nostdlib”  Link 옵션을 주셔야 합니다.
    - 또한, LDFLAGS 에서 dynamic-linker 를 지정할 때 그냥 path 를 “/system/bin/linker” 를 지정하면 msys 에서 상대 Path로 인식해서 앞에 D:/MinGW/msys/ 를 자동으로 붙여버리기 때문에, 절대 경로를 지정하기 위해서 “//system/bin/linker” 로 지정하셔야 합니다.
    - 예제에서 사용한 Makefile 은 다음과 같습니다. 

    TARGET = hello

    CROSS  = /d/Android/arm-2010q1/bin/arm-none-linux-gnueabi-
    INCDIR = /d/Android/android-ndk-r4b/build/platforms/android-8/arch-arm/usr/include
    LIBDIR = /d/Android/android-ndk-r4b/build/platforms/android-8/arch-arm/usr/lib

    CC  = $(CROSS)gcc
    AR  = $(CROSS)ar
    LD  = $(CROSS)ld
    NM  = $(CROSS)nm
    RANLIB  = $(CROSS)ranlib
    STRIP = $(CROSS)strip

    INCLUDE = -I. -I$(INCDIR)

    CFLAGS = -Wall
    LDFLAGS = -nostdlib -Wl,--dynamic-linker,"//system/bin/linker" -L$(LIBDIR) -lc -lm -lstdc++ -ldl

    # crt
    CRTOBJ = $(LIBDIR)/crtbegin_dynamic.o

    # application file
    APPSOURCES = main.c
    APPOBJS = $(APPSOURCES:.c=.o)

    # define the rule
    .SUFFIXES:.c .o
    .c.o:
    @echo Compiling: $<
    $(CC) -c $(CFLAGS)  $(INCLUDE) -o $@ $<

    all: app

    app: $(APPOBJS)
    @echo Linking: $(TARGET)
    $(CC) -o $(TARGET) $(APPOBJS) $(CRTOBJ) $(LDFLAGS)
    $(STRIP) -s $(TARGET)

    clean:
    @rm -vf $(APPOBJS) $(TARGET)

    5. 실행

    - Android emulator 를 실행한 다음, adb 를 이용하여 컴파일된 파일을 실행하시면 다음과 같은 결과를 얻으실 수 있습니다.


    --------------------------------

    윈도우 환경에서 안드로이드 NDK 설치 및 사용법을 간단하게 적어보겠습니다. 

    1. 안드로이드 NDK 다운로드
    - 1.5 NDK  http://developer.android.com/sdk/ndk/1.5_r1/index.html 
    - (현재 버전은 1.5 입니다만 최신 버전을 http://developer.android.com에서 확인하시고 다운받으세요)

    2. Cygwin 설치 (Cygwin은 Linux같은 환경을 Window에서 사용할 수 있는 환경을 제공합니다)
    - Install or Update now (using setup.exe)를 클릭하여 다운로드 받고 실행합니다.
    - Install from Internet 선택/Install Directory 선택/임시 디렉토리 선택/Direct Connection/
    - Choose a download site에서 ftp://ftp.kaist.ac.kr 를 선택
    - Select Packages에서 다음 패키지들 선택
     + devel/gcc-core
     + devel/gcc-g++
     + devel/make
     + editor/vim (Optional)
    - 설치완료 

    3. Cygwin 실행
    - ndk설치 디렉토리로 이동 
     + cd /cygdrive/d/android-ndk-1.5_r1/
    - build/host-setup.sh 실행 (build 디렉토리로 들어가지 마시고 ndk의 root에서 실행하셔야 합니다.)
    - 샘플 라이브러리 빌드
     + make APP=hello-jni
    - 라이브러리 빌드 완료

    4. NDK 사용법
    - android-ndk의 밑에 docs 디렉토리가 있습니다. 참고하시기 바랍니다.

    안드로이드 NDK는 애플리케이션에서 사용할 C/C++ 라이브러리를 생성하는데 사용합니다. apk의 생성등 애플리케이션 빌드는 기존과 동일하게 Eclipse에서 작업하시면 됩니다. C/C++용 라이브러리는 애플리케이션 디렉토리의 libs 및에 각 아키텍쳐별로 디렉토리가 생성이 되고 so파일이 들어가게 됩니다. ADT에서 알아서 apk를 생성할때 해당 라이브러리를 넣어줍니다. 기존에 libs및에 jar파일 두고 사용하시는 분들도 많으셨을텐데 특별히 충돌은 나지 않습니다만 헷갈리지 않게 디렉토리를 잘관리하셔야할것 같습니다.

    이 설치법은 1.5 NDK에서 테스트 되었습니다.




    And