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.

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