Status Update
Comments
ra...@google.com <ra...@google.com>
ch...@google.com <ch...@google.com> #2
Thanks for the feedback. Can you please share some more details on what kind of issues you are experiencing?
ch...@gmail.com <ch...@gmail.com> #3
The root cause of this issue is that all overridden methods are being converted to final after the code is obfuscated by R8.
We need proguard flag to prevent this conversion in code obfuscation.
Original Code :
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
After obfuscation
public final void onCreate(Bundle var1) {
super.onCreate(var1);
}
ch...@google.com <ch...@google.com> #5
Can you please shed some more light on why this is a problem? Do you run any dex-to-dex transformations after running R8 or do you perform dynamic code loading?
ch...@gmail.com <ch...@gmail.com> #6
I am build the application in Android Studio. This issue is not observed AGP : 7.1.0 and gradle : 7.4 .
ch...@gmail.com <ch...@gmail.com> #7
sg...@google.com <sg...@google.com> #8
Looking at the build.gradle
file it looks like you are missing the default rules provided by AGP. You release configuration is:
buildTypes {
release {
minifyEnabled true
proguardFiles 'proguard-rules.pro'
}
}
which should include getDefaultProguardFile('proguard-android-optimize.txt')
like this:
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
Please check if that addresses the issue. It might not if you are also dynamic code loading as mentioned in
You can see the combined rules in app/build/outputs/mapping/release/configuration.txt
to validate. These rules should also contain any rules generated by aapt2.
ch...@gmail.com <ch...@gmail.com> #9
ch...@google.com <ch...@google.com> #10
Again, can you please shed some more light on why this is a problem? Making MainActivity#onCreate
final should be a perfectly valid performance optimization.
Does your app crash at runtime? If so, do you have a subclass of MainActivity
that overrides MainActivity#onCreate
? Can you share the logcat of the error?
sg...@google.com <sg...@google.com> #11
The class file shown in aar
. Is the code you are building an Android Library and not an android app? If the library is used in a way where onCreate
is overridden, then I would expect the compilation to fail. If that is not the case then it still looks like you are not providing the whole program to the app build but loading additional classes at runtime.
ch...@gmail.com <ch...@gmail.com> #12
However, we are encountering a compilation error while building the application.
TestSplashScreen.java:12: error: onCreate(Bundle) in TestSplashScreen cannot override onCreate(Bundle) in MainActivity
INFO [exec-shell]
[exec-shell] public void onCreate(Bundle savedInstanceState) {
[exec-shell] ^
please provide solution to prevent override methods from being converted into final.
Multiple functions are converted to final in my library.
bh...@gmail.com <bh...@gmail.com> #13
Can we explicitly stop the replacements false and fallback to previous behavior using any gradle flags or settings.
ag...@gmail.com <ag...@gmail.com> #14
ch...@google.com <ch...@google.com> #15
Thanks for the extra information. It makes sense that this can be an issue when building a library. This is ultimately due to missing keep rules. In order to guarantee that classes/fields/methods will not be optimized they must be kept. When building a library it is therefore necessary to keep the public API surface of the library.
As an example, consider the R8Command
@KeepForApi
annotation. This annotation is backed by a generic -keep
rule that keeps all public and protected members of the class.
You could introduce an annotation @KeepForApi
, annotate the public API surface of your library, and add the following rule.
-keep @com.example.KeepForApi class * {
public *;
protected *;
}
I am assuming that you must already have some keep rules to protect the API surface of your library, or do you only rely on the AAPT generated keep rules?
It should also be possible to avoid this issue by merely adding the following rule. Unlike the proposal above this may also prohibit optimization of the non-public API of your library,
-if class *
-keep,allowobfuscation,allowshrinking public class * {
public *;
protected *;
}
An alternative naive solution is to add -dontoptimize
.
Description
AGP : 8.3.0
Gradle : 8.4
Java : 17
All overridden methods are being converted to final after R8 obfuscates the code, which can lead to unexpected behavior in our application.
We cannot use the -keep option because multiple functions are being converted to final.
Is there a ProGuard flag to prevent this conversion in the project?