かすたあどくりいむ定食

育児、プラモ、音楽(バンド)が趣味なエンジニア(かもしれない)ブログです。

JavaのAndroidアプリでhelloworld

zzzzzzzz前回の続きです。

hellhellmymy.hatenablog.com

やっぱchatGPTに全部任せるとどうしてもうまくいかんので、本を買った。偉いぞ!俺。 これを参考に。

基礎&応用力をしっかり育成!Androidアプリ開発の教科書 第3版 Java対応 なんちゃって開発者にならないための実践ハンズオン

下記画面でNewProjectを選択。

EmptyActivityを選択。

※↑のこちら2023年現在EmptyActivityだとres/配下にlayoutディレクトリがないので、Empty View Activityを選ぶ。

アプリの名前を入力し、"Finish"をクリックします。

こんな感じでエディタが開きます。

ADVの作成

AVD(Android Virtual Device)を作ります。 右端にあるDevice Managerクリック→create devise→Pixel6→next→ →API34→next→Enable Device Frameからチェックを外しFinish Device Managerで作製したADVが表示されます。

ADVの起動

Pixel6のADVの▶アイコンをクリック 画面右下にEmulatorが表示されます(Googleスマホ画面)

File(ヘッダメニュ)→Settings→Tools→Emulator→Launch in a tool windowのチェックを外しOK

別タブでエミュレータ画面が出るので、その中で設定アプリを選択し言語を日本語にしてあげる。

上部のPixel6-API34横の▶アイコンでアプリ実行で、エミュレータでアプリ起動のはずが、、、できなかった、、、エラー

 1.  Dependency 'androidx.activity:activity:1.8.2' 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).

  2.  Dependency 'androidx.activity:activity-ktx:1.8.2' 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).

  3.  Dependency 'androidx.activity:activity-compose:1.8.2' 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.

以下chatGPT回答

このエラーは、androidx.activity ライブラリのバージョンが 1.8.2 で、それが依存している Android API のバージョンが現在のプロジェクトの compileSdkVersion よりも新しいものであるために発生しています。エラーメッセージにもある通り、現在の compileSdkVersion が android-33 であり、これを少なくとも 34 以上に更新する必要があります。
--------------
android {
    compileSdkVersion 34
    // 他の設定
}
---------------


プロジェクトを同期してビルドします。Android Studio のメニューから "File" > "Sync Project with Gradle Files" を選択してプロジェクトを同期させ、その後 "Build" > "Rebuild Project" を選択してプロジェクトを再ビルドします。

これで再実行でなんとかhelloworldできたけど、マジ長かった。。。。。