Utility library that utilizes KSP to generate Room type converter classes.

Overview

Licence Kotlin Android

Roomie

Roomie is an annotation processing library that utilizes KSP to generate TypeConverter classes for Room. TypeConverter classes most often involve same boiler-plate code and Roomie makes it really easy to quickly create them with a single annotation.

Usage

Annotate the specific model class using @AddConverter

@AddConverter
data class Person(val firstName: String, val lastName: String)

It's as simple as that! Rebuild the project to generate the converter. A converter class with the name PersonConverter will be generated for the Person data class above. In your case, use [Model name]Converter to get the corresponding converter for your data class.

Specifying converter name

By default, the generated converter has Converter appended to its name but it can be changed by using the name argument specified in @AddConverter annotation.

@AddConverter(name = "PeopleConverter")
data class Person(val firstName: String, val lastName: String)

This will generate the converter using PeopleConverter as its name.

Working with lists

By default, the generated converter class will be similar to the code below:

open class PersonConverter {
  @TypeConverter
  fun fromString(value: String): Person {
    val type = object : TypeToken() {}.type
    return Gson().fromJson(value, type)
  }

  @TypeConverter
  fun fromList(value: Person): String {
    val gson = Gson()
    return gson.toJson(value)
  }
}

In certain cases you may want to wrap the model class with a kotlin list. This can be achieved by using the asList argument which takes a boolean specified in @AddConverter annotation, which by default is false.

@AddConverter(asList = true)
data class Person(val firstName: String, val lastName: String)

The resulting generated converter class is shown below:

open class PersonListConverter {
  @TypeConverter
  fun fromString(value: String): List {
    val type = object : TypeToken>() {}.type
    return Gson().fromJson>(value, type)
  }

  @TypeConverter
  fun fromList(value: List): String {
    val gson = Gson()
    return gson.toJson(value)
  }
}
  • In this case the resulting converter class now has ListConverter appended to its name. To get the corresponding converter class when asList is set to true append ListConverter to its name, as in [Model name]ListConverter.

Download

JitPack

1. Add the JitPack repository to your project

groovy - settings.gradle (project level)

repositories {
    google()
    mavenCentral()
    jcenter() // Warning: this repository is going to shut down soon
    maven { url 'https://jitpack.io' }
  }

kotlin - setting.gradle.kts

dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()
        maven { url = uri("https://www.jitpack.io" ) }
    }
}

2. Add the KSP plugin:

The version you chose for the KSP plugin depends on the Kotlin version your project uses.
You can check https://github.com/google/ksp/releases for the list of KSP versions, then pick the last release that matches your Kotlin version.
Example: If you're using 1.5.30 Kotlin version, then the corresponding KSP version is 1.5.30-1.0.0.

groovy - build.gradle (module level)

plugins {
    //...
    id 'com.google.devtools.ksp' version '1.5.30-1.0.0' // Depends on your kotlin version
}

kotlin - build.gradle.kts (module level)

plugins {
    //...
    id("com.google.devtools.ksp") version "1.5.30-1.0.0" // Depends on your kotlin version
}

3. Add depedencies:

groovy - build.gradle (module level)

implementation 'com.github.ezechuka.roomie:roomie-annotation:1.0.0-beta02'
ksp 'com.github.ezechuka.roomie:roomie-processor:1.0.0-beta02'

kotlin - build.gradle.kts (module level)

implementation("com.github.ezechuka.roomie:roomie-annotation:1.0.0-beta02")
ksp("com.github.ezechuka.roomie:roomie-processor:1.0.0-beta02")

Also Room and Gson needs to be included in the build.gradle (module level) for Groovy or build.gradle.kts (module level) for Kotlin

4. Specify build generated folder to IDE

Within the android block add:

groovy - build.gradle (module level)

applicationVariants.all { variant ->
    kotlin.sourceSets {
        getByName(variant.name) {
            kotlin.srcDir("build/generated/ksp/${variant.name}/kotlin")
        }
    }
}

kotlin - build.gradle.kts (module level)

applicationVariants.all {
    kotlin.sourceSets {
        getByName(name) {
            kotlin.srcDir("build/generated/ksp/$name/kotlin")
        }
    }
}
You might also like...
Utility for detecting and notifying when your Android app goes background / becomes foreground

Foredroid Utility for detecting and notifying when your Android app goes background / becomes foreground. API-level 14+. Usage: Initialise Foreground

A collection of small utility functions to make it easier to deal with some otherwise nullable APIs on Android.

requireKTX is a collection of small utility functions to make it easier to deal with some otherwise nullable APIs on Android, using the same idea as requireContext, requireArguments, and other similar Android SDK methods.

Small utility to create/restore encrypted backups

Quick Backup Choose some files and quickly create a complete encrypted and compressed file. Usage Clone the repository Run gradlew installShadowDist T

Utility tool to make you a computer ninja.

Cmd Window Never spend 6 minutes doing something by hand when you can spend 6 hours failing to automate it - Zhuowej Zhang What is this about? This to

Small utility used to add Homewizard Energy Socket tiles to Android Quick Settings
Small utility used to add Homewizard Energy Socket tiles to Android Quick Settings

TileWizard [Alpha! Unstable!] Settings Result Functionality: Add up to 5 Wi-Fi Energy Sockets to Android Quick Settings. What do you need: Android dev

Small command-line utility to safely merge multiple WhatsApp backups (msgstore.db) into one.

whatsapp-database-merger A small command-line utility that can be used to merge multiple WhatsApp databases (msgstore.db) into one. A few notes: input

A command line utility to help you investigate the sensitive data associated with Macie findings.
A command line utility to help you investigate the sensitive data associated with Macie findings.

Macie Finding Data Reveal This project contains a command line utility to help you investigate the sensitive data associated with Macie findings.

 CHAOS - Like a utility knife for discord
CHAOS - Like a utility knife for discord

CHAOS - Like a utility knife for discord. Currently under development. If you feel inclined, please support me by contributing to this project. Or alt

Obsi-bot: the next generation discord utility bot 🔥

obsi-bot obsi-bot is the next generation discord utility bot. It is developed in Kotlin using kordex and kord Help me translating Feel free to help me

Releases(1.0.0-beta02)
Owner
Chukwuka Eze
Android Engineer
Chukwuka Eze
FractalUtils - A collection of utility functions and classes, with an emphasis on game related utilities

A collection of utility functions and classes written in Kotlin. There is some emphasis on utilities useful for games (Geometry, Random, Time, Updating, etc).

null 2 Nov 11, 2022
A support library for VectorDrawable and AnimatedVectorDrawable classes introduced in Lollipop

vector-compat A support library for VectorDrawable and AnimatedVectorDrawable introduced in Lollipop with fully backwards compatible tint support (api

Wael N 1.2k Nov 29, 2022
A simple Android utils library to write any type of data into cache files and read them later.

CacheUtilsLibrary This is a simple Android utils library to write any type of data into cache files and then read them later, using Gson to serialize

Wesley Lin 134 Nov 25, 2022
compaKTset is a small library aimed at providing you with the most memory efficient Set implementation for any particular data type of your choosing.

compaKTset is a small library aimed at providing you with the most memory efficient Set implementation for any particular data type of your choosing.

Ignat Beresnev 3 Nov 16, 2021
CreditCardHelper 🖊️ A Jetpack-Compose library providing useful credit card utilities such as card type recognition and TextField ViewTransformations

CreditCardHelper ??️ A Jetpack-Compose library providing useful credit card utilities such as card type recognition and TextField ViewTransformations

Stelios Papamichail 18 Dec 19, 2022
A set of helper classes for using dagger 1 with Android components such as Applications, Activities, Fragments, BroadcastReceivers, and Services.

##fb-android-dagger A set of helper classes for using dagger with Android components such as Applications, Activities, Fragments, BroadcastReceivers,

Andy Dennie 283 Nov 11, 2022
Type safe intent building for services and activities

#IntentBuilder Type safe intent building for services and activities. IntentBuilder is a type safe way of creating intents and populating them with ex

Emil Sjölander 348 Oct 10, 2022
Validator - Notify type based validation for input fields.

Validator - Notify type based validation for input fields.

Mustafa Yiğit 57 Dec 8, 2022
A logger with a small, extensible API which provides utility on top of Android's normal Log class.

This is a logger with a small, extensible API which provides utility on top of Android's normal Log class. I copy this class into all the little apps

Jake Wharton 9.8k Dec 30, 2022
A small utility to record Android device screen to a GIF

RoboGif A small utility to record Android device screen to an optimized GIF so you can paste it to GitHub or a similar service. Requirements Python 2.

Jernej Virag 526 Dec 9, 2022