I was playing around Kotlin and learned about ViewBinding and how it works. It seems quite easy for me but there is one thing I don't understand. I edit activity_main.xml and instantly get hints on ActivityMainBinding instance's fields. For example, I add a new TextView in xml layout and AndroidStudio immediately shows me hints on this View, even though the class has not been generated yet. Is it just an AndroidStudio built-in analyzer feature or something else? Does it really calculate this in real time and allows you to see the class fields that have not yet been generated, but will appear at the next build of the application?
我在科特林周围玩耍,了解了视图绑定及其工作原理。这对我来说似乎很容易,但有一件事我不明白。我编辑了active_main.xml,并立即获得有关ActivityMainBinding实例的字段的提示。例如,我在XML布局中添加了一个新的TextView,androidStudio会立即向我显示关于这个视图的提示,即使类还没有生成。它只是一个androidStudio内置分析器功能还是其他什么?它是否真的实时计算并允许您查看尚未生成但将在应用程序的下一次构建中出现的类字段?
更多回答
Please provide enough code so others can better understand or reproduce the problem.
请提供足够的代码,以便其他人能够更好地理解或重现问题。
“Even though the class isn’t generated yet” why do you think this is the case? It generates the new binding class automatically. The compiler is constantly recompiling your Kotlin code as you type it. That’s how it can show you errors before you build the app.
“即使类还没有生成”,您认为为什么会这样呢?它会自动生成新的绑定类。当您键入Kotlin代码时,编译器会不断地重新编译它。这就是它在你构建应用程序之前向你显示错误的方式。
@Tenfour04 I found ActivityMainBinding.java file in biuld/generated directory, but it changes only when I run application. So there's something under the hood of IntelliJ that I can't see in real time?
@Tenour04我在biuld/Generated目录中找到了ActivityMainBinding.Java文件,但它只有在我运行应用程序时才会改变。所以在IntelliJ的引擎盖下有我不能实时看到的东西?
I guess I never really thought about it but that must be the case. The stuff in build
is where it copies all the relevant code for a build and that gets compiled fully when doing a build. That’s not what it’s looking at to generate code hints and errors as you are programming.
我想我从来没有真正想过这一点,但肯定是这样的。Build中的东西是复制构建的所有相关代码,并在进行构建时完全编译这些代码。这不是它在编程时生成代码提示和错误所要考虑的。
优秀答案推荐
我是一名优秀的程序员,十分优秀!