title: Room Persistence lib implementation in Kotlin
body: <p>I am implementing Room persistence lib in kotlin for my database implementation.</p>
  
  <p>Following are my <code>Entity</code>, <code>Dao</code> and <code>Database</code> classes:</p>
  
  <p><strong>Food.kt</strong></p>
  
  <pre><code>@Entity
  class Food(@ColumnInfo(name = "food_name") var foodName: String,
             @ColumnInfo(name = "food_desc") var foodDesc: String,
             @ColumnInfo(name = "protein") var protein: Double,
             @ColumnInfo(name = "carbs") var carbs: Double,
             @ColumnInfo(name = "fat") var fat: Double)
  {
      @ColumnInfo(name = "id")
      @PrimaryKey(autoGenerate = true)
      var id: Long = 0
      @ColumnInfo(name = "calories")
      var calories: Double = 0.toDouble()
  }
  </code></pre>
  
  <p><strong>PersonalizedFood.kt</strong></p>
  
  <pre><code>@Entity(primaryKeys = arrayOf("food_id","date"))
  class PersonalizedFood(@ColumnInfo(name = "quantity") var quantity: Int,
                         @ColumnInfo(name = "unit") var unit: String,
                         @ColumnInfo(name = "date") var date: Date){
  
      @ColumnInfo(name = "food_id")
      var foodId:Long = 0
  }
  </code></pre>
  
  <p><strong>FoodDao.kt</strong></p>
  
  <pre><code>@Dao
  interface FoodDao {
  
      companion object{
          const val ID = "id"
          const val NAME = "name"
          const val PROTEIN = "protein"
          const val DESC = "desc"
          const val CARBS = "carbs"
          const val FAT = "fat"
  
          const val DATE = "date"
          const val FOOD_ID = "food_id"
  
          const val ALL_FOOD_LIST = "food"
          const val PERSONALISED_FOOD_LIST = "personalised_food"
      }
  
      /**
       * Returns food details of a food given by food_id
       */
      @Query("SELECT * FROM $ALL_FOOD_LIST WHERE $ID=:food_id")
      fun getFoodDetails(food_id:Long):Food
  
      /**
       * Inserts food items in all_food_list
       */
      @Insert
      fun addFoodList(list:ArrayList&lt;Food&gt;)
  
      @Insert(onConflict = REPLACE)
      fun saveFood(food:PersonalizedFood)
  
      @Query("SELECT * FROM $PERSONALISED_FOOD_LIST WHERE $FOOD_ID=:foodId and $DATE=:date")
      fun getFood(foodId:Int, data:Date):PersonalizedFood
  
      @Query("SELECT * FROM $ALL_FOOD_LIST where $ID in (select $FOOD_ID from $PERSONALISED_FOOD_LIST where $DATE = :date)")
      fun getFood(date:Date):ArrayList&lt;Food&gt;
  }
  </code></pre>
  
  <p><strong>Converter.kt</strong></p>
  
  <pre><code>class Converter {
  
      companion object{
          @TypeConverter
          fun fromTimestamp(value: Long?): Date? {
              return if (value == null) null else Date(value)
          }
  
          @TypeConverter
          fun dateToTimestamp(date: Date): Long {
              return date.time
          }
      }
  }
  </code></pre>
  
  <p><strong>FoodDatabase.kt</strong></p>
  
  <pre><code>@Database(entities = arrayOf(Food::class, PersonalizedFood::class), version = 1)
  @TypeConverters(Converter::class)
  abstract class FoodDatabase : RoomDatabase(){
      abstract fun foodDao():FoodDao
  
      companion object{
          private val databaseName = "diet"
  
          var dbInstance:FoodDao? = null
          fun getInstance(context:Context):FoodDao?{
              if(dbInstance == null)
                  dbInstance = Room.inMemoryDatabaseBuilder(context, FoodDatabase::class.java).build().foodDao()
              return dbInstance;
          }
      }
  }
  </code></pre>
  
  <p>And when i run following code to create database:</p>
  
  <p><code>FoodDatabase.getInstance(baseContext)?.getFood(Calendar.getInstance().time)</code></p>
  
  <p>It gives me following exception:</p>
  
  <blockquote>
    <p>Caused by: java.lang.RuntimeException: cannot find implementation for
    com.chandilsachin.diettracker.database.FoodDatabase. FoodDatabase_Impl
    does not exist
                                                                                           at
    android.arch.persistence.room.Room.getGeneratedImplementation(Room.java:90)
                                                                                           at
    android.arch.persistence.room.RoomDatabase$Builder.build(RoomDatabase.java:340)
                                                                                           at
    com.chandilsachin.diettracker.database.FoodDatabase$Companion.getInstance(FoodDatabase.kt:21)
                                                                                           at
    com.chandilsachin.diettracker.MainActivity$SetUpFoodDatabase.doInBackground(MainActivity.kt:95)
                                                                                           at
    com.chandilsachin.diettracker.MainActivity$SetUpFoodDatabase.doInBackground(MainActivity.kt:77)
                                                                                           at android.os.AsyncTask$2.call(AsyncTask.java:295)
                                                                                           at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                                                                                           at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234) 
                                                                                           at
    java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) 
                                                                                           at
    java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) 
                                                                                           at java.lang.Thread.run(Thread.java:818)</p>
  </blockquote>
  
  <p>Has anyone implemented room persistence in kotlin?</p>
  
  <p><strong>Edited</strong>
  This question was marked duplicate of <a href="https://stackoverflow.com/questions/44065371/android-room-persistences-library-and-kotlin">this</a>. Though problem statement is same but solution given does not solve my problem. Solution says i have to add replace <code>annotationProcessor</code> to <code>kapt "android.arch.persistence.room:compiler:1.0.0-alpha1"</code> dependency. I made those changes and it resulted in gradle error while project build.</p>
  
  <blockquote>
    <p>Information:Gradle tasks [:app:assembleDebug] Warning:warning:
    Supported source version 'RELEASE_7' from annotation processor
    'android.arch.persistence.room.RoomProcessor' less than -source '1.8'
    Warning:warning: The following options were not recognized by any
    processor: '[kapt.kotlin.generated]'
    /Users/BBI-M1025/Documents/BBI/Workspace_fun/Android/diet-tracker/app/src/main/java/com/chandilsachin/diettracker/database/Food.kt
        Error:(1, 1) Some error(s) occurred while processing annotations. Please see the error messages above.
        Error:Execution failed for task ':app:kaptDebugKotlin'.</p>
    
    <blockquote>
      <p>Compilation error. See log for more details
          Information:BUILD FAILED in 10s
          Information:2 errors
          Information:2 warnings
          Information:See complete output in console</p>
    </blockquote>
  </blockquote>
  
  <p>I am attaching my gradle file also:</p>
  
  <pre><code>apply plugin: 'com.android.application'
  apply plugin: 'kotlin-android'
  apply plugin: 'kotlin-android-extensions'
  apply plugin: 'kotlin-kapt'
  
  android {
      compileSdkVersion 25
      buildToolsVersion "25.0.2"
      defaultConfig {
          applicationId "com.chandilsachin.diettracker"
          minSdkVersion 16
          targetSdkVersion 25
          versionCode 1
          versionName "1.0"
          testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
      }
      buildTypes {
          release {
              minifyEnabled false
              proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
          }
      }
  }
  
  dependencies {
      compile fileTree(dir: 'libs', include: ['*.jar'])
      androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
          exclude group: 'com.android.support', module: 'support-annotations'
      })
      compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
      compile 'com.android.support:appcompat-v7:25.0.1'
      compile 'com.android.support.constraint:constraint-layout:1.0.0-alpha8'
      compile 'com.android.support:cardview-v7:25.0.1'
      compile 'com.android.support:recyclerview-v7:25.0.1'
      compile 'com.github.ne1c:rainbowmvp:1.2.1'
      compile "org.jetbrains.anko:anko-commons:0.10.0"
  
      /*annotationProcessor "android.arch.persistence.room:compiler:1.0.0-alpha1"
      compile "android.arch.lifecycle:extensions:1.0.0-alpha1"
      annotationProcessor "android.arch.lifecycle:compiler:1.0.0-alpha1"*/
  
      compile "android.arch.persistence.room:runtime:1.0.0-alpha1"
      annotationProcessor "android.arch.persistence.room:compiler:1.0.0-alpha1"
      kapt "android.arch.persistence.room:compiler:1.0.0-alpha1"
  
      testCompile 'junit:junit:4.12'
  }
  repositories {
      mavenCentral()
  }
  </code></pre>
  
  <p>Has anyone come across this issue?</p>