Platform/Android

[Resource] R.java

개랭갱깽스타 2020. 6. 8. 09:55

0.

다시 처음부터 안드로이드 책[실무에 바로 적용하는 안드로이드 프로그래밍] 을 보고 있다. R.java는 맨 처음(p.21) 나오는 부분이고, 형광펜으로 밑줄도 쳐놨는데, 처음보는 느낌이였다. 충격을 받아 R 클래스에 대해서 정리해보았다.


1. 이게 뭐지?

리소스를 코드영역에서 식별할 수 있게 도와주는 파일

안드로이드 빌드 프로세스에서 자동으로 생성해준다. 더 정확히 말하면, AAPT(Android 스튜디오 및 Android Gradle 플러그인이 앱의 리소스를 컴파일하고 패키징하는 데 사용하는 빌드 도구)가 만들어 준다.

= 수정하면 안된다!

앱을 실행하기 위해 빌드할 때 마다 자동으로 변경된다.

  • 위치: app/build/generated/source/debug/앱패키지명/R.java

 

2. 코드

R.java

  • 각각의 리소스를 내부 클래스명인 string, drawble, layout 등으로 구분하고 있다.
  • 각각의 변수는 대부분 파일명을 이용한다.
public static final class string {
    public static final int abc_action_bar_home_description=0x7f0b0000;
    public static final int abc_action_bar_home_description_format=0x7f0b0001;
    ...
}

public static final class drawable {
    public static final int abc_ab_share_pack_mtrl_alpha=0x7f070000;
    public static final int abc_action_bar_item_background_material=0x7f070001;
    ...
}

 public static final class layout {
    public static final int abc_action_bar_title_item=0x7f0a0000;
    public static final int abc_action_bar_up_container=0x7f0a0001;
    ...
}

따라서

  • res 밑에 임의의 폴더를 생성해서는 안된다!
  • 리소스 파일명도 자바 명명규칙으로 생성해야 한다!
  • 리소스 파일명에 대문자를 사용할 수 없다.

 

3. 더 자세히

실제로 매핑되는 모습을 보면, 아래와 같다!

R.java - class string 안의 변수

public static final class string {
    ...
 	public static final int hello_world=0x7f0b002c;
    ...
}

strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello_world">Hello world!</string>
	...
</resources>

 

코드 내부에서 사용

R.string.hello_world

XML 내부에서 사용

@string/hello_world

 

4. 궁그매

  • DataBinding을 이용할 때는 생성되지 않았다. 왜 그럴까?? (2020.06.08)

아시는 분 계시면 댓글로 달아주세요~

 

참고

실무에 바로 적용하는 안드로이드 프로그래밍
국내도서
저자 : 빌 필립스(Bill Phillips),브라이언 하디(Brian Hardy),크리스 스튜어트(Chris Stewart),크리스틴 마시캐노(Kristin Marsicano) / 심재철역
출판 : 제이펍 2016.06.20
상세보기

https://medium.com/@hongbeomi/r-java%EB%9E%80-b352a95b74a8

 

R.java란 ?

R.java 파일은 무엇에 쓰는 것일까?

medium.com

https://developer.android.com/guide/topics/resources/accessing-resources?hl=ko

 

리소스 액세스  |  Android 개발자  |  Android Developers

Once you provide a resource in your application (discussed in Providing Resources ), you can apply it by referencing its resource ID. All resource IDs are defined in your project's R class, which the aapt tool automatically generates. When your application

developer.android.com

 

반응형

'Platform > Android' 카테고리의 다른 글

[RecyclerView]  (0) 2020.06.08
[Concept]  (0) 2020.06.08
[Activity] 그놈의 생명주기(LifeCycle)  (0) 2020.06.07
[Receiver]  (0) 2020.06.05
[다운로드]  (0) 2020.06.02