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

Import response type keeper #4067

Merged
merged 1 commit into from
Feb 2, 2024
Merged

Import response type keeper #4067

merged 1 commit into from
Feb 2, 2024

Conversation

JakeWharton
Copy link
Member

This annotation processor parses all types used as response bodies in Retrofit service methods and adds a keep rule for them. This ensures that even if the type isn't used by callers, it is kept and used to parse the body.

Closes #3588

We have been using this internally on Cash for a few years now. I've expanded its support to cover all Retrofit annotations as well as written actual tests (gasp!).

This annotation processor parses all types used as response bodies in Retrofit service methods and adds a keep rule for them. This ensures that even if the type isn't used by callers, it is kept and used to parse the body.
returnType.recursiveParameterizedTypesTo(referenced)

// Retrofit has special support for 'suspend fun' in Kotlin which manifests as a
// final Continuation parameter whose generic type is the declared return type.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice comment


for ((element, referencedTypes) in elementToReferencedTypes) {
val typeName = element.qualifiedName.toString()
val outputFile = "META-INF/proguard/retrofit-response-type-keeper-$typeName.pro"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is surprising but makes sense.


@Test
fun kotlinSuspend() {
val service = JavaFileObjects.forSourceString(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sneaky

@JakeWharton JakeWharton merged commit e34aee6 into trunk Feb 2, 2024
7 checks passed
@JakeWharton JakeWharton deleted the jw.apt.2024-01-29 branch February 2, 2024 19:48
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 this pull request may close these issues.

Add annotation processor for generating keep rules for types mentioned only inside generics
2 participants