Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions Documentation/guides/D8andR8.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,21 @@ union of keys files and writes deterministic type-root rules to
Each retained key emits both alternatives because the key format does not
encode whether the Java type is a class or interface. The root generator
never emits member rules or global R8 options.

On CoreCLR, the root generator also writes these rules for each key:

```text
-if interface example.Outer$Inner
-keep,allowshrinking,allowobfuscation class * implements example.Outer$Inner
```

Managed interface proxy selection calls `Class.getInterfaces ()`, which R8
cannot see, so implementers of bound interfaces keep their interface edges.
R8 also matches `implements` against superclasses; `-if interface` limits the
rule to keys that are interfaces. The rule matches indirect implementers too.
It only has an effect on interfaces in the app's Java code (AndroidX, Kotlin,
and so on), because R8 never removes library interfaces such as `android.*`.
Proxy selection compares interface names, so implementers can be renamed.
CoreCLR uses a separate `GenerateTypeMapMemberProguardConfiguration` task to
write `proguard/proguard_typemap_members.cfg` from the same retained keys:

Expand All @@ -71,8 +86,7 @@ callable wrappers. Fields generated for a non-public `[ExportField]` are not
kept. R8's member matching includes inherited accessible methods and
superclass fields. The separate
`proguard_typemap_coreclr.cfg` configuration preserves runtime bootstrap
classes and their members, reflection-visible interface implementation
relationships used for managed proxy selection, plus View constructors used by
classes and their members, plus View constructors used by
layout inflation. View setters are not kept globally; `proguard-android-optimize.txt`
keeps `void set*(***)` on public View subclasses for property animations.
Dynamic JNI or reflection access to types or members not represented
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public class GenerateTypeMapProguardConfiguration : AndroidTask
[Required]
public string OutputFile { get; set; } = "";

public bool KeepInterfaceImplementations { get; set; }

public override bool RunTask ()
{
var classes = new SortedSet<string> (StringComparer.Ordinal);
Expand Down Expand Up @@ -81,6 +83,12 @@ protected virtual void WriteClassRule (TextWriter writer, string name)
{
writer.WriteLine ($"-keep class {name}");
writer.WriteLine ($"-keep interface {name}");
if (KeepInterfaceImplementations) {
// Managed interface proxy selection observes Class.getInterfaces(), which R8 cannot see.
// R8 "implements" also matches subclasses, so limit the rule to interface keys.
writer.WriteLine ($"-if interface {name}");
writer.WriteLine ($"-keep,allowshrinking,allowobfuscation class * implements {name}");
}
}

internal static bool IsClassName (string name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ string EmitMap (string name, params string [] keys)
string [] arguments = obsoleteImplementation == null ? [] : [$"-p:AndroidTypeMapImplementation={obsoleteImplementation}"];
Build (project, arguments);
var rules = Path.Combine (directory, "obj", "proguard", "proguard_project_references.cfg");
Assert.AreEqual (TypeRules ("test.Alias", "test.Live", "test.Second"), File.ReadAllText (rules));
Assert.AreEqual (CoreClrTypeRules ("test.Alias", "test.Live", "test.Second"), File.ReadAllText (rules));
File.Delete (second);
Assert.Contains ("XA4327", Build (project, false, arguments));
}
Expand Down Expand Up @@ -691,6 +691,11 @@ public void InactivePathsNeedNoTypemapInputsOrModernTasks (
static string TypeRules (params string [] names) =>
string.Concat (names.Select (name => $"-keep class {name}\n-keep interface {name}\n"));

static string CoreClrTypeRules (params string [] names) =>
string.Concat (names.Select (name =>
$"-keep class {name}\n-keep interface {name}\n" +
$"-if interface {name}\n-keep,allowshrinking,allowobfuscation class * implements {name}\n"));

string CreateProject (string runtime, params string [] sourceItems)
{
var targets = Path.Combine (RepositoryDirectory (), "src", "Xamarin.Android.Build.Tasks", "Microsoft.Android.Sdk", "targets", "Microsoft.Android.Sdk.TypeMap.Proguard.targets");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,8 @@
Outputs="$(_ProguardProjectConfiguration)">
<GenerateTypeMapProguardConfiguration
TypeMapKeyFiles="@(_AndroidTypeMapKeyFile)"
OutputFile="$(_ProguardProjectConfiguration)" />
OutputFile="$(_ProguardProjectConfiguration)"
KeepInterfaceImplementations="$(_AndroidUseScopedTypeMapMembers)" />
<ItemGroup>
<FileWrites Include="$(_ProguardProjectConfiguration)" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
-keep class net.dot.android.ApplicationRegistration { *; }
-keep class net.dot.android.crypto.** { *; <init>(...); }

# Managed interface proxy selection observes Class.getInterfaces().
-keep,allowshrinking class * implements **
# Managed interface proxy selection observes Class.getInterfaces(). The rules that
# keep implementers of bound interfaces are in proguard_project_references.cfg.

# Framework-driven view inflation.
-keepclassmembers class * extends android.view.View {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ public void RetainedTypeMapRulesDoNotRootAllAcwsOrObfuscatePrivateMembers (bool
Assert.DoesNotContain ("-keepclassmembers class * {", common);
Assert.Contains ("-keep class mono.android.Runtime { *; }", common);
Assert.Contains ("-keep interface mono.android.IGCUserPeer { *; }", common);
Assert.Contains ("-keep,allowshrinking class * implements **", common);
Assert.DoesNotContain ("implements", common);
} else {
Assert.Contains ("-keepclassmembers class * {", common);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,11 @@ public void RetainedTypeMapRulesDriveR8 (AndroidRuntime runtime)
CollectionAssert.DoesNotContain (keys, dead);
var configuration = Path.Combine (intermediate, "proguard", "proguard_project_references.cfg");
CollectionAssert.AreEqual (
keys.OrderBy (key => key, StringComparer.Ordinal).SelectMany (key => new [] {
"-keep class " + key.Replace ('/', '.'),
"-keep interface " + key.Replace ('/', '.'),
keys.OrderBy (key => key, StringComparer.Ordinal).SelectMany (key => {
var name = key.Replace ('/', '.');
string [] rules = ["-keep class " + name, "-keep interface " + name];
return runtime != AndroidRuntime.CoreCLR ? rules :
[.. rules, "-if interface " + name, "-keep,allowshrinking,allowobfuscation class * implements " + name];
}).ToArray (),
File.ReadAllLines (configuration));
var common = File.ReadAllText (Path.Combine (intermediate, "proguard", "proguard_xamarin.cfg"));
Expand Down Expand Up @@ -4274,9 +4276,11 @@ void AssertPostTrimR8InputsExcludeDeadFrameworkImplementor (string dexFile, stri
Assert.IsNotEmpty (rules, "Retained typemap keys should provide type roots.");
Assert.IsTrue (rules.All (line =>
(line.StartsWith ("-keep class ", StringComparison.Ordinal) ||
line.StartsWith ("-keep interface ", StringComparison.Ordinal)) &&
line.StartsWith ("-keep interface ", StringComparison.Ordinal) ||
line.StartsWith ("-if interface ", StringComparison.Ordinal) ||
line.StartsWith ("-keep,allowshrinking,allowobfuscation class * implements ", StringComparison.Ordinal)) &&
!line.Contains ('{')),
"Typemap ProGuard rules must contain only class and interface roots, not member rules.");
"Typemap ProGuard rules must contain only class, interface, and interface-implementation roots, not member rules.");
Assert.IsFalse (rules.Any (line => line.Contains (deadJavaDotName, StringComparison.Ordinal)),
"Retained typemap type roots should exclude trimmed framework implementors.");
var common = File.ReadAllText (Path.Combine (proguardDirectory, "proguard_xamarin.cfg"));
Expand Down