ZacSweers / MoshiX

Extensions for Moshi including IR plugins, moshi-sealed, and more.
Apache License 2.0
491 stars 37 forks source link

Malformed generated Proguard rules for classes with `data` in package #624

Closed mhelder closed 1 week ago

mhelder commented 1 week ago

This is a follow-up on https://github.com/ZacSweers/MoshiX/issues/556.

I've updated the previously shared sample to use 0.27.1, but now seem to be running into https://github.com/ZacSweers/MoshiX/issues/415:

> Task :app:minifyReleaseWithR8 FAILED
ERROR: <snip>/moshix-sealed-missing-proguard-rules/app/build/intermediates/generated_proguard_file/release/proguard.txt:10:1-27: R8: Expecting '-keep' option after '-if' option.

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:minifyReleaseWithR8'.
> A failure occurred while executing com.android.build.gradle.internal.tasks.R8Task$R8Runnable
   > Compilation failed to complete, position: Text range from: 'offset: 365, line: 10, column: 1', to: 'offset: 391, line: 10, column: 27', origin: <snip>/moshix-sealed-missing-proguard-rules/app/build/intermediates/generated_proguard_file/release/proguard.txt

Rules generated under resources/META-INF/proguard:

moshi-com.example.app.data.model.Message.Success.pro:

-if class com.example.app.data.model.Message$Success
-keepnames class com.example.app.data.model.Message$Success
-if class com.example.app.data.model.Message$Success
-keep class com.example.app.data.model.Message_SuccessJsonAdapter {
    public <init>(com.squareup.moshi.Moshi);
}

moshi-com.example.app.data.model.Message.pro:

# Conditionally keep this adapter for every possible nested subtype that uses it.
-if class com.example.app.`data`.model.Message.Success
-keep class com.example.app.data.model.MessageJsonAdapter {
    public <init>(com.squareup.moshi.Moshi);
}
-if class com.example.app.`data`.model.Message.Unknown
-keep class com.example.app.data.model.MessageJsonAdapter {
    public <init>(com.squareup.moshi.Moshi);
}

Rules for the latter were previously missing (see https://github.com/ZacSweers/MoshiX/issues/556), which was fixed in https://github.com/ZacSweers/MoshiX/pull/603. (Thanks!).

However, note:

Comparing the contents of the two files, it seems like the rules for the nested class Success itself are correctly generated , but not the sealed class in which it is nested.

In case it's relevant: the package declaration of the generated JsonAdapters also contains `data`:

package com.example.app.`data`.model`
ZacSweers commented 1 week ago

Thanks, I'll take a look. PR is welcome as well

mhelder commented 1 week ago

I opened https://github.com/ZacSweers/MoshiX/pull/626!

It looks like it wasn't just generating malformed rules for packages with reserved keywords, but also generating incorrect rules for nested classes.