Visitar URL original
Support Gradle build cache · Issue #1114 · objectbox/objectbox-java · GitHub
Skip to content

Support Gradle build cache #1114

Description

@alexsurinov

The bug is occurred in an Android Project.

Description of the bug
During a manual conflict resolution, the objectbox-models/default.json file may accidentally contain duplicated indexId entries. During the build time, we don't have any errors. The project builds successfully, and the build output is cached to the Gradle Build Cache. Only at runtime, when we build BoxStore instance, an error occurred: io.objectbox.exception.DbSchemaException: Duplicate index ID 2 found for property .... Moreover, when a developer fixes duplicated indexId entries in the objectbox-models/default.json file and rebuilds the project, the error is still in place due to the result of the previous build is used from the Gradle Build Cache. It incurs hard-to-debug problems for our dev team.

Basic info:

  • ObjectBox version: 3.5.1
  • Reproducibility: always
  • Device: any device
  • OS: reproduced on Android 11, 13

Steps to reproduce the behavior:

  1. Activate the Gradle Build Cache: add org.gradle.caching=true in the gradle.properties file.
  2. Add two ObjectBox entities (e.g. User and Company) with the unique name property.
  3. Build the project to generate a objectbox-models/default.json file.
  4. Make changes to the objectbox-models/default.json file: set the same value to both indexId entries (Company.name and User.name).
  5. Rebuild the project.
  6. Install APK and launch the app. There is the error: io.objectbox.exception.DbSchemaException: Duplicate index ID 2 found for property ...
  7. Fix duplicated indexId in the objectbox-models/default.json file.
  8. Rebuild the project.
  9. Install APK and launch the app. There is still the same error. The result of the previous build is used.

Expected behavior

  • The Duplicate index ID error should occur during the build time, not at runtime.
  • Any change to the objectbox-models/default.json file should lead to the invalidation of the Gradle Build Cache to prevent the usage of stale data from the previous build.

Code
I've prepared the demo project to reproduce the problem with the described above steps:
https://github.com/rAseri/ObjectBoxDemo

Logs, stack traces

Process: com.example.myapplication, PID: 24377
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myapplication/com.example.myapplication.MainActivity}: io.objectbox.exception.DbSchemaException: Duplicate index ID 2 found for property User.name
	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3449)
	at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
	at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
	at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
	at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
	at android.os.Handler.dispatchMessage(Handler.java:106)
	at android.os.Looper.loop(Looper.java:223)
	at android.app.ActivityThread.main(ActivityThread.java:7656)
	at java.lang.reflect.Method.invoke(Native Method)
	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)

Activity

  1. greenrobot-team commented on Feb 13, 2023

    @greenrobot-team
    Member

    Thanks for reporting! I guess the culprit is that the model file is only touched by the annotation processor. We need to check if it can somehow be indicated to Gradle that the processor has the model file as an input and output. Also likely the generated code as output (or is it already?). The generated code is created using the Filer API, where output is monitored by Gradle.

    A workaround could be to add the model file as an input to e.g. an assemble task (or whichever necessary):

    tasks.named("assembleDebug") {
        inputs.property("objectboxModel") {
            file("objectbox-models/default.json")
        }
    }
    

    Maybe we can also do this automatically with the Gradle plugin.

    (Somewhat related, though configuration cache other than build cache is still experimental: #948)

  2. changed the title [-]Duplicate index ID at runtime[/-] [+]Support Gradle build cache[/+] on Feb 13, 2023
  3. alexsurinov commented on Feb 13, 2023

    @alexsurinov
    Author

    Thanks for your reply!

    I am not a Gradle expert, but it looks like a working solution (I'm using Kotlin Scripts here):

        plugins {
            id("io.objectbox")
            // ...
        }
      
        // ...
        tasks.withType<org.jetbrains.kotlin.gradle.tasks.BaseKapt>().configureEach {
            inputs.file("objectbox-models/default.json")
        }

    I added the model file as an input for BaseKapt task. Now every change in the model file causes the kaptDebugKotlin task to rerun as expected!
    Screenshot 2023-02-13 at 17 55 39

  4. alexsurinov commented on Feb 13, 2023

    @alexsurinov
    Author

    Maybe we can also do this automatically with the Gradle plugin.

    I think it's a good idea!

  5. greenrobot-team commented on Feb 14, 2023

    @greenrobot-team
    Member

    @Raseri Thanks for testing this! Looks like adding an input to the kapt (or Java equivalent annotation processor) task is what we will let the Gradle plugin do then.

    For our reference, this is internal issue objectbox-java#174

  6. greenrobot-team commented on Jul 28, 2025

    @greenrobot-team
    Member

    While researching something related, I noticed that kapt also has an option to turn off build cache for itself exactly for this reason.

    So as an alternative to configuring the model JSON file as an input like suggested above (and if done correctly also as output, which may lead to further issues), it should also be possible to use this workaround:

    kapt {
        useBuildCache = false
    }
  7. soloturn commented on Sep 26, 2026

    @soloturn

    hey @greenrobot-team @alexsurinov would objectbox/objectbox-java-generator#12 be acceptable to close this issue?

  8. greenrobot-team commented on Sep 28, 2026

    @greenrobot-team
    Member

    @soloturn Thanks, but these generated pull requests without actual knowledge of the project aren't really helpful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

bugSomething isn't working

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions