Java から Kotlin 移行の進捗を「cloc」で確認する

これを確認したかったのです.

特定のディレクトリのファイルやコード行数の確認.

googlesamples/android-architecture at todo-mvp-rxjava

「cloc」てやつを使います.

AlDanial/cloc: cloc counts blank lines, comment lines, and physical lines of source code in many programming languages.

こんな感じで結果表示されます.


for d in ./*/ ; do (cd "$d" && echo "$d" && cloc --vcs git); done

Android プロジェクトの場合, アプリのルートディレクトリでやると,


$ pwd
/Users/maochanz/projects/PlainTemplates
$ cloc .
     674 text files.
     567 unique files.
     140 files ignored.

github.com/AlDanial/cloc v 1.74  T=10.63 s (50.4 files/s, 8239.6 lines/s)
--------------------------------------------------------------------------------
Language                      files          blank        comment           code
--------------------------------------------------------------------------------
JSON                            253              0              0          30758
XML                             223           1749            233          17707
Java                             36             38          24757          10428
Kotlin                           18            245            324            993
Bourne Again Shell                1             19             20            121
DOS Batch                         1             24              2             64
Groovy                            3              8              3             55
Prolog                            1              3              0             18
--------------------------------------------------------------------------------
SUM:                            536           2086          25339          60144
--------------------------------------------------------------------------------

となって,

build ディレクトリもカウントされるので,

src ディレクトリ以下で実行するといいでしょう.


$ pwd
/Users/maochanz/projects/PlainTemplates/app/src
$ cloc .
      77 text files.
      76 unique files.
       1 file ignored.

github.com/AlDanial/cloc v 1.74  T=0.44 s (171.9 files/s, 7442.6 lines/s)
-------------------------------------------------------------------------------
Language                     files          blank        comment           code
-------------------------------------------------------------------------------
XML                             58            177             89           1463
Kotlin                          18            245            324            993
-------------------------------------------------------------------------------
SUM:                            76            422            413           2456
-------------------------------------------------------------------------------

Kotlin による冗長なコードの削減具合もよく分かるはずです.

CLOC -- Count Lines of Code


【AndroidStudio 3.0】「flavor dimension」とは何?

AndroidStudio 3.0 にアップデートしましたが,

こんな build.gradle で 以下のエラーです.


android {
  // ...
}

productFlavors {
  flavor1 {
    // ...
  }
}

buildTypes {
  release {
    // ...
  }
  debug {
    // ...
  }
}


Error:All flavors must now belong to a named flavor dimension. Learn more at https://d.android.com/r/tools/flavorDimensions-missing-error-message.html

StackOverflowで調べてみると以下の記述で大丈夫と書かれている.

Android Studio 3.0 Flavor Dimension Issue - Stack Overflow


android {
  // ...

  flavorDimensions "default" // OK
  //flavorDimensions "versionCode" // OK

}

productFlavors {
  flavor1 {
    // ...
  }
}

buildTypes {
  release {
    // ...
  }
  debug {
    // ...
  }
}

これでビルドは通る.

なんだか気持ち悪いし意味が不明すぎますね.

 

試してみる

いろいろやってみましたが, 以下のような記述がOKです.


android {
  // ...

  flavorDimensions "a"
}

productFlavors {
  flavor1 {
    // ...
  }
}

buildTypes {
  release {
    // ...
  }
  debug {
    // ...
  }
}

productFlavor を複数にする.


android {
  // ...

  flavorDimensions "a"

}

productFlavors {

  flavor1 {
    // ...
  }

  flavor2 {
    // ...
  }

  flavor3 {
    // ...
  }

  flavor4 {
    // ...
  }

}

buildTypes {
  release {
    // ...
  }
  debug {
    // ...
  }
}

flavorDimensions で productFlavor を2段階に分ける.


android {
  // ...
  flavorDimensions "a", "b"
}

productFlavors {

  flavor1 {
    dimension "a"
    // ...
  }

  flavor2 {
    dimension "a"
    // ...
  }

  flavor3 {
    dimension "b"
    // ...
  }

  flavor4 {
    dimension "b"
    // ...
  }
}

buildTypes {
  release {
    // ...
  }
  debug {
    // ...
  }
}

dimension で階層化された Build Variant が作成される.

 

まとめ

もともと productFlavor では以下のようなものが設定できました.

- applicationId
- versionCode
- minSdkVersion
- versionName/Suffix
- ソースコードやリソースの位置
- dependencies

これらを階層化させたい場合に使うのがいいように思います.

まずは, 開発時の時間短縮でしょうか.


flavorDimensions "minSdkVersion", "others"

Migrate to Android Plugin for Gradle 3.0.0 | Android Studio

ビルド バリアントの設定 | Android Studio


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.

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