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