Jsondb / jsondb-core

JsonDB a pure java database that stores its data as Json Files
http://www.jsondb.io
MIT License
213 stars 45 forks source link

Android does not find any classes #42

Open Hatzen opened 4 years ago

Hatzen commented 4 years ago

TL;DR; It seems this project is not able to run on android.

At first thanks for this library. But i m struggeling a lot at getting it working with android.

currently my problem is that the lib is initalized properly but as soon as i want to call jsonDBTemplate.createCollection(DeviceData1::class.java) it throws

io.jsondb.InvalidJsonDbApiUsageException: No class found with @Document Annotation ` 2020-08-31 21:26:20.854 15038-15038/de.hartz.software.parannoying E/AndroidRuntime: FATAL EXCEPTION: main Process: de.hartz.software.parannoying, PID: 15038 java.lang.RuntimeException: Unable to instantiate application de.hartz.software.parannoying.App: io.jsondb.InvalidJsonDbApiUsageException: No class found with @Document Annotation and attribute collectionName as: root at android.app.LoadedApk.makeApplication(LoadedApk.java:989) at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5733) at android.app.ActivityThread.-wrap1(Unknown Source:0) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1660) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6521) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) Caused by: io.jsondb.InvalidJsonDbApiUsageException: No class found with @Document Annotation and attribute collectionName as: root at io.jsondb.JsonDBTemplate.createCollection(JsonDBTemplate.java:285) at io.jsondb.JsonDBTemplate.createCollection(JsonDBTemplate.java:275) at de.hartz.software.parannoying.model.Storage.init(Storage.kt:94) at de.hartz.software.parannoying.App.initStorage(App.kt:58) at de.hartz.software.parannoying.App.initApp(App.kt:26) at de.hartz.software.parannoying.App.initApp$default(App.kt:21) at de.hartz.software.parannoying.App.attachBaseContext(App.kt:50) at android.app.Application.attach(Application.java:189) at android.app.Instrumentation.newApplication(Instrumentation.java:1104) at android.app.Instrumentation.newApplication(Instrumentation.java:1088) at android.app.LoadedApk.makeApplication(LoadedApk.java:983) at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5733)  at android.app.ActivityThread.-wrap1(Unknown Source:0)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1660)  at android.os.Handler.dispatchMessage(Handler.java:106)  at android.os.Looper.loop(Looper.java:164)  at android.app.ActivityThread.main(ActivityThread.java:6521)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)  `

So it is not finding the entityclasses also the base package is properly set and the anntitation is set like mentioned in the usage section. On the other hand determineCollectionName is working without any problems..

when calling reflections.getAllTypes() in CollectionMetaData.builder(JsonDBConfig dbConfig) from the debugger it throws: Couldn't find subtypes of Object. Make sure SubTypesScanner initialized to include Object class - new SubTypesScanner(false)

I disabled obfusication via gradle.build file: ... minifyEnabled false useProguard false ... I m using Java 8 and also the Java 8 Lib ('io.jsondb:jsondb-core:1.0.115-j8'). In special i use Kotlin in the whole application, but also when i m creating java files for the Entities these are not found.


I dont have any further ideas on how to debugg this problem. Is this problem maybe already known or are there any work arounds?

Hatzen commented 4 years ago

When adding the code to a simple to a junit test within the exact same project everything works just fine, creating, reading, and saving. But adding it to an instrumentedTest which is the same as running the app. The classes wont be found. So it does not seem to be a kotlin issue but more an android thing or maybe a wrong configuration or something.

@RunWith(AndroidJUnit4::class)
class ImportExportDataTest {

    lateinit var context: Context

    @Before
    open fun setUp() {
        context = InstrumentationRegistry.getTargetContext()
    }

    @Test
    fun testJsonDb () {
        val privateFiles = context.filesDir.absolutePath + "/samples"
        //Actual location on disk for database files, process should have read-write permissions to this folder
        val dbFilesLocation = privateFiles
        //Java package name where POJO's are present
        val baseScanPackage = "de.hartz.software.parannoying.model.DeviceData1"
        val jsonDBTemplate = JsonDBTemplate(dbFilesLocation, baseScanPackage)
        jsonDBTemplate.createCollection(DeviceData1::class.java)
        // => io.jsondb.InvalidJsonDbApiUsageException: No class found with @Document Annotation and attribute collectionName as: root1
        var list = jsonDBTemplate.findById(1L, DeviceData1::class.java)
        val deviceData: DeviceData1 = DeviceData1()
        if (list != null) {
            Assert.fail()
        } else {
            jsonDBTemplate.insert<DeviceData1>(deviceData)
        }
        jsonDBTemplate.save<DeviceData1>(deviceData, "root")

        list = jsonDBTemplate.findById(1L, DeviceData1::class.java)
        if (list != null) {

        } else {
            Assert.fail()
        }
    }
}
package de.hartz.software.parannoying.model;

@Document(collection = "root1", schemaVersion= "1.0")
public class DeviceData1 implements Serializable{
    @Id Long id = 1L; // Use 1 so it is always the same object.

    public DeviceData1() {}

    public void setId(Long id) {
        this.id = id;
    }

    public Long getId() {
        return id;
    }

}

It seems to be an issue with Reflections API https://stackoverflow.com/questions/25136305/android-reflection-doesnt-work-it-dont-find-any-class

https://github.com/ronmamo/reflections/issues/127

DzDev202020 commented 2 years ago

Hello, Could please tell me how did you solve this problem ?? I am already 5day and no solution for it :/ and the library very useful. if you could help I appreciate it.