contentful / vault

Easy persistence of Contentful data for Android over SQLite.
https://contentful.github.io/vault/
Apache License 2.0
85 stars 19 forks source link

Proguard issue #164

Closed rwozniak closed 3 years ago

rwozniak commented 3 years ago

We’re developing an app using Contentful java SDK and the Vault for caching content. Everything works fine until I turn on Proguard (minifyEnabled true). I’m using the proguard config file from the repo (https://github.com/contentful/vault/blob/master/proguard-vault.cfg), yet I’m getting an error when querying Vault.

val allResources = vault.fetch(FeedResource::class.java)
            .where("`${`FeedResource$Fields`.FEED_TYPE}` = ?", feedType)
            .all()

and the exception I’m getting:

java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.List com.contentful.vault.ModelHelper.getFields()' on a null object reference

Any idea what might be causing this? I’m struggling with Proguard configuration for a few hours now without any success (I’m even keeping all the classes from my codebase as well as from Vault and it still does not work).

rafalniski commented 3 years ago

Hey @rwozniak,

could you please annotate FeedResource class with @Keep annotation?

rwozniak commented 3 years ago

Hi @rafalniski ,

unfortunately it does not help - and I already have the Proguard configuration in place to not obfuscate any of my Resource classes: -keep class com.my.app.data.vault.resource.** { *; }

rafalniski commented 3 years ago

Ok, one more try. Could you add those rules?

-keepnames class * implements java.io.Serializable
-keepclassmembers class * implements java.io.Serializable {
    private static final long serialVersionUID;
    private static final java.io.ObjectStreamField[] serialPersistentFields;
    !static !transient <fields>;
    private void writeObject(java.io.ObjectOutputStream);
    private void readObject(java.io.ObjectInputStream);
    java.lang.Object writeReplace();
    java.lang.Object readResolve();
}
rwozniak commented 3 years ago

Unfortunately I have already seen that in one of the issues here, have it in the vault.pro Proguard file and I have the same error

rwozniak commented 3 years ago

Hi, any updates here?

rafalniski commented 3 years ago

Sorry for the late response. Have you annotated your model class fields with @Field? If yes, can I see your model classes? Also could you please try this tool: https://playground.proguard.com/ and see if your model classes are not affected by proguard?

rwozniak commented 3 years ago

Sorry for late reply also, it turns out that the culprit in here was Kotlin. A debugging session after downloading the library revealed that the problematic class was this one:

@ContentType("external")
class ExternalResource : Resource() {

    @Field
    lateinit var title: String

    //...
}

The external content type is crucial here as it is also a keyword in Kotlin. I have not dived deeper into the bottom of this, but changing implementation of this class from Kotlin to Java solved the issue:

@ContentType("external")
public class ExternalResource extends Resource {

    @Field
    public String title;

    //...
}
rafalniski commented 3 years ago

@rwozniak Glad, you managed to resolve this, thanks!