2019年6月8日土曜日

Android Studioでオフライン時にビルド失敗した

新幹線でAndroidアプリをビルドしたら次のエラーが出た。


Unknown host 'dl.google.com: nodename nor servname provided, or not known'. You may need to adjust the proxy settings in Gradle.
Enable Gradle 'offline mode' and sync project
Learn about configuring HTTP proxies in Gradle


言われている通りGradleをoffline modeにするか。
`Enable Gradle 'offline mode' and sync project`を押してしばらく待つ。
そして再ビルド。

No cached version of io.realm:realm-annotations-processor:5.11.0 available for offline mode.
Disable Gradle 'offline mode' and sync project

え〜。

すると秀策さん(@shumach217)からリプがきた。




というわけで調べてみた。
stack overflowにはこういう記事がある。

The Offline mode does not allow you to completely work offline. Its actually a GOOD cache mode. Whenever you add new dependencies, You HAVE to gradle sync the project, ATLEAST ONCE. Android studio has to download(then cache) the dependencies for the app to work. Once you sync the gradle files, then you can switch to offline mode, and you can work.
オフラインモードで完全に作業することはできません。モジュールを追加した場合、少なくとも1回は依存関係解決のためにgradle syncする必要があります。Android studioはモジュールの依存関係解決のためにダウンロードする必要があり、ダウンロードしたらキャッシュされます。一度Syncしたら、オフラインモードでも動くでしょう。

なるほど。
でも、モジュールを追加したらSyncしたんだけどな。
ビルド時に依存関係を解決しているのかな?

そんなわけで、1度ネットワークに繋いでビルドしました。
その後はオフラインでもオンラインでも動いています。