Fixing ProGuard Warning: “Can’t Find Referenced Method” for `clone` and `finalize`
ProGuard is a powerful tool for shrinking, obfuscating, and optimizing your Android applications. During the process, it might throw warnings like “Can’t find referenced method” for the methods `clone` and `finalize` from the `java.lang.Object` class. This article explains the reasons for this warning and provides solutions to fix it.
Understanding the Warning
The warning “Can’t find referenced method” for `clone` and `finalize` is a result of ProGuard’s optimization process. It happens when ProGuard identifies that these methods are not used in your application and decides to remove them to reduce the overall size of the final APK. However, this removal can lead to unexpected behavior in certain scenarios.
Causes of the Warning
This warning arises primarily due to two main causes:
1. Unused Methods
If your code does not explicitly call `clone` or `finalize`, ProGuard might deem them unnecessary and remove them from the final output.
2. Method Overriding
If you’ve overridden either `clone` or `finalize` in your own classes, ProGuard needs to know about them. If you have overridden these methods, ProGuard may not be aware of them, and thus, might remove them.
Fixing the Warning
Here are the steps to fix the warning:
1. Using ProGuard Rules
You can tell ProGuard to keep these methods by adding the following rules to your ProGuard configuration file (proguard-rules.pro
):
“`
-keep class java.lang.Object {
public clone() throws java.lang.CloneNotSupportedException;
public finalize() throws java.lang.Throwable;
}
“`
2. Enabling Explicit Calling
If you have an overridden `clone` method and you want to use it, call it explicitly in your code. This will ensure that ProGuard recognizes it as an active method. For example:
“`java
class MyClass extends Object {
@Override
public Object clone() {
// clone implementation
return super.clone();
}
public static void main(String[] args) {
MyClass obj = new MyClass();
try {
MyClass copy = (MyClass) obj.clone();
// use copy
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
}
}
“`
3. Using `-dontwarn`
If you are confident that the removal of `clone` or `finalize` will not affect your application, you can temporarily disable the warning using `-dontwarn` rule:
“`
-dontwarn java.lang.Object.**
“`
However, this is not a recommended solution as it can potentially mask real issues.
Comparison Table
Method | ProGuard Warning | Solution |
---|---|---|
`clone` | “Can’t find referenced method clone” | – Add `-keep` rule or call it explicitly |
`finalize` | “Can’t find referenced method finalize” | – Add `-keep` rule or override and call it explicitly |
Conclusion
By understanding the causes and fixing the warning for `clone` and `finalize`, you can ensure that your Android applications are properly optimized by ProGuard. This allows you to deploy smaller, more efficient APKs while avoiding unexpected behaviors and potential runtime errors. Always be aware of the implications of ProGuard optimizations and carefully assess the need for these methods in your specific project.