blob: e38056afb5dbb59ff8b28d9277f2b742fef1f2c3 [file] [log] [blame]
Rico Winda6e4efc2023-08-03 07:51:44 +02001/*
2 * Copyright (C) 2022 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.build.shrinker;
18
19import com.android.annotations.NonNull;
20import java.io.File;
21import java.io.IOException;
22import java.util.Map;
23import javax.xml.parsers.ParserConfigurationException;
24import org.xml.sax.SAXException;
25
26/**
27 * Interface for unit that analyzes all resources (after resource merging, compilation and code
28 * shrinking has been completed) and figures out which resources are unused, and replaces them with
29 * dummy content inside zip archive file.
30 */
31public interface ResourceShrinker extends AutoCloseable {
32
33 /**
34 * Analyzes resources and detects unreachable ones. It includes the following steps:
35 *
36 * <ul>
37 * <li>Gather resources available in project.
38 * <li>Record resource usages via analyzing compiled code, AndroidManifest etc.
39 * <li>Build reference graph and connect dependent resources.
40 * <li>Detects WebView and/or {@code Resources#getIdentifier} usages and guess which
41 * resources are reachable analyzing string constants available in compiled code.
42 * <li>Processes resources explicitly asked to keep and discard. &lt;tools:keep&gt; and
43 * &lt;tools:discard&gt; attribute.
44 * <li>Based on the root references computes unreachable resources.
45 * </ul>
46 */
47 void analyze() throws IOException, ParserConfigurationException, SAXException;
48
49 /**
50 * Returns count of unused resources. Should be called after {@code ResourceShrinker#analyze}.
51 */
52 int getUnusedResourceCount();
53
54 /**
55 * Replaces entries in {@param source} zip archive that belong to unused resources with dummy
56 * content and produces a new {@param dest} zip archive. Zip archive should contain resources
57 * in 'res/' folder like it is stored in APK.
58 *
59 * <p>For now, doesn't change resource table and applies to file-based resources like layouts,
60 * menus and drawables, not value-based resources like strings and dimensions.
61 *
62 * <p>Should be called after {@code ResourceShrinker#analyze}.
63 */
64 void rewriteResourcesInApkFormat(
65 @NonNull File source,
66 @NonNull File dest,
67 @NonNull LinkedResourcesFormat format
68 ) throws IOException;
69
70 /**
71 * Replaces entries in {@param source} zip archive that belong to unused resources with dummy
72 * content and produces a new {@param dest} zip archive. Zip archive represents App Bundle which
73 * may have multiple modules and each module has its own resource directory: '${module}/res/'.
74 * Package name for each bundle module should be passed as {@param moduleToPackageName}.
75 *
76 * <p>For now, doesn't change resource table and applies to file-based resources like layouts,
77 * menus and drawables, not value-based resources like strings and dimensions.
78 *
79 * <p>Should be called after {@code ResourceShrinker#analyze}.
80 */
81 void rewriteResourcesInBundleFormat(
82 @NonNull File source,
83 @NonNull File dest,
84 @NonNull Map<String, String> moduleToPackageName
85 ) throws IOException;
86}