* 목적
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

android 기본 개발환경인 이클립스 ADT 만을 이용하면 환경설정파일을 자동으로 관리해줄수가 없습니다.
그래서 이 문서에서는 maven 을 이용해 환경설정파일을 소스 수정없이 빌드옵션 수정만으로 관리할수있도록 구성하는 방법을 알아봅니다.

* maven 을 이용한 android 자동 빌드환경 구성

   - http://cranix.net/374

 

* resource 디렉토리 만들기
  - src/main/resources 디렉토리를 생성합니다.
image

- 프로젝트 마우스오른쪽 –> maven –> update project configuration 을 실행하면 위와같이 소스폴더에 포함되는것을 확인할수 있습니다.

 


* config 파일 읽기
  - resource 디렉토리에 config.properties 파일을 생성하고 아래와같이 입력합니다.

url = http://test/real

  - properties 파일을 읽기위한 코드는 아래와 같습니다.

package net.cranix.android.hello;


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

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

   - 이것의 사용은 간단합니다.

package net.cranix.android.hello;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class HelloActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        TextView tv = (TextView) findViewById(R.id.textView1);
        tv.setText(Constants.URL);
    }
}

 


* 디렉토리 구조 만들기

   - config 디렉토리를 생성하고 config_beta.properties,config_real.properties 파일을 만들어서 집어넣습니다.

image

  - 위와같은 형태로 디렉토리 구조를 만듭니다.
  - config_beta.properties  파일과 config_real.properties 파일에는 beta 와 real 에서 사용될 적절한 환경변수를 집어넣습니다.

 

* maven pom 파일 수정하기
  - pom 파일을 아래와같이 수정합니다.


<properties>
   …
   <config>beta</config>
</properties>


<build>

    <plugins>
        <plugin>
            <artifactId>maven-antrun-plugin</artifactId>
            <version>1.7</version>
            <executions>
                <execution>
                    <phase>generate-resources</phase>
                    <configuration>
                        <target>
                            <delete
                                file="${project.basedir}/src/main/resources/config.properties" />
                            <copy tofile="${project.basedir}/src/main/resources/config.properties"
                                file="${project.basedir}/config/config_${config}.properties" />
                        </target>
                    </configuration>
                    <goals>
                        <goal>run</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        …
   </plugins>

</build>

   - 위와같이 했을때 execution 에서 오류나는 경우가 있는데 이것은 단순 버그임으로 이클립스의 Problems view 에서 delete 합니다.

image

  - 프로젝트 마우스오른쪽 –> maven –> update project configuration 을 실행합니다.

 

* build 파라메터를 변경하여 실행
   - real 환경으로 실행하기
     --> run as –> maven build … 을 클릭하고 아래와같이 입력하고 실행합니다.
image

   - 이제부터는 위와같이 build 의 parameter 를 변경하는것 만으로 beta 와 real 설정파일을 변경할 수 있습니다.

by cranix 2012. 1. 25. 12:09

* 필요한 eclipse 플러그인 설치 (마켓에서 다운로드 가능합니다.)
- Maven Integration for Eclipse
- Android Configurator for M2E


* 이클립스 환경설정
- maven 3.0.3 이상 설치
--> http://maven.apache.org/download.html
현재의 eclipse 플러그인의 maven 은 3.0.3 이하 버젼이라서 최신버젼을 다운받아야 합니다.
다운받은후에 eclipse 의 maven 설정에서 설치한 3.0.3 을 선택해 줍니다.

- jdk 6이상 선택
이클립스 기본 컴파일러를 jdk 6 이상으로 선택해 줍니다.
jre 가 선택되어 있다면 컴파일시 오류납니다.


* eclipse 에서 android 프로젝트 생성
- 이 부분은 기존의 android 프로젝트 생성하는것과 동일합니다.


* maven 프로젝트로 변경
- 생성된 android 프로젝트에 마우스오른쪽 -> Configure -> Convert to maven project 를 클릭합니다.

위와같이 입력하면 pom.xml 이 생기면서 maven 프로젝트가 됩니다.

- 추가후 바로 오류가 나는데 아래와같이 pom.xml 파일을 변경하면 오류가 없어집니다.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>net.cranix.android</groupId>
    <artifactId>mvnandroidtest2</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>apk</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.google.android</groupId>
            <artifactId>android</artifactId>
            <version>2.3.3</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>


    <build> 
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>com.jayway.maven.plugins.android.generation2</groupId>
                <artifactId>android-maven-plugin</artifactId>
                <version>3.0.0-alpha-13</version>
                <extensions>true</extensions>
            </plugin>
        </plugins>
    </build>
</project>

- 원래 있던 src 폴더를 src/main/java 로 변경하여 아래와같은 구조가 되도록 합니다.

image

- 프로젝트 마우스오른쪽 –> maven –> update project configuration 을 실행해서 pom 에 설정된값을 eclipse 에 적용합니다.
- 이작업을 하면 위의 src/main/java 폴더가 자동으로 소스폴더가 되는것을 확인할수 있습니다.

 

* 빌드 테스트 해보기
- Run As –> maven build … 을 클릭해서 아래와 같이 채웁니다.



- 실행한후에 target 디렉토리에 apk 파일이 생성되었다면 제대로 셋팅 완료된것입니다.

 

* apk 실행하기
apk 를 실행해보기 위해서는 에뮬레이터를 켜놓거나 디버그용 폰을 꼽아놔야 합니다.
그상태에서 maven 으로 아래 명령을 실행하면 됩니다.

 

 

* sign 파일로 서명하기
apk 를 만들때 서명하기 위해서는 아래와같은 플러그인을 추가하면 됩니다.

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jarsigner-plugin</artifactId>
    <version>1.2</version>
    <executions>
        <execution>
            <id>signing</id>
            <goals>
                <goal>sign</goal>
            </goals>
            <phase>package</phase>
            <inherited>true</inherited>
            <configuration>
                <archiveDirectory></archiveDirectory>
                <includes>
                    <include>target/*.apk</include>
                </includes>
                <keystore>${keystorepath}</keystore>
                <storepass>${storepass}</storepass>
                <keypass>${keypass}</keypass>
                <alias>${alias}</alias>

            </configuration>
        </execution>
    </executions>
</plugin>

굵은 부분은 자신이 가지고있는 사인파일과 비밀번호로 쓰시고 clean package 를 했을때 오류없이 잘 되었다면 제대로 설정된 것입니다.

 

* 문제점
- 테스트는 지원이 되지만 기존에 ant 기반에서 지원하던 emma coverage report 는 지원되지 않습니다.

--> emma coverage report 를 뽑아내는 과정이 실행되는곳은 test 프로젝트 인데 여기서 원래 프로젝트의 소스가 필요합니다. 그런데 maven 기반이면 각 프로젝트를 dependency 로서 관리하기때문에 따로 소스에 접근할 방법이 없습니다.

 

 

* 참고사이트

- http://code.google.com/p/maven-android-plugin/

by cranix 2012. 1. 25. 10:50
| 1 |