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 |