LoginSignup
1
3

More than 5 years have passed since last update.

KotlinからJavaにDecompileする

Posted at

自分が書いたKotlinがJavaではどうなるか気になりませんか?

Android Studioが提供するbyteコードのDecompileでJavaのコードが確認できますので、そのやり方を調べます。

Kotlin Decomplie

Android StudioでKotlinプラグインがインストールされていれば、Kotlin Bytecodeの確認ができます。

Tools > Kotlin > Show Kotlin Bytecode
スクリーンショット 2018-09-27 23.54.35.png

Show Kotlin Bytecodeをクリックすると下記のようにKotlin Bytecodeが確認できます。Bytecodeを確認したりJavaコードでの確認が可能です。
スクリーンショット 2018-09-27 23.57.15.png

上記の画像でDecompileをクリックするとJavaコードでデコンパイルの結果が確認できます。

デコンパイルしてJavaで確認

下記はwhenで作成したサンプルコードです。

when (item.itemId) {
    R.id.navigation_home -> {
        message.setText(R.string.title_home)
        return@OnNavigationItemSelectedListener true
    }
    R.id.navigation_dashboard -> {
        message.setText(R.string.title_dashboard)
        return@OnNavigationItemSelectedListener true
    }
    R.id.navigation_notifications -> {
        message.setText(R.string.title_notifications)
        return@OnNavigationItemSelectedListener true
    }
}

デコンパイルしてJavaコードの確認できます。

switch(item.getItemId()) {
    case 2131230822:
    ((TextView)MainActivity.this._$_findCachedViewById(id.message)).setText(2131623990);
    return true;
    case 2131230823:
    default:
    return false;
    case 2131230824:
    ((TextView)MainActivity.this._$_findCachedViewById(id.message)).setText(2131623991);
    return true;
    case 2131230825:
    ((TextView)MainActivity.this._$_findCachedViewById(id.message)).setText(2131623992);
    return true;
}

Bytecodeが読めたらいいですが、慣れないのでDecomplieボタンをクリックして上記のようにJavaで確認した方が良いと思います。

1
3
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
1
3