JetBrains / Exposed

Kotlin SQL Framework
http://jetbrains.github.io/Exposed/
Apache License 2.0
8.25k stars 689 forks source link

Exposed Proguard Problem #1377

Open goksunonal opened 2 years ago

goksunonal commented 2 years ago

Hi, I am using IntEntityClass and EntityId in my database class and getting this error while using proguard.

Caused by: java.lang.NullPointerException: null
    at org.jetbrains.exposed.dao.EntityClass.<init>(EntityClass.kt:18)
    at org.jetbrains.exposed.dao.IntEntityClass.<init>(IntEntity.kt:8)
    at org.jetbrains.exposed.dao.IntEntityClass.<init>(IntEntity.kt:8)
    at myPath.MyClass$Companion.<init>(SourceFile:50)
    at myPath.MyClass$Companion.<init>(SourceFile)
    at myPath.MyClass.<clinit>(SourceFile:52)

Tried these rules too but didn't seem to solve:

-keepclassmembers class myPath.MyClass{
    public static ** Companion;
}

-keep class myPath.MyClass$Companion { *; }
-keep class org.jetbrains.** { *; }
-keep interface org.jetbrains.** { *; }
-keep enum org.jetbrains.** { *; }

class context ->

internal object MyClasses: IntIdTable(Constant.MyCLASS) {
    val name = varchar("name", length = 50).uniqueIndex()
}

class MyClass(id: EntityID<Int>) : IntEntity(id) {
    companion object : IntEntityClass<MyClass>(MyClasses)

    var name by MyClasses.name
}

I am not sure if problem is totally related to 'Exposed' but i would like to hear your opinions.

stengvac commented 2 years ago

Did you tried to remove internal on MyClasses?

RationalityFrontline commented 2 years ago

No proguard rules are needed. Just pass your DAO class to your EntityClass's constructor:

class MyClass(id: EntityID<Int>) : IntEntity(id) {
    companion object : IntEntityClass<MyClass>(MyClasses, MyClass::class.java)

    var name by MyClasses.name
}
goksunonal commented 2 years ago

Tried both ways, didn't work. I am suspecting usage of companion object.

RationalityFrontline commented 2 years ago

@goksunonal It's wired, my solution should work. The NullPointerException was thrown from here: image So passing in the entityType should solve the problem.

goksunonal commented 2 years ago

@RationalityFrontline 'Null Pointer' problem is happening less often after your recommendation. There is another problem which starting to happen more which may give us more idea:

java.lang.NoClassDefFoundError: Could not initialize class myPath.MyClass
    at myPath.MyClass.I.a(SourceFile:82)
    at myPath.MyClass.I.invoke(SourceFile:81)
    at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt$transaction$1.invoke(ThreadLocalTransactionManager.kt:131)
    at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt.keepAndRestoreTransactionRefAfterRun(ThreadLocalTransactionManager.kt:202)
    at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt.transaction(ThreadLocalTransactionManager.kt:123)
    at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt.transaction(ThreadLocalTransactionManager.kt:121)
    at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt.transaction$default(ThreadLocalTransactionManager.kt:120)
    at myPath.MyClass.F.a(SourceFile:81)
    at myPath.MyClass.F.a(SourceFile:62)
    at myPath.MyClass.H.a(SourceFile:97)
    at myPath.MyClass.H.invoke(SourceFile:95)
    at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt$inTopLevelTransaction$1.invoke(ThreadLocalTransactionManager.kt:173)
    at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt$inTopLevelTransaction$2.invoke(ThreadLocalTransactionManager.kt:194)
    at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt.keepAndRestoreTransactionRefAfterRun(ThreadLocalTransactionManager.kt:202)

Related function:

    private fun findBySystemName(id: Int): String {
        return transaction {
            MyClass.findById(id).toString()
        }
    }

image

Tapac commented 2 years ago

Did you relocate packages with Proguard?

goksunonal commented 2 years ago

Yep,

problem is definitely with companion object : IntEntityClass<MyClass>(MyClasses) that part. No error occurs when i remove it.

joc-a commented 1 year ago

Hi @goksunonal. Are you still getting this error?

goksunonal commented 1 year ago

It's been a long time since I looked, the last time I worked on it, I did find a workaround and got rid of the companion object usage. I am no longer working on the project but the error was still there when I last tried it in mid 2022.