Convert Java File to Kotlin の後に その2 「apply」

View や Presenter内での処理振り分け.


private void showFilterLabel() {
    switch (mCurrentFiltering) {
        case ACTIVE_TASKS:
            mTasksView.showActiveFilterLabel();
            break;
        case COMPLETED_TASKS:
            mTasksView.showCompletedFilterLabel();
            break;
        default:
            mTasksView.showAllFilterLabel();
            break;
    }
}

android-architecture/TasksPresenter.java at todo-mvp · googlesamples/android-architecture

apply

同じオブジェクトに対して複数のメソッドをすばやく呼び出す必要がある場合は、apply {} を使用します


private fun showFilterLabel() {
    tasksView.apply {
        when (filtering) {
            TasksFilterType.ACTIVE_TASKS -> showActiveFilterLabel()
            TasksFilterType.COMPLETED_TASKS -> showCompletedFilterLabel()
            else -> showAllFilterLabel()
        }
    }
}

「with」との区別が良く話題になっていますので見ておくといいです.

Mindorks | Become a complete and happy Android developer

Convert Java File to Kotlin の後に その1「メンバとコンストラクタ」

Convert Java File to Kotlin の後に その2 「apply」

Convert Java File to Kotlin の後に その3 「Null Safety」

Convert Java File to Kotlin の後に その4 「lateinit」

Convert Java File to Kotlin の後に その5 「String Templates」


アイコン叩いても何も反応しない Android Studio

[Help] - [Edit Custom VMOptions..]

GUI上から編集できるようになっておりますが.

ふと

AndroidStudio自体が起動できなくなりました.


$ open -a Android\ Studio.app
LSOpenURLsWithRole() failed for the application /Applications/Android Studio.app with error -10810.

なんだこれ...


$ open /Applications/Android\ Studio.app
LSOpenURLsWithRole() failed with error -10810 for the file /Applications/Android Studio.app.

直で叩く.


$ /Applications/Android\ Studio.app/Contents/MacOS/studio
2017-05-21 22:03:57.938 studio[3424:190690] Value of STUDIO_JDK: /Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home
2017-05-21 22:03:57.939 studio[3424:190690] allVms required 1.8*,1.8+
2017-05-21 22:03:57.941 studio[3424:190695] fullFileName is: /Applications/Android Studio.app/Contents/bin/studio.vmoptions
2017-05-21 22:03:57.941 studio[3424:190695] fullFileName exists: /Applications/Android Studio.app/Contents/bin/studio.vmoptions
2017-05-21 22:03:57.941 studio[3424:190695] Value of STUDIO_VM_OPTIONS is (null)
2017-05-21 22:03:57.942 studio[3424:190695] Processing VMOptions file at /Applications/Android Studio.app/Contents/bin/studio.vmoptions
2017-05-21 22:03:57.942 studio[3424:190695] Done
2017-05-21 22:03:57.942 studio[3424:190695] Processing VMOptions file at /Users/maochanz/Library/Preferences/AndroidStudio2.3/studio.vmoptions
2017-05-21 22:03:57.942 studio[3424:190695] Done
2017-05-21 22:03:57.942 studio[3424:190695] Processing VMOptions file at
2017-05-21 22:03:57.943 studio[3424:190695] No content found
Invalid maximum heap size: -Xmx4096m ###
2017-05-21 22:03:57.947 studio[3424:190695] JNI_CreateJavaVM (/Applications/Android Studio.app/Contents/jre/jdk) failed: 4294967290

コメントのつもりの ### が原因でした.


$ cat ~/Library/Preferences/AndroidStudio2.3/studio.vmoptions
# custom Android Studio VM options, see http://tools.android.com/tech-docs/configuration
-Xms1024m
-Xmx4096m ###
-XX:MaxPermSize=1024m
-XX:ReservedCodeCacheSize=440m
-XX:+UseCompressedOops
-XX:+HeapDumpOnOutOfMemoryError
-Dfile.encoding=UTF-8

直接テキストエディタで書き換えればよしとな.

お年寄りは自動車運転なんかよりマニュアル車のがいいと思います.


Gradle 3.5 に更新する方法

Gradle 3.5 released : androiddev


./gradlew wrapper --gradle-version 3.5 --distribution-type all

ちなみに以下を書き換える方法については


distributionUrl=https\://services.gradle.org/distributions/gradle-3.5-all.zip

Jake がコメント.

That's mostly fine, but it doesn't update the bootstrap script nor the bootstrap jar. Basically you just miss out on improvements to those two components which can help with things like OS compatibility and caching of the Gradle runtime.

コマンドラインから更新したほうが良さそうです.


開発者が見ておくべき /r/androiddev にみるAndroid環境の王道

それなりに王道の本筋ではないかと思えますがどう思います?

/r/androiddev survey time! : androiddev

以下, 箇条書きに.

端末は Google Nexus シリーズ.

Root化やROMカスタマイズはしない.

Linux はラップトップで Ubuntu (Unity).

Windows はデスクトップで10.

Mac はラップトップで 10.12 Sierra.

主携帯は Android, タブレットは持っていない.

開発OS は OSX Intl Core i7, SSD, メモリ16G.

アプリにタブレット向けデザインは作成しない.

minSdkVersion は 16 (4.1.x).

アーキテクチャは MVP.

DIライブラリは Dagger 2.

Rx ライブラリは RxAndroid + RxJava2.

代替のフレームワークは Cordova.

データのシリアライズは Gson.

データ永続化は Realm.

ネットワーク処理は Retrofit.

画像読込みは Picasso.

Viewバインドは ButterKnife.

思ったより RxJava2 の躍進が早いなあと思いました.


android-apt が不要となった ButterKnife

Butter Knife - Android library statistics - AppBrain

バージョンを上げてみると落ちるのでなぜかな, と.

これまでAndroid開発でannotation processingを実行したい場合はhvisser/android-aptを利用するのが王道となっていましたが、Android Gradle plugin version 2.2から公式にサポートが開始されました。

android-apt becomes obsolete - Qiita

2016-10-04
From apt to annotationProcessor
As of the Android Gradle plugin version 2.2, all functionality that was previously provided by android-apt is now available in the Android plugin. This means that android-apt is officially obsolete 😉

hvisser / android-apt / wiki / Migration — Bitbucket

Reflection を利用していた ButterKnife も予定通りに,

実行時に全力でReflectionするButter Knifeと違い・・・(c.f. Butter KnifeもAnnotation Processingする方式に切り替えるっぽい? => Split the compiler and runtime into separate artifacts. by serj-lotutovici · Pull Request #323 · JakeWharton/butterknife).

Butter Knife、今までありがとう。 Data Binding、これからよろしく。 - Qiita

JakeWharton on Aug 1, 2015
This dependency can probably be removed

Split the compiler and runtime into separate artifacts. by serj-lotutovici · Pull Request #323 · JakeWharton/butterknife

ButterKnife 8.4.0 で, Gradle Plugin の annotation processor を利用するようになり, android-apt を利用が不要になっていました.


-Note: If you are using the new Jack compiler with version 2.2.0 or newer you do not need the
-'android-apt' plugin and can instead replace `apt` with `annotationProcessor` when declaring the
-compiler dependency.

Just use 'annotationProcessor' for instructions. · JakeWharton/butterknife@716e1f3

この android-apt 利用時の記述が無駄にあると, フィールドにバインドされずにリスナーセットなど操作時に「んおっ?」となります.

まとめ

Gradle Android plugin 2.2.+ で ButterKnife 8.4.+ の場合 android-apt は不要.

以下2行のみ. proguardの記述不要.


dependencies {
  compile 'com.jakewharton:butterknife:8.4.0'
  annotationProcessor 'com.jakewharton:butterknife-compiler:8.4.0'
}

Butter Knife

Annotation Processor のこれまで流れを知っておけばハマることもなかったのだろうと思います.

ButterKnife GitHubリポジトリ の issue などから調べようとすると, 謎の漢字での書き込みが激しく逆に混乱してしまいました.

Issues · JakeWharton/butterknife

com.android.support.* の dependencies のバージョンが揃わない件