Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Kotlin support #9

Closed
greenrobot opened this Issue Jan 24, 2017 · 56 comments

Comments

Projects
None yet
7 participants
@greenrobot
Copy link
Member

commented Jan 24, 2017

Right now entities must be defined in Java. This is because the ObjectBox Gradle plugin uses JDT to parse and modify Java code.

Possible options:

  • Find (wait for) a Kotlin AST library like JDT
  • (Also) support plain annotation processing with its limitations (no modifications of existing sources)
  • ?
@EricKuck

This comment has been minimized.

Copy link

commented Jan 25, 2017

FWIW, I don't view not being able to change source files to be a bad thing. I never, ever want a library to change code that I have written. I would view using the standard annotation processor as a big upgrade, not only because it would add support for Kotlin/Scala/Groovy/whatever, but because it would leave my files alone.

@greenrobot

This comment has been minimized.

Copy link
Member Author

commented Jan 25, 2017

+EricKuck I see your point. Just a few comments on that: technically, ObjectBox never touches code you have written. It only adds code to your class or replaces previously generated code. Other tools do byte code manipulation, which can get confusing during debugging. One could say ObjectBox is fully transparent about those changes and see this as an advantage even.

@EricKuck

This comment has been minimized.

Copy link

commented Jan 25, 2017

I agree that bytecode weaving isn't ideal, but annotation processing doesn't typically do that. I haven't looked into what this project uses code manipulation for enough to know if an apt dependency would cover everything, but it would be a huge upgrade to switch to something like this if it did.

@greenrobot greenrobot added the feature label Jan 28, 2017

@Miha-x64

This comment has been minimized.

Copy link

commented Jan 30, 2017

As examples say, ObjectBox (and GreenDAO) need our models to have all-arg-constructor, no-arg constructor, and getters/setters.
In Kotlin, a primary constructor with all default values specified yields a compiler to generate both all-arg and no-arg constructors. And getters/setters are exist in Kotlin by default.
So, ObjectBox just should force us to declare all default values in a primary constructor.

@greenrobot

This comment has been minimized.

Copy link
Member Author

commented Jan 30, 2017

ObjectBox just should force us to declare all default values in a primary constructor.

The "force us" might be the problem. The big constructor expects the parameters in a certain order, for example. Potential for big trouble if left to humans... 🤖

@LouisCAD

This comment has been minimized.

Copy link

commented Jan 30, 2017

@greenrobot There could be a check at compile time, and maybe a lint too to prevent us from compiling and shipping such an app with mis-ordered all parameters constructor.

@greenrobot

This comment has been minimized.

Copy link
Member Author

commented Jan 30, 2017

And there also should be some fallback. Some plan B that works without the constructor.

@Miha-x64

This comment has been minimized.

Copy link

commented Jan 30, 2017

The big constructor expects the parameters in a certain order

GreenDAO uses such constructors from generated classes.
And humans can (and should) use named arguments.

@LouisCAD

This comment has been minimized.

Copy link

commented Jan 31, 2017

Hope this issue is resolved before ObjectBox leaves beta status

sebaslogen added a commit to sebaslogen/KotlinWeatherApp that referenced this issue Feb 22, 2017

Configure ObjectBox
FAiling due to @id annotation not yet supported for Kotlin
objectbox/objectbox-java#9
@carmas123

This comment has been minimized.

Copy link

commented Mar 12, 2017

+1

@greenrobot

This comment has been minimized.

Copy link
Member Author

commented Jun 1, 2017

Internally, we have the first Kotlin entities (data classes) running with ObjectBox.

Next step is to look into how to improve the API specific to Kotlin, for example with extension functions. An obvious improvement would be BoxStore.boxFor to work nicely with KClass.

Any other ideas?

@greenrobot

This comment has been minimized.

Copy link
Member Author

commented Jun 28, 2017

We just released a release candidate with full Kotlin support. Please give version "0.9.13-RC" a try!

Check the Kotlin docs and the example project (currently only in dev branch).

Because this is a rather big update, we would like to get some feedback early on. Thanks!!

@jumaallan

This comment has been minimized.

Copy link

commented Jul 9, 2017

have tried the version 0.9.13-RC, however am getting this error, Error:(33, 20) Unresolved reference: MyObjectBox. How do I resolve this?

@greenrobot

This comment has been minimized.

Copy link
Member Author

commented Jul 9, 2017

@jumadeveloper Did you try to build the projects first? This class is generated during build.

@jumaallan

This comment has been minimized.

Copy link

commented Jul 9, 2017

@greenrobot I have tried several times. Still no luck, When i switch to Java Model, it works just fine. I dont think am missing anything, because I have been using Objectbox for a while

@jumaallan

This comment has been minimized.

Copy link

commented Jul 9, 2017

I have retried, still not working. That's the only issue am getting : Unresolved reference: MyObjectBox.

@greenrobot greenrobot added this to the 0.9.13 milestone Jul 10, 2017

@greenrobot

This comment has been minimized.

Copy link
Member Author

commented Jul 10, 2017

@jumadeveloper Does this occur in Android Studio? Did you check building with gradlew?

@greenrobot

This comment has been minimized.

Copy link
Member Author

commented Jul 10, 2017

There's a new version "0.9.13-RC2" with fixes for transient lists etc.

@jumaallan

This comment has been minimized.

Copy link

commented Jul 10, 2017

@greenrobot Yes, this occurs with Android Studio. Just added the 0.9.13-RC2 dependency, synced gradle, but getting the error during run. See my code snippets for my model, and my gradle files. Am not sure if there is something am missing here.

User Model
`package com.androidstudy.kotlinretrofitobjectbox.models

import io.objectbox.annotation.Entity
import io.objectbox.annotation.Id

/**

  • Created by Juma on 7/9/2017.
    */

@entity
data class User(
@id var id: Long = 0,
val first_name: String,
val last_name: String,
val username: String,
val email: String,
val password: String,
val image: String
)`

Build.gradle (Project)
`// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
ext.kotlin_version = '1.1.3-2'
ext.objectBoxVersion = "0.9.13-RC2"
repositories {
jcenter()
maven { url "http://objectbox.net/beta-repo/" }
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.3'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "io.objectbox:objectbox-gradle-plugin:$objectBoxVersion"

    // NOTE: Do not place your application dependencies here; they belong
    // in the individual module build.gradle files
}

}

allprojects {
repositories {
jcenter()
maven { url "http://objectbox.net/beta-repo/" }
}
}

task clean(type: Delete) {
delete rootProject.buildDir
}
`

build.gradle (app)
`apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'io.objectbox.android.transform'

android {
compileSdkVersion 25
buildToolsVersion "26.0.0"
defaultConfig {
applicationId "com.androidstudy.kotlinretrofitobjectbox"
minSdkVersion 10
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'
}
}
sourceSets {
main.java.srcDirs += 'build/generated/source/objectbox'
}
}

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'
})
androidTestCompile 'com.google.code.findbugs:jsr305:3.0.2'
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
testCompile 'junit:junit:4.12'
compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"

compile "io.objectbox:objectbox-android:$objectBoxVersion"
compile "io.objectbox:objectbox-kotlin:$objectBoxVersion"
kapt "io.objectbox:objectbox-processor:$objectBoxVersion"

compile 'com.squareup.retrofit2:retrofit:2.3.0'
compile 'com.squareup.retrofit2:converter-gson:2.3.0'

}
repositories {
mavenCentral()
}
And this is my App.ktpackage com.androidstudy.kotlinretrofitobjectbox

/**

  • Created by Juma on 7/9/2017.
    */
    import android.app.Application
    import android.os.Environment

import java.io.File

import io.objectbox.BoxStore

class App : Application() {

companion object Constants {
    const val TAG = "ObjectBoxExample"
    const val EXTERNAL_DIR = false
}

lateinit var boxStore: BoxStore
    private set

override fun onCreate() {
    super.onCreate()

    //        if (EXTERNAL_DIR) {
    //            // Example how you could use a custom dir in "external storage"
    //            // (Android 6+ note: give the app storage permission in app info settings)
    //            File directory = new File(Environment.getExternalStorageDirectory(), "objectbox-notes");
    //            boxStore = MyObjectBox.builder().androidContext(App.this).directory(directory).build();
    //        } else {
    // This is the minimal setup required on Android
    boxStore = MyObjectBox.builder().androidContext(this).build()
    //        }
}

}`

@greenrobot

This comment has been minimized.

Copy link
Member Author

commented Jul 10, 2017

@jumadeveloper What do you see if you do ./gradlew clean build --stacktrace?

@jumaallan

This comment has been minimized.

Copy link

commented Jul 10, 2017

@greenrobot How do i run this? on my terminal or where?

@jumaallan

This comment has been minimized.

Copy link

commented Jul 10, 2017

I am currently on a Windows machine, so cmd not terminal

@LouisCAD

This comment has been minimized.

Copy link

commented Jul 10, 2017

@jumadeveloper Most gradle commands are platform agnostic, hence work on Windows cmd too. You also have access to it from the Terminal tool window in Android Studio

@greenrobot

This comment has been minimized.

Copy link
Member Author

commented Jul 10, 2017

@jumadeveloper cmd is fine, go to your project dir, and run gradlew clean build --stacktrace

@jumaallan

This comment has been minimized.

Copy link

commented Jul 10, 2017

@LouisCAD Thanks, just learnt that now

@jumaallan

This comment has been minimized.

Copy link

commented Jul 11, 2017

I have matched my gradle with your documentation, still getting the same error. I'll probably try this with another machine and see if it works

@greenrobot

This comment has been minimized.

Copy link
Member Author

commented Jul 11, 2017

@jumadeveloper If you want to put your gradle files somewhere (e.g. do a gist), I'm happy to have a look too.

@jumaallan

This comment has been minimized.

@greenrobot

This comment has been minimized.

Copy link
Member Author

commented Jul 12, 2017

@jumadeveloper Using your code we could reproduce the issue. This line was missing in the app Gradle file:
apply plugin: 'kotlin-kapt'

Without that line, kapt1 is used which may or may not work - for our test projects it worked. That line enables kapt3, which fixes issues like that.

You can also check 0.9.13-RC3, which should become 0.9.13 later today.

Thanks for working on that issue!

@jumaallan

This comment has been minimized.

Copy link

commented Jul 12, 2017

Thanks a lot, Appreciated. When is version one expected to be released?

@jumaallan

This comment has been minimized.

Copy link

commented Jul 12, 2017

And one question, why is it that i can not use a variable _id yet Objectbox uses id?

@greenrobot

This comment has been minimized.

Copy link
Member Author

commented Jul 12, 2017

@jumadeveloper do you have an example to help me understand your question?

@jumaallan

This comment has been minimized.

Copy link

commented Jul 12, 2017

@greenrobot I got some error a while back when using _id as an Entity. Let me try use the version RC3 and see if get any error

@jumaallan

This comment has been minimized.

Copy link

commented Jul 12, 2017

@greenrobot It works now, perfectly well. Thanks!

@greenrobot greenrobot closed this Jul 12, 2017

@greenrobot

This comment has been minimized.

Copy link
Member Author

commented Jul 13, 2017

PS.: Here's the announcement post

@jumaallan

This comment has been minimized.

Copy link

commented Jul 13, 2017

@greenrobot Thanks, just checked it out. I will be writting an article about it sometime today in my blog, make sure to check it out. Thanks

@jumaallan

This comment has been minimized.

Copy link

commented Jul 18, 2017

@greenrobot Does objectbox have a maximum limit? Or when does it log Database Full, and what could be the reason? Because it appears on some phones

@greenrobot

This comment has been minimized.

Copy link
Member Author

commented Jul 18, 2017

@jumadeveloper Thanks for sharing your first article on ObjectBox. Ping us if you continue with the second, we're happy to have a look. Yes, there is a maximum size, which is 512 MB. This is a precaution to avoid the disk being flooded when your app is misbehaving. If you need more space, check out BoxStoreBuilder.maxSizeInKByte(long).

@jumaallan

This comment has been minimized.

Copy link

commented Jul 19, 2017

@greenrobot Thanks, I will be writting it later today. Appreciated

@sadeqzadeh

This comment has been minimized.

Copy link

commented Sep 26, 2017

Hey @greenrobot Thanks for the great work! I stumbled upon the same issue (MyObjectBox not resolved), and I seem to have got everything right. Have added the repositories in the top-level project and applied the plugin (after applying kotlin-kapt plugin) and added the dependencies (both compile and annotation processing stage - kapt) to the app module. I also tried adding the generated directory to the projects's source sets: main.java.srcDirs += 'build/generated/source/objectbox', but no effect. I was afraid that Proguard is removing the generated class, but tried adding an explicit keep rule, with no effect again. Any ideas?

Here is the stacktrace:

:app:clean :sdk:clean :app:objectboxPrepareBuild :app:preBuild :app:extractProguardFiles :app:preDebugBuild :app:checkDebugManifest :app:preReleaseBuild :app:preStagingBuild :app:prepareComAndroidSupportAnimatedVectorDrawable2531Library :app:prepareComAndroidSupportAppcompatV72531Library :app:prepareComAndroidSupportCardviewV72531Library :app:prepareComAndroidSupportDesign2531Library :app:prepareComAndroidSupportGridlayoutV72531Library :app:prepareComAndroidSupportPercent2531Library :app:prepareComAndroidSupportRecyclerviewV72531Library :app:prepareComAndroidSupportSupportCompat2531Library :app:prepareComAndroidSupportSupportCoreUi2531Library :app:prepareComAndroidSupportSupportCoreUtils2531Library :app:prepareComAndroidSupportSupportFragment2531Library :app:prepareComAndroidSupportSupportMediaCompat2531Library :app:prepareComAndroidSupportSupportV42531Library :app:prepareComAndroidSupportSupportVectorDrawable2531Library :app:prepareComAndroidSupportTransition2531Library :app:prepareComBraintreepaymentsApiBraintree253Library :app:prepareComBraintreepaymentsApiCore253Library :app:prepareComBraintreepaymentsBrowserSwitch013Library :app:prepareComGoogleAndroidGmsPlayServicesAnalytics1102Library :app:prepareComGoogleAndroidGmsPlayServicesBase1102Library :app:prepareComGoogleAndroidGmsPlayServicesBasement1102Library :app:prepareComGoogleAndroidGmsPlayServicesLocation1102Library :app:prepareComGoogleAndroidGmsPlayServicesMaps1102Library :app:prepareComGoogleAndroidGmsPlayServicesPlaces1102Library :app:prepareComGoogleAndroidGmsPlayServicesTasks1102Library :app:prepareComGoogleFirebaseFirebaseAnalytics1102Library :app:prepareComGoogleFirebaseFirebaseAnalyticsImpl1102Library :app:prepareComGoogleFirebaseFirebaseCommon1102Library :app:prepareComGoogleFirebaseFirebaseConfig1102Library :app:prepareComGoogleFirebaseFirebaseCore1102Library :app:prepareComGoogleFirebaseFirebaseIid1102Library :app:prepareComGoogleFirebaseFirebaseMessaging1102Library :app:prepareComGoogleMapsAndroidAndroidMapsUtils043Library :app:prepareComJakewhartonButterknife801Library :app:prepareComJakewhartonTimberTimber451Library :app:prepareComMcxiaokeViewpagerindicatorLibrary241Library :app:prepareComMixpanelAndroidMixpanelAndroid487Library :app:prepareComOrhanobutLogger115Library :app:prepareComOrhanobutTracklyticsTracklyticsRuntime200Library :app:prepareComPaypalAndroidSdkPaypalOneTouch253Library :app:prepareComSquareupLeakcanaryLeakcanaryAndroid15Library :app:prepareComSquareupPicassoPicasso300SNAPSHOTLibrary :app:prepareComWdullaerMaterialdatetimepicker230Library :app:preDebugAndroidTestBuild :app:preDebugUnitTestBuild :app:preReleaseUnitTestBuild :app:preStagingUnitTestBuild :app:prepareIoMironovSmugglerSmugglerRuntime01213Library :app:prepareIoObjectboxObjectboxAndroid101Library :app:prepareIoReactivexRxandroid120Library :app:prepareMeZhanghaiAndroidMaterialprogressbarLibrary115Library :sdk:compileKotlin Using Kotlin incremental compilation :sdk:compileJava Note: Some input files use unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details. :sdk:copyMainKotlinClasses :sdk:compileRetrolambdaMain :sdk:processResources UP-TO-DATE :sdk:classes :sdk:jar :app:prepareDebugDependencies :app:compileDebugAidl :app:compileDebugRenderscript :app:generateDebugBuildConfig :app:mergeDebugShaders :app:compileDebugShaders :app:generateDebugAssets :app:mergeDebugAssets :app:processDebugManifest :app:fabricGenerateResourcesDebug :app:generateDebugResValues :app:processDebugGoogleServices Parsing json file: /Users/me/dev/pepper/app/google-services.json :app:generateDebugResources :app:mergeDebugResources :app:processDebugResources :app:generateDebugSources :app:incrementalDebugJavaCompilationSafeguard :app:javaPreCompileDebug :app:kaptGenerateStubsDebugKotlin Using Kotlin incremental compilation :app:kaptDebugKotlin :app:compileDebugKotlin Using Kotlin incremental compilation e: /Users/me/dev/pepper/app/src/main/kotlin/com/foo/bar/DefaultApplication.kt: (38, 18): Unresolved reference: MyObjectBox :app:compileDebugKotlin FAILED

@jumaallan

This comment has been minimized.

Copy link

commented Sep 26, 2017

@sadeqzadeh Hi there, which version of objectbox are you using?

@sadeqzadeh

This comment has been minimized.

Copy link

commented Sep 26, 2017

Hi @jumadeveloper I use 1.0.1

@jumaallan

This comment has been minimized.

Copy link

commented Sep 26, 2017

@sadeqzadeh If you don't mind, I have done a tutorial on my blog, on how to set it up using Kotlin. You could check it out here http://androidstudy.com

@sadeqzadeh

This comment has been minimized.

Copy link

commented Sep 26, 2017

Thanks for your help @jumadeveloper but the sections related to your build script and initilization of your BoxStore in your Application class is missing in the tutorial.
Update: My bad, I can now see in another post that you initialize your boxStore using MyObjectBox class which is missing for me.

@jumaallan

This comment has been minimized.

Copy link

commented Sep 26, 2017

@sadeqzadeh So have you managed to make it work?

@sadeqzadeh

This comment has been minimized.

Copy link

commented Sep 26, 2017

@jumadeveloper No, not so far. What I meant by "missing" was that MyObjectBox class is not generated in my project, so I cannot reference it

@jumaallan

This comment has been minimized.

Copy link

commented Sep 26, 2017

@sadeqzadeh Have you created at least one model? With the @entity annotation? Then tried to Make Project in Android Studio?

@sadeqzadeh

This comment has been minimized.

Copy link

commented Sep 27, 2017

Yes, I have done so, but the issue still exists @jumadeveloper. For now, I will go with storing a serialized json in SharedPreferences

@jumaallan

This comment has been minimized.

Copy link

commented Sep 27, 2017

That is pretty akward @sadeqzadeh Create a gist and add your code there, or create a demo repo and push it there, so that I can try building the same code from my end, if you don't mind

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.