我如何使用 Android Studio 查看 BR 文件中生成的代码的来源?

How do I see with Android Studio from where the generated code in a BR file originates?

我正在进入一个新的代码库并且必须了解它是如何工作的。有一个自动生成的 BR 文件。我认为它来自数据绑定库。那个 BR 文件包含很多值,但我看不出有什么方法可以自动搜索代码中指定该文件内容的区域。

当我有例如:

    public static final int currentDate = 35;
    public static final int currentEmail = 36;

如何查找导致这些变量存在的代码的来源?

通常生成的代码依赖于这个路径下:

project/app/build/generated/rs

的路径

To check that in Android studio, you can change project structure view from 'Android' to 'Project' and follow above path to see generated code.

这有点难找。您可以右键单击一个字段并选择 "Find Usages",但这只会向您显示它的使用位置,而不是它的来源。 BR.java 中的字段是从标有 @Bindable Like

的方法生成的
@Bindable
public String getSomeStringValue() {
    return "Some arbitrary string.";
}

这将导致 BR.java 文件中的字段如下所示:

public static final int someStringValue = 126;

因此,为了能够找到 BR 文件中某些内容的来源,您可以获取 BR.java 文件中的内容并点击 "get",然后进行搜索。我自己可能会搜索 @Bindable

I see no way to automatically search for the area in the code from where the content of that file gets specified.

首先,BR 文件是自动生成的,它不会让您知道从哪里生成的字段。就像您无法搜索 R 字段一样,它们属于这些字段。

但是 BR 文件中通常会生成两种类型的字段。

  • 第一个是 <variable 在您的布局中定义的。

    <variable
        name="model"
        type="sample.data.Model"/>
    
  • 第二个是@Bindable字段。

    @Bindable
    public String getPassword() {
        return password;
    }
    

所以 BR 文件看起来像

public class BR {
        public static final int _all = 0;
        public static final int model = 0;
        public static final int password = 1;
}

_all 是一些默认整数,它总是生成。