Open9

Androidアプリ開発に触れてみる2

kurehajimekurehajime

レイアウト

GUI上での相対レイアウトを学ぶ。
ポトペタツール使うの久しぶり。

「やっぱりUIはコードで実装しなきゃ」派だけど、この本の手順ではちゃんとコードの構造を意識した感じで実装が進むから抵抗感はない。

kurehajimekurehajime

ViewBinding

「うわーコンパイル時のオプションでなんか黒魔術するの嫌だなぁ」と思ったが、ChatGPTとの対話でViewBindingの仕組みを深掘りしたら納得感が出た。

https://chat.openai.com/share/49a62c7f-809a-43a5-9dd6-2293b0c5ccd0

でもactivity_main.xmlが暗黙でActivityMainBindingになるのは何かちょっと嫌だ。明示的に指定されていないと気持ち悪い。

kurehajimekurehajime

ビルドこけた。

An issue was found when checking AAR metadata:

  1.  Dependency 'androidx.activity:activity:1.8.0' requires libraries and applications that
      depend on it to compile against version 34 or later of the
      Android APIs.

      :app is currently compiled against android-33.

      Recommended action: Update this project to use a newer compileSdk
      of at least 34, for example 34.

      Note that updating a library or application's compileSdk (which
      allows newer APIs to be used) can be done separately from updating
      targetSdk (which opts the app in to new runtime behavior) and
      minSdk (which determines which devices the app can be installed
      on).
kurehajimekurehajime

言われるがままにcompileSdk = 34にしたら壊れた。

Android resource linking failed
com.example.slider.app-mergeDebugResources-30:/layout/activity_main.xml:23: error: '@tools:sample/avatars' is incompatible with attribute src (attr) reference|color.
error: failed linking file resources.
kurehajimekurehajime

よく分からないが、
activity_main.xmlのandroid:src="@tools:sample/avatars"が使えないっぽい。
この画像はIDEでしか使わないから別に適当でよさそう。

android:src="@drawable/dog"

と、実在する画像に置き換えたら動いた。