10000 Edge case where R8 removes the TypeToken's generic type (while consuming the library's recently updated proguard rules) · Issue #2658 · google/gson · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Edge case where R8 removes the TypeToken's generic type (while consuming the library's recently updated proguard rules) #2658

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

Open
2 tasks done
alipov opened this issue Mar 29, 2024 · 2 comments
Labels
bug proguard-r8 Issues relating to the use of ProGuard and/or R8, such as problems due to obfuscation

Comments

@alipov
Copy link
alipov commented Mar 29, 2024

(Originally reported here).

Gson version

2.10.1 + those proguard rules.

Java / Android version

I have tested on Android 34.

Used tools

  • Gradle; version: 8.2
  • R8; version: 8.2.47

Description

@Marcono1234 First of all thanks a lot for introducing consumer proguard rules!

I have added the rules to one of my projects. Everything seemed to work fine, but I ran into one issue.
Here's a sample code that makes use of Gson's TypeToken:

private List<? extends MyInterface> getData() {
    String input = "[{\"value\": \"data\"}]";
    Type type = new TypeToken<List<MyImplementation>>() { }.getType();
    return new Gson().fromJson(input, type);
}

MyImplementation class implements the MyInterface interface, and this is its only usage in code.

Upon minification, R8 removes the MyImplementation class completely, while TypeToken is being left with java.lang.Object as its generic argument:

.class Linfo/osom/typetokenminify/a;
.super Lh/a;
.source "SourceFile"

# annotations
.annotation system Ldalvik/annotation/Signature;
    value = {
        "Lh/a<",
        "Ljava/util/List<",
        "Ljava/lang/Object;",
        ">;>;"
    }
.end annotation

This, in turn, causes a runtime exception:

Caused by: java.lang.ClassCastException
    at c.a.a(SourceFile:1)
    at info.osom.typetokenminify.MainActivity.onCreate(SourceFile:163)

As a workaround, I currently have to explicitly keep the class:

-keep,allowobfuscation,allowoptimization class info.osom.typetokenminify.MyImplementation {
  @com.google.gson.annotations.SerializedName <fields>;
}

I'm wondering if more general rule can be used in such cases.

Expected behavior

Run the application without errors.

Actual behavior

The application crash at runtime.

Reproduction steps

I have uploaded a sample project for demonstration.

@alipov alipov added the bug label Mar 29, 2024
@alipov alipov changed the title Edge case where R8 removes the TypeToken generic type (while consuming the recently updated library's proguard rules) Edge case where R8 removes the TypeToken's generic type (while consuming the recently updated library's proguard rules) Mar 29, 2024
@alipov alipov changed the title Edge case where R8 removes the TypeToken's generic type (while consuming the recently updated library's proguard rules) Edge case where R8 removes the TypeToken's generic type (while consuming the library's recently updated proguard rules) Mar 29, 2024
@Marcono1234 Marcono1234 added the proguard-r8 Issues relating to the use of ProGuard and/or R8, such as problems due to obfuscation label Mar 29, 2024
@Goooler
Copy link
Contributor
Goooler commented Apr 15, 2025

I guess this should be fixed after updating to Gson 2.11 or above.

@Marcono1234
Copy link
Collaborator
Marcono1234 commented Apr 15, 2025

@Goooler, no it looks like this is not fixed in recent Gson versions see #2845.

I think @alipov had reported it for 2.10.1 but manually applied gson.pro because at that time the rules were already part of the main branch but the release 2.11.0 (which includes those rules in the JAR) had not been published yet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug proguard-r8 Issues relating to the use of ProGuard and/or R8, such as problems due to obfuscation
Projects
None yet
Development

No branches or pull requests

3 participants
0