RoomAsset

Additional

Language
Kotlin
Version
1.0.3 (Apr 14, 2018)
Created
Jul 21, 2017
Updated
Aug 17, 2018
Owner
Ibrahim Eid (humazed)
Contributors
Ibrahim Eid (humazed)
spiral6
2
Activity
Badge
Generate
Download
Source code
APK file

Show card

RoomAsset

An Android helper class to manage database creation and version management using an application's raw asset files.

This library provides developers with a simple way to ship their Android app with an existing SQLite database (which may be pre-populated with data) and to manage its initial creation and any upgrades required with subsequent version releases.

It is implemented as an extension to Room, providing an easy way to use Room with an existing SQLite database.


Add the dependency

dependencies {
    // ... other dependencies
    implementation 'com.github.humazed:RoomAsset:1.0.3'
}

RoomAsset is intended as a drop in alternative for the framework's Room.

You can use

RoomAsset as you use Room but with two changes:Use RoomAsset.databaseBuilder() instead of Room.databaseBuilder()In @Database use version = 2 instead of version = 1valdb= RoomAsset.databaseBuilder(applicationContext, AppDatabase::class.java, "chinook.db").build() valemployees= db.chinookDao().employees

RoomAsset relies upon asset file and folder naming conventions. Your assets folder will either be under your project root, or under src/main if you are using the default gradle project structure. At minimum, you must provide the following:A databases folder inside assetsA SQLite database inside the databases folder whose file name matches the database name you provide in code (including the file extension, if any)

For the example above, the project would contain the following:

assets/databases/chinook.db 

The database will be extracted from the assets and copied into place within your application's private data directory. If you prefer to store the database file somewhere else (such as external storage) you can use the alternate constructor to specify a storage path. You must ensure that this path is available and writable whenever your application needs to access the database.