nashcft's blog

時々何か書く。

AGP 4.0.0 以降で発生する local aar 起因のビルドエラーについて

起こってること

AGP 4.0.0 から local aar に依存している library module をビルドすると失敗するようになった。 Local aar というのは libs/ 以下に .aar が置いてあって以下のように依存してるみたいなやつ。

dependendencies {
  implementation(files("libs/my-aar.aar"))
}

このときの error message は以下のような感じ:

Direct local .aar file dependencies are not supported when building an AAR. The resulting AAR would be broken because the classes and Android resources from any local .aar file dependencies would not be packaged in the resulting AAR. Previous versions of the Android Gradle Plugin produce broken AARs in this case too (despite not throwing this error). The following direct local .aar file dependencies of the :[module name] project caused this error: [path to .aar file]

関係しそうな commit は以下:

この時点で影響したのは library module のビルドだけで*1、直接ビルドする必要がなければ = それに依存する aab/apk だけ成果物として欲しければ、 application module を指定してビルドするとなんでか成功するので*2そのような回避方法もある。

そのあと AGP 7.0.0 で bundle<Variant>LocalLintAar という task が追加されて、これのために lint task でも同様の build error が発生するようになった。

関連 commit:

Lint に関しては特に逃げ道がなさそうだったのできちんと対応するしかなさそう。 Local aar を local module として使うようにすると解決する。

Local aar を local module として扱う

まず module 用の directory を作って以下のファイルを置く:

  • build.gradle
  • 使いたい aar ファイル

build.gradle の中身は以下:

configurations.maybeCreate("default")
artifacts.add("default", file("my-aar.aar"))

最終的な directory の中身はこんな感じ:

my-aar-module/
+-- build.gradle
+-- my-aar.aar

あとは他の local module と同じように settings.gradle で include して依存元の記述を書き換えたらOK。

dependencies {
-  implementation(files("libs/my-aar.aar"))
+  implementation(project(":my-aar-module"))
}

終わりに

公式からアナウンスあるか軽く探した感じでは見つけられなかったのだけど何かあったっけ?

調べてた時のメモ:

scrapbox.io

*1:application module が local aar に依存している場合は問題なくビルドできる

*2:理由は調べてない