Android Architecture Components: Room の Migration で IllegalStateException

データを移行せずに捨ててしまうのなら、以下でいいのですが。


Room.databaseBuilder(context, RepoDatabase.class, DB_NAME)
    .fallbackToDestructiveMigration()
    .build();

きっと捨てることができませんよね。

テーブル定義を変更しながら、データを移行しますよね。


Room.databaseBuilder(context, RepoDatabase.class, DB_NAME)
    .addMigrations(FROM_1_TO_2)
    .build();

static final Migration FROM_1_TO_2 = new Migration(1, 2) {
    @Override
    public void migrate(final SupportSQLiteDatabase database) {
        database.execSQL("ALTER TABLE Repo
                         ADD COLUMN createdAt TEXT");
        }
    };

database.execSQL()でSQLをベタに実行しながらデータを別テーブルにRENAME後、CREATE→INSERT→DROP というかんじでスキーマを変更していますが。

すると、こんなのに遭遇します。

java.lang.IllegalStateException: Room cannot verify the data integrity. Looks like you’ve changed schema but forgot to update the version number. You can simply fix this by increasing the version number.

データベースのバージョンナンバーは上げることは、まあ上げるとして、それでもインデクスなどうまく意図通りに移行できてない場合があります。

データモデルにアノテーションで記述した「Room が利用しようとしているスキーマ」と、Migration部分にベタ記述した「SQLiteのスキーマ」が合致しないといけません。

また、最近のAndroidでは、.dbファイルが、OS上で取り回しづらく、実態を把握しづらかったりします。

Roomが認識しようとしているテーブルスキーマは以下で書き出すことができます。


android {
    javaCompileOptions {
        annotationProcessorOptions {
            arguments = ["room.schemaLocation":
                         "$projectDir/schemas".toString()]
            }
        }
    }
}


"tableName": "Repo",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER NOT NULL, `name` TEXT, `url` TEXT, PRIMARY KEY(`id`))"

モデルクラスに記述したアノテーションのRoomが認識している状態(expected)をSQLで書き出してくれます。

これと、MIGRATION部分のベタSQL(found)を比較すると、意味が分かってきます。

複数フィールドに対してのUNIQUE なインデクスなど、公式ドキュメントとは違う内部的絶賛更新中な処理な部分など、書き出してみると先に進むことができます。


Android Studio のビルドの体感速度を上げる IDEAプラグイン「Nyan Progress Bar」

プログレスバーのカスタマイズプラグインです。

こういうことです。

 

蓄積されていくイライラは少しずつでも解消していきたいものですよね。

Nyan Progress Bar :: JetBrains Plugin Repository


Gradle Dependency バージョンのチェックを「最新」に更新する方法

便利そうなのでました!

Android Developers Blog: Announcing new SDK versioning in Google Play services and Firebase

com.google.android.gms:play-services-*
com.google.firebase:firebase-*

に関しては、それぞれが独立したバージョンで記述してよい。

なので、以下のような「バージョンをを揃える」記述は不要。


buildscript {
    ext {
        play_version = '15.0.0'
    }
}

dependencies {
    // DON'T DO THIS!!
    // The following use of the above buildscript property is no longer valid.
    implementation "com.google.android.gms:play-services-auth:${play_version}"
    implementation "com.google.firebase:firebase-auth:${play_version}"
    implementation "com.google.firebase:firebase-firestore:${play_version}"
}

利用方法は以下2パターン。


classpath 'com.google.gms:google-services:3.3.0'

// 最終行
apply plugin: 'com.google.gms.google-services' 

かまたは、


classpath 'com.google.android.gms:strict-version-matcher-plugin:1.0.0'

// 最終行
apply plugin: 'com.google.android.gms.strict-version-matcher-plugin'

とのこと。

 

やってみたが...

エラー。

ビルドできず。


The library com.google.android.gms:play-services-measurement-base
is being requested by various other libraries at [[15.0.0,15.0.0], [15.0.2,15.0.2]],
but resolves to 15.0.2. Disable the plugin and check your dependencies tree
using ./gradlew :app:dependencies.

記述にはない「play-services-measurement-base」が

内部的に呼ばれてこけている。

メッセージに書いてある


./gradlew :app:dependencies

は実行すらできない。

それぞれは、Andrid Studioの自動チェックで最新のはずなのだが。

 

それぞれのバージョンを確認してみる

目視でGoogleリポジトリを確認。

Google's Maven Repository

AndroidStudioの自動チェックで最新版の「15.0.0」だと思っていたが

実は、古いバージョンのままだった。

実際は「15.0.2」が最新。

これに書き換えたらいけた。

しかし、AndroidStudioの自動最新バージョンチェック機能らしきは、

機能してなくね?

かまたは、微妙に古くね?

 

Lint「Newer Library Versions Available」 を確認する

「Preference」-「Editor」-「Inspections」 にある

「Lint」-「Newer Library Versions Available」

OFFになってるが。

Description
Newer Library Versions Available This detector checks with a central repository to see if there are newer versions available for the dependencies used by this project. This is similar to the GradleDependency check, which checks for newer versions available in the Android SDK tools and libraries, but this works with any MavenCentral dependency, and connects to the library every time, which makes it more flexible but also much slower.

「毎回チェックするのでとろい」ということで

デフォルトでOFFになっているのか。

毎回でなく適時、意図的に実行したい場合は、

「Analyze」-「Run Inspection by Name」からいきましょう。

 

グレーのハイライトと同時にツールチップで最新版である

「15.0.2」

をサジェストしてくれました!

【Android Studio】ビルド環境を安定した最新バージョンにする