greenrobot / greenDAO

greenDAO is a light & fast ORM solution for Android that maps objects to SQLite databases.
http://greenrobot.org/greendao/
12.63k stars 2.89k forks source link

Support Kotlin #395

Open ghost opened 8 years ago

ghost commented 8 years ago

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.

greenrobot-team commented 8 years ago

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

ghost commented 8 years ago

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.

greenrobot-team commented 8 years ago

@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

hotchemi commented 8 years ago

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.

modislaszlox2 commented 7 years ago

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.

barryzzz commented 7 years ago

Does greendao have a plan to support kotlin?

greenrobot-team commented 7 years ago

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

mykola-dev commented 7 years ago

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

greenrobot-team commented 7 years ago

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'
    }
}
namchuai commented 7 years ago

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

Thank you!

erfanio commented 7 years ago

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

rmr356 commented 7 years ago

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

dns21395 commented 7 years ago

@rmr356 Yes. Indeed its not supported still.

recoverrelax commented 7 years ago

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.

Kobzol commented 7 years ago

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.

HarryHaiVn commented 7 years ago

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

HarryHaiVn commented 7 years ago

..... 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}"

....

}

Kobzol commented 7 years ago

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.

duenytz commented 7 years ago

@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.

mykola-dev commented 7 years ago

try this order

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

works for me

duenytz commented 7 years ago

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

scimetfoo commented 6 years ago

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

ttgdz commented 6 years ago
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

VictorQueiroz commented 5 years ago

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

539863010 commented 5 years ago

@deviant-studio Thanks man!

noman-augmedix commented 5 years ago

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
greenrobot-team commented 5 years ago

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

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

dahezi commented 5 years ago

ttgdz`s solution works perfect.

below is my workaround

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

liuzhp3 commented 4 years ago

@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'

}