Home  >  Article  >  Java  >  What file is r.java?

What file is r.java?

醉折花枝作酒筹
醉折花枝作酒筹Original
2021-07-27 11:25:375025browse

The R.java file is automatically generated by the compiler and does not require developers to maintain it. R.java will automatically include all resources in the current application and create corresponding IDs based on these resources. R.java can be simply understood as the resource dictionary of the current Android application.

What file is r.java?

The operating environment of this tutorial: windows7 system, java10 version, DELL G3 computer.

1. Introduction to R.java file

In Android studio, the directory where R.java is located is: app/build/generated/source/r/arm/debug/com.example.dfanghu. The R.java file in the myapplication/R.java

gen directory is automatically generated by the compiler and does not require developers to maintain it. R.java will automatically collect all resources in the current application and establish corresponding IDs based on these resources, including: layout resources, control resources, String resources, Drawable resources, etc. We can simply understand R.java as the resource dictionary of the current Android application.

Under the premise that the current project cannot contain any errors, the R.java file is manually deleted, and the compiler will immediately regenerate an R.java file; a new resource is added to the res/ directory, and the compiler The processor will also immediately include the ID of this resource into the R.java file. , but there is a premise, that is, the current project cannot contain any errors. When it is found that R.java does not include this resource after updating it, then you need to check whether there are errors in the current project.

2. R.java file content analysis

R.java file in HelloWorld project

package com.android.hellworld;

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=0x7f040001;
        public static final int hello=0x7f040000;
    }
}

The R.java file contains attr, drawable, layout, string, etc. by default There are four static inner classes. Each static inner class corresponds to a resource. For example, the layout static inner class corresponds to the interface file in the layout. The static constants in each static inner class define a resource identifier, such as public static final int main=0x7f030000; corresponds to the main.xml file in the layout directory.

Since there is currently only one image file icon.png in the [drawable-*dpi] directory, icon.png files with the same name and different details at this time always have only one icon attribute in the drawable internal class. If we add another picture in the [drawable-*dpi] directory, an icon attribute will be automatically added to the internal class.

PS: Add naming rules for resources: resource files can only start with lowercase letters and underscores, and only [a-z0-9_.] characters can appear in subsequent names, otherwise the R.java file It will not update automatically, and eclipse will prompt an error.

Understand the source of the content in the R.java file, that is, when the developer adds a file of the corresponding type in any subdirectory in the res/ directory, ADT will add the corresponding internal class in the R.java file A constant of static int type is automatically generated to index the added files. If a new interface is added to the layout directory, the corresponding static int constant will also be added to the public static final class layout. On the contrary, when we delete any file in the res directory, its corresponding record in R.java will be automatically deleted by ADT.

In addition to the indexing function of automatically marking resources, the R.java file also has another main function. If a resource in the res directory is not used in the application, it will be When compiling, the system will not compile the corresponding resources into the APK package of the application, which can save resources on the Android phone.

3. How to reference the required resources through the R.java file

1. Reference resources in the java program

Reference according to the java syntax: R .resource_type.resource_name

Note: resource_name does not require a file suffix

The Android system itself comes with a lot of resources, we can also reference them, we just need to add "Android. "To declare that the resource comes from the Android system, that is: Android.R.resource_type.resouce_name

Example: icon.png under [drawable-*hpi]

The image is in the R.java file The mapping is the icon parameter under the drawable internal class. If you want to obtain the resource, the writing method is: R.drawble.icon

2. Quoting the resource in the XML file

Format: @[ package:]type/name

is obtained through "@drawable/icon" in xml files, such as main.xml and AndroidMainfest.xml files. Among them, "@" represents the R.java class. "drawable" represents the static inner class "drawable" in R.java, "/icon" represents the static attribute "icon" in the static inner class "drawable", and this attribute can point to "drawable-* in the res directory dpi" icon.png resource. Other types of files are similar. All resources defined in R files can be obtained through "@Static_inner_classes_name/resource_name". Such as "@id/button", "@string/app_name".

If you are accessing the files that come with the Android system, you need to add the package name "Android:". For example: android:textColor="@android:color/red"

4. How to add a resource record to the R.java file

In the layout file, when we need to add the Id attribute as an identifier for some components, we can use the following expression: "@ id/string_name", where " " means adding it to the internal class named id in R.java a record. For example: "@id/button" means adding a constant named button to the static internal class id in the R.java file. This constant is the identifier of the resource. If the static inner class id does not exist, it will be generated first.

Recommended related video tutorials: Java video tutorial

The above is the detailed content of What file is r.java?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn