LoginSignup
8
4

More than 1 year has passed since last update.

[Android] 古いバージョンのRoomを使用した時に出るビルドエラーを解消する(M1 Mac)

Posted at

はじめに

M1 MacでAndroid開発をする際、ライブラリのバージョンが古かったりすると問題が起こるケースがあります。

ここでは、その内のひとつの内容・解決方法をご紹介します。

私が問題に遭遇した時は日本語での情報が少なく、解決に苦労したということもあり、ここに残しておこうと思います。

開発環境

PC : M1 MacBook Air(2020) 16GB OS:Big Sur
AndroidStudio : ArcticFox arm64 (2020.3.1 patch3)

Room 2.2.5

なにが起きたか

Androidアプリ開発で使用される、ライブラリRoomのビルドが出来ない

ビルド時のエラーメッセージ

Execution failed for task ':app:kaptDevelopmentDebugKotlin'.
> A failure occurred while executing org.jetbrains.kotlin.gradle.internal.KaptExecution
   > java.lang.reflect.InvocationTargetException (no error message)

stacktraceにて、詳細を確認

Caused by: java.lang.Exception: No native library is found for os.name=Mac and os.arch=aarch64. path=/org/sqlite/native/Mac/aarch64
    at org.sqlite.SQLiteJDBCLoader.loadSQLiteNativeLibrary(SQLiteJDBCLoader.java:333)
    at org.sqlite.SQLiteJDBCLoader.initialize(SQLiteJDBCLoader.java:64)
    at androidx.room.verifier.DatabaseVerifier.<clinit>(DatabaseVerifier.kt:68)

エラー解消方法①(推奨)

Roomのバージョンを2.4.0-alpha03以上にする
これで解消します。

が、おいそれと変更できない状況であれば、以下②で。

エラー解消方法②

build.gradleの、Roomの設定に1行追記する

App.build.gradle
dependencies {
// 省略
    def room_version = "2.2.5"
    kapt "org.xerial:sqlite-jdbc:3.34.0" // この1行を追記
    implementation "androidx.room:room-runtime:$room_version"
    implementation "androidx.room:room-ktx:$room_version"
    kapt "androidx.room:room-compiler:$room_version"
}

上記で解消しない場合は以下で対応

Project.build.gradle
allprojects {
    configurations.all {
        resolutionStrategy {
            force 'org.xerial:sqlite-jdbc:3.34.0'
        }
    }
}

原因はなんなのか

エラー文から分かるように、SQLiteJDBCLoaderで問題があるようです。

jbdcのgithubに上がっているissue
java.lang.StringIndexOutOfBoundsException exception for Android Studio Arctic Fox (stable build) with Apple M1 ARM

どうやらRoomで使用されているsqlite-jdbcが、arm版ではバグを吐いてしまうようです。
sqlite-jdbcとは、JavaでSQLiteを使用する際に必要なドライバ(ライブラリ)です。だいぶざっくりですが...

解決にはここを参考にさせて頂きました。
IssueTracker(google)
Build failed on Apple M1 with JDK8-arm64

Roomのバージョンを2.4.0-alpha03以上にするsqlite-jdbcのバージョン3.34.0を使用する といった方法で問題を回避できると言っています。
公式で本バク対応をしてくれており、2.4.0-alpha03で解消されています。

当時はsqlite-jdbc:3.36.0.1が最新だったので、バージョンを落として使いましょうということですね。

公式アプデきた

2021/12/15
https://developer.android.com/jetpack/androidx/releases/room?hl=ja#2.4.0

安定版リリースやったね!ばんざい!

8
4
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
4