LoginSignup
63
65

More than 5 years have passed since last update.

ButterKnife 7.0への移行ガイド

Last updated at Posted at 2015-06-29

引用: http://yslibrary.net/2015/06/29/how-to-migrate-to-butterknife-7/

ButterKnife 7.0への移行ガイド

2015/06/28、ButterKnifeの7.0.0がリリースされました。

6.x系からAPIの変更が何点かあるので、簡単に移行手順をまとめます

アノテーション

@InjectView/@InjectViews -> @Bind

Viewを変数にひもづけるアノテーションが、@Bindに変更になりました。

複数の場合も@Bindです。とりあえず全部置換しましょう。

@Optionalの廃止

レイアウトに存在しないかもしれないViewの時、今までは@Optionalを利用していましたが、7.0からは@Nullableを使います。support-annotationsライブラリの@Nullableでも、別のライブラリの@Nullableでも構わないようです。

メソッド名

下記のように変更になっています。

ButterKnife.inject -> ButterKnife.bind

ButterKnife.reset -> ButterKnife.unbind

余談ですが、Fragmentの場合はonDestroyViewButterKnife.unbindするようにしましょう。

Githubでソース見てると忘れてるものを偶に見かけます。

Proguard設定

ButterKnifeによって自動生成されるクラス名がFooActivity$$ViewInjectorからFooActivity$$ViewBinderに変更されたので、Proguard設定も変える必要があります。

下記のようになります。

-keep class butterknife.** { *; }
-dontwarn butterknife.internal.**
-keep class **$$ViewBinder { *; }

-keepclasseswithmembernames class * {
    @butterknife.* <fields>;
}

-keepclasseswithmembernames class * {
    @butterknife.* <methods>;
}

新しく追加された機能

Resource Binding

stringやbool, color等のxmlに定義されたリソースを変数にバインド出来るようになりました。

下記のアノテーションを使います。

  • @BindBool: R.boolbooleanにバインド
  • @BindColor: R.colorint/ClorStateListにバインド
  • @BindDimen: R.dimenint(ピクセルサイズ)あるいはfloat(正確な数値)にバインド
  • @BindDrawable: R.drawableDrawableにバインド
  • @BindInt: R.intintにバインド
  • @BindString: R.stringStringにバインド

こちらからは以上です。

参考: butterknife/CHANGELOG.md at master · JakeWharton/butterknife

63
65
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
63
65