Hot questions for Using Butter Knife in android proguard

Top 10 Java Open Source / Butter Knife / android proguard

Question:

I am running ButterKnife version 8.5.1 in my Android app. If I try to make ProGuard settings for ButterKnife I still got crashes in my release version. There is an issue on the official ButterKnife Github page which tells you to make the following settings:

-keep class butterknife.** { *; }
-dontwarn butterknife.internal.**
-keep class **$$ViewBinder { *; }

-keepclasseswithmembernames class * {
     @butterknife.* <fields>;
}

-keepclasseswithmembernames class * {
    @butterknife.* <methods>;
}

Alternatively it is written to use these settings:

# Butterknife
-dontwarn butterknife.internal.**
-keep class **$$ViewInjector { *; }
-keepnames class * { @butterknife.InjectView *;}

None of these work with my configuration. I still got crashes when accessing Views bound by ButterKnife. So are there any new settings which you need to make for ButterKnife 8?


Answer:

In ButterKnife 8 the auto generated Subclasses are called different. You can handle ProGuard settings for Version 7 and 8 this way:

###---------------Begin: proguard configuration for ButterKnife  ----------
# For Butterknife:
-keep class butterknife.** { *; }
-dontwarn butterknife.internal.**

# Version 7
-keep class **$$ViewBinder { *; }
# Version 8
-keep class **_ViewBinding { *; }

-keepclasseswithmembernames class * { @butterknife.* <fields>; }
-keepclasseswithmembernames class * { @butterknife.* <methods>; }
###---------------End: proguard configuration for ButterKnife  ----------

Although I am using @OnClick methods I even do not need the last line. These settings work perfect for me. Try it out!

Question:

Since Butterknife uses reflection to generate classes associating with every activity or fragment used. due to which Proguard ignores those classes and not obfuscate it in its rules.

found one work around to this is to use Butterknife generated classes by Replacing ButterKnife.bind(this) with new Whatever_ViewBinder(this) and use them in my code. which does work and Progurad obfuscate my classes. but only problem here is i have to copy them from Debug folder to my package from getting changed/deleted. and update it every time i add/remove views. source

another question i found which says it better to use Data binding instead Butterknife vs Data Binding. but its too late now for me to change the whole code

Please share across if you'll have found any work around to this. I am using Butterknife v8.6.0. if this is of any help


Answer:

You even linked to your answers yourself with #746.

[...] until I remove all imports of Butter Knife from the class, Proguard doesn't work on it.

This is because the Proguard rules are supplied with Butter Knife itself until version 9.0.

The ProGuard rules will be separated in 9.0 when using the generated code API becomes stable.

So basically it's not possible to do easily what you're intending to do. The best would be to fork Butter Knife and remove the consumerProguardFiles definition from build.gradle. Then wait for version 9.0 to be released and then use the original again.