Android/kotlin/javaからkotlinへの変換
提供: 初心者エンジニアの簡易メモ
2020年2月17日 (月) 14:33時点におけるAdmin (トーク | 投稿記録)による版 (→"Platform declaration clash: The following declarations have the same JVM signature"エラー)
androidstudioでjavaからkotlinへの変換
- MainActivity.javaを開いた状態で、codeの"Convert Java File to Kotlin File"を選択
- MainActivity.javaがMainActivity.ktへ変更され、コードもkotlinとなる
- GradleにKotlinを認識させるために、Tool/Kotlin/ConfigureKotlinInProjectを選択
"Assignments are not expressions, and only expressions are allowed in this context"エラーの時
前
var bytesRead: Int
while ((bytesRead = dataInputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead)
}
後
var bytesRead: Int
do {
bytesRead = dataInputStream.read(buffer)
if (bytesRead == -1)
break
outputStream.write(buffer, 0, bytesRead)
} while (true)
参考:https://discuss.kotlinlang.org/t/assignment-not-allow-in-while-expression/339
"Platform declaration clash: The following declarations have the same JVM signature"エラー
publicプロパティはsetterが自動でつき、名前がかぶるため。
前
class HogeClass {
companion object {
var name = ""
fun setName(name: String) {
this.name = name
}
fun getName(): String {
return this.name
}
}
}
後
class HogeClass {
companion object {
private var name = ""
fun setName(name: String) {
this.name = name
}
fun getName(): String {
return this.name
}
}
}
プロパティにprivateをつければ良い。
