【Jetpack Compose】Compose Settings で数分で設定画面を作る

意外と面倒な設定画面が数分でできます。

This library provides a set of Settings like composable items to help android Jetpack Compose developers build complex settings screens without all the boilerplate

このライブラリは、開発者がボイラープレートなしで複雑な設定画面を構成できるアイテム詰め合わせです。

👉 alorma/Compose-Settings: Android #JetpackCompose Settings library hatena-bookmark

以下コピペですぐに表示できます。


implementation 'com.github.alorma:compose-settings-ui:$version'


SettingsMenuLink(
  icon = { Icon(imageVector = Icons.Default.Wifi, contentDescription = "Wifi") },
  title = { Text(text = "Link") },
  subtitle = { Text(text = "This is a longer text") },
  onClick = {},
)
Divider()
SettingsSwitch(
  icon = { Icon(imageVector = Icons.Default.Wifi, contentDescription = "Wifi") },
  title = { Text(text = "Switch") },
  subtitle = { Text(text = "This is a longer text") },
  onCheckedChange = { },
)
Divider()
SettingsCheckbox(
  icon = { Icon(imageVector = Icons.Default.Wifi, contentDescription = "Wifi") },
  title = { Text(text = "Checkbox") },
  subtitle = { Text(text = "This is a longer text") },
  onCheckedChange = { },
)
Divider()
SettingsSlider(
  icon = {
    Icon(
      imageVector = Icons.Default.BrightnessMedium,
      contentDescription = "Brightness Medium"
    )
  },
  title = { Text(text = "Slider") },
)
Divider()
SettingsList(
  title = { Text(text = "List") },
  subtitle = { Text(text = "Select a fruit") },
  items = listOf("Banana", "Kiwi", "Pineapple"),
  action = {
    IconButton(onClick = {  }) {
      Icon(
        imageVector = Icons.Default.Clear,
        contentDescription = "Clear",
      )
    }
  },
)

alorma/Compose-Settings: Android #JetpackCompose Settings library


UIだけでなく Preferences への読み書きも用意されています。


implementation 'com.github.alorma:compose-settings-storage-preferences:$version'

state を更新するとそのまま Preferences に書き込みされます。


val preferenceStorage = rememberPreferenceBooleanSettingState(
    key = "switch",
    defaultValue = false,
)
SettingsCheckbox(
    state = preferenceStorage, // *
    icon = {

超便利です!!

コードも良い参考にもなります。

👉 【Jetpack Compose】Icon() や Image() で ImageVector をより便利に使う hatena-bookmark


ビルド時間を計測・比較したいときの gradle(w) オプション

Terminal 開いてから。


./gradlew --profile --offline --rerun-tasks --max-workers=4 assembleDebug

👉 Compose におけるデベロッパーのエルゴノミクス  |  Jetpack Compose  |  Android Developers hatena-bookmark

結果がHTMLで出力されます。

Profile report Profiled build: assembleDebug

各オプションの意味については以下。

--profile
Generates a high-level performance report in the $buildDir/reports/profile directory. --scan is preferred.

--offline
Specifies that the build should operate without accessing network resources.

--rerun-tasks
You can force Gradle to execute all tasks ignoring up-to-date checks

--max-workers
Sets maximum number of workers that Gradle may use. Default is number of processors.

👉 Command-Line Interface hatena-bookmark

実行前にクリーンするのがいいそうです。


// On Mac or Linux, run the Gradle wrapper using "./gradlew".
gradlew clean

gradlew --profile --offline --rerun-tasks assembleFlavorDebug

👉 ビルドのプロファイリングを行う  |  Android デベロッパー  |  Android Developers hatena-bookmark


./gradlew clean && ./gradlew --profile --offline --rerun-tasks --max-workers=4 assembleDebug

10回やってみてどうなるか。

ある平均的なぶれのないビルド時間になりますかね。


【Jetpack Compose】Icon() や Image() で ImageVector をより便利に使う

コード記述のみでベクターのマテリアルアイコン使えます。

drawable の作成が不要なので便利、変更もしやすいです。


Icon(
  imageVector = Icons.Filled.Favorite,
  contentDescription = null
)


Image(
  imageVector = Icons.Filled.Favorite,
  contentDescription = null
)

悪い点としては、

絵柄が49個しかない

絵柄を見ながら選択できない

というところでしょうか。

対応策を考えてみましょう。

 

絵柄が49個しかない

compose 公式のアイコン群(2500個以上)を追加できます。


implementation "androidx.compose.material:material-icons-extended:x.y.z"

エディタのサジェスチョンも大量に増えます。

androidx.compose.material:material-icons-extended

ただ、少し読み込みが遅い。

そこらへんは、公式に注意点があります。

警告: material-icons-extended は大規模なライブラリであり、APK のサイズに影響する可能性があります。そのため、製品版ビルドでは R8/Proguard を使用し、使用されていないリソースを取り除くことを検討してください。また、サイズが大きいために、開発中は、プロジェクトのビルド時間と Android Studio のプレビューの読み込み時間が増加する可能性があります。

👉 Compose のリソース  |  Jetpack Compose  |  Android Developers hatena-bookmark

サイズにも注意する必要があるようです。

 

絵柄を見ながら選択できない

これがすごく困ります。

別で、WEB画面を開くか、Android Studio の Vector Asset Tool を開くかして、絵柄を見て選択して、その名前から、サジェスチョンさせる、くらいしか方法がない。なんかいい方法あったら教えなさいよ。

Material Symbols and Icons - Google Fonts

👉 Material Symbols and Icons - Google Fonts hatena-bookmark

 Vector Asset Tool



将来的には、ライブプレビュー(今現在はリテラルのみ)ですばやく見れるようになるのかもしれません。

Android Studio Electric Eel 以降では、ライブ編集を使用して Compose の開発を高速化できます。ライブ編集は、リテラルのライブ編集をより強力にしたものです。この機能では、プレビューを自動的に更新し、コードの変更をエミュレータまたはデバイスにデプロイすることで、コンポーザブルの更新の影響をリアルタイムで確認できます。

 

👉 Compose のツール  |  Jetpack Compose  |  Android Developers hatena-bookmark

 

まとめ

将来性を見越して、ImageVector を使って


@Composable
fun LikeButton() {
  Button(onClick = {}) {
    Icon(
      imageVector = Icons.Filled.ThumbUp,
      contentDescription = null
    )
    Spacer(Modifier.size(ButtonDefaults.IconSpacing))
    Text("Like")
  }
}

と書きたいです!

Icons.Filled.ThumbUp

ちなみに、これまでのように ベクター drawable を作成して、id で使う記述もできます。


Icon(
  painter = painterResource(id = R.drawable.ic_baseline_favorite_24),
  contentDescription = null
)

しかし、変更時に drawable 消し忘れでゴミが溜まりそう。

あと、

Icons.Default は Icons.Filled のエイリアス

だそうです。

👉 Android Jetpack Compose Icons doesn't contain some of the material icons - Stack Overflow hatena-bookmark

👉 【Jetpack Compose】Compose Settings で数分で設定画面を作る hatena-bookmark


【Dagger-Hilt】「androidx.hilt:hilt-lifecycle-viewmodel」を使ってはならない Dagger 2.34+

The Hilt Android Gradle plugin is applied but no com.google.dagger:hilt-android dependency was found.

The Hilt Android Gradle plugin is applied but no com.google.dagger:hilt-android dependency was found.

すいません、またハマっちゃいました、2回目です。

原因は以下なのですが。

androidx.hilt:hilt-lifecycle-viewmodel artifacts were deprecated in the Dagger 2.34 release in favor of native Hilt API.

👉 ComponentProcessingStep was unable to process '*Application_HiltComponents.SingletonC' · Issue #3257 · google/dagger hatena-bookmark

Migration steps:
...
4. Remove the old androidx.hilt:hilt-lifecycle-viewmodel dependency from your build.gradle file

👉 Release Dagger 2.34 · google/dagger hatena-bookmark

以下があると、ビルドでコケます。


implementation 'androidx.hilt:hilt-lifecycle-viewmodel:x.y.z'

知ってます、みんなハマっています。

 

対応方法

これでいけます。


dependencies {

  //implementation 'androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha03'
  //kapt 'androidx.hilt:hilt-compiler:1.0.0'

  implementation 'com.google.dagger:hilt-android:2.42'
  kapt 'com.google.dagger:hilt-compiler:2.42'

androidx.hilt.* だけでは、ビルドできないか、インストール→起動後に落ちます。まだあやしい。

androidx.hilt:hilt-lifecycle-viewmodel

 

なぜ、ハマるのか

公式リファレンスに記述があるんですよね、この記述。

Hilt と Jetpack の統合  |  Android デベロッパー  |  Android Developers

👉 Hilt と Jetpack の統合  |  Android デベロッパー  |  Android Developers hatena-bookmark

Hilt に隠れて Dagger のバージョンが見えづらくなってることにも原因があるように思います。

👉 Error: ComponentProcessingStep was unable to process 'AppApplication_HiltComponents.SingletonC' because 'DefaultActivityViewModelFactory' could not be resolved. hatena-bookmark


【Python】「pip search」の代わりは何なの?

モジュールがないといわれるので、


pip search ***

で調べようとしますが、

使えません。

ERROR: XMLRPC request failed [code: -32500] RuntimeError: PyPI's XMLRPC API is currently disabled due to unmanageable load and will be deprecated in the near future.

👉 Remove the pip search command · Issue #5216 · pypa/pip hatena-bookmark

👉 Search results · PyPI hatena-bookmark

代替はこれですかね、「pip-search」 。

ERROR: XMLRPC request failed [code: -32500] RuntimeError: PyPI's XMLRPC API is currently disabled due to unmanageable load and will be deprecated in the near future.

「pip」 にラッパー気味に関数をセットしながら使うといいようです。


alias pip='function _pip(){
    if [ $1 = "search" ]; then
        pip_search "$2";
    else pip "$@";
    fi;
};_pip'

👉 pip-search · PyPI hatena-bookmark

しかしこれ公式ではないですよね?

👉 victorgarric/pip_search: Searching thought pip when hard times strike hatena-bookmark

みんなはどうしてるの?

ブラウザで検索してるの?

最近、python コマンドラインのまわりはいろいろ混乱します。

👉 macOS 12.3 で Python2 が消え、スクリプトが「bad interpreter: /usr/bin/python: no such file or directory」とは。 hatena-bookmark