1

I have a directory within my assets folder with json for a database.

assets/database/database_1/locations.json

When using minifyEnabled true, it's removing those assets as it doesn't think they're used.

How do I tell ProGuard that they're used?

ouflak
  • 2,458
  • 10
  • 44
  • 49
advice
  • 5,778
  • 10
  • 33
  • 60

3 Answers3

6

It turns out minifyEnabled true does not remove any assets, it's only for code optimization and obfuscates the remaining classes.

The issue was I'm using Gson with my database models without @SerializedName and with the obfuscation it wouldn't be able to bind the values.

dataclass MyModel( val myName : String )

would turn into: (or something more obfuscated)

dataclass XYZ( val 123 : String )

And then Gson could not find the value "myName" to put the JSON.

To solve this, you have two solutions.

  • Either add @SerializedName to each variable.
  • Or if you don't care for obfuscation, such as with open source apps, you can disable that optimization by adding useProguard false.
Pexers
  • 953
  • 1
  • 7
  • 20
advice
  • 5,778
  • 10
  • 33
  • 60
  • 1
    Should be `@SerializedName` rather than `@SerializableName`. But thanks for pointing me in the right direction! – jedwidz Jan 01 '22 at 10:53
0

It is not ProGuard who is doing this kind of optimization. See https://developer.android.com/studio/build/shrink-code.html#keep-resources. That being said, assets should not be affected, just resources.

Xavier Rubio Jansana
  • 6,388
  • 1
  • 27
  • 50
  • I was testing this, and using `minifyEnabled true` makes the app crash due to a value being null. And if I add the flag `useProguard false`, the app works as expected. – advice Jan 15 '18 at 07:43
  • 1
    `minifyEnabled true` and `useProguard false` uses internal experimental minification, instead of ProGuard. This disables optimization and obfuscation. See also this answer https://stackoverflow.com/questions/37007485/whats-the-difference-between-minifyenabled-and-useproguard-in-the-android-p#37007689 Are you sure the asset is gone? Have you used APK Analyzer to check this? Also, does this happen on release builds, debug ones or both? – Xavier Rubio Jansana Jan 15 '18 at 10:56
  • I realized what was the problem. It was null not because it didn't exist, but because I'm using Gson, and it couldn't find it by it's name. – advice Jan 15 '18 at 23:03
0

minifyEnabled=true removes all my gpx files stored in assets folder. The same project rebuild with minifyEnabled=false keeps all the gpx-files in the assets f

ZoemZoem
  • 1
  • 1
  • Please provide additional details in your answer. As it's currently written, it's hard to understand your solution. – Community Aug 29 '21 at 11:24
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). – Vatsal Dholakiya Aug 31 '21 at 13:45