英文:
Kotlin to Java conversion not working in Android studio Electric Eel. Is there another method to convert?
问题
conversion functions are unavailable
I accidentally created a project in Kotlin, while creating an activity I noticed that my class is not in Java, I tried to use conversion techniques but none of them are available(show Kotlin bytecode & decompile to Java.
Is there another way to convert the project to Java?
英文:
conversion functions are unavailable
I accidentally created a project in Kotlin, while creating an activity I noticed that my class is not in Java, I tried to use conversion techniques but none of them are available(show Kotlin bytecode & decompile to Java.
Is there another way to convert the project to Java?
答案1
得分: 0
以下是您要的翻译部分:
"有一些IDE(如IntelliJ)肯定具有内置工具,可以将Kotlin代码反编译为Java,但它们可能会生成一些比您手工编写的Java代码更冗长的Java代码。
很抱歉要说,取决于您的初始代码有多复杂,您可能最好尝试手工将Kotlin代码翻译成Java。一般来说,Java语言中有更多的模板代码和冗余代码。
例如,Kotlin允许您这样做:
val myString = "yo"
val myInt = 1
而Java会要求您明确地为变量指定类型,如:
String myString = "yo"
int myInt = 1
函数也不同。Kotlin使用fun
来定义函数,首先是参数的名称,然后是类型,然后在方法参数和函数体之间有返回类型。
// 返回void的函数可以省略void关键字
fun myVoidFunction() {
println("oh hello!")
}
fun myFunctionReturnsAString(someName: String): String {
return ("oh hi, $someName!")
}
Java在函数开头使用返回类型,然后使用Type parameter
格式(与Kotlin相反)。
public void myVoidFunction() {
System.out.println("oh hello!");
}
public String myFunctionReturnsAString(String param) {
return (param);
}
如果您的代码相当简单,这可能足够帮助您解决问题。如果更复杂,您可以尝试使用IntelliJ的社区版之类的工具将其反编译为Java,然后手动清理它。此页面有一个部分描述如何使用IntelliJ从Kotlin转换为Java。
https://www.baeldung.com/kotlin/convert-to-java
绝对不要指望它能够完全干净地完成一切。您可能仍然需要检查Java中的生成代码,并修复代码以使其达到生产就绪状态。祝您好运!"
英文:
There's definitely some IDEs (like IntelliJ) that have built-in tools that might be able to decompile Kotlin code into Java, but they're probably likely to produce some pretty verbose Java code than what you might write by hand.
Sorry to say that depending on how complicated your initial code is, you may be better off trying to translate the Kotlin code to Java by hand. Generally, Java has more boilerplate and redundancy in the language than Kotlin.
For example, Kotlin will let you do something like:
val myString = "yo"
val myInt = 1
Where Java will want you to explicitly type variables like
String myString = "yo"
int myInt = 1
Functions also differ.
Kotlin uses fun
to define functions. It has the name of the parameter first followed by the type and then has the return type between the method params and the body.
// functions that return void can omit the void keyword
fun myVoidFunction() {
println("oh hello!")
}
fun myFunctionReturnsAString(someName: String): String {
return ("oh hi, $someName!")
}
Java has the return type at the start of the function and then uses the format of Type parameter
(reverse of Kotlin)
public void myVoidFunction() {
System.out.println("oh hello!");
}
public String myFunctionReturnsAString(String param) {
return (param);
}
If your code is pretty simple, that might be enough to get you unstuck. If it's more complicated, you might try doing the decompile to Java from something like the community version of IntelliJ and then hand clean it up afterwards. This page has a section describing how to use IntelliJ to go from Kotlin -> Java
https://www.baeldung.com/kotlin/convert-to-java
Definitely don't trust it to do everything cleanly though. You'll likely still need to go through what it creates in Java and fix up code and make it production ready. Good luck!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论