Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The use of assumenosideeffects in ProGuard

I read from http://proguard.sourceforge.net/manual/usage.html about the use of assumenosideeffects. My question refers to the following paragraph:

-assumenosideeffects class_specification Specifies methods that don't have any side effects (other than maybe returning a value). In the optimization step, ProGuard will then remove calls to such methods, if it can determine that the return values aren't used. ProGuard will analyze your program code to find such methods automatically. It will not analyze library code, for which this option can therefore be useful. For example, you could specify the method System.currentTimeMillis(), so that any idle calls to it will be removed. With some care, you can also use the option to remove logging code. Note that ProGuard applies the option to the entire hierarchy of the specified methods. Only applicable when optimizing. In general, making assumptions can be dangerous; you can easily break the processed code. Only use this option if you know what you're doing!

I would need to understand how does ProGuard determine "that the return values aren't used". In particular, if the method calls do not return any value, are they always removed?

like image 633
Monica Marcus Avatar asked Oct 13 '15 17:10

Monica Marcus


People also ask

How do you keep all classes in ProGuard?

-keepclassmembernames. This is the most permissive keep directive; it lets ProGuard do almost all of its work. Unused classes are removed, the remaining classes are renamed, unused members of those classes are removed, but then the remaining members keep their original names.

What is Dontwarn in ProGuard?

If your code works fine without the missing classes, you can suppress the warnings with '-dontwarn' options. ( http://proguard.sourceforge.net/manual/troubleshooting.html#unresolvedclass) Warning: there were 2 unresolved references to program class members. Your input classes appear to be inconsistent.

Does ProGuard remove unused classes?

ProGuard Facts: Helps to remove unused classes, members & fields. It removes unused codes from your libraries as well. (Which means, any method or field that is not referenced anywhere will be removed by ProGuard Shrinker). Helps to reduce the build size by obfuscating the classes, methods, & fields with short names.

What is the use of ProGuard?

ProGuard is a tool to help minify, obfuscate, and optimize your code. It is not only especially useful for reducing the overall size of your Android application as well as removing unused classes and methods that contribute towards the intrinsic 64k method limit of Android applications.


1 Answers

if the method calls do not return any value, are they always removed?

The answer is Yes. An example is in Chromium sources:

private ChromeBrowserInitializer(Context context) {
    mApplication = (ChromeApplication) context.getApplicationContext();
    mHandler = new Handler(Looper.getMainLooper());
    initLeakCanary();
}

@RemovableInRelease
private void initLeakCanary() {
    // Watch that Activity objects are not retained after their onDestroy() has been called.
    // This is a no-op in release builds.
    LeakCanary.install(mApplication);
}

Methods annotated with @RemovableInRelease will be removed from release builds. chromium_code.flags:

# Remove methods annotated with this if their return value is unused.
-assumenosideeffects class ** {
@org.chromium.base.annotations.RemovableInRelease <methods>;
}
like image 77
j2ko Avatar answered Dec 03 '22 15:12

j2ko