* 목적
android 기본적으로 제공되는 ant 스크립트로 자동 build 환경을 구성하는 방법을 알아보도록 하겠습니다.


* android project 만들기
- 기존 이클립스 프로젝트 만드는것과 동일합니다.


* ant project 만들기
- sdk/tools 디렉토리 path 설정
여기서는 sdk 에 포함되어있는 툴을 많이 사용하기때문에 path 로 잡아두는것이 편합니다.
androidsdk/tools 디렉토리를 path 에 포함시킵니다.


- android update project ./ 실행
해당 프로젝트 디렉토리로 가서 아래 명령어를 실행하면 자동으로 build.xml 파일이 생성됩니다.

android update project –p ./
image

- keystore 설정하기
패키징을 하기위해서는 keystore 파일을 설정해야 합니다.

local.properties 파일을 열어서 아래와같이 keystore 의 위치와 alias 를 잡아줍니다.
image

 

* 컴파일직전에 config 설정하기
- build.xml 수정하기
maven 에서처럼 컴파일전에 자동으로 config 를 수정하도록 하려면 아래와같은 코드를 build.xml 에 추가합니다.
그리고 ant 실행시 파라메터에 config 를 변경해주면 됩니다.

<!-- extension targets. Uncomment the ones where you want to do custom work
     in between standard targets -->
    <property name="config" value="beta" />   
    <target name="-pre-build">
        <echo>config setting!</echo>
        <delete file="src/config.properties" />
        <copy tofile="src/config.properties" file="config/config_${config}.properties" />
    </target>

- 코드상에서 config.properties 사용하기

package net.cranix.android.anthello;


import java.io.IOException;
import java.util.Properties;

/**
* @author cranix
*/
public class Constants {
    private static final Properties config = new Properties();
    static {
        try {
            config.load(Constants.class.getClassLoader().getResourceAsStream("config.properties"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
   
   
    public static final String URL = config.getProperty("url");
}


* ant 를 이용하여 실행하기
- build.xml 수정하기
google 에서 기본적으로 제공하는 ant build 스크립트에는 실행할수있는 스크립트가 없습니다.
그래서 아래와같은 코드를 build.xml 에 추가하면 실행을 해볼수 있습니다.

    <target name="run">
        <xpath input="AndroidManifest.xml" expression="/manifest/@package" output="manifest.package" />
        <xpath input="AndroidManifest.xml" expression="/manifest/application/activity[intent-filter/action/@android:name='android.intent.action.MAIN']/@android:name" output="manifest.mainactivity" />

        <exec executable="${adb}" failonerror="true">
            <arg line="${adb.device.arg}" />
            <arg value="shell" />
            <arg value="am" />
            <arg value="start" />
            <arg value="-a" />
            <arg value="android.intent.action.MAIN" />
            <arg value="-n" />
            <arg value="${manifest.package}/${manifest.mainactivity}" />
        </exec>
    </target>


* ant 를 이용하여 빌드하기
프로젝트 디렉토리에서 아래와같은 명령으로 빌드를 해볼수 있습니다.
단 이전에 ant 는 받아서 path 로 잡혀 있어야 합니다.

- ant debug -> debug 키로 빌드해서 bin 디렉토리에 apk 생성
- ant debug install -> debug 키로 빌드해서 bin 디렉토리에 apk 생성하고 디바이스에 설치
- ant release -> release 키로 빌드해서 bin 디렉토리에 apk 생성
- ant release install -> release 키로 빌드해서 bin 디렉토리에 apk 생성하고 디바이스에 설치
- ant debug install run –> apk 생성하고 디바이스에 설치해서 실행해보기
- ant debug install run –Dconfig=beta –> beta 환경으로 디바이스에서 실행해보기
- ant emma debug install test –> test 를 돌리고 emma coverage report 를 뽑아냅니다. (test 프로젝트에서 실행)


* 문제점
- maven 에서와같이 dependency 를 관리해주지 않아서 수동으로 해야 합니다.

by cranix 2012. 1. 30. 11:14