aar のビルドで local の .aar ファイルを依存に使えない
Android Gradle Plugin 4.0.0 に上げてから出るようになった 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>
Error が発生する task
bundle<Variant>Aar
Library module に対して直接実行した場合
Application module の assemble/bundle 中に依存先の library module に対して行われた場合は通る
bundle<Variant>LocalLintAar
AGP 7.0.0 から
関連 commit
どうする
bundle<Variant> task
application module (bundle<Variant>) と library module (bundle<Variant>Aar) で分かれていて前者を実行する分には対象 module を指定しなくても application module 向けに実行されるようになるので aab 作成は特に不都合なさそう
assemble<Variant> task
application module をビルドしたいだけならその module を指定してビルドする
:app:assembleRelease みたいな
AGP 7.0.0 以降は上記に加えて lintOptions.checkReleaseBuilds を false にすれば apk のビルドは大丈夫
lint task はダメなまま
Local aar をどうにかする
local module として扱う
module 用の directory を作って対象の aar ファイルと以下が記述された build.gradle を配置する
code:gradle
configurations.maybeCreate("default")
artifacts.add("default", file("<local_aar_file>.aar"))
↑で作った directory を他の local module 同様 settings.gradle で include して、 dependency も書き換える
code:gradle
implementation project(":my-aar-module")
以下調査時のあれこれ
Android Studio から aar を import したら展開されて local module として扱えるようになるそう
APK/AAB をビルドする時に依存してる android library module が local aar に依存してる分には (4.0.0 時点では) 大丈夫っぽい
Multi-module 構成の repository とかで task だけの記述で実行するとこのエラーに引っかかることがあり、application module を指定して build task を実行すると回避できる
./gradlew :app:assembleDebug みたいな
AGP 7.0.0 からは release build の場合 assemble task で application module を指定しても上記のエラーが出るようになった
bundleLocalLintAar task が追加されたため
lint じゃん
release build だと lint 走るからね
bundle task の場合はまだビルドできるし application module の指定がなくてもビルドが通る
AGP 7.0.0 以降で local aar 有りの library module を持つ application module の apk が欲しい場合は以下の手順が必要そう
bundle task で .aab 作成
bundletool build-apk --bundle=<path/to/aab> output=<path/to/apks> <ks とか> --mode=universal
中身には universal.apk だけがある apks ができるので unzip とかで取り出す
library module を直接ビルドする場合は何やってもダメ
assemble でも bundleXxxAar でもダメ
わかんなくなってきた
code:sh
# :app は application module, :library は :app が依存してる library module
# AGP 4.x まで
./gradlew assembleDebug # -> error at :library:bundleDebugAar
./gradlew assembleRelease # -> error at :library:bundleReleaseAar
./gradlew :app:assembleDebug # -> OK
./gradlew :app:assembleRelease # -> OK
./gradlew bundleDebug # -> OK
./gradlew bundleRelease # -> OK
./gradlew :app:bundleDebug # -> OK
./gradlew :app:bundleRelease # -> OK
# AGP 7.x から
./gradlew assembleDebug # -> error at :library:bundleDebugAar
./gradlew assembleRelease # -> error at :library:bundleReleaseAar
./gradlew :app:assembleDebug # -> OK
./gradlew :app:assembleRelease # -> error at :library:bundleReleaseLocalLintAar
./gradlew bundleDebug # -> OK
./gradlew bundleRelease # -> OK
./gradlew :app:bundleDebug # -> OK
./gradlew :app:bundleRelease # -> OK