HelloAndroid 프로젝트를 이클립스에서 생성했을 떄.

자동으로 만들어지는 파일중에 HelloAndroid.java 파일 말고 또 하나가 있다.

그 파일이 바로 R.java 파일이다.

/* AUTO-GENERATED FILE.  DO NOT MODIFY.
*
* This class was automatically generated by the
* aapt tool from the resource data it found.  It
* should not be modified by hand.
*/

package com.min.hello;

public final class R {
    public static final class attr {
    }
    public static final class drawable {
        public static final int icon=0x7f020000;
    }
    public static final class layout {
        public static final int main=0x7f030000;
    }
    public static final class string {
        public static final int app_name=0x7f040000;
    }
}

소스를 보니 가장 눈에 띄는 것은 DO NOT MODIFY 이다.

손대지 말라고?

왜그럴까?

한번 이클립스에서 이 파일을 수정해보자.


아무리 수정하려고 해도 글자가 입력되지 않을 것이다. 왜냐하면 자동으로 generate 되는 파일이기 때문에...


이 파일에는 여러개의 내부 클래스(inner class)들이 존재한다. 각 클래스들이 뭔지는 모르겠지만, 두번째와 세번째 클래스는 프로젝트의 res 디렉토리 밑에 있는 drawable 과 layout이라는 디렉토리 명과 동일하다. 그리고, 그 안에 있는 파일의 이름과 동이란 것을 확인할 수 있다.


즉, 이 자바 파일에서는 우리의 이클립스 프로젝트의 res 디렉토리에 선언되어 있는 파일들을 접근할 수 있도록 레퍼런스를 제공한다고 생각하면 된다. 다시 말해서, 아이콘 파일을 사용하기 위해서는 R.drawable.icon 으로 사용하면 되고, main.xml 파일에 정의된 레이아웃을 가져오기 위해서는 R.layout.main 으로 접근하면 된다는 의미가 된다.


마지막으로 그럼, 레이아웃을 추가하면 어떻게 되는지 보자.

res/layout/test.xml 파일을 만들어보자.

그 파일을 만들고, main.xml에 있는 내용들을 복사해서 붙여놓아 보자.

그러면,  R.java 파일은 다음과 같이 자동으로 수정된다.

/* AUTO-GENERATED FILE.  DO NOT MODIFY.
*
* This class was automatically generated by the
* aapt tool from the resource data it found.  It
* should not be modified by hand.
*/

package com.min.hello;

public final class R {
    public static final class attr {
    }
    public static final class drawable {
        public static final int icon=0x7f020000;
    }
    public static final class layout {
        public static final int main=0x7f030000;
        public static final int test=0x7f030001;
    }
    public static final class string {
        public static final int app_name=0x7f040000;
    }
}


참고 : 이 내용은 http://code.google.com/android/index.html 의 내용을 참조하여 작성되었음.

Posted by tuning-java
,