LoginSignup
0
0

More than 3 years have passed since last update.

KotlinのExtensionでNoSuchMethodError

Last updated at Posted at 2019-10-23

KotlinのExtension機能で詰まったのでメモ。
OS側のクラスを拡張して作ったんですが「そんなメソッドねぇよ!」と怒られるの巻。

環境

Android, Kotlin 1.3.50

背景

android.os.Parcel にBooleanを書くメソッドがないので
Parcel.readBoolean, Parcel.writeBoolean として作った

fun Parcel.writeBoolean(value: Boolean) {
    if (value) {
        this.writeByte(1)
    } else {
        this.writeByte(0)
    }
}

fun Parcel.readBoolean(): Boolean {
    return this.readByte() == 1.toByte()
}

現象

OS ver device result
Android10 Pixel3 問題ない
Android9 Pixel3a Parcelで落ちる
Android7 HUAWEI honor 8 Parcelで落ちる
Android6 Galaxy Grand Prime Parcelで落ちる

エラー

Parcelの拡張関数としてアプリで定義しているのに、なぜかOS側のjarを読み込もうとして落ちている

Fatal Exception: java.lang.NoSuchMethodError: No virtual method writeBoolean(Z)V in class Landroid/os/Parcel; or its super classes (declaration of 'android.os.Parcel' appears in /system/framework/framework.jar!classes2.dex)

対応内容

readCustomBoolean, writeCustomBoolean にRenameした

反省

めちゃ一般的な名前はやめよう、、、

参考

Kotlinの拡張関数のガイドライン
https://dev.classmethod.jp/smartphone/kotlin-extensions-guideline/

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0