question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

Otherwise they will be removed by R8 since it cannot determine whether we are doing reflection on them.

Old issue below:


Using com.squareup.retrofit2:retrofit:2.9.0

After upgrading gradle plugin from

com.android.tools.build:gradle:4.1.3

to

com.android.tools.build:gradle:4.2.2

I’m getting following error when running with minifyEnabled true:

java.lang.IllegalArgumentException: Method return type must not include a type variable or wildcard

Here’s how looks the method, which generates the error:

    @FormUrlEncoded
    @POST("view_ad") //NON-NLS
    Call<GenericDataResponse<EmptyData>> viewAd(
            @Field("session_id") String sessionId, //NON-NLS
            @Field("id") int id //NON-NLS
    );

    public class EmptyData {

        public EmptyData(){}

    }

    public class GenericDataResponse<T> {

        @SerializedName("result") //NON-NLS
        public String result;

        @SerializedName("data") //NON-NLS
        public T data;

        @SerializedName("mess") //NON-NLS
        public String[] messages;
    }

In order to fix this I have to either:

  • downgrade to the previous gradle plugin version
  • add proguard rule: -keep class com.example.app.EmptyData
  • disable proguard/r8

I’ve read the docs about gradle 4.2 changes and nothing seems to be related to proguard directly. Can someone explain what’s going on?

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:4
  • Comments:21 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
ulmaxycommented, Jul 20, 2021

On a class, the signature I’ve got is similar to this: @GET("endpoint") fun getData(): Single<List<SomeClass>>

And adding the @Keep annotation to SomeClass helped

1reaction
JakeWhartoncommented, Apr 8, 2022

When running R8 in full mode all generic types are removed unless they are explicitly kept. Retrofit automatically keeps the generic part for users of Call, but if you are using Rx or Kotlin or some other library as your adapter it will not work (and it’s wasteful to keep the generic on all usages of those types). The only way is to explicitly keep the body types, hence the annotation processor.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Configure annotation processors | IntelliJ IDEA Documentation
You can create a new profile, group several modules under it and configure an annotation processing just for the specified group of modules....
Read more >
Annotation Processing 101 - Hannes Dorfmann
Annotation processing is a tool build in javac for scanning and processing annotations at compile time. You can register your own annotation ......
Read more >
Annotation Processor appears to break Java generics
You say that the user will need the concrete (generic) types, but they actually won't need them if they only interact with the...
Read more >
An Introduction to Annotations and Annotation Processing in ...
The java.lang package provides some core annotations and also gives us the capability to create our custom annotations that can be processed ...
Read more >
javac - Java programming language compiler
The directory must already exist; javac will not create it. ... -proc:only means that only annotation processing is done, without any subsequent compilation ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found