【Android Studio】Rクラスの確認方法

Sara

Android Studio 画面左側にあるプロジェクト構造に res フォルダがあります。

この res フォルダにある全てのレイアウトや画像には ID が付けられていて、R クラスで管理されています。

例えば strings.xml にはアプリ名が定義されています。

<string name="app_name">Sample</string>

この値にも以下のように ID が付けられています。

この記事ではこの R ファイルの確認方法を紹介します。

準備

開発環境

Android StudioElectric Eel | 2022.1.1
Android EmulatorNexus 4(API 33)
compileSdk / targetSdk33
minSdk19

プロジェクトの用意

Sample という名前のプロジェクトを作成して、パッケージ名は com.example.sample としています。

activity_main.xml に TextView と Button を用意して、それぞれに id 属性をつけています。

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/myTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/myButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ボタン"
        android:layout_marginTop="40dp"
        app:layout_constraintTop_toBottomOf="@id/myTextView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

Rクラスの確認方法

ステップ1

アプリをエミュレータで実行します。一度実行したら停止して大丈夫です。

ステップ2

Android Studio 画面左側にあるプロジェクト構造の表示方法を Project にします。

ステップ3

app → build → intermediates → apk → debug → app-debug.apk を開きます。

ステップ4

classes.dex を選択します。

ステップ5

下に表示されるプロジェクト構造からパッケージ名のフォルダを開きます。

ステップ6

R$id の上で右クリックして Show Bytecode を選択します。

ステップ7

activity_main.xml に書いた TextView と Button の ID を確認できます。

drawable や string などの R ファイルも同じ手順で開くことができます。

guest
0 Comments
Inline Feedbacks
View all comments
ABOUT ME
Sara
Sara
運営者
書籍やオンライン講座でプログラミングを勉強してフリーランスのプログラマーになりました。
このサイトでは「わかりやすく・シンプル」をモットーに、プログラミングの基礎からアプリ開発まで紹介します。
独学でプログラミングを勉強をしている方、基礎は勉強したけれど次に何をすれば良いか分からない...という方のお役に立てるサイトを目指しています。
0
この記事にコメントするx
記事URLをコピーしました