| Colin Cross | 8faf8fc | 2019-01-16 15:15:52 -0800 | [diff] [blame] | 1 | // Copyright 2019 Google Inc. All rights reserved. | 
|  | 2 | // | 
|  | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 4 | // you may not use this file except in compliance with the License. | 
|  | 5 | // You may obtain a copy of the License at | 
|  | 6 | // | 
|  | 7 | //     http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 8 | // | 
|  | 9 | // Unless required by applicable law or agreed to in writing, software | 
|  | 10 | // distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 12 | // See the License for the specific language governing permissions and | 
|  | 13 | // limitations under the License. | 
|  | 14 |  | 
|  | 15 | package java | 
|  | 16 |  | 
|  | 17 | import ( | 
| Colin Cross | 8faf8fc | 2019-01-16 15:15:52 -0800 | [diff] [blame] | 18 | "github.com/google/blueprint" | 
|  | 19 |  | 
|  | 20 | "android/soong/android" | 
|  | 21 | ) | 
|  | 22 |  | 
| Cole Faust | a5f64f0 | 2023-02-14 17:50:31 -0800 | [diff] [blame] | 23 | var ( | 
|  | 24 | hiddenAPIGenerateCSVRule = pctx.AndroidStaticRule("hiddenAPIGenerateCSV", blueprint.RuleParams{ | 
|  | 25 | Command:     "${config.Class2NonSdkList} --stub-api-flags ${stubAPIFlags} $in $outFlag $out", | 
|  | 26 | CommandDeps: []string{"${config.Class2NonSdkList}"}, | 
|  | 27 | }, "outFlag", "stubAPIFlags") | 
|  | 28 |  | 
|  | 29 | hiddenAPIGenerateIndexRule = pctx.AndroidStaticRule("hiddenAPIGenerateIndex", blueprint.RuleParams{ | 
|  | 30 | Command:     "${config.MergeCsvCommand} --zip_input --key_field signature --output=$out $in", | 
|  | 31 | CommandDeps: []string{"${config.MergeCsvCommand}"}, | 
|  | 32 | }) | 
|  | 33 | ) | 
| Colin Cross | 8faf8fc | 2019-01-16 15:15:52 -0800 | [diff] [blame] | 34 |  | 
| Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 35 | type hiddenAPI struct { | 
| Paul Duffin | f75e527 | 2021-02-09 14:34:25 +0000 | [diff] [blame] | 36 | // True if the module containing this structure contributes to the hiddenapi information or has | 
|  | 37 | // that information encoded within it. | 
| Paul Duffin | 4103e92 | 2021-02-01 19:01:34 +0000 | [diff] [blame] | 38 | active bool | 
|  | 39 |  | 
| Martin Stjernholm | 8be1e6d | 2021-09-15 03:34:04 +0100 | [diff] [blame] | 40 | // The path to the dex jar that is in the boot class path. If this is unset then the associated | 
| Paul Duffin | ff774a0 | 2021-01-29 12:53:15 +0000 | [diff] [blame] | 41 | // module is not a boot jar, but could be one of the <x>-hiddenapi modules that provide additional | 
|  | 42 | // annotations for the <x> boot dex jar but which do not actually provide a boot dex jar | 
|  | 43 | // themselves. | 
| Paul Duffin | 4103e92 | 2021-02-01 19:01:34 +0000 | [diff] [blame] | 44 | // | 
|  | 45 | // This must be the path to the unencoded dex jar as the encoded dex jar indirectly depends on | 
|  | 46 | // this file so using the encoded dex jar here would result in a cycle in the ninja rules. | 
| Spandan Das | 3a39201 | 2024-01-17 18:26:27 +0000 | [diff] [blame] | 47 | bootDexJarPath    OptionalDexJarPath | 
|  | 48 | bootDexJarPathErr error | 
| Paul Duffin | ff774a0 | 2021-01-29 12:53:15 +0000 | [diff] [blame] | 49 |  | 
| Paul Duffin | 36187b2 | 2021-04-22 16:43:06 +0100 | [diff] [blame] | 50 | // The paths to the classes jars that contain classes and class members annotated with | 
|  | 51 | // the UnsupportedAppUsage annotation that need to be extracted as part of the hidden API | 
|  | 52 | // processing. | 
|  | 53 | classesJarPaths android.Paths | 
| Paul Duffin | 1bbd062 | 2021-05-14 15:52:25 +0100 | [diff] [blame] | 54 |  | 
|  | 55 | // The compressed state of the dex file being encoded. This is used to ensure that the encoded | 
|  | 56 | // dex file has the same state. | 
|  | 57 | uncompressDexState *bool | 
| Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 58 | } | 
|  | 59 |  | 
| Spandan Das | 3a39201 | 2024-01-17 18:26:27 +0000 | [diff] [blame] | 60 | func (h *hiddenAPI) bootDexJar(ctx android.ModuleErrorfContext) OptionalDexJarPath { | 
|  | 61 | if h.bootDexJarPathErr != nil { | 
|  | 62 | ctx.ModuleErrorf(h.bootDexJarPathErr.Error()) | 
|  | 63 | } | 
| Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 64 | return h.bootDexJarPath | 
|  | 65 | } | 
|  | 66 |  | 
| Paul Duffin | 36187b2 | 2021-04-22 16:43:06 +0100 | [diff] [blame] | 67 | func (h *hiddenAPI) classesJars() android.Paths { | 
|  | 68 | return h.classesJarPaths | 
|  | 69 | } | 
|  | 70 |  | 
| Paul Duffin | 1bbd062 | 2021-05-14 15:52:25 +0100 | [diff] [blame] | 71 | func (h *hiddenAPI) uncompressDex() *bool { | 
|  | 72 | return h.uncompressDexState | 
|  | 73 | } | 
|  | 74 |  | 
| Paul Duffin | 537ea3d | 2021-05-14 10:38:00 +0100 | [diff] [blame] | 75 | // hiddenAPIModule is the interface a module that embeds the hiddenAPI structure must implement. | 
|  | 76 | type hiddenAPIModule interface { | 
|  | 77 | android.Module | 
|  | 78 | hiddenAPIIntf | 
| Paul Duffin | 09817d6 | 2022-04-28 17:45:11 +0100 | [diff] [blame] | 79 |  | 
| Spandan Das | 8c9ae7e | 2023-03-03 21:20:36 +0000 | [diff] [blame] | 80 | MinSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel | 
| Paul Duffin | 537ea3d | 2021-05-14 10:38:00 +0100 | [diff] [blame] | 81 | } | 
|  | 82 |  | 
| Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 83 | type hiddenAPIIntf interface { | 
| Spandan Das | 3a39201 | 2024-01-17 18:26:27 +0000 | [diff] [blame] | 84 | bootDexJar(ctx android.ModuleErrorfContext) OptionalDexJarPath | 
| Paul Duffin | 36187b2 | 2021-04-22 16:43:06 +0100 | [diff] [blame] | 85 | classesJars() android.Paths | 
| Paul Duffin | 1bbd062 | 2021-05-14 15:52:25 +0100 | [diff] [blame] | 86 | uncompressDex() *bool | 
| Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 87 | } | 
|  | 88 |  | 
|  | 89 | var _ hiddenAPIIntf = (*hiddenAPI)(nil) | 
|  | 90 |  | 
| Paul Duffin | 4103e92 | 2021-02-01 19:01:34 +0000 | [diff] [blame] | 91 | // Initialize the hiddenapi structure | 
| Paul Duffin | 1bbd062 | 2021-05-14 15:52:25 +0100 | [diff] [blame] | 92 | // | 
|  | 93 | // uncompressedDexState should be nil when the module is a prebuilt and so does not require hidden | 
|  | 94 | // API encoding. | 
| Martin Stjernholm | 8be1e6d | 2021-09-15 03:34:04 +0100 | [diff] [blame] | 95 | func (h *hiddenAPI) initHiddenAPI(ctx android.ModuleContext, dexJar OptionalDexJarPath, classesJar android.Path, uncompressedDexState *bool) { | 
| Paul Duffin | 74d18d1 | 2021-05-14 14:18:47 +0100 | [diff] [blame] | 96 |  | 
|  | 97 | // Save the classes jars even if this is not active as they may be used by modular hidden API | 
|  | 98 | // processing. | 
|  | 99 | classesJars := android.Paths{classesJar} | 
|  | 100 | ctx.VisitDirectDepsWithTag(hiddenApiAnnotationsTag, func(dep android.Module) { | 
| Colin Cross | 313aa54 | 2023-12-13 13:47:44 -0800 | [diff] [blame] | 101 | javaInfo, _ := android.OtherModuleProvider(ctx, dep, JavaInfoProvider) | 
| Paul Duffin | 74d18d1 | 2021-05-14 14:18:47 +0100 | [diff] [blame] | 102 | classesJars = append(classesJars, javaInfo.ImplementationJars...) | 
|  | 103 | }) | 
|  | 104 | h.classesJarPaths = classesJars | 
|  | 105 |  | 
|  | 106 | // Save the unencoded dex jar so it can be used when generating the | 
|  | 107 | // hiddenAPISingletonPathsStruct.stubFlags file. | 
|  | 108 | h.bootDexJarPath = dexJar | 
|  | 109 |  | 
| Paul Duffin | 1bbd062 | 2021-05-14 15:52:25 +0100 | [diff] [blame] | 110 | h.uncompressDexState = uncompressedDexState | 
|  | 111 |  | 
| Paul Duffin | 4103e92 | 2021-02-01 19:01:34 +0000 | [diff] [blame] | 112 | // If hiddenapi processing is disabled treat this as inactive. | 
| Pratyush | faec4db | 2023-07-20 11:19:04 +0000 | [diff] [blame] | 113 | if ctx.Config().DisableHiddenApiChecks() { | 
| Paul Duffin | 4103e92 | 2021-02-01 19:01:34 +0000 | [diff] [blame] | 114 | return | 
|  | 115 | } | 
|  | 116 |  | 
| Paul Duffin | 74d18d1 | 2021-05-14 14:18:47 +0100 | [diff] [blame] | 117 | // The context module must implement hiddenAPIModule. | 
|  | 118 | module := ctx.Module().(hiddenAPIModule) | 
|  | 119 |  | 
| Paul Duffin | b6f53c0 | 2021-05-14 07:52:42 +0100 | [diff] [blame] | 120 | // If the frameworks/base directories does not exist and no prebuilt hidden API flag files have | 
|  | 121 | // been configured then it is not possible to do hidden API encoding. | 
|  | 122 | if !ctx.Config().FrameworksBaseDirExists(ctx) && ctx.Config().PrebuiltHiddenApiDir(ctx) == "" { | 
|  | 123 | return | 
|  | 124 | } | 
|  | 125 |  | 
| Paul Duffin | 4103e92 | 2021-02-01 19:01:34 +0000 | [diff] [blame] | 126 | // It is important that hiddenapi information is only gathered for/from modules that are actually | 
|  | 127 | // on the boot jars list because the runtime only enforces access to the hidden API for the | 
|  | 128 | // bootclassloader. If information is gathered for modules not on the list then that will cause | 
|  | 129 | // failures in the CtsHiddenApiBlocklist... tests. | 
| Paul Duffin | 82b3fcf | 2021-02-12 15:42:46 +0000 | [diff] [blame] | 130 | h.active = isModuleInBootClassPath(ctx, module) | 
| Paul Duffin | 4103e92 | 2021-02-01 19:01:34 +0000 | [diff] [blame] | 131 | } | 
|  | 132 |  | 
| Spandan Das | 3a39201 | 2024-01-17 18:26:27 +0000 | [diff] [blame] | 133 | // Store any error encountered during the initialization of hiddenapi structure (e.g. unflagged co-existing prebuilt apexes) | 
|  | 134 | func (h *hiddenAPI) initHiddenAPIError(err error) { | 
|  | 135 | h.bootDexJarPathErr = err | 
|  | 136 | } | 
|  | 137 |  | 
| Paul Duffin | 82b3fcf | 2021-02-12 15:42:46 +0000 | [diff] [blame] | 138 | func isModuleInBootClassPath(ctx android.BaseModuleContext, module android.Module) bool { | 
| satayev | d604b21 | 2021-07-21 14:23:52 +0100 | [diff] [blame] | 139 | // Get the configured platform and apex boot jars. | 
|  | 140 | nonApexBootJars := ctx.Config().NonApexBootJars() | 
|  | 141 | apexBootJars := ctx.Config().ApexBootJars() | 
|  | 142 | active := isModuleInConfiguredList(ctx, module, nonApexBootJars) || | 
|  | 143 | isModuleInConfiguredList(ctx, module, apexBootJars) | 
| Paul Duffin | 82b3fcf | 2021-02-12 15:42:46 +0000 | [diff] [blame] | 144 | return active | 
|  | 145 | } | 
|  | 146 |  | 
| Paul Duffin | afaa47c | 2021-05-14 13:04:04 +0100 | [diff] [blame] | 147 | // hiddenAPIEncodeDex is called by any module that needs to encode dex files. | 
| Paul Duffin | 4103e92 | 2021-02-01 19:01:34 +0000 | [diff] [blame] | 148 | // | 
|  | 149 | // It ignores any module that has not had initHiddenApi() called on it and which is not in the boot | 
| Paul Duffin | afaa47c | 2021-05-14 13:04:04 +0100 | [diff] [blame] | 150 | // jar list. In that case it simply returns the supplied dex jar path. | 
| Paul Duffin | 4103e92 | 2021-02-01 19:01:34 +0000 | [diff] [blame] | 151 | // | 
| Paul Duffin | afaa47c | 2021-05-14 13:04:04 +0100 | [diff] [blame] | 152 | // Otherwise, it creates a copy of the supplied dex file into which it has encoded the hiddenapi | 
|  | 153 | // flags and returns this instead of the supplied dex jar. | 
| Paul Duffin | 1bbd062 | 2021-05-14 15:52:25 +0100 | [diff] [blame] | 154 | func (h *hiddenAPI) hiddenAPIEncodeDex(ctx android.ModuleContext, dexJar android.OutputPath) android.OutputPath { | 
| Paul Duffin | 001e606 | 2021-05-14 01:13:55 +0100 | [diff] [blame] | 155 |  | 
| Paul Duffin | 4103e92 | 2021-02-01 19:01:34 +0000 | [diff] [blame] | 156 | if !h.active { | 
|  | 157 | return dexJar | 
|  | 158 | } | 
| Paul Duffin | d2aceca | 2019-02-28 16:13:20 +0000 | [diff] [blame] | 159 |  | 
| Paul Duffin | 1bbd062 | 2021-05-14 15:52:25 +0100 | [diff] [blame] | 160 | // A nil uncompressDexState prevents the dex file from being encoded. | 
|  | 161 | if h.uncompressDexState == nil { | 
|  | 162 | ctx.ModuleErrorf("cannot encode dex file %s when uncompressDexState is nil", dexJar) | 
|  | 163 | } | 
|  | 164 | uncompressDex := *h.uncompressDexState | 
|  | 165 |  | 
| Paul Duffin | f8f4af8 | 2021-02-12 15:42:20 +0000 | [diff] [blame] | 166 | // Create a copy of the dex jar which has been encoded with hiddenapi flags. | 
| Paul Duffin | 0916595 | 2021-05-16 23:32:52 +0100 | [diff] [blame] | 167 | flagsCSV := hiddenAPISingletonPaths(ctx).flags | 
|  | 168 | outputDir := android.PathForModuleOut(ctx, "hiddenapi").OutputPath | 
| Spandan Das | 8c9ae7e | 2023-03-03 21:20:36 +0000 | [diff] [blame] | 169 | encodedDex := hiddenAPIEncodeDex(ctx, dexJar, flagsCSV, uncompressDex, android.NoneApiLevel, outputDir) | 
| Paul Duffin | 4103e92 | 2021-02-01 19:01:34 +0000 | [diff] [blame] | 170 |  | 
| Paul Duffin | f8f4af8 | 2021-02-12 15:42:20 +0000 | [diff] [blame] | 171 | // Use the encoded dex jar from here onwards. | 
| Paul Duffin | 0916595 | 2021-05-16 23:32:52 +0100 | [diff] [blame] | 172 | return encodedDex | 
| Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 173 | } | 
|  | 174 |  | 
| Paul Duffin | 850e61f | 2021-05-14 09:58:48 +0100 | [diff] [blame] | 175 | // buildRuleToGenerateAnnotationFlags builds a ninja rule to generate the annotation-flags.csv file | 
|  | 176 | // from the classes jars and stub-flags.csv files. | 
| Paul Duffin | afaa47c | 2021-05-14 13:04:04 +0100 | [diff] [blame] | 177 | // | 
|  | 178 | // The annotation-flags.csv file contains mappings from Java signature to various flags derived from | 
|  | 179 | // annotations in the source, e.g. whether it is public or the sdk version above which it can no | 
|  | 180 | // longer be used. | 
|  | 181 | // | 
|  | 182 | // It is created by the Class2NonSdkList tool which processes the .class files in the class | 
|  | 183 | // implementation jar looking for UnsupportedAppUsage and CovariantReturnType annotations. The | 
|  | 184 | // tool also consumes the hiddenAPISingletonPathsStruct.stubFlags file in order to perform | 
|  | 185 | // consistency checks on the information in the annotations and to filter out bridge methods | 
|  | 186 | // that are already part of the public API. | 
| Paul Duffin | 850e61f | 2021-05-14 09:58:48 +0100 | [diff] [blame] | 187 | func buildRuleToGenerateAnnotationFlags(ctx android.ModuleContext, desc string, classesJars android.Paths, stubFlagsCSV android.Path, outputPath android.WritablePath) { | 
| Colin Cross | 8faf8fc | 2019-01-16 15:15:52 -0800 | [diff] [blame] | 188 | ctx.Build(pctx, android.BuildParams{ | 
|  | 189 | Rule:        hiddenAPIGenerateCSVRule, | 
| Paul Duffin | 850e61f | 2021-05-14 09:58:48 +0100 | [diff] [blame] | 190 | Description: desc, | 
| Paul Duffin | 031d869 | 2021-02-12 11:46:42 +0000 | [diff] [blame] | 191 | Inputs:      classesJars, | 
| Paul Duffin | 850e61f | 2021-05-14 09:58:48 +0100 | [diff] [blame] | 192 | Output:      outputPath, | 
| David Brazdil | 0f670a2 | 2019-01-18 16:30:03 +0000 | [diff] [blame] | 193 | Implicit:    stubFlagsCSV, | 
| Colin Cross | 8faf8fc | 2019-01-16 15:15:52 -0800 | [diff] [blame] | 194 | Args: map[string]string{ | 
| David Brazdil | 0f670a2 | 2019-01-18 16:30:03 +0000 | [diff] [blame] | 195 | "outFlag":      "--write-flags-csv", | 
|  | 196 | "stubAPIFlags": stubFlagsCSV.String(), | 
| Colin Cross | 8faf8fc | 2019-01-16 15:15:52 -0800 | [diff] [blame] | 197 | }, | 
|  | 198 | }) | 
| Paul Duffin | 850e61f | 2021-05-14 09:58:48 +0100 | [diff] [blame] | 199 | } | 
| Colin Cross | 8faf8fc | 2019-01-16 15:15:52 -0800 | [diff] [blame] | 200 |  | 
| Paul Duffin | 850e61f | 2021-05-14 09:58:48 +0100 | [diff] [blame] | 201 | // buildRuleToGenerateMetadata builds a ninja rule to generate the metadata.csv file from | 
|  | 202 | // the classes jars and stub-flags.csv files. | 
| Paul Duffin | afaa47c | 2021-05-14 13:04:04 +0100 | [diff] [blame] | 203 | // | 
|  | 204 | // The metadata.csv file contains mappings from Java signature to the value of properties specified | 
|  | 205 | // on UnsupportedAppUsage annotations in the source. | 
|  | 206 | // | 
|  | 207 | // Like the annotation-flags.csv file this is also created by the Class2NonSdkList in the same way. | 
|  | 208 | // Although the two files could potentially be created in a single invocation of the | 
|  | 209 | // Class2NonSdkList at the moment they are created using their own invocation, with the behavior | 
|  | 210 | // being determined by the property that is used. | 
| Paul Duffin | 850e61f | 2021-05-14 09:58:48 +0100 | [diff] [blame] | 211 | func buildRuleToGenerateMetadata(ctx android.ModuleContext, desc string, classesJars android.Paths, stubFlagsCSV android.Path, metadataCSV android.WritablePath) { | 
| Colin Cross | 8faf8fc | 2019-01-16 15:15:52 -0800 | [diff] [blame] | 212 | ctx.Build(pctx, android.BuildParams{ | 
|  | 213 | Rule:        hiddenAPIGenerateCSVRule, | 
| Paul Duffin | 850e61f | 2021-05-14 09:58:48 +0100 | [diff] [blame] | 214 | Description: desc, | 
| Paul Duffin | 031d869 | 2021-02-12 11:46:42 +0000 | [diff] [blame] | 215 | Inputs:      classesJars, | 
| Colin Cross | 8faf8fc | 2019-01-16 15:15:52 -0800 | [diff] [blame] | 216 | Output:      metadataCSV, | 
| David Brazdil | 0f670a2 | 2019-01-18 16:30:03 +0000 | [diff] [blame] | 217 | Implicit:    stubFlagsCSV, | 
| Colin Cross | 8faf8fc | 2019-01-16 15:15:52 -0800 | [diff] [blame] | 218 | Args: map[string]string{ | 
| David Brazdil | 0f670a2 | 2019-01-18 16:30:03 +0000 | [diff] [blame] | 219 | "outFlag":      "--write-metadata-csv", | 
|  | 220 | "stubAPIFlags": stubFlagsCSV.String(), | 
| Colin Cross | 8faf8fc | 2019-01-16 15:15:52 -0800 | [diff] [blame] | 221 | }, | 
|  | 222 | }) | 
| Paul Duffin | 850e61f | 2021-05-14 09:58:48 +0100 | [diff] [blame] | 223 | } | 
| Colin Cross | 8faf8fc | 2019-01-16 15:15:52 -0800 | [diff] [blame] | 224 |  | 
| Paul Duffin | afaa47c | 2021-05-14 13:04:04 +0100 | [diff] [blame] | 225 | // buildRuleToGenerateIndex builds a ninja rule to generate the index.csv file from the classes | 
| Paul Duffin | 850e61f | 2021-05-14 09:58:48 +0100 | [diff] [blame] | 226 | // jars. | 
| Paul Duffin | afaa47c | 2021-05-14 13:04:04 +0100 | [diff] [blame] | 227 | // | 
|  | 228 | // The index.csv file contains mappings from Java signature to source location information. | 
|  | 229 | // | 
|  | 230 | // It is created by the merge_csv tool which processes the class implementation jar, extracting | 
|  | 231 | // all the files ending in .uau (which are CSV files) and merges them together. The .uau files are | 
|  | 232 | // created by the unsupported app usage annotation processor during compilation of the class | 
|  | 233 | // implementation jar. | 
| Paul Duffin | 850e61f | 2021-05-14 09:58:48 +0100 | [diff] [blame] | 234 | func buildRuleToGenerateIndex(ctx android.ModuleContext, desc string, classesJars android.Paths, indexCSV android.WritablePath) { | 
| Cole Faust | a5f64f0 | 2023-02-14 17:50:31 -0800 | [diff] [blame] | 235 | ctx.Build(pctx, android.BuildParams{ | 
|  | 236 | Rule:        hiddenAPIGenerateIndexRule, | 
|  | 237 | Description: desc, | 
|  | 238 | Inputs:      classesJars, | 
|  | 239 | Output:      indexCSV, | 
|  | 240 | }) | 
| Colin Cross | 8faf8fc | 2019-01-16 15:15:52 -0800 | [diff] [blame] | 241 | } | 
|  | 242 |  | 
|  | 243 | var hiddenAPIEncodeDexRule = pctx.AndroidStaticRule("hiddenAPIEncodeDex", blueprint.RuleParams{ | 
| Artur Satayev | b5df8a0 | 2020-02-19 16:39:59 +0000 | [diff] [blame] | 244 | Command: `rm -rf $tmpDir && mkdir -p $tmpDir && mkdir $tmpDir/dex-input && mkdir $tmpDir/dex-output && | 
| Colin Cross | d783bbb | 2020-07-11 22:30:45 -0700 | [diff] [blame] | 245 | unzip -qoDD $in 'classes*.dex' -d $tmpDir/dex-input && | 
| Artur Satayev | b5df8a0 | 2020-02-19 16:39:59 +0000 | [diff] [blame] | 246 | for INPUT_DEX in $$(find $tmpDir/dex-input -maxdepth 1 -name 'classes*.dex' | sort); do | 
|  | 247 | echo "--input-dex=$${INPUT_DEX}"; | 
|  | 248 | echo "--output-dex=$tmpDir/dex-output/$$(basename $${INPUT_DEX})"; | 
|  | 249 | done | xargs ${config.HiddenAPI} encode --api-flags=$flagsCsv $hiddenapiFlags && | 
|  | 250 | ${config.SoongZipCmd} $soongZipFlags -o $tmpDir/dex.jar -C $tmpDir/dex-output -f "$tmpDir/dex-output/classes*.dex" && | 
| Paul Duffin | 4de9450 | 2021-05-16 05:21:16 +0100 | [diff] [blame] | 251 | ${config.MergeZipsCmd} -j -D -zipToNotStrip $tmpDir/dex.jar -stripFile "classes*.dex" -stripFile "**/*.uau" $out $tmpDir/dex.jar $in`, | 
| Colin Cross | 8faf8fc | 2019-01-16 15:15:52 -0800 | [diff] [blame] | 252 | CommandDeps: []string{ | 
|  | 253 | "${config.HiddenAPI}", | 
|  | 254 | "${config.SoongZipCmd}", | 
|  | 255 | "${config.MergeZipsCmd}", | 
|  | 256 | }, | 
| David Brazdil | 91b4e3e | 2019-01-23 21:04:05 +0000 | [diff] [blame] | 257 | }, "flagsCsv", "hiddenapiFlags", "tmpDir", "soongZipFlags") | 
| Colin Cross | 8faf8fc | 2019-01-16 15:15:52 -0800 | [diff] [blame] | 258 |  | 
| Paul Duffin | 0916595 | 2021-05-16 23:32:52 +0100 | [diff] [blame] | 259 | // hiddenAPIEncodeDex generates the build rule that will encode the supplied dex jar and place the | 
|  | 260 | // encoded dex jar in a file of the same name in the output directory. | 
|  | 261 | // | 
|  | 262 | // The encode dex rule requires unzipping, encoding and rezipping the classes.dex files along with | 
|  | 263 | // all the resources from the input jar. It also ensures that if it was uncompressed in the input | 
|  | 264 | // it stays uncompressed in the output. | 
| Spandan Das | 8c9ae7e | 2023-03-03 21:20:36 +0000 | [diff] [blame] | 265 | func hiddenAPIEncodeDex(ctx android.ModuleContext, dexInput, flagsCSV android.Path, uncompressDex bool, minSdkVersion android.ApiLevel, outputDir android.OutputPath) android.OutputPath { | 
| Colin Cross | cd964b3 | 2019-01-18 22:03:02 -0800 | [diff] [blame] | 266 |  | 
| Paul Duffin | 0916595 | 2021-05-16 23:32:52 +0100 | [diff] [blame] | 267 | // The output file has the same name as the input file and is in the output directory. | 
|  | 268 | output := outputDir.Join(ctx, dexInput.Base()) | 
| Colin Cross | 8faf8fc | 2019-01-16 15:15:52 -0800 | [diff] [blame] | 269 |  | 
| Paul Duffin | 0916595 | 2021-05-16 23:32:52 +0100 | [diff] [blame] | 270 | // Create a jar specific temporary directory in which to do the work just in case this is called | 
|  | 271 | // with the same output directory for multiple modules. | 
|  | 272 | tmpDir := outputDir.Join(ctx, dexInput.Base()+"-tmp") | 
|  | 273 |  | 
|  | 274 | // If the input is uncompressed then generate the output of the encode rule to an intermediate | 
|  | 275 | // file as the final output will need further processing after encoding. | 
| Colin Cross | cd964b3 | 2019-01-18 22:03:02 -0800 | [diff] [blame] | 276 | soongZipFlags := "" | 
| Paul Duffin | 0916595 | 2021-05-16 23:32:52 +0100 | [diff] [blame] | 277 | encodeRuleOutput := output | 
| Colin Cross | cd964b3 | 2019-01-18 22:03:02 -0800 | [diff] [blame] | 278 | if uncompressDex { | 
|  | 279 | soongZipFlags = "-L 0" | 
| Paul Duffin | 0916595 | 2021-05-16 23:32:52 +0100 | [diff] [blame] | 280 | encodeRuleOutput = outputDir.Join(ctx, "unaligned", dexInput.Base()) | 
| Colin Cross | cd964b3 | 2019-01-18 22:03:02 -0800 | [diff] [blame] | 281 | } | 
| Jiyong Park | 93e57a0 | 2020-02-21 16:04:53 +0900 | [diff] [blame] | 282 |  | 
| Jiyong Park | 93e57a0 | 2020-02-21 16:04:53 +0900 | [diff] [blame] | 283 | // b/149353192: when a module is instrumented, jacoco adds synthetic members | 
|  | 284 | // $jacocoData and $jacocoInit. Since they don't exist when building the hidden API flags, | 
|  | 285 | // don't complain when we don't find hidden API flags for the synthetic members. | 
| Paul Duffin | 0916595 | 2021-05-16 23:32:52 +0100 | [diff] [blame] | 286 | hiddenapiFlags := "" | 
| Paul Duffin | c495d2b | 2020-05-19 21:07:52 +0100 | [diff] [blame] | 287 | if j, ok := ctx.Module().(interface { | 
|  | 288 | shouldInstrument(android.BaseModuleContext) bool | 
|  | 289 | }); ok && j.shouldInstrument(ctx) { | 
| David Brazdil | 91b4e3e | 2019-01-23 21:04:05 +0000 | [diff] [blame] | 290 | hiddenapiFlags = "--no-force-assign-all" | 
|  | 291 | } | 
| Colin Cross | cd964b3 | 2019-01-18 22:03:02 -0800 | [diff] [blame] | 292 |  | 
| Paul Duffin | 09817d6 | 2022-04-28 17:45:11 +0100 | [diff] [blame] | 293 | // If the library is targeted for Q and/or R then make sure that they do not | 
|  | 294 | // have any S+ flags encoded as that will break the runtime. | 
| Spandan Das | 8c9ae7e | 2023-03-03 21:20:36 +0000 | [diff] [blame] | 295 | minApiLevel := minSdkVersion | 
| Paul Duffin | 09817d6 | 2022-04-28 17:45:11 +0100 | [diff] [blame] | 296 | if !minApiLevel.IsNone() { | 
|  | 297 | if minApiLevel.LessThanOrEqualTo(android.ApiLevelOrPanic(ctx, "R")) { | 
|  | 298 | hiddenapiFlags = hiddenapiFlags + " --max-hiddenapi-level=max-target-r" | 
|  | 299 | } | 
|  | 300 | } | 
|  | 301 |  | 
| Colin Cross | 8faf8fc | 2019-01-16 15:15:52 -0800 | [diff] [blame] | 302 | ctx.Build(pctx, android.BuildParams{ | 
|  | 303 | Rule:        hiddenAPIEncodeDexRule, | 
|  | 304 | Description: "hiddenapi encode dex", | 
|  | 305 | Input:       dexInput, | 
| Paul Duffin | 0916595 | 2021-05-16 23:32:52 +0100 | [diff] [blame] | 306 | Output:      encodeRuleOutput, | 
| Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 307 | Implicit:    flagsCSV, | 
| Colin Cross | 8faf8fc | 2019-01-16 15:15:52 -0800 | [diff] [blame] | 308 | Args: map[string]string{ | 
| Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 309 | "flagsCsv":       flagsCSV.String(), | 
| David Brazdil | 91b4e3e | 2019-01-23 21:04:05 +0000 | [diff] [blame] | 310 | "tmpDir":         tmpDir.String(), | 
|  | 311 | "soongZipFlags":  soongZipFlags, | 
|  | 312 | "hiddenapiFlags": hiddenapiFlags, | 
| Colin Cross | 8faf8fc | 2019-01-16 15:15:52 -0800 | [diff] [blame] | 313 | }, | 
|  | 314 | }) | 
|  | 315 |  | 
| Nicolas Geoffray | 65fd8ba | 2019-01-21 23:20:23 +0000 | [diff] [blame] | 316 | if uncompressDex { | 
| Cole Faust | 51d7bfd | 2023-09-07 05:31:32 +0000 | [diff] [blame] | 317 | TransformZipAlign(ctx, output, encodeRuleOutput, nil) | 
| Nicolas Geoffray | 65fd8ba | 2019-01-21 23:20:23 +0000 | [diff] [blame] | 318 | } | 
| Paul Duffin | 0916595 | 2021-05-16 23:32:52 +0100 | [diff] [blame] | 319 |  | 
|  | 320 | return output | 
| Colin Cross | 8faf8fc | 2019-01-16 15:15:52 -0800 | [diff] [blame] | 321 | } | 
| Paul Duffin | 031d869 | 2021-02-12 11:46:42 +0000 | [diff] [blame] | 322 |  | 
|  | 323 | type hiddenApiAnnotationsDependencyTag struct { | 
|  | 324 | blueprint.BaseDependencyTag | 
| Colin Cross | ce56425 | 2022-01-12 11:13:32 -0800 | [diff] [blame] | 325 | android.LicenseAnnotationSharedDependencyTag | 
| Paul Duffin | 031d869 | 2021-02-12 11:46:42 +0000 | [diff] [blame] | 326 | } | 
|  | 327 |  | 
|  | 328 | // Tag used to mark dependencies on java_library instances that contains Java source files whose | 
|  | 329 | // sole purpose is to provide additional hiddenapi annotations. | 
|  | 330 | var hiddenApiAnnotationsTag hiddenApiAnnotationsDependencyTag | 
|  | 331 |  | 
|  | 332 | // Mark this tag so dependencies that use it are excluded from APEX contents. | 
|  | 333 | func (t hiddenApiAnnotationsDependencyTag) ExcludeFromApexContents() {} | 
|  | 334 |  | 
|  | 335 | var _ android.ExcludeFromApexContentsTag = hiddenApiAnnotationsTag |