greenDAO icon indicating copy to clipboard operation
greenDAO copied to clipboard

Support Kotlin

Open ghost opened this issue 8 years ago • 29 comments

build.gradle

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'org.greenrobot:greendao-gradle-plugin:3.1.0'
    }
}

apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'org.greenrobot.greendao'

android {
    ...
    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'
    }
}

kapt {
    generateStubs = true
}

dependencies {
    compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    ...
    //db
    compile "org.greenrobot:greendao:${greendaoVersion}"
    ...
}
repositories {
    mavenCentral()
}

gradlew greendao --info output:

All projects evaluated.
Selected primary task 'greendao' from project :
Tasks to be executed: [task ':model:greendaoPrepare', task ':model:greendao']
:model:greendaoPrepare (Thread[main,5,main]) started.
:model:greendaoPrepare
Skipping task ':model:greendaoPrepare' as it is up-to-date (took 0.045 secs).
:model:greendaoPrepare UP-TO-DATE
:model:greendaoPrepare (Thread[main,5,main]) completed. Took 0.061 secs.
:model:greendao (Thread[main,5,main]) started.
:model:greendao
Skipping task ':model:greendao' as it is up-to-date (took 0.002 secs).
:model:greendao UP-TO-DATE
:model:greendao (Thread[main,5,main]) completed. Took 0.011 secs.

Search in build\generated\source\ by keyword "greendao" do not find anything.

ghost avatar Aug 12 '16 16:08 ghost

As far as I know Kotlin is not supported. I assume your entity classes are written in Java? -ut

greenrobot-team avatar Aug 16 '16 05:08 greenrobot-team

As far as I know Kotlin is not supported.

Kotlin works with most code-gen libs(Dagger 2, Realm) without any problems. Will be nice if your library will work with it too.

I assume your entity classes are written in Java?

Nope. It was written in Kotlin.

alexandr2levin avatar Aug 16 '16 09:08 alexandr2levin

@Try4W Alright: the plugin only supports entities written in Java. So I suppose converting your entities to Java classes should make the plugin and generator work. -ut

greenrobot-team avatar Aug 16 '16 10:08 greenrobot-team

Any concrete milestone to support it? Yes we can write Entity definitions by Java but Kotlin has been becoming the majority of Android development and its support make user's code more concise.

hotchemi avatar Aug 22 '16 15:08 hotchemi

Writing Entity definitions in Java doesn't fix the problem. The code generated from the entity is not generated yet when compileDebugKotlin is executed.

Workaround for the issue: create a new module which is using greenDAO(without kotlin) and link it to the main module.

modislaszlox2 avatar Nov 29 '16 09:11 modislaszlox2

Does greendao have a plan to support kotlin?

barryzzz avatar Jan 15 '17 15:01 barryzzz

@lishoulin greenDAO for now only supports entities written in Java. See @modislaszlox2 workaround to use it in a Kotlin project. -ut

greenrobot-team avatar Jan 16 '17 07:01 greenrobot-team

Since Kotlin is official language now, it would be nice to have ability write models in Kotlin

mykola-dev avatar May 26 '17 08:05 mykola-dev

Note: with your entity classes written in Java, to use the generated greenDAO files in your Kotlin classes you currently may have to manually add the generated source folder for Kotlin compilation to succeed:

android {
    ...
    sourceSets {
        main.java.srcDirs += 'build/generated/source/greendao'
    }
}

greenrobot-team avatar May 29 '17 06:05 greenrobot-team

This project is incredible. We really want it to work with Kotlin. Please make it true

Thank you!

namchuai avatar Jun 16 '17 06:06 namchuai

With Google pushing Kotlin super hard I wouldn't be surprised if a sizable amount of work was moved to Kotlin. Please add support for Kotlin

erfanio avatar Jun 16 '17 14:06 erfanio

Not supported yet I guess right? Is really converting the code to Java the easiest workaround? (a pitty...)

rmr356 avatar Jul 13 '17 09:07 rmr356

@rmr356 Yes. Indeed its not supported still.

dns21395 avatar Jul 15 '17 06:07 dns21395

Plz add a code gen plugin for kotlin. I wanna use this lib but i wont unless it supports kotlin. In my opinion, this is the only viable alternative to realm. I got bored of realm complexity. Threading, callbacks, no native rxjava support, and i find auto-updates useless and buggy. My project is 100% kotlin, i wanna avoid having to make a separate library project for java.

recoverrelax avatar Jul 26 '17 11:07 recoverrelax

While I agree that it would be nice to have Kotlin support in GreenDao, nothing prevents you from having 99.9 % Kotlin project and just having the entity classes written in Java. This works for me (using kapt), without making a separate GreenDAO project/module.

Kobzol avatar Aug 08 '17 13:08 Kobzol

I'm can't find DaoSession ,when in using greendao with dagger in kotlin.can you give me some advice

HarryHaiVn avatar Aug 14 '17 09:08 HarryHaiVn

..... apply plugin: 'org.greenrobot.greendao' ... android {... sourceSets { main.java.srcDirs += 'build/generated/source/greendao' } } repositories { maven { url "https://oss.sonatype.org/content/repositories/snapshots" } mavenCentral() maven { url 'https://maven.fabric.io/public' } maven { url "https://jitpack.io" } } greendao { schemaVersion 1 } dependencies { testImplementation 'junit:junit:4.12' api "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version" api "com.android.support:multidex:$rootProject.multidexVersion" api 'com.android.support.constraint:constraint-layout:1.0.2' .... // database compile "org.greenrobot:greendao:$rootProject.greenDaoVersion"

//Kotter Knife
api 'com.jakewharton:kotterknife:0.1.0-SNAPSHOT'

// dependency injection
implementation "com.google.dagger:dagger:${daggerVersion}"
kapt "com.google.dagger:dagger-compiler:${daggerVersion}"
compileOnly "org.glassfish:javax.annotation:${annotationVersion}"

....

}

HarryHaiVn avatar Aug 14 '17 09:08 HarryHaiVn

Do you have your entity classes written in Java? Try commenting out all the uses of the DaoSession, then build the project and uncomment them.

Kobzol avatar Aug 16 '17 15:08 Kobzol

@HarryHaiVn I'm having the same problem and it's preventing me to migrate the Dagger Graph to Kotlin, did you find any solution so far? there's no way the DaoSession can be found with Dagger in Kotlin, even having all GreeDAO entities in java.

duenytz avatar Aug 23 '17 15:08 duenytz

try this order

apply plugin: 'com.android.application'
apply plugin: 'org.greenrobot.greendao'
apply plugin: 'kotlin-android'

works for me

mykola-dev avatar Aug 23 '17 15:08 mykola-dev

@deviant-studio Thanks man! It worked like a charm!

duenytz avatar Aug 23 '17 16:08 duenytz

Any updates on this? deviant-studio's solution didn't work for me.

scimetfoo avatar Apr 21 '18 07:04 scimetfoo

afterEvaluate {
    if (project.plugins.hasPlugin('org.greenrobot.greendao')) {
        def taskGraph = project.getGradle().getTaskGraph()
        taskGraph.whenReady {
            Task greendaoPrepareTask = project.tasks.getByName("greendaoPrepare")
            Task greendaoTask = project.tasks.getByName("greendao")

            Task debugKotlin = project.tasks.getByName("kaptDebugKotlin")
            Task releaseKotlin = project.tasks.getByName("kaptReleaseKotlin")
            debugKotlin.doFirst {
                greendaoPrepareTask.execute()
                greendaoTask.execute()
            }
            releaseKotlin.doFirst {
                greendaoPrepareTask.execute()
                greendaoTask.execute()
            }
        }
    }
}

I add this in my library project ,if you want to add this in your application module you can check the build type variants to start the greendao task

ttgdz avatar Jul 20 '18 05:07 ttgdz

Any news on that? Do you guys plan to support Kotlin soon enough?

VictorQueiroz avatar Dec 16 '18 21:12 VictorQueiroz

@deviant-studio Thanks man!

539863010 avatar Jan 07 '19 07:01 539863010

I am getting an error, can you please help me?

e: error: cannot access Database
  class file for org.greenrobot.greendao.database.Database not found
  Consult the following stack trace for details.
  com.sun.tools.javac.code.Symbol$CompletionFailure: class file for org.greenrobot.greendao.database.Database not found
:app:kaptDebugKotlin FAILED

noman-augmedix avatar Jan 11 '19 11:01 noman-augmedix

There likely will be no support for entities written in Kotlin.

-- Check out ObjectBox, our new fast, easy to use database!

greenrobot-team avatar Feb 26 '19 14:02 greenrobot-team

ttgdz`s solution works perfect.

below is my workaround

1.comment kapt 2.make project. greendao generated 3.reopen kapt

dahezi avatar Mar 28 '19 07:03 dahezi

@greenrobot @greenrobot-team I meet the same issue when i write the entity with java , but call Dao that haven't generated in kotlin class, report "compileDebugKotlin Unresolved reference: xxxDao" .For constrast, if i call the Dao in a java class, all going well. How can i fix that? thanks

my library module .gradle as below:

apply plugin: 'com.android.library' apply plugin: 'org.greenrobot.greendao' apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions'

android { compileSdkVersion 30 buildToolsVersion "29.0.3"

defaultConfig {
    minSdkVersion 29
    targetSdkVersion 30
    versionCode 1
    versionName "1.0"

    consumerProguardFiles "consumer-rules.pro"
}

buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
    }
}

}

greendao { schemaVersion 1 //database version targetGenDir 'src/main/java' //specify the directory where the code is generated daoPackage 'com.coloros.testdir.greendao' //generate code to specific packages }

dependencies { implementation fileTree(dir: "libs", include: ["*.jar"])

implementation 'androidx.appcompat:appcompat:1.2.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'

implementation "org.greenrobot:greendao:3.2.2"
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.3.1'

}

liuzhp3 avatar Sep 10 '20 09:09 liuzhp3