Status Update
Comments
il...@google.com <il...@google.com> #2
Branch: androidx-main
commit 57ca221882695bd6a52549f4d9ea3b812e6fe87c
Author: Simon Schiller <simonschiller@users.noreply.github.com>
Date: Mon Mar 22 16:09:30 2021
[GH] [FragmentStrictMode] Detect <fragment> tag usage
## Proposed Changes
- Detect `<fragment>` tag usage inside XML layouts
## Testing
Test: See `FragmentStrictModeTest#detectFragmentTagUsage`
## Issues Fixed
Fixes: 153738235
This is an imported pull request from
Resolves #141
Github-Pr-Head-Sha: 4ea052596e4341b9f11bcf335e2bc38045a91f19
GitOrigin-RevId: 62e7487aa4874eef6bb556490e193717cf937251
Change-Id: Iae48578e85e4e4897f806d7ade2e2a660adf9479
M fragment/fragment/api/public_plus_experimental_current.txt
M fragment/fragment/api/restricted_current.txt
M fragment/fragment/src/androidTest/java/androidx/fragment/app/strictmode/FragmentStrictModeTest.kt
M fragment/fragment/src/main/java/androidx/fragment/app/FragmentLayoutInflaterFactory.java
M fragment/fragment/src/main/java/androidx/fragment/app/strictmode/FragmentStrictMode.java
A fragment/fragment/src/main/java/androidx/fragment/app/strictmode/FragmentTagUsageViolation.java
sg...@google.com <sg...@google.com> #3
I have not tried to create a project, but the way -if
rules are evaluated is that if the condition public class ** extends androidx.fragment.app.Fragment
holds (that is at least one class extends androidx.fragment.app.Fragment
then the subsequent -keep
rule is activated, so if at least one class extends androidx.fragment.app.Fragment
this keep rule is activated:
-keep public class ** extends androidx.fragment.app.Fragment {
public <init>();
}
Causing all classes in the app extending androidx.fragment.app.Fragment
to be kept. The -if
rule captures <n>
are used to parameterize the subsequent keep rule with what was matched by the condition. Each wildcard in the condition will be given a capture number (starting from 1) and they can then be used in the subsequent rule. So if the keep rule is:
-keep public class <1> {
public <init>();
}
Then if e.g. class com.example.app.MyFragment
extends androidx.fragment.app.Fragment
, then the following rule will be activated:
-keep public class com.example.app.MyFragment {
public <init>();
}
Which I assume is the expected behaviour.
It might be for most apps all classes which extend androidx.fragment.app.Fragment
ends up being part of the program, so the end result ends up being the same, but I still think that the rule should be precise and only keep what the condition matches.
ap...@google.com <ap...@google.com> #4
Branch: androidx-master-dev
commit faa9401ccc7c645f56f12b9928c916a04dbb4c28
Author: Ian Lake <ilake@google.com>
Date: Tue Feb 25 09:25:48 2020
Ensure conditional keep only keeps what is necessary
Use the <1> placeholder to only apply the -keep
rule to the exact classes that the -if applies to
rather than having the keeping of one Fragment class
cause all Fragments to be kept.
Test: manual testing in project
Fixes: 149665169
Change-Id: I39ffb80e69adfa8fab3a3ed732822a4d937f403b
M fragment/fragment/proguard-rules.pro
il...@google.com <il...@google.com> #5
We've fixed the ProGuard rules and it'll be available in the upcoming Fragment 1.3.0-alpha01 (and if we do a Fragment 1.2.3, it'll also be a part of that).
an...@google.com <an...@google.com> #6
tb...@gmail.com <tb...@gmail.com> #7
Maybe this is intentional, but I updated my project to androidx fragments 1.2.3 (from 1.2.2), and now necessary fragments ("MyFragment" below) that used to be kept are now being stripped by R8. The fragment in question is referenced in a layout that is set using setContentView() on an activity.
<androidx.fragment.app.FragmentContainerView
android:id="@+id/fragment_id"
android:name="com.mycompany.MyFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
AGP 3.6.1. Not sure how I'm supposed to keep it now. It has normal default constructor and has androidx.fragment.Fragment in the superclass hierarchy.
il...@google.com <il...@google.com> #8
Re #7 - that's unrelated and expected when using AGP 3.6.1 as per the
The only reason that it "worked" in Fragment 1.2.2 was because all Fragments were being kept if even one Fragment was being kept (which is what this bug fixed).
dy...@gmail.com <dy...@gmail.com> #9
What's the reason for having Proguard rules included with the Fragment library in the first place?
I'm asking because right now, this rule is actually preventing me from obfuscating, shrinking and optimizing my fragments for reasons I don't understand. One reason I could think of is reflection. Instead, is it possible to completely remove them and suggest developers to add these rules themselves?
il...@google.com <il...@google.com> #10
Re #9 - please star
Description
Component used: Fragment Version used: Master as of Feb 17 2020
Just noticed the Proguard rule which was added in ag/1216798 (and updated in ag/1227696), which looks like this:
I think the
**
in the conditional-keep
should have been<1>
to refer back to the class implementingandroidx.fragment.app.Fragment
(and then theextends
part does not matter then). Otherwise if one class extendsandroidx.fragment.app.Fragment
then the default constructor for all classes extendingandroidx.fragment.app.Fragment
will be kept.Like this: