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

Obfuscation and enums #689

Closed
gabrielittner opened this issue Sep 25, 2018 · 6 comments · Fixed by #691
Closed

Obfuscation and enums #689

gabrielittner opened this issue Sep 25, 2018 · 6 comments · Fixed by #691

Comments

@gabrielittner
Copy link
Contributor

The integrated EnumJsonAdapter in StandardJsonAdapters can't be instantiated because the field name doesn't match the name anymore.

My solution is to annotate enums that are used with Moshi with @JsonClass(generateAdapter = false) and then keep the field names for annotated enums:

-keepclassmembernames @com.squareup.moshi.JsonClass class * extends java.lang.Enum {
    <fields>;
}

This way it's possible to avoid keeping all enums.

Do you think it would make sense to add it to the shipped Proguard config?

@swankjesse
Copy link
Collaborator

Yeah, I like that.

@ezamelczykespeo
Copy link

Does that mean that enums will only work with moshi if they're annotated with @JsonClass(generateAdapter = false)?

@gabrielittner
Copy link
Contributor Author

If you're using proguard/R8 and they would rename or remove one of the enum fields, yes.

@ezamelczykespeo
Copy link

I don't understand why @Json(name = "name) isn't sufficient. Enums work with Gsons @SerializedName

@realdadfish
Copy link

@ezamelczykespeo Issue probably resides in this line:

https://github.com/square/moshi/blob/master/moshi/src/main/java/com/squareup/moshi/StandardJsonAdapters.java#L240

The Java method getEnumConstants() still gives back the literal enum names, not the obfuscated ones, so initializing Moshi with any enum adapter will crash with an AssertionError that wraps a NoSuchFieldException stemming from the failed reflection call.

@VarunBarad
Copy link

@gabrielittner you just saved my day. 🙇

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants