It should be possible to just rely on persisting TaskIds, and deriving class names from them. Doing so will allow BackgroundTasks to be obfuscated & optimized more by proguard. It will also make it safe to rename these classes, rather than hoping no one does so.
Side Note: There's an androidx library for background tasks now called "WorkManager". However, I peeked at the code for it and it is tragically large. We should avoid it.
Steps here are:
1. Remove mBackgroundTaskClass from TaskInfo.java.
2. Stop persisting it in these places:
https://cs.chromium.org/search/?q=getName+file:%5Esrc/components/background_task_scheduler/android/java/src/org/chromium/components/background_task_scheduler/+package:%5Echromium$&type=cs
3. Introduce a helper in TaskIds to get a class by id:
class TaskIds {
public static final int GCM_BACKGROUND_TASK_JOB_ID = 1;
public static final int NOTIFICATION_SERVICE_JOB_ID = 21;
...
@IdentifierNameString
private static final String GCM_BACKGROUND_TASK_CLASS_NAME =
"org.chromium.chrome.browser.services.gcm.GCMBackgroundTask";
...
BackgroundTask createById(int id) {
switch (id):
case GCM_BACKGROUND_TASK_JOB_ID:
return (BackgroundTask) Class.forName(GCM_BACKGROUND_TASK_CLASS_NAME).newInstance();
...
}
}
Notes:
* It's a bit odd to have the class names here, but the class already has an ID for each client, so seems no worse to me to also encode the class name.
* R8 will convert the reflection into direct calls, eliminating reflection overhead.
To enable obfuscation:
1. Add a @IdentifierNameString annotation to base
2. Add proguard rule:
-identifiernamestring class * {
@com.google.android.apps.common.proguard.IdentifierNameString *;
}
3. Revert 3fa2e21d7b4feff2d345fd5a2786c1a7ab84e8ae
Comment 1 by fgorski@google.com
, Dec 20