fix(android): keep plugin annotation types from R8 full mode

Merged
BO
17d ago
+7-0

Fixes #8589.
Fixes #8587.

R8 full mode treats annotation types that are only reached through Class.getAnnotation() / Method.getAnnotation() as unused, then folds the cached annotation in PluginHandle to null. -keepattributes *Annotation* does not stop that. The consumer rules kept the annotated plugin classes, but not the annotation interfaces themselves.

#8400 (8.5.2) guards Bridge.getPermissionStates() so checkPermissions() no longer crashes. On current main the annotation is still null, so Plugin.requestPermissions() takes the legacy @NativePlugin branch and NPEs on legacyAnnotation.permissions() (Plugin.java:883, inlined into requestPermissions).

This adds -keep @interface rules for the annotation types Capacitor reads reflectively (com.getcapacitor.annotation.**, PluginMethod, NativePlugin). NativePlugin was removed on next, so that one line should be dropped when this is merged forward to Capacitor 9.

Verification

Minimal release app, minifyEnabled true, proguard-android-optimize.txt, R8 full mode, one plugin:

@CapacitorPlugin(name = "Repro", permissions = { @Permission(alias = "camera", strings = { Manifest.permission.CAMERA }) })

The app calls inherited checkPermissions() then requestPermissions(). Same app, three Capacitor builds:

BuildcheckPermissionsrequestPermissions
8.5.1NPE in getPermissionStates, process diesnot reached
main (includes #8400)logs missing @CapacitorPlugin annotation for plugin Repro, resolves undefinedNPE, process dies
this branchresolves {"camera":"prompt"}system permission dialog, process stays alive

dexdump -d of Plugin.getPermissionStates() on main allocates the map, logs the missing-annotation warning, and returns. It never reads permissions(). On this branch the same method loads the kept field and calls the interface:

iget-object v2, v2, Lcom/getcapacitor/f0;.f:Lcom/getcapacitor/annotation/CapacitorPlugin;
if-eqz v2, ...
invoke-interface {v2}, Lcom/getcapacitor/annotation/CapacitorPlugin;.permissions:()[Lcom/getcapacitor/annotation/Permission;

Plugin.requestPermissions() on main is folded to a null check and then invoke-virtual on a null reference (the legacy annotation). On this branch it loads both kept fields:

iget-object v1, v0, Lcom/getcapacitor/f0;.f:Lcom/getcapacitor/annotation/CapacitorPlugin;
if-nez v1, ...          # @CapacitorPlugin path
iget-object v0, v0, Lcom/getcapacitor/f0;.e:Lcom/getcapacitor/NativePlugin;
invoke-interface {v0}, Lcom/getcapacitor/NativePlugin;.permissions:()[Ljava/lang/String;

usage.txt on main still removes PluginHandle.pluginAnnotation and legacyPluginAnnotation, and the annotation types are renamed (CapacitorPlugin -> v0.b). With this change those fields are no longer removed, and seeds.txt keeps CapacitorPlugin, Permission, PermissionCallback, ActivityCallback, PluginMethod, and NativePlugin under their original names.