blob: 04b80178f26237fea75fe33689fa0c7f681648af [file] [log] [blame]
Christoffer Adamsen5616ebe2025-03-13 13:18:04 +01001// Copyright (c) 2025, the R8 project authors. Please see the AUTHORS file
2// for details. All rights reserved. Use of this source code is governed by a
3// BSD-style license that can be found in the LICENSE file.
4
5import com.android.tools.r8.dex.ApplicationReader;
6import com.android.tools.r8.optimize.compose.ComposeReferences;
7import com.android.tools.r8.utils.AndroidApp;
8import com.android.tools.r8.utils.InternalOptions;
9import com.android.tools.r8.utils.timing.Timing;
10import com.google.common.collect.Streams;
11import java.io.IOException;
12import java.nio.file.Paths;
13
14public class ComposableSizeInApk {
15
16 public static void main(String[] args) throws IOException {
17 AndroidApp androidApp = AndroidApp.builder().addProgramFile(Paths.get(args[0])).build();
18 InternalOptions options = new InternalOptions();
19 ComposeReferences composeReferences = new ComposeReferences(options.dexItemFactory());
20 int size =
21 new ApplicationReader(androidApp, options, Timing.empty())
22 .read().classes().stream()
23 .flatMap(c -> Streams.stream(c.programMethods()))
24 .filter(m -> m.getAnnotations().hasAnnotation(composeReferences.composableType))
25 .filter(m -> m.getDefinition().hasCode())
26 .mapToInt(m -> m.getDefinition().getCode().asDexCode().codeSizeInBytes())
27 .sum();
28 if (size == 0) {
29 String name = "androidx.compose.runtime.Composable";
30 System.err.println("Warning: To collect the size of @Composable functions, ");
31 System.err.println("all @Composable annotations must be retained.");
32 System.err.println("");
33 System.err.println("This can be achieved using the following keep annotation:");
34 System.err.println("");
35 System.err.println("@KeepEdge(");
36 System.err.println("consequences =");
37 System.err.println(" @KeepTarget(");
38 System.err.println(" kind = KeepItemKind.ONLY_METHODS,");
39 System.err.println(" methodAnnotatedByClassName = \"" + name + "\",");
40 System.err.println(" constraints = {},");
41 System.err.println(" constrainAnnotations =");
42 System.err.println(" @AnnotationPattern(");
43 System.err.println(" name = \"" + name + "\",");
44 System.err.println(" retention = RetentionPolicy.CLASS)))");
45 }
46 System.out.println(size);
47 }
48}