Christoffer Adamsen | 5616ebe | 2025-03-13 13:18:04 +0100 | [diff] [blame] | 1 | // 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 | |
| 5 | import com.android.tools.r8.dex.ApplicationReader; |
| 6 | import com.android.tools.r8.optimize.compose.ComposeReferences; |
| 7 | import com.android.tools.r8.utils.AndroidApp; |
| 8 | import com.android.tools.r8.utils.InternalOptions; |
| 9 | import com.android.tools.r8.utils.timing.Timing; |
| 10 | import com.google.common.collect.Streams; |
| 11 | import java.io.IOException; |
| 12 | import java.nio.file.Paths; |
| 13 | |
| 14 | public 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 | } |