💭

Android Studioのビルドエラーの対処

2023/01/22に公開

そもそも何がしたかったのか

Target API level 30でプロジェクトを作成し、エミュレータを入れてデバッグを行おうとしたところ下記のようなエラーが出ました。

発生したエラー1

15 issues were found when checking AAR metadata:

  1.  Dependency 'androidx.appcompat:appcompat-resources:1.6.0' requires libraries and applications that
      depend on it to compile against version 33 or later of the
      Android APIs.

      :app is currently compiled against android-30.

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

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

  2.  Dependency 'androidx.appcompat:appcompat:1.6.0' requires libraries and applications that
      depend on it to compile against version 33 or later of the
      Android APIs.

      :app is currently compiled against android-30.

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

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

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

      :app is currently compiled against android-30.

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

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

  4.  Dependency 'androidx.lifecycle:lifecycle-viewmodel:2.5.1' requires libraries and applications that
      depend on it to compile against version 31 or later of the
      Android APIs.

      :app is currently compiled against android-30.

      Recommended action: Update this project to use a newer compileSdkVersion
      of at least 31, for example 33.

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

  5.  Dependency 'androidx.lifecycle:lifecycle-viewmodel-savedstate:2.5.1' requires libraries and applications that
      depend on it to compile against version 31 or later of the
      Android APIs.

      :app is currently compiled against android-30.

      Recommended action: Update this project to use a newer compileSdkVersion
      of at least 31, for example 33.

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

  6.  Dependency 'androidx.emoji2:emoji2-views-helper:1.2.0' requires libraries and applications that
      depend on it to compile against version 32 or later of the
      Android APIs.

      :app is currently compiled against android-30.

      Recommended action: Update this project to use a newer compileSdkVersion
      of at least 32, for example 33.

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

  7.  Dependency 'androidx.emoji2:emoji2:1.2.0' requires libraries and applications that
      depend on it to compile against version 32 or later of the
      Android APIs.

      :app is currently compiled against android-30.

      Recommended action: Update this project to use a newer compileSdkVersion
      of at least 32, for example 33.

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

  8.  Dependency 'androidx.core:core:1.9.0' requires libraries and applications that
      depend on it to compile against version 33 or later of the
      Android APIs.

      :app is currently compiled against android-30.

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

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

  9.  Dependency 'androidx.core:core-ktx:1.9.0' requires libraries and applications that
      depend on it to compile against version 33 or later of the
      Android APIs.

      :app is currently compiled against android-30.

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

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

 10.  Dependency 'androidx.savedstate:savedstate:1.2.0' requires libraries and applications that
      depend on it to compile against version 31 or later of the
      Android APIs.

      :app is currently compiled against android-30.

      Recommended action: Update this project to use a newer compileSdkVersion
      of at least 31, for example 33.

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

 11.  Dependency 'androidx.annotation:annotation-experimental:1.3.0' requires libraries and applications that
      depend on it to compile against version 33 or later of the
      Android APIs.

      :app is currently compiled against android-30.

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

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

 12.  Dependency 'androidx.lifecycle:lifecycle-process:2.4.1' requires libraries and applications that
      depend on it to compile against version 31 or later of the
      Android APIs.

      :app is currently compiled against android-30.

      Recommended action: Update this project to use a newer compileSdkVersion
      of at least 31, for example 33.

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

 13.  Dependency 'androidx.lifecycle:lifecycle-runtime:2.5.1' requires libraries and applications that
      depend on it to compile against version 31 or later of the
      Android APIs.

      :app is currently compiled against android-30.

      Recommended action: Update this project to use a newer compileSdkVersion
      of at least 31, for example 33.

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

 14.  Dependency 'androidx.startup:startup-runtime:1.1.1' requires libraries and applications that
      depend on it to compile against version 31 or later of the
      Android APIs.

      :app is currently compiled against android-30.

      Recommended action: Update this project to use a newer compileSdkVersion
      of at least 31, for example 33.

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

 15.  Dependency 'androidx.lifecycle:lifecycle-livedata-core:2.5.1' requires libraries and applications that
      depend on it to compile against version 31 or later of the
      Android APIs.

      :app is currently compiled against android-30.

      Recommended action: Update this project to use a newer compileSdkVersion
      of at least 31, for example 33.

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

数が多いですが、全て依存しているパッケージがAPI level 31以上とか33以上にしか対応していない、という内容です。アドバイスとしてAPI levelを33に上げるように言われていますがこちらはAPI level 30のエミュレータを使いたいのでこの方法は使えません。

Target API level 30でプロジェクトを作成したならgradleファイルもそれに合わせてくれれば良いのですが、そういった配慮は無いようです。最新のパッケージを使うよう指定されたgradleファイルが作成されました。

解決方法

gradleファイルで指定されたパッケージのバージョンを下げます。
build.gradleで問題になっていたのがこの恐らく3つ。

implementation 'androidx.core:core-ktx:1.9.0'
implementation 'androidx.appcompat:appcompat:1.6.0'
implementation 'com.google.android.material:material:1.7.0'

AndroidX Techでこれらのパッケージの旧バージョンでAPI level 30に対応しているものを探します。

  • androidx.core:core-ktx:1.6.0
    • minSdkVersion: 14
    • targetSdkVersion: 30
  • androidx.appcompat:appcompat:1.3.1
    • minSdkVersion: 14
    • targetSdkVersion: 30
  • com.google.android.material:material:1.4.0
    • これはtargetSdkVersionが明示されていなかったので最新バージョンから一つづつ下げて行ってエラーが出ないものを選びました。

この3つを修正するとビルドエラーは出なくなりました。

発生したエラー2

attribute android:dataExtractionRules not found.
     error: failed processing manifest.

こちらはAndroidManifest.xmlの問題でした。

解決方法

android:dataExtractionRulesという属性はAPI level 31以上のようなのでこの行を削除します。

実行できない

エミュレータを起動しても下の図のようにRunが選べません。

解決方法

File > Sync Project with Gradle Filesを実行すると選べるようになり、無事デバッグ実行もできました。

GitHubで編集を提案

Discussion