简体   繁体   English

为什么新的 Android 功能(Proto DataStore)仍然生成 Java 代码而不是 Kotlin?

[英]Why are new Android features (Proto DataStore) still generating Java code and Not Kotlin?

I'm investigation我在调查

implementation  "androidx.datastore:datastore-core:1.0.0-alpha01"
implementation  "com.google.protobuf:protobuf-javalite:3.10.0"

via this codelab通过这个代码实验室

I do not understand why the associated DataStore generated classes are Java我不明白为什么关联的 DataStore 生成的类是 Java

I thought Google announced that Kotlin had replaced Java as the primary Android development language?我以为 Google 宣布 Kotlin 已经取代 Java 成为主要的 Android 开发语言?

I was under the impression Kotlin had many advantages over Java我的印象是 Kotlin 比 Java 有很多优势

is the issue proto buffers do not support Kotlin?问题是 proto 缓冲区不支持 Kotlin 吗?

I can answer that quickly!我可以这么快回答!

According to codelab link you shared, If you visit page 6 first line indicates something where your answer relies:根据您共享的 codelab 链接,如果您访问第 6 页,第一行表示您的答案所依赖的内容:

Protocol buffers are a mechanism for serializing structured data.协议缓冲区是一种用于序列化结构化数据的机制。 You define how you want your data to be structured once and then the compiler generates source code to easily write and read the structured data.您可以一次定义数据的结构方式,然后编译器生成源代码以轻松编写和读取结构化数据。

So, basically under the hood, library is using code generator plugin that generates classes required for protobuf to work with project.因此,基本上在幕后,库正在使用代码生成器插件来生成 protobuf 与项目一起工作所需的类。 (Yes, you can relate same thing with data-binding where you write code on xml and under the hood there's generated class that actually implements that logic for you) (是的,您可以将相同的事情与数据绑定联系起来,您可以在 xml 上编写代码,并且在后台生成的类实际上为您实现了该逻辑)

And that's the reason 'generated classes are in Java'.这就是“生成的类在 Java 中”的原因。 It has nothing to do with source in this context (Java/Kotlin support to library) .它与此上下文中的源无关(Java/Kotlin 对库的支持) Anything written in Java will seamlessly work on Kotlin and vice versa (Considering Android development context)任何用 Java 编写的东西都可以在 Kotlin 上无缝运行,反之亦然(考虑 Android 开发环境)

Side note: You can also relate it to annotation processor where we actually generate Java code based on annotation on any criteria.旁注:您还可以将其与注解处理器相关联,在该处理器中我们根据任何标准的注解实际生成 Java 代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 是否可以将原型数据存储类生成为 Kotlin 而不是 Java - Is it possible to generate proto datastore classes as Kotlin instead of Java Jetpack 原型数据存储 - gradle 配置和 Kotlin dsl - Jetpack proto datastore - gradle config with Kotlin dsl Android如何使用2.2的新功能并仍保持与2.1 API的代码兼容 - Android how to use new features of 2.2 and still keep code compatible with 2.1 api Android Dagger 2无法在Java / Kotlin项目中生成组件 - Android Dagger 2 not generating component in java/kotlin project 为什么这个 Kotlin/Android 代码会抛出 java.lang.ArrayIndexOutOfBoundsException? - Why this Kotlin/Android code throws java.lang.ArrayIndexOutOfBoundsException? 新的 Jetpack Datastore 是否仅适用于 Kotlin? - Is new Jetpack Datastore only for Kotlin? Android N Java 8功能(Jack编译器)和Kotlin互操作 - Android N Java 8 features (Jack compiler) and Kotlin interop 如何测试 androidx (proto) 数据存储区,java.lang.ExceptionInInitializerError - How to test androidx (proto) datastore, java.lang.ExceptionInInitializerError 如何将此 Android Kotlin 代码转换为 Java 代码 - How to translate this Android Kotlin code to Java code 为什么此Kotlin代码仍然需要? 访问成员? - Why does this Kotlin code still require ?. to access members?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM