blob: d5a85f749d56d4d20de4d2f7fec90b43fd268b25 [file] [log] [blame]
Colin Cross2fe66872015-03-30 17:20:39 -07001// Copyright 2015 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
15package java
16
17// This file contains the module types for compiling Java for Android, and converts the properties
Colin Cross46c9b8b2017-06-22 16:51:17 -070018// into the flags and filenames necessary to pass to the Module. The final creation of the rules
Colin Cross2fe66872015-03-30 17:20:39 -070019// is handled in builder.go
20
21import (
Colin Crossf19b9bb2018-03-26 14:42:44 -070022 "fmt"
Colin Crossfc3674a2017-09-18 17:41:52 -070023 "path/filepath"
Alix289e9c62023-08-16 15:06:31 +000024 "sort"
Wei Libafb6d62021-12-10 03:14:59 -080025 "strings"
Colin Cross2fe66872015-03-30 17:20:39 -070026
Jihoon Kang0ac87c22022-11-15 19:06:14 +000027 "android/soong/remoteexec"
Aditya Choudhary9b593522023-10-06 19:54:58 +000028 "android/soong/testing"
Jihoon Kang6592e872023-12-19 01:13:16 +000029
Colin Cross2fe66872015-03-30 17:20:39 -070030 "github.com/google/blueprint"
Colin Cross76b5f0c2017-08-29 16:02:06 -070031 "github.com/google/blueprint/proptools"
Colin Cross2fe66872015-03-30 17:20:39 -070032
Colin Cross635c3b02016-05-18 15:37:25 -070033 "android/soong/android"
Colin Crossf8d9c492021-01-26 11:01:43 -080034 "android/soong/cc"
Ulya Trafimovich31e444e2020-08-14 17:32:16 +010035 "android/soong/dexpreopt"
Colin Cross3e3e72d2017-06-22 17:20:19 -070036 "android/soong/java/config"
Colin Cross303e21f2018-08-07 16:49:25 -070037 "android/soong/tradefed"
Colin Cross2fe66872015-03-30 17:20:39 -070038)
39
Colin Cross463a90e2015-06-17 14:20:06 -070040func init() {
Paul Duffin535e0a12021-03-30 23:34:32 +010041 registerJavaBuildComponents(android.InitRegistrationContext)
Paul Duffin255f18e2019-12-13 11:22:16 +000042
Jaewoong Jungbc15e3a2021-03-10 17:02:43 -080043 RegisterJavaSdkMemberTypes()
44}
45
Paul Duffin535e0a12021-03-30 23:34:32 +010046func registerJavaBuildComponents(ctx android.RegistrationContext) {
Jaewoong Jungbc15e3a2021-03-10 17:02:43 -080047 ctx.RegisterModuleType("java_defaults", DefaultsFactory)
48
49 ctx.RegisterModuleType("java_library", LibraryFactory)
50 ctx.RegisterModuleType("java_library_static", LibraryStaticFactory)
51 ctx.RegisterModuleType("java_library_host", LibraryHostFactory)
52 ctx.RegisterModuleType("java_binary", BinaryFactory)
53 ctx.RegisterModuleType("java_binary_host", BinaryHostFactory)
54 ctx.RegisterModuleType("java_test", TestFactory)
55 ctx.RegisterModuleType("java_test_helper_library", TestHelperLibraryFactory)
56 ctx.RegisterModuleType("java_test_host", TestHostFactory)
57 ctx.RegisterModuleType("java_test_import", JavaTestImportFactory)
58 ctx.RegisterModuleType("java_import", ImportFactory)
59 ctx.RegisterModuleType("java_import_host", ImportFactoryHost)
60 ctx.RegisterModuleType("java_device_for_host", DeviceForHostFactory)
61 ctx.RegisterModuleType("java_host_for_device", HostForDeviceFactory)
62 ctx.RegisterModuleType("dex_import", DexImportFactory)
Jihoon Kang0ac87c22022-11-15 19:06:14 +000063 ctx.RegisterModuleType("java_api_library", ApiLibraryFactory)
64 ctx.RegisterModuleType("java_api_contribution", ApiContributionFactory)
Jihoon Kangfdf32362023-09-12 00:36:43 +000065 ctx.RegisterModuleType("java_api_contribution_import", ApiContributionImportFactory)
Jaewoong Jungbc15e3a2021-03-10 17:02:43 -080066
Martin Stjernholm0e4cceb2021-05-13 02:38:35 +010067 // This mutator registers dependencies on dex2oat for modules that should be
68 // dexpreopted. This is done late when the final variants have been
69 // established, to not get the dependencies split into the wrong variants and
70 // to support the checks in dexpreoptDisabled().
Jaewoong Jungbc15e3a2021-03-10 17:02:43 -080071 ctx.FinalDepsMutators(func(ctx android.RegisterMutatorsContext) {
72 ctx.BottomUp("dexpreopt_tool_deps", dexpreoptToolDepsMutator).Parallel()
Sam Delmerico1e3f78f2022-09-07 12:07:07 -040073 // needs access to ApexInfoProvider which is available after variant creation
74 ctx.BottomUp("jacoco_deps", jacocoDepsMutator).Parallel()
Jaewoong Jungbc15e3a2021-03-10 17:02:43 -080075 })
76
LaMont Jones0c10e4d2023-05-16 00:58:37 +000077 ctx.RegisterParallelSingletonType("logtags", LogtagsSingleton)
78 ctx.RegisterParallelSingletonType("kythe_java_extract", kytheExtractJavaFactory)
Jaewoong Jungbc15e3a2021-03-10 17:02:43 -080079}
80
81func RegisterJavaSdkMemberTypes() {
Paul Duffin255f18e2019-12-13 11:22:16 +000082 // Register sdk member types.
Paul Duffin7b81f5e2020-01-13 21:03:22 +000083 android.RegisterSdkMemberType(javaHeaderLibsSdkMemberType)
Paul Duffin2da04242021-04-23 19:43:28 +010084 android.RegisterSdkMemberType(javaLibsSdkMemberType)
85 android.RegisterSdkMemberType(javaBootLibsSdkMemberType)
Jiakai Zhangea180332021-09-26 08:58:02 +000086 android.RegisterSdkMemberType(javaSystemserverLibsSdkMemberType)
Paul Duffin2da04242021-04-23 19:43:28 +010087 android.RegisterSdkMemberType(javaTestSdkMemberType)
88}
89
Jihoon Kangfe914ed2024-02-12 22:49:21 +000090type StubsLinkType int
91
92const (
93 Unknown StubsLinkType = iota
94 Stubs
95 Implementation
96)
97
Paul Duffin2da04242021-04-23 19:43:28 +010098var (
99 // Supports adding java header libraries to module_exports and sdk.
100 javaHeaderLibsSdkMemberType = &librarySdkMemberType{
Paul Duffin7ed6ff82022-11-21 10:57:30 +0000101 android.SdkMemberTypeBase{
Paul Duffin2da04242021-04-23 19:43:28 +0100102 PropertyName: "java_header_libs",
103 SupportsSdk: true,
104 },
Paul Duffin7ed6ff82022-11-21 10:57:30 +0000105 func(_ android.SdkMemberContext, j *Library) android.Path {
Paul Duffin2da04242021-04-23 19:43:28 +0100106 headerJars := j.HeaderJars()
107 if len(headerJars) != 1 {
108 panic(fmt.Errorf("there must be only one header jar from %q", j.Name()))
109 }
110
111 return headerJars[0]
112 },
Paul Duffin7ed6ff82022-11-21 10:57:30 +0000113 sdkSnapshotFilePathForJar,
114 copyEverythingToSnapshot,
Paul Duffin2da04242021-04-23 19:43:28 +0100115 }
Paul Duffin255f18e2019-12-13 11:22:16 +0000116
Paul Duffin22ff0aa2021-02-04 11:15:34 +0000117 // Export implementation classes jar as part of the sdk.
Paul Duffin2da04242021-04-23 19:43:28 +0100118 exportImplementationClassesJar = func(_ android.SdkMemberContext, j *Library) android.Path {
Paul Duffin22ff0aa2021-02-04 11:15:34 +0000119 implementationJars := j.ImplementationAndResourcesJars()
120 if len(implementationJars) != 1 {
121 panic(fmt.Errorf("there must be only one implementation jar from %q", j.Name()))
122 }
123 return implementationJars[0]
124 }
125
Paul Duffin2da04242021-04-23 19:43:28 +0100126 // Supports adding java implementation libraries to module_exports but not sdk.
127 javaLibsSdkMemberType = &librarySdkMemberType{
Paul Duffin7ed6ff82022-11-21 10:57:30 +0000128 android.SdkMemberTypeBase{
Paul Duffinf5c0a9c2020-02-28 14:39:53 +0000129 PropertyName: "java_libs",
130 },
Paul Duffin7ed6ff82022-11-21 10:57:30 +0000131 exportImplementationClassesJar,
132 sdkSnapshotFilePathForJar,
133 copyEverythingToSnapshot,
Paul Duffin2da04242021-04-23 19:43:28 +0100134 }
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000135
Paul Duffin13648912022-07-15 13:12:35 +0000136 snapshotRequiresImplementationJar = func(ctx android.SdkMemberContext) bool {
137 // In the S build the build will break if updatable-media does not provide a full implementation
138 // jar. That issue was fixed in Tiramisu by b/229932396.
139 if ctx.IsTargetBuildBeforeTiramisu() && ctx.Name() == "updatable-media" {
140 return true
141 }
142
143 return false
144 }
145
Paul Duffin2da04242021-04-23 19:43:28 +0100146 // Supports adding java boot libraries to module_exports and sdk.
Paul Duffindb170e42020-12-08 17:48:25 +0000147 //
148 // The build has some implicit dependencies (via the boot jars configuration) on a number of
149 // modules, e.g. core-oj, apache-xml, that are part of the java boot class path and which are
150 // provided by mainline modules (e.g. art, conscrypt, runtime-i18n) but which are not otherwise
151 // used outside those mainline modules.
152 //
153 // As they are not needed outside the mainline modules adding them to the sdk/module-exports as
154 // either java_libs, or java_header_libs would end up exporting more information than was strictly
155 // necessary. The java_boot_libs property to allow those modules to be exported as part of the
156 // sdk/module_exports without exposing any unnecessary information.
Paul Duffin2da04242021-04-23 19:43:28 +0100157 javaBootLibsSdkMemberType = &librarySdkMemberType{
Paul Duffin7ed6ff82022-11-21 10:57:30 +0000158 android.SdkMemberTypeBase{
Paul Duffindb170e42020-12-08 17:48:25 +0000159 PropertyName: "java_boot_libs",
160 SupportsSdk: true,
161 },
Paul Duffin7ed6ff82022-11-21 10:57:30 +0000162 func(ctx android.SdkMemberContext, j *Library) android.Path {
Paul Duffin13648912022-07-15 13:12:35 +0000163 if snapshotRequiresImplementationJar(ctx) {
164 return exportImplementationClassesJar(ctx, j)
165 }
166
Paul Duffin5c211452021-07-15 12:42:44 +0100167 // Java boot libs are only provided in the SDK to provide access to their dex implementation
168 // jar for use by dexpreopting and boot jars package check. They do not need to provide an
169 // actual implementation jar but the java_import will need a file that exists so just copy an
170 // empty file. Any attempt to use that file as a jar will cause a build error.
Paul Duffin7ed6ff82022-11-21 10:57:30 +0000171 return ctx.SnapshotBuilder().EmptyFile()
Paul Duffin5c211452021-07-15 12:42:44 +0100172 },
Paul Duffin7ed6ff82022-11-21 10:57:30 +0000173 func(ctx android.SdkMemberContext, osPrefix, name string) string {
Paul Duffin13648912022-07-15 13:12:35 +0000174 if snapshotRequiresImplementationJar(ctx) {
175 return sdkSnapshotFilePathForJar(ctx, osPrefix, name)
176 }
177
Paul Duffin5c211452021-07-15 12:42:44 +0100178 // Create a special name for the implementation jar to try and provide some useful information
179 // to a developer that attempts to compile against this.
180 // TODO(b/175714559): Provide a proper error message in Soong not ninja.
181 return filepath.Join(osPrefix, "java_boot_libs", "snapshot", "jars", "are", "invalid", name+jarFileSuffix)
182 },
Paul Duffin7ed6ff82022-11-21 10:57:30 +0000183 onlyCopyJarToSnapshot,
Paul Duffin2da04242021-04-23 19:43:28 +0100184 }
Paul Duffindb170e42020-12-08 17:48:25 +0000185
Jiakai Zhangea180332021-09-26 08:58:02 +0000186 // Supports adding java systemserver libraries to module_exports and sdk.
187 //
188 // The build has some implicit dependencies (via the systemserver jars configuration) on a number
189 // of modules that are part of the java systemserver classpath and which are provided by mainline
190 // modules but which are not otherwise used outside those mainline modules.
191 //
192 // As they are not needed outside the mainline modules adding them to the sdk/module-exports as
193 // either java_libs, or java_header_libs would end up exporting more information than was strictly
194 // necessary. The java_systemserver_libs property to allow those modules to be exported as part of
195 // the sdk/module_exports without exposing any unnecessary information.
196 javaSystemserverLibsSdkMemberType = &librarySdkMemberType{
Paul Duffin7ed6ff82022-11-21 10:57:30 +0000197 android.SdkMemberTypeBase{
Jiakai Zhangea180332021-09-26 08:58:02 +0000198 PropertyName: "java_systemserver_libs",
199 SupportsSdk: true,
Paul Duffinf861df72022-07-01 15:56:06 +0000200
201 // This was only added in Tiramisu.
202 SupportedBuildReleaseSpecification: "Tiramisu+",
Jiakai Zhangea180332021-09-26 08:58:02 +0000203 },
Paul Duffin7ed6ff82022-11-21 10:57:30 +0000204 func(ctx android.SdkMemberContext, j *Library) android.Path {
Jiakai Zhangea180332021-09-26 08:58:02 +0000205 // Java systemserver libs are only provided in the SDK to provide access to their dex
206 // implementation jar for use by dexpreopting. They do not need to provide an actual
207 // implementation jar but the java_import will need a file that exists so just copy an empty
208 // file. Any attempt to use that file as a jar will cause a build error.
Paul Duffin7ed6ff82022-11-21 10:57:30 +0000209 return ctx.SnapshotBuilder().EmptyFile()
Jiakai Zhangea180332021-09-26 08:58:02 +0000210 },
Paul Duffin7ed6ff82022-11-21 10:57:30 +0000211 func(_ android.SdkMemberContext, osPrefix, name string) string {
Jiakai Zhangea180332021-09-26 08:58:02 +0000212 // Create a special name for the implementation jar to try and provide some useful information
213 // to a developer that attempts to compile against this.
214 // TODO(b/175714559): Provide a proper error message in Soong not ninja.
215 return filepath.Join(osPrefix, "java_systemserver_libs", "snapshot", "jars", "are", "invalid", name+jarFileSuffix)
216 },
Paul Duffin7ed6ff82022-11-21 10:57:30 +0000217 onlyCopyJarToSnapshot,
Jiakai Zhangea180332021-09-26 08:58:02 +0000218 }
219
Paul Duffin2da04242021-04-23 19:43:28 +0100220 // Supports adding java test libraries to module_exports but not sdk.
221 javaTestSdkMemberType = &testSdkMemberType{
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000222 SdkMemberTypeBase: android.SdkMemberTypeBase{
223 PropertyName: "java_tests",
224 },
Paul Duffin2da04242021-04-23 19:43:28 +0100225 }
Zi Wangca65b402022-10-10 13:45:06 -0700226
227 // Rule for generating device binary default wrapper
228 deviceBinaryWrapper = pctx.StaticRule("deviceBinaryWrapper", blueprint.RuleParams{
229 Command: `echo -e '#!/system/bin/sh\n` +
230 `export CLASSPATH=/system/framework/$jar_name\n` +
231 `exec app_process /$partition/bin $main_class "$$@"'> ${out}`,
232 Description: "Generating device binary wrapper ${jar_name}",
233 }, "jar_name", "partition", "main_class")
Paul Duffin2da04242021-04-23 19:43:28 +0100234)
Jeongik Cha538c0d02019-07-11 15:54:27 +0900235
Sam Delmerico95d70942023-08-02 18:00:35 -0400236type ProguardSpecInfo struct {
237 // If true, proguard flags files will be exported to reverse dependencies across libs edges
238 // If false, proguard flags files will only be exported to reverse dependencies across
239 // static_libs edges.
240 Export_proguard_flags_files bool
241
242 // TransitiveDepsProguardSpecFiles is a depset of paths to proguard flags files that are exported from
243 // all transitive deps. This list includes all proguard flags files from transitive static dependencies,
244 // and all proguard flags files from transitive libs dependencies which set `export_proguard_spec: true`.
245 ProguardFlagsFiles *android.DepSet[android.Path]
246
247 // implementation detail to store transitive proguard flags files from exporting shared deps
248 UnconditionallyExportedProguardFlags *android.DepSet[android.Path]
249}
250
Colin Crossbc7d76c2023-12-12 16:39:03 -0800251var ProguardSpecInfoProvider = blueprint.NewProvider[ProguardSpecInfo]()
Sam Delmerico95d70942023-08-02 18:00:35 -0400252
Colin Crossdcf71b22021-02-01 13:59:03 -0800253// JavaInfo contains information about a java module for use by modules that depend on it.
254type JavaInfo struct {
255 // HeaderJars is a list of jars that can be passed as the javac classpath in order to link
256 // against this module. If empty, ImplementationJars should be used instead.
257 HeaderJars android.Paths
258
Joe Onorato349ae8d2024-02-05 22:46:00 +0000259 RepackagedHeaderJars android.Paths
260
Sam Delmerico9f9c0a22022-11-29 11:19:37 -0500261 // set of header jars for all transitive libs deps
Colin Crossc85750b2022-04-21 12:50:51 -0700262 TransitiveLibsHeaderJars *android.DepSet[android.Path]
Sam Delmerico9f9c0a22022-11-29 11:19:37 -0500263
264 // set of header jars for all transitive static libs deps
Colin Crossc85750b2022-04-21 12:50:51 -0700265 TransitiveStaticLibsHeaderJars *android.DepSet[android.Path]
Sam Delmerico9f9c0a22022-11-29 11:19:37 -0500266
Colin Crossdcf71b22021-02-01 13:59:03 -0800267 // ImplementationAndResourceJars is a list of jars that contain the implementations of classes
268 // in the module as well as any resources included in the module.
269 ImplementationAndResourcesJars android.Paths
270
271 // ImplementationJars is a list of jars that contain the implementations of classes in the
Paul Duffin7ed6ff82022-11-21 10:57:30 +0000272 //module.
Colin Crossdcf71b22021-02-01 13:59:03 -0800273 ImplementationJars android.Paths
274
275 // ResourceJars is a list of jars that contain the resources included in the module.
276 ResourceJars android.Paths
277
278 // AidlIncludeDirs is a list of directories that should be passed to the aidl tool when
279 // depending on this module.
280 AidlIncludeDirs android.Paths
281
282 // SrcJarArgs is a list of arguments to pass to soong_zip to package the sources of this
283 // module.
284 SrcJarArgs []string
285
286 // SrcJarDeps is a list of paths to depend on when packaging the sources of this module.
287 SrcJarDeps android.Paths
288
Anton Hansson0e73f9e2023-09-20 13:39:57 +0000289 // The source files of this module and all its transitive static dependencies.
290 TransitiveSrcFiles *android.DepSet[android.Path]
291
Colin Crossdcf71b22021-02-01 13:59:03 -0800292 // ExportedPlugins is a list of paths that should be used as annotation processors for any
293 // module that depends on this module.
294 ExportedPlugins android.Paths
295
296 // ExportedPluginClasses is a list of classes that should be run as annotation processors for
297 // any module that depends on this module.
298 ExportedPluginClasses []string
299
300 // ExportedPluginDisableTurbine is true if this module's annotation processors generate APIs,
301 // requiring disbling turbine for any modules that depend on it.
302 ExportedPluginDisableTurbine bool
303
304 // JacocoReportClassesFile is the path to a jar containing uninstrumented classes that will be
305 // instrumented by jacoco.
306 JacocoReportClassesFile android.Path
Jihoon Kangfe914ed2024-02-12 22:49:21 +0000307
308 // StubsLinkType provides information about whether the provided jars are stub jars or
309 // implementation jars. If the provider is set by java_sdk_library, the link type is "unknown"
310 // and selection between the stub jar vs implementation jar is deferred to SdkLibrary.sdkJars(...)
311 StubsLinkType StubsLinkType
Colin Crossdcf71b22021-02-01 13:59:03 -0800312}
313
Colin Crossbc7d76c2023-12-12 16:39:03 -0800314var JavaInfoProvider = blueprint.NewProvider[JavaInfo]()
Colin Crossdcf71b22021-02-01 13:59:03 -0800315
Colin Cross75ce9ec2021-02-26 16:20:32 -0800316// SyspropPublicStubInfo contains info about the sysprop public stub library that corresponds to
317// the sysprop implementation library.
318type SyspropPublicStubInfo struct {
319 // JavaInfo is the JavaInfoProvider of the sysprop public stub library that corresponds to
320 // the sysprop implementation library.
321 JavaInfo JavaInfo
322}
323
Colin Crossbc7d76c2023-12-12 16:39:03 -0800324var SyspropPublicStubInfoProvider = blueprint.NewProvider[SyspropPublicStubInfo]()
Colin Cross75ce9ec2021-02-26 16:20:32 -0800325
Paul Duffin44b481b2020-06-17 16:59:43 +0100326// Methods that need to be implemented for a module that is added to apex java_libs property.
327type ApexDependency interface {
Nan Zhanged19fc32017-10-19 13:06:22 -0700328 HeaderJars() android.Paths
Paul Duffin44b481b2020-06-17 16:59:43 +0100329 ImplementationAndResourcesJars() android.Paths
330}
331
Ulya Trafimovich31e444e2020-08-14 17:32:16 +0100332// Provides build path and install path to DEX jars.
333type UsesLibraryDependency interface {
Spandan Das59a4a2b2024-01-09 21:35:56 +0000334 DexJarBuildPath(ctx android.ModuleErrorfContext) OptionalDexJarPath
Ulya Trafimovich9f3052c2020-06-09 14:31:19 +0100335 DexJarInstallPath() android.Path
Ulya Trafimovichdbf31662020-12-17 12:07:54 +0000336 ClassLoaderContexts() dexpreopt.ClassLoaderContextMap
Ulya Trafimovich31e444e2020-08-14 17:32:16 +0100337}
338
Jaewoong Jung26342642021-03-17 15:56:23 -0700339// TODO(jungjw): Move this to kythe.go once it's created.
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800340type xref interface {
341 XrefJavaFiles() android.Paths
342}
343
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800344func (j *Module) XrefJavaFiles() android.Paths {
345 return j.kytheFiles
346}
347
Yu Liu67a28422024-03-05 00:36:31 +0000348func (d dependencyTag) PropagateAconfigValidation() bool {
349 return d.static
350}
351
352var _ android.PropagateAconfigValidationDependencyTag = dependencyTag{}
353
Colin Crossbe1da472017-07-07 15:59:46 -0700354type dependencyTag struct {
355 blueprint.BaseDependencyTag
356 name string
Colin Cross65cb3142021-12-10 23:05:02 +0000357
358 // True if the dependency is relinked at runtime.
359 runtimeLinked bool
Colin Crossce564252022-01-12 11:13:32 -0800360
361 // True if the dependency is a toolchain, for example an annotation processor.
362 toolchain bool
Yu Liu67a28422024-03-05 00:36:31 +0000363
364 static bool
Colin Cross2fe66872015-03-30 17:20:39 -0700365}
366
Colin Crosse9fe2942020-11-10 18:12:15 -0800367// installDependencyTag is a dependency tag that is annotated to cause the installed files of the
368// dependency to be installed when the parent module is installed.
369type installDependencyTag struct {
370 blueprint.BaseDependencyTag
371 android.InstallAlwaysNeededDependencyTag
372 name string
373}
374
Colin Cross65cb3142021-12-10 23:05:02 +0000375func (d dependencyTag) LicenseAnnotations() []android.LicenseAnnotation {
376 if d.runtimeLinked {
377 return []android.LicenseAnnotation{android.LicenseAnnotationSharedDependency}
Colin Crossce564252022-01-12 11:13:32 -0800378 } else if d.toolchain {
379 return []android.LicenseAnnotation{android.LicenseAnnotationToolchain}
Colin Cross65cb3142021-12-10 23:05:02 +0000380 }
381 return nil
382}
383
384var _ android.LicenseAnnotationsDependencyTag = dependencyTag{}
385
Ulya Trafimovichb5218112020-10-07 15:11:32 +0100386type usesLibraryDependencyTag struct {
387 dependencyTag
Ulya Trafimovichf5d91bb2022-05-04 12:00:02 +0100388 sdkVersion int // SDK version in which the library appared as a standalone library.
389 optional bool // If the dependency is optional or required.
Ulya Trafimovichb5218112020-10-07 15:11:32 +0100390}
391
Ulya Trafimovichf5d91bb2022-05-04 12:00:02 +0100392func makeUsesLibraryDependencyTag(sdkVersion int, optional bool) usesLibraryDependencyTag {
Ulya Trafimovichb5218112020-10-07 15:11:32 +0100393 return usesLibraryDependencyTag{
Colin Cross65cb3142021-12-10 23:05:02 +0000394 dependencyTag: dependencyTag{
395 name: fmt.Sprintf("uses-library-%d", sdkVersion),
396 runtimeLinked: true,
397 },
398 sdkVersion: sdkVersion,
399 optional: optional,
Ulya Trafimovichb5218112020-10-07 15:11:32 +0100400 }
401}
402
Jiyong Park8be103b2019-11-08 15:53:48 +0900403func IsJniDepTag(depTag blueprint.DependencyTag) bool {
Colin Crossde78d132020-10-09 18:59:49 -0700404 return depTag == jniLibTag
Jiyong Park8be103b2019-11-08 15:53:48 +0900405}
406
Colin Crossbe1da472017-07-07 15:59:46 -0700407var (
Colin Cross75ce9ec2021-02-26 16:20:32 -0800408 dataNativeBinsTag = dependencyTag{name: "dataNativeBins"}
Sam Delmericob3342ce2022-01-20 21:10:28 +0000409 dataDeviceBinsTag = dependencyTag{name: "dataDeviceBins"}
Yu Liu67a28422024-03-05 00:36:31 +0000410 staticLibTag = dependencyTag{name: "staticlib", static: true}
Colin Cross65cb3142021-12-10 23:05:02 +0000411 libTag = dependencyTag{name: "javalib", runtimeLinked: true}
Liz Kammeref28a4c2022-09-23 16:50:56 -0400412 sdkLibTag = dependencyTag{name: "sdklib", runtimeLinked: true}
Colin Cross65cb3142021-12-10 23:05:02 +0000413 java9LibTag = dependencyTag{name: "java9lib", runtimeLinked: true}
Colin Crossce564252022-01-12 11:13:32 -0800414 pluginTag = dependencyTag{name: "plugin", toolchain: true}
415 errorpronePluginTag = dependencyTag{name: "errorprone-plugin", toolchain: true}
416 exportedPluginTag = dependencyTag{name: "exported-plugin", toolchain: true}
Colin Cross65cb3142021-12-10 23:05:02 +0000417 bootClasspathTag = dependencyTag{name: "bootclasspath", runtimeLinked: true}
418 systemModulesTag = dependencyTag{name: "system modules", runtimeLinked: true}
Colin Cross75ce9ec2021-02-26 16:20:32 -0800419 frameworkResTag = dependencyTag{name: "framework-res"}
Colin Cross65cb3142021-12-10 23:05:02 +0000420 kotlinStdlibTag = dependencyTag{name: "kotlin-stdlib", runtimeLinked: true}
421 kotlinAnnotationsTag = dependencyTag{name: "kotlin-annotations", runtimeLinked: true}
Colin Crossce564252022-01-12 11:13:32 -0800422 kotlinPluginTag = dependencyTag{name: "kotlin-plugin", toolchain: true}
Colin Cross75ce9ec2021-02-26 16:20:32 -0800423 proguardRaiseTag = dependencyTag{name: "proguard-raise"}
424 certificateTag = dependencyTag{name: "certificate"}
425 instrumentationForTag = dependencyTag{name: "instrumentation_for"}
Colin Crossce564252022-01-12 11:13:32 -0800426 extraLintCheckTag = dependencyTag{name: "extra-lint-check", toolchain: true}
Colin Cross65cb3142021-12-10 23:05:02 +0000427 jniLibTag = dependencyTag{name: "jnilib", runtimeLinked: true}
Sam Delmerico9f9c0a22022-11-29 11:19:37 -0500428 r8LibraryJarTag = dependencyTag{name: "r8-libraryjar", runtimeLinked: true}
Colin Cross75ce9ec2021-02-26 16:20:32 -0800429 syspropPublicStubDepTag = dependencyTag{name: "sysprop public stub"}
Jihoon Kang01e522c2023-03-14 01:09:34 +0000430 javaApiContributionTag = dependencyTag{name: "java-api-contribution"}
431 depApiSrcsTag = dependencyTag{name: "dep-api-srcs"}
Jihoon Kang84b25892023-12-01 22:01:06 +0000432 aconfigDeclarationTag = dependencyTag{name: "aconfig-declaration"}
Colin Cross75ce9ec2021-02-26 16:20:32 -0800433 jniInstallTag = installDependencyTag{name: "jni install"}
434 binaryInstallTag = installDependencyTag{name: "binary install"}
Ulya Trafimovichf5d91bb2022-05-04 12:00:02 +0100435 usesLibReqTag = makeUsesLibraryDependencyTag(dexpreopt.AnySdkVersion, false)
436 usesLibOptTag = makeUsesLibraryDependencyTag(dexpreopt.AnySdkVersion, true)
437 usesLibCompat28OptTag = makeUsesLibraryDependencyTag(28, true)
438 usesLibCompat29ReqTag = makeUsesLibraryDependencyTag(29, false)
439 usesLibCompat30OptTag = makeUsesLibraryDependencyTag(30, true)
Colin Crossbe1da472017-07-07 15:59:46 -0700440)
Colin Cross2fe66872015-03-30 17:20:39 -0700441
Jiyong Park83dc74b2020-01-14 18:38:44 +0900442func IsLibDepTag(depTag blueprint.DependencyTag) bool {
Liz Kammeref28a4c2022-09-23 16:50:56 -0400443 return depTag == libTag || depTag == sdkLibTag
Jiyong Park83dc74b2020-01-14 18:38:44 +0900444}
445
446func IsStaticLibDepTag(depTag blueprint.DependencyTag) bool {
447 return depTag == staticLibTag
448}
449
Colin Crossfc3674a2017-09-18 17:41:52 -0700450type sdkDep struct {
Pete Gilline3d44b22020-06-29 11:28:51 +0100451 useModule, useFiles, invalidVersion bool
Colin Cross47ff2522017-10-02 14:22:08 -0700452
Colin Cross6cef4812019-10-17 14:23:50 -0700453 // The modules that will be added to the bootclasspath when targeting 1.8 or lower
454 bootclasspath []string
Paul Duffine25c6442019-10-11 13:50:28 +0100455
456 // The default system modules to use. Will be an empty string if no system
457 // modules are to be used.
Colin Cross1369cdb2017-09-29 17:58:17 -0700458 systemModules string
459
Pete Gilline3d44b22020-06-29 11:28:51 +0100460 // The modules that will be added to the classpath regardless of the Java language level targeted
461 classpath []string
462
Colin Cross6cef4812019-10-17 14:23:50 -0700463 // The modules that will be added ot the classpath when targeting 1.9 or higher
Pete Gilline3d44b22020-06-29 11:28:51 +0100464 // (normally these will be on the bootclasspath when targeting 1.8 or lower)
Colin Cross6cef4812019-10-17 14:23:50 -0700465 java9Classpath []string
466
Colin Crossa97c5d32018-03-28 14:58:31 -0700467 frameworkResModule string
468
Colin Cross86a60ae2018-05-29 14:44:55 -0700469 jars android.Paths
Colin Cross3047fa22019-04-18 10:56:44 -0700470 aidl android.OptionalPath
Paul Duffin250e6192019-06-07 10:44:37 +0100471
472 noStandardLibs, noFrameworksLibs bool
473}
474
475func (s sdkDep) hasStandardLibs() bool {
476 return !s.noStandardLibs
477}
478
479func (s sdkDep) hasFrameworkLibs() bool {
480 return !s.noStandardLibs && !s.noFrameworksLibs
Colin Cross1369cdb2017-09-29 17:58:17 -0700481}
482
Colin Crossa4f08812018-10-02 22:03:40 -0700483type jniLib struct {
Colin Cross403cc152020-07-06 14:15:24 -0700484 name string
485 path android.Path
486 target android.Target
487 coverageFile android.OptionalPath
488 unstrippedFile android.Path
Jihoon Kangf78a8902022-09-01 22:47:07 +0000489 partition string
Colin Crossa4f08812018-10-02 22:03:40 -0700490}
491
Jiyong Parkf1691d22021-03-29 20:11:58 +0900492func sdkDeps(ctx android.BottomUpMutatorContext, sdkContext android.SdkContext, d dexer) {
Liz Kammerd6c31d22020-08-05 15:40:41 -0700493 sdkDep := decodeSdkDep(ctx, sdkContext)
494 if sdkDep.useModule {
495 ctx.AddVariationDependencies(nil, bootClasspathTag, sdkDep.bootclasspath...)
496 ctx.AddVariationDependencies(nil, java9LibTag, sdkDep.java9Classpath...)
Liz Kammeref28a4c2022-09-23 16:50:56 -0400497 ctx.AddVariationDependencies(nil, sdkLibTag, sdkDep.classpath...)
Liz Kammerd6c31d22020-08-05 15:40:41 -0700498 if d.effectiveOptimizeEnabled() && sdkDep.hasStandardLibs() {
Jihoon Kangb5078312023-03-29 23:25:49 +0000499 ctx.AddVariationDependencies(nil, proguardRaiseTag,
Jihoon Kang6c0df882023-06-14 22:43:25 +0000500 config.LegacyCorePlatformBootclasspathLibraries...,
Jihoon Kangb5078312023-03-29 23:25:49 +0000501 )
Liz Kammerd6c31d22020-08-05 15:40:41 -0700502 }
503 if d.effectiveOptimizeEnabled() && sdkDep.hasFrameworkLibs() {
504 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.FrameworkLibraries...)
505 }
506 }
507 if sdkDep.systemModules != "" {
508 ctx.AddVariationDependencies(nil, systemModulesTag, sdkDep.systemModules)
509 }
510}
511
Colin Cross32f676a2017-09-06 13:41:06 -0700512type deps struct {
Colin Cross9bb9bfb2022-03-17 11:12:32 -0700513 // bootClasspath is the list of jars that form the boot classpath (generally the java.* and
514 // android.* classes) for tools that still use it. javac targeting 1.9 or higher uses
515 // systemModules and java9Classpath instead.
516 bootClasspath classpath
517
518 // classpath is the list of jars that form the classpath for javac and kotlinc rules. It
519 // contains header jars for all static and non-static dependencies.
520 classpath classpath
521
522 // dexClasspath is the list of jars that form the classpath for d8 and r8 rules. It contains
523 // header jars for all non-static dependencies. Static dependencies have already been
524 // combined into the program jar.
525 dexClasspath classpath
526
527 // java9Classpath is the list of jars that will be added to the classpath when targeting
528 // 1.9 or higher. It generally contains the android.* classes, while the java.* classes
529 // are provided by systemModules.
530 java9Classpath classpath
531
Colin Cross748b2d82020-11-19 13:52:06 -0800532 processorPath classpath
533 errorProneProcessorPath classpath
534 processorClasses []string
535 staticJars android.Paths
536 staticHeaderJars android.Paths
537 staticResourceJars android.Paths
538 aidlIncludeDirs android.Paths
539 srcs android.Paths
540 srcJars android.Paths
541 systemModules *systemModules
542 aidlPreprocess android.OptionalPath
543 kotlinStdlib android.Paths
544 kotlinAnnotations android.Paths
Colin Crossa1ff7c62021-09-17 14:11:52 -0700545 kotlinPlugins android.Paths
Jihoon Kang6592e872023-12-19 01:13:16 +0000546 aconfigProtoFiles android.Paths
Colin Crossbe9cdb82019-01-21 21:37:16 -0800547
548 disableTurbine bool
Colin Cross32f676a2017-09-06 13:41:06 -0700549}
Colin Cross2fe66872015-03-30 17:20:39 -0700550
Colin Cross54250902017-12-05 09:28:08 -0800551func checkProducesJars(ctx android.ModuleContext, dep android.SourceFileProducer) {
552 for _, f := range dep.Srcs() {
553 if f.Ext() != ".jar" {
554 ctx.ModuleErrorf("genrule %q must generate files ending with .jar to be used as a libs or static_libs dependency",
555 ctx.OtherModuleName(dep.(blueprint.Module)))
556 }
557 }
558}
559
Jiyong Parkf1691d22021-03-29 20:11:58 +0900560func getJavaVersion(ctx android.ModuleContext, javaVersion string, sdkContext android.SdkContext) javaVersion {
Nan Zhang357466b2018-04-17 17:38:36 -0700561 if javaVersion != "" {
Colin Cross1e743852019-10-28 11:37:20 -0700562 return normalizeJavaVersion(ctx, javaVersion)
Colin Cross17dec172020-05-14 18:05:32 -0700563 } else if ctx.Device() {
Jiyong Park92315372021-04-02 08:45:46 +0900564 return defaultJavaLanguageVersion(ctx, sdkContext.SdkVersion(ctx))
Sorin Basca384250c2023-02-02 17:56:19 +0000565 } else {
Sorin Bascabe302732023-02-15 17:52:27 +0000566 return JAVA_VERSION_17
Nan Zhang357466b2018-04-17 17:38:36 -0700567 }
Nan Zhang357466b2018-04-17 17:38:36 -0700568}
569
Jihoon Kangff878bf2022-12-22 21:26:06 +0000570// Java version for stubs generation
571func getStubsJavaVersion() javaVersion {
572 return JAVA_VERSION_8
573}
574
Colin Cross1e743852019-10-28 11:37:20 -0700575type javaVersion int
576
577const (
578 JAVA_VERSION_UNSUPPORTED = 0
579 JAVA_VERSION_6 = 6
580 JAVA_VERSION_7 = 7
581 JAVA_VERSION_8 = 8
582 JAVA_VERSION_9 = 9
Sorin Bascac0244da2021-11-26 17:26:33 +0000583 JAVA_VERSION_11 = 11
Sorin Bascace720c32022-05-24 12:13:50 +0100584 JAVA_VERSION_17 = 17
Colin Cross1e743852019-10-28 11:37:20 -0700585)
586
587func (v javaVersion) String() string {
588 switch v {
589 case JAVA_VERSION_6:
Sorin Bascad567a512024-01-15 16:38:46 +0000590 // Java version 1.6 no longer supported, bumping to 1.8
591 return "1.8"
Colin Cross1e743852019-10-28 11:37:20 -0700592 case JAVA_VERSION_7:
Sorin Bascad567a512024-01-15 16:38:46 +0000593 // Java version 1.7 no longer supported, bumping to 1.8
594 return "1.8"
Colin Cross1e743852019-10-28 11:37:20 -0700595 case JAVA_VERSION_8:
596 return "1.8"
597 case JAVA_VERSION_9:
598 return "1.9"
Sorin Bascac0244da2021-11-26 17:26:33 +0000599 case JAVA_VERSION_11:
600 return "11"
Sorin Bascace720c32022-05-24 12:13:50 +0100601 case JAVA_VERSION_17:
602 return "17"
Colin Cross1e743852019-10-28 11:37:20 -0700603 default:
604 return "unsupported"
605 }
606}
607
Cole Faustd96eebf2022-06-28 14:41:27 -0700608func (v javaVersion) StringForKotlinc() string {
609 // $ ./external/kotlinc/bin/kotlinc -jvm-target foo
610 // error: unknown JVM target version: foo
Sorin Bascad567a512024-01-15 16:38:46 +0000611 // Supported versions: 1.8, 9, 10, 11, 12, 13, 14, 15, 16, 17
Cole Faustd96eebf2022-06-28 14:41:27 -0700612 switch v {
Sorin Bascad567a512024-01-15 16:38:46 +0000613 case JAVA_VERSION_6:
614 return "1.8"
Cole Faustd96eebf2022-06-28 14:41:27 -0700615 case JAVA_VERSION_7:
Sorin Bascad567a512024-01-15 16:38:46 +0000616 return "1.8"
Cole Faustd96eebf2022-06-28 14:41:27 -0700617 case JAVA_VERSION_9:
618 return "9"
619 default:
620 return v.String()
621 }
622}
623
Colin Cross1e743852019-10-28 11:37:20 -0700624// Returns true if javac targeting this version uses system modules instead of a bootclasspath.
625func (v javaVersion) usesJavaModules() bool {
626 return v >= 9
627}
628
629func normalizeJavaVersion(ctx android.BaseModuleContext, javaVersion string) javaVersion {
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100630 switch javaVersion {
631 case "1.6", "6":
Sorin Bascad567a512024-01-15 16:38:46 +0000632 // Java version 1.6 no longer supported, bumping to 1.8
633 return JAVA_VERSION_8
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100634 case "1.7", "7":
Sorin Bascad567a512024-01-15 16:38:46 +0000635 // Java version 1.7 no longer supported, bumping to 1.8
636 return JAVA_VERSION_8
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100637 case "1.8", "8":
Colin Cross1e743852019-10-28 11:37:20 -0700638 return JAVA_VERSION_8
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100639 case "1.9", "9":
Colin Cross1e743852019-10-28 11:37:20 -0700640 return JAVA_VERSION_9
Sorin Bascac0244da2021-11-26 17:26:33 +0000641 case "11":
642 return JAVA_VERSION_11
Sorin Bascace720c32022-05-24 12:13:50 +0100643 case "17":
Sorin Basca5938fed2022-06-22 12:53:51 +0100644 return JAVA_VERSION_17
Sorin Bascace720c32022-05-24 12:13:50 +0100645 case "10", "12", "13", "14", "15", "16":
646 ctx.PropertyErrorf("java_version", "Java language level %s is not supported", javaVersion)
Colin Cross1e743852019-10-28 11:37:20 -0700647 return JAVA_VERSION_UNSUPPORTED
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100648 default:
649 ctx.PropertyErrorf("java_version", "Unrecognized Java language level")
Colin Cross1e743852019-10-28 11:37:20 -0700650 return JAVA_VERSION_UNSUPPORTED
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100651 }
652}
653
Colin Cross2fe66872015-03-30 17:20:39 -0700654//
655// Java libraries (.jar file)
656//
657
Colin Crossf506d872017-07-19 15:53:04 -0700658type Library struct {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700659 Module
Colin Crossf0f2e2c2019-10-15 16:36:40 -0700660
Colin Cross312634e2023-11-21 15:13:56 -0800661 combinedExportedProguardFlagsFile android.Path
Jared Duke5979b302022-12-19 21:08:39 +0000662
Colin Cross09ad3a62023-11-15 12:29:33 -0800663 InstallMixin func(ctx android.ModuleContext, installPath android.Path) (extraInstallDeps android.InstallPaths)
Colin Cross2fe66872015-03-30 17:20:39 -0700664}
665
Jiyong Park45bf82e2020-12-15 22:29:02 +0900666var _ android.ApexModule = (*Library)(nil)
667
satayevd604b212021-07-21 14:23:52 +0100668// Provides access to the list of permitted packages from apex boot jars.
Paul Duffine739f1e2020-05-29 11:24:51 +0100669type PermittedPackagesForUpdatableBootJars interface {
670 PermittedPackagesForUpdatableBootJars() []string
671}
672
673var _ PermittedPackagesForUpdatableBootJars = (*Library)(nil)
674
675func (j *Library) PermittedPackagesForUpdatableBootJars() []string {
676 return j.properties.Permitted_packages
677}
678
Spandan Dase21a8d42024-01-23 23:56:29 +0000679func shouldUncompressDex(ctx android.ModuleContext, libName string, dexpreopter *dexpreopter) bool {
Ulya Trafimovichf491dde2020-01-24 12:19:45 +0000680 // Store uncompressed (and aligned) any dex files from jars in APEXes.
Colin Crossff694a82023-12-13 15:54:49 -0800681 if apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider); !apexInfo.IsForPlatform() {
Ulya Trafimovichf491dde2020-01-24 12:19:45 +0000682 return true
683 }
684
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +0000685 // Store uncompressed (and do not strip) dex files from boot class path jars.
686 if inList(ctx.ModuleName(), ctx.Config().BootJars()) {
687 return true
688 }
689
Jared Dukea561efb2024-02-09 18:45:24 +0000690 // Store uncompressed dex files that are preopted on /system or /system_other.
691 if !dexpreopter.dexpreoptDisabled(ctx, libName) {
Vladimir Markoe8b00d62018-12-21 15:54:16 +0000692 return true
693 }
Jared Dukea561efb2024-02-09 18:45:24 +0000694
Colin Cross083a2aa2019-02-06 16:37:12 -0800695 if ctx.Config().UncompressPrivAppDex() &&
696 inList(ctx.ModuleName(), ctx.Config().ModulesLoadedByPrivilegedModules()) {
697 return true
698 }
699
Colin Cross2fc72f62018-12-21 12:59:54 -0800700 return false
701}
702
Jiakai Zhang22450f22021-10-11 03:05:20 +0000703// Sets `dexer.dexProperties.Uncompress_dex` to the proper value.
704func setUncompressDex(ctx android.ModuleContext, dexpreopter *dexpreopter, dexer *dexer) {
705 if dexer.dexProperties.Uncompress_dex == nil {
706 // If the value was not force-set by the user, use reasonable default based on the module.
Spandan Dase21a8d42024-01-23 23:56:29 +0000707 dexer.dexProperties.Uncompress_dex = proptools.BoolPtr(shouldUncompressDex(ctx, android.RemoveOptionalPrebuiltPrefix(ctx.ModuleName()), dexpreopter))
Jiakai Zhang22450f22021-10-11 03:05:20 +0000708 }
709}
710
Spandan Das8469e932024-02-20 16:47:07 +0000711// list of java_library modules that set platform_apis: true
712// this property is a no-op for java_library
713// TODO (b/215379393): Remove this allowlist
714var (
715 aospPlatformApiAllowlist = map[string]bool{
716 "adservices-test-scenarios": true,
717 "aidl-cpp-java-test-interface-java": true,
718 "aidl-test-extras-java": true,
719 "aidl-test-interface-java": true,
720 "aidl-test-interface-permission-java": true,
721 "aidl_test_java_client_permission": true,
722 "aidl_test_java_client_sdk1": true,
723 "aidl_test_java_client_sdk29": true,
724 "aidl_test_java_client": true,
725 "aidl_test_java_service_permission": true,
726 "aidl_test_java_service_sdk1": true,
727 "aidl_test_java_service_sdk29": true,
728 "aidl_test_java_service": true,
729 "aidl_test_loggable_interface-java": true,
730 "aidl_test_nonvintf_parcelable-V1-java": true,
731 "aidl_test_nonvintf_parcelable-V2-java": true,
732 "aidl_test_unstable_parcelable-java": true,
733 "aidl_test_vintf_parcelable-V1-java": true,
734 "aidl_test_vintf_parcelable-V2-java": true,
735 "android.aidl.test.trunk-V1-java": true,
736 "android.aidl.test.trunk-V2-java": true,
737 "android.frameworks.location.altitude-V1-java": true,
738 "android.frameworks.location.altitude-V2-java": true,
739 "android.frameworks.stats-V1-java": true,
740 "android.frameworks.stats-V2-java": true,
741 "android.frameworks.stats-V3-java": true,
742 "android.hardware.authsecret-V1-java": true,
743 "android.hardware.authsecret-V2-java": true,
744 "android.hardware.biometrics.common-V1-java": true,
745 "android.hardware.biometrics.common-V2-java": true,
746 "android.hardware.biometrics.common-V3-java": true,
747 "android.hardware.biometrics.common-V4-java": true,
748 "android.hardware.biometrics.face-V1-java": true,
749 "android.hardware.biometrics.face-V2-java": true,
750 "android.hardware.biometrics.face-V3-java": true,
751 "android.hardware.biometrics.face-V4-java": true,
752 "android.hardware.biometrics.fingerprint-V1-java": true,
753 "android.hardware.biometrics.fingerprint-V2-java": true,
754 "android.hardware.biometrics.fingerprint-V3-java": true,
755 "android.hardware.biometrics.fingerprint-V4-java": true,
756 "android.hardware.bluetooth.lmp_event-V1-java": true,
757 "android.hardware.confirmationui-V1-java": true,
758 "android.hardware.confirmationui-V2-java": true,
759 "android.hardware.gatekeeper-V1-java": true,
760 "android.hardware.gatekeeper-V2-java": true,
761 "android.hardware.gnss-V1-java": true,
762 "android.hardware.gnss-V2-java": true,
763 "android.hardware.gnss-V3-java": true,
764 "android.hardware.gnss-V4-java": true,
765 "android.hardware.graphics.common-V1-java": true,
766 "android.hardware.graphics.common-V2-java": true,
767 "android.hardware.graphics.common-V3-java": true,
768 "android.hardware.graphics.common-V4-java": true,
769 "android.hardware.graphics.common-V5-java": true,
770 "android.hardware.identity-V1-java": true,
771 "android.hardware.identity-V2-java": true,
772 "android.hardware.identity-V3-java": true,
773 "android.hardware.identity-V4-java": true,
774 "android.hardware.identity-V5-java": true,
775 "android.hardware.identity-V6-java": true,
776 "android.hardware.keymaster-V1-java": true,
777 "android.hardware.keymaster-V2-java": true,
778 "android.hardware.keymaster-V3-java": true,
779 "android.hardware.keymaster-V4-java": true,
780 "android.hardware.keymaster-V5-java": true,
781 "android.hardware.oemlock-V1-java": true,
782 "android.hardware.oemlock-V2-java": true,
783 "android.hardware.power.stats-V1-java": true,
784 "android.hardware.power.stats-V2-java": true,
785 "android.hardware.power.stats-V3-java": true,
786 "android.hardware.power-V1-java": true,
787 "android.hardware.power-V2-java": true,
788 "android.hardware.power-V3-java": true,
789 "android.hardware.power-V4-java": true,
790 "android.hardware.power-V5-java": true,
791 "android.hardware.rebootescrow-V1-java": true,
792 "android.hardware.rebootescrow-V2-java": true,
793 "android.hardware.security.authgraph-V1-java": true,
794 "android.hardware.security.keymint-V1-java": true,
795 "android.hardware.security.keymint-V2-java": true,
796 "android.hardware.security.keymint-V3-java": true,
797 "android.hardware.security.keymint-V4-java": true,
798 "android.hardware.security.secureclock-V1-java": true,
799 "android.hardware.security.secureclock-V2-java": true,
800 "android.hardware.thermal-V1-java": true,
801 "android.hardware.thermal-V2-java": true,
802 "android.hardware.threadnetwork-V1-java": true,
803 "android.hardware.weaver-V1-java": true,
804 "android.hardware.weaver-V2-java": true,
805 "android.hardware.weaver-V3-java": true,
806 "android.security.attestationmanager-java": true,
807 "android.security.authorization-java": true,
808 "android.security.compat-java": true,
809 "android.security.legacykeystore-java": true,
810 "android.security.maintenance-java": true,
811 "android.security.metrics-java": true,
812 "android.system.keystore2-V1-java": true,
813 "android.system.keystore2-V2-java": true,
814 "android.system.keystore2-V3-java": true,
815 "android.system.keystore2-V4-java": true,
816 "binderReadParcelIface-java": true,
817 "binderRecordReplayTestIface-java": true,
818 "car-experimental-api-static-lib": true,
819 "collector-device-lib-platform": true,
820 "com.android.car.oem": true,
821 "com.google.hardware.pixel.display-V10-java": true,
822 "com.google.hardware.pixel.display-V1-java": true,
823 "com.google.hardware.pixel.display-V2-java": true,
824 "com.google.hardware.pixel.display-V3-java": true,
825 "com.google.hardware.pixel.display-V4-java": true,
826 "com.google.hardware.pixel.display-V5-java": true,
827 "com.google.hardware.pixel.display-V6-java": true,
828 "com.google.hardware.pixel.display-V7-java": true,
829 "com.google.hardware.pixel.display-V8-java": true,
830 "com.google.hardware.pixel.display-V9-java": true,
831 "conscrypt-support": true,
832 "cts-keystore-test-util": true,
833 "cts-keystore-user-auth-helper-library": true,
834 "ctsmediautil": true,
835 "CtsNetTestsNonUpdatableLib": true,
836 "DpmWrapper": true,
837 "flickerlib-apphelpers": true,
838 "flickerlib-helpers": true,
839 "flickerlib-parsers": true,
840 "flickerlib": true,
841 "hardware.google.bluetooth.ccc-V1-java": true,
842 "hardware.google.bluetooth.sar-V1-java": true,
843 "monet": true,
844 "pixel-power-ext-V1-java": true,
845 "pixel-power-ext-V2-java": true,
846 "pixel_stateresidency_provider_aidl_interface-java": true,
847 "pixel-thermal-ext-V1-java": true,
848 "protolog-lib": true,
849 "RkpRegistrationCheck": true,
850 "rotary-service-javastream-protos": true,
851 "service_based_camera_extensions": true,
852 "statsd-helper-test": true,
853 "statsd-helper": true,
854 "test-piece-2-V1-java": true,
855 "test-piece-2-V2-java": true,
856 "test-piece-3-V1-java": true,
857 "test-piece-3-V2-java": true,
858 "test-piece-3-V3-java": true,
859 "test-piece-4-V1-java": true,
860 "test-piece-4-V2-java": true,
861 "test-root-package-V1-java": true,
862 "test-root-package-V2-java": true,
863 "test-root-package-V3-java": true,
864 "test-root-package-V4-java": true,
865 "testServiceIface-java": true,
866 "wm-flicker-common-app-helpers": true,
867 "wm-flicker-common-assertions": true,
868 "wm-shell-flicker-utils": true,
869 "wycheproof-keystore": true,
870 }
Paul Duffin3f1ae0b2022-07-27 16:27:42 +0000871
Spandan Das8469e932024-02-20 16:47:07 +0000872 // Union of aosp and internal allowlists
873 PlatformApiAllowlist = map[string]bool{}
874)
875
876func init() {
877 for k, v := range aospPlatformApiAllowlist {
878 PlatformApiAllowlist[k] = v
879 }
880}
881
882func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffin3f1ae0b2022-07-27 16:27:42 +0000883 j.provideHiddenAPIPropertyInfo(ctx)
884
Jiyong Park92315372021-04-02 08:45:46 +0900885 j.sdkVersion = j.SdkVersion(ctx)
886 j.minSdkVersion = j.MinSdkVersion(ctx)
satayev0a420e72021-11-29 17:25:52 +0000887 j.maxSdkVersion = j.MaxSdkVersion(ctx)
Jiyong Park92315372021-04-02 08:45:46 +0900888
Jihoon Kangfe914ed2024-02-12 22:49:21 +0000889 // SdkLibrary.GenerateAndroidBuildActions(ctx) sets the stubsLinkType to Unknown.
890 // If the stubsLinkType has already been set to Unknown, the stubsLinkType should
891 // not be overridden.
892 if j.stubsLinkType != Unknown {
893 if proptools.Bool(j.properties.Is_stubs_module) {
894 j.stubsLinkType = Stubs
895 } else {
896 j.stubsLinkType = Implementation
897 }
898 }
899
Jihoon Kang1bfb6f22023-07-01 00:13:47 +0000900 j.stem = proptools.StringDefault(j.overridableDeviceProperties.Stem, ctx.ModuleName())
901
Sam Delmericoc8e040c2023-10-31 17:27:02 +0000902 proguardSpecInfo := j.collectProguardSpecInfo(ctx)
Colin Cross40213022023-12-13 15:19:49 -0800903 android.SetProvider(ctx, ProguardSpecInfoProvider, proguardSpecInfo)
Colin Cross312634e2023-11-21 15:13:56 -0800904 exportedProguardFlagsFiles := proguardSpecInfo.ProguardFlagsFiles.ToList()
905 j.extraProguardFlagsFiles = append(j.extraProguardFlagsFiles, exportedProguardFlagsFiles...)
906
907 combinedExportedProguardFlagFile := android.PathForModuleOut(ctx, "export_proguard_flags")
908 writeCombinedProguardFlagsFile(ctx, combinedExportedProguardFlagFile, exportedProguardFlagsFiles)
909 j.combinedExportedProguardFlagsFile = combinedExportedProguardFlagFile
Sam Delmericoc8e040c2023-10-31 17:27:02 +0000910
Colin Crossff694a82023-12-13 15:54:49 -0800911 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
Colin Cross56a83212020-09-15 18:30:11 -0700912 if !apexInfo.IsForPlatform() {
913 j.hideApexVariantFromMake = true
914 }
915
Artur Satayev2db1c3f2020-04-08 19:09:30 +0100916 j.checkSdkVersions(ctx)
Mark Whitea15790a2023-08-22 21:28:11 +0000917 j.checkHeadersOnly(ctx)
Colin Cross61df14a2021-12-01 13:04:46 -0800918 if ctx.Device() {
919 j.dexpreopter.installPath = j.dexpreopter.getInstallPath(
Spandan Dase21a8d42024-01-23 23:56:29 +0000920 ctx, j.Name(), android.PathForModuleInstall(ctx, "framework", j.Stem()+".jar"))
Colin Cross61df14a2021-12-01 13:04:46 -0800921 j.dexpreopter.isSDKLibrary = j.deviceProperties.IsSDKLibrary
922 setUncompressDex(ctx, &j.dexpreopter, &j.dexer)
923 j.dexpreopter.uncompressedDex = *j.dexProperties.Uncompress_dex
924 j.classLoaderContexts = j.usesLibrary.classLoaderContextForUsesLibDeps(ctx)
Spandan Das0727ba72024-02-13 16:37:43 +0000925 if j.usesLibrary.shouldDisableDexpreopt {
926 j.dexpreopter.disableDexpreopt()
927 }
Colin Cross61df14a2021-12-01 13:04:46 -0800928 }
Colin Cross4eae06d2023-06-20 22:40:02 -0700929 j.compile(ctx, nil, nil, nil)
Colin Crossb7a63242015-04-16 14:09:14 -0700930
Colin Cross56a83212020-09-15 18:30:11 -0700931 exclusivelyForApex := !apexInfo.IsForPlatform()
Jiyong Park7f7766d2019-07-25 22:02:35 +0900932 if (Bool(j.properties.Installable) || ctx.Host()) && !exclusivelyForApex {
Colin Cross09ad3a62023-11-15 12:29:33 -0800933 var extraInstallDeps android.InstallPaths
Colin Crossf0f2e2c2019-10-15 16:36:40 -0700934 if j.InstallMixin != nil {
935 extraInstallDeps = j.InstallMixin(ctx, j.outputFile)
936 }
Colin Cross1d0eb7a2021-11-03 14:08:20 -0700937 hostDexNeeded := Bool(j.deviceProperties.Hostdex) && !ctx.Host()
938 if hostDexNeeded {
Colin Cross3108ce12021-11-10 14:38:50 -0800939 j.hostdexInstallFile = ctx.InstallFile(
940 android.PathForHostDexInstall(ctx, "framework"),
Colin Cross1d0eb7a2021-11-03 14:08:20 -0700941 j.Stem()+"-hostdex.jar", j.outputFile)
942 }
943 var installDir android.InstallPath
944 if ctx.InstallInTestcases() {
945 var archDir string
946 if !ctx.Host() {
947 archDir = ctx.DeviceConfig().DeviceArch()
948 }
949 installDir = android.PathForModuleInstall(ctx, ctx.ModuleName(), archDir)
950 } else {
951 installDir = android.PathForModuleInstall(ctx, "framework")
952 }
953 j.installFile = ctx.InstallFile(installDir, j.Stem()+".jar", j.outputFile, extraInstallDeps...)
Colin Cross2c429dc2017-08-31 16:45:16 -0700954 }
Colin Crossb7a63242015-04-16 14:09:14 -0700955}
956
Colin Crossf506d872017-07-19 15:53:04 -0700957func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700958 j.deps(ctx)
Ulya Trafimoviche4432872021-08-18 16:57:11 +0100959 j.usesLibrary.deps(ctx, false)
Colin Cross46c9b8b2017-06-22 16:51:17 -0700960}
961
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000962const (
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000963 aidlIncludeDir = "aidl"
964 javaDir = "java"
965 jarFileSuffix = ".jar"
966 testConfigSuffix = "-AndroidTest.xml"
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000967)
968
Paul Duffina0dbf432019-12-05 11:25:53 +0000969// path to the jar file of a java library. Relative to <sdk_root>/<api_dir>
Paul Duffin13648912022-07-15 13:12:35 +0000970func sdkSnapshotFilePathForJar(_ android.SdkMemberContext, osPrefix, name string) string {
Paul Duffina04c1072020-03-02 10:16:35 +0000971 return sdkSnapshotFilePathForMember(osPrefix, name, jarFileSuffix)
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000972}
973
Paul Duffina04c1072020-03-02 10:16:35 +0000974func sdkSnapshotFilePathForMember(osPrefix, name string, suffix string) string {
975 return filepath.Join(javaDir, osPrefix, name+suffix)
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000976}
977
Paul Duffin13879572019-11-28 14:31:38 +0000978type librarySdkMemberType struct {
Paul Duffin255f18e2019-12-13 11:22:16 +0000979 android.SdkMemberTypeBase
Paul Duffinf5c0a9c2020-02-28 14:39:53 +0000980
981 // Function to retrieve the appropriate output jar (implementation or header) from
Paul Duffin7ed6ff82022-11-21 10:57:30 +0000982 // the library.
Paul Duffindb170e42020-12-08 17:48:25 +0000983 jarToExportGetter func(ctx android.SdkMemberContext, j *Library) android.Path
984
985 // Function to compute the snapshot relative path to which the named library's
986 // jar should be copied.
Paul Duffin13648912022-07-15 13:12:35 +0000987 snapshotPathGetter func(ctx android.SdkMemberContext, osPrefix, name string) string
Paul Duffindb170e42020-12-08 17:48:25 +0000988
989 // True if only the jar should be copied to the snapshot, false if the jar plus any additional
990 // files like aidl files should also be copied.
991 onlyCopyJarToSnapshot bool
Paul Duffin13879572019-11-28 14:31:38 +0000992}
993
Paul Duffindb170e42020-12-08 17:48:25 +0000994const (
995 onlyCopyJarToSnapshot = true
996 copyEverythingToSnapshot = false
997)
998
Paul Duffin296701e2021-07-14 10:29:36 +0100999func (mt *librarySdkMemberType) AddDependencies(ctx android.SdkDependencyContext, dependencyTag blueprint.DependencyTag, names []string) {
1000 ctx.AddVariationDependencies(nil, dependencyTag, names...)
Paul Duffin13879572019-11-28 14:31:38 +00001001}
1002
1003func (mt *librarySdkMemberType) IsInstance(module android.Module) bool {
1004 _, ok := module.(*Library)
1005 return ok
1006}
1007
Paul Duffin3a4eb502020-03-19 16:11:18 +00001008func (mt *librarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
1009 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_import")
Paul Duffin14eb4672020-03-02 11:33:02 +00001010}
Paul Duffina0dbf432019-12-05 11:25:53 +00001011
Paul Duffin14eb4672020-03-02 11:33:02 +00001012func (mt *librarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
Paul Duffina551a1c2020-03-17 21:04:24 +00001013 return &librarySdkMemberProperties{}
Paul Duffin14eb4672020-03-02 11:33:02 +00001014}
1015
1016type librarySdkMemberProperties struct {
1017 android.SdkMemberPropertiesBase
1018
Paul Duffin7ed6ff82022-11-21 10:57:30 +00001019 JarToExport android.Path `android:"arch_variant"`
Paul Duffina551a1c2020-03-17 21:04:24 +00001020 AidlIncludeDirs android.Paths
Paul Duffin869de142021-07-15 14:14:41 +01001021
1022 // The list of permitted packages that need to be passed to the prebuilts as they are used to
1023 // create the updatable-bcp-packages.txt file.
1024 PermittedPackages []string
Paul Duffinbb638eb2022-11-26 13:36:38 +00001025
1026 // The value of the min_sdk_version property, translated into a number where possible.
1027 MinSdkVersion *string `supported_build_releases:"Tiramisu+"`
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08001028
1029 DexPreoptProfileGuided *bool `supported_build_releases:"UpsideDownCake+"`
Paul Duffin14eb4672020-03-02 11:33:02 +00001030}
1031
Paul Duffin3a4eb502020-03-19 16:11:18 +00001032func (p *librarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
Paul Duffin13879572019-11-28 14:31:38 +00001033 j := variant.(*Library)
1034
Paul Duffin7ed6ff82022-11-21 10:57:30 +00001035 p.JarToExport = ctx.MemberType().(*librarySdkMemberType).jarToExportGetter(ctx, j)
Paul Duffindb170e42020-12-08 17:48:25 +00001036
Paul Duffina551a1c2020-03-17 21:04:24 +00001037 p.AidlIncludeDirs = j.AidlIncludeDirs()
Paul Duffin869de142021-07-15 14:14:41 +01001038
1039 p.PermittedPackages = j.PermittedPackagesForUpdatableBootJars()
Paul Duffinbb638eb2022-11-26 13:36:38 +00001040
1041 // If the min_sdk_version was set then add the canonical representation of the API level to the
1042 // snapshot.
1043 if j.deviceProperties.Min_sdk_version != nil {
Cole Faust34867402023-04-28 12:32:27 -07001044 canonical, err := android.ReplaceFinalizedCodenames(ctx.SdkModuleContext().Config(), j.minSdkVersion.String())
1045 if err != nil {
1046 ctx.ModuleErrorf("%s", err)
1047 }
Paul Duffinbb638eb2022-11-26 13:36:38 +00001048 p.MinSdkVersion = proptools.StringPtr(canonical)
1049 }
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08001050
1051 if j.dexpreopter.dexpreoptProperties.Dex_preopt_result.Profile_guided {
1052 p.DexPreoptProfileGuided = proptools.BoolPtr(true)
1053 }
Paul Duffin14eb4672020-03-02 11:33:02 +00001054}
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001055
Paul Duffin3a4eb502020-03-19 16:11:18 +00001056func (p *librarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffina551a1c2020-03-17 21:04:24 +00001057 builder := ctx.SnapshotBuilder()
Paul Duffin3a4eb502020-03-19 16:11:18 +00001058
Paul Duffindb170e42020-12-08 17:48:25 +00001059 memberType := ctx.MemberType().(*librarySdkMemberType)
1060
Paul Duffina551a1c2020-03-17 21:04:24 +00001061 exportedJar := p.JarToExport
1062 if exportedJar != nil {
Paul Duffindb170e42020-12-08 17:48:25 +00001063 // Delegate the creation of the snapshot relative path to the member type.
Paul Duffin13648912022-07-15 13:12:35 +00001064 snapshotRelativeJavaLibPath := memberType.snapshotPathGetter(ctx, p.OsPrefix(), ctx.Name())
Paul Duffindb170e42020-12-08 17:48:25 +00001065
1066 // Copy the exported jar to the snapshot.
Paul Duffin14eb4672020-03-02 11:33:02 +00001067 builder.CopyToSnapshot(exportedJar, snapshotRelativeJavaLibPath)
1068
Paul Duffina551a1c2020-03-17 21:04:24 +00001069 propertySet.AddProperty("jars", []string{snapshotRelativeJavaLibPath})
1070 }
1071
Paul Duffinbb638eb2022-11-26 13:36:38 +00001072 if p.MinSdkVersion != nil {
1073 propertySet.AddProperty("min_sdk_version", *p.MinSdkVersion)
1074 }
1075
Paul Duffin869de142021-07-15 14:14:41 +01001076 if len(p.PermittedPackages) > 0 {
1077 propertySet.AddProperty("permitted_packages", p.PermittedPackages)
1078 }
1079
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08001080 dexPreoptSet := propertySet.AddPropertySet("dex_preopt")
1081 if p.DexPreoptProfileGuided != nil {
1082 dexPreoptSet.AddProperty("profile_guided", proptools.Bool(p.DexPreoptProfileGuided))
1083 }
1084
Paul Duffindb170e42020-12-08 17:48:25 +00001085 // Do not copy anything else to the snapshot.
1086 if memberType.onlyCopyJarToSnapshot {
1087 return
1088 }
1089
Paul Duffina551a1c2020-03-17 21:04:24 +00001090 aidlIncludeDirs := p.AidlIncludeDirs
1091 if len(aidlIncludeDirs) != 0 {
1092 sdkModuleContext := ctx.SdkModuleContext()
1093 for _, dir := range aidlIncludeDirs {
Paul Duffin14eb4672020-03-02 11:33:02 +00001094 // TODO(jiyong): copy parcelable declarations only
1095 aidlFiles, _ := sdkModuleContext.GlobWithDeps(dir.String()+"/**/*.aidl", nil)
1096 for _, file := range aidlFiles {
1097 builder.CopyToSnapshot(android.PathForSource(sdkModuleContext, file), filepath.Join(aidlIncludeDir, file))
1098 }
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001099 }
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001100
Paul Duffina551a1c2020-03-17 21:04:24 +00001101 // TODO(b/151933053) - add aidl include dirs property
Paul Duffin14eb4672020-03-02 11:33:02 +00001102 }
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001103}
1104
Colin Cross1b16b0e2019-02-12 14:41:32 -08001105// java_library builds and links sources into a `.jar` file for the device, and possibly for the host as well.
1106//
1107// By default, a java_library has a single variant that produces a `.jar` file containing `.class` files that were
1108// compiled against the device bootclasspath. This jar is not suitable for installing on a device, but can be used
1109// as a `static_libs` dependency of another module.
1110//
1111// Specifying `installable: true` will product a `.jar` file containing `classes.dex` files, suitable for installing on
1112// a device.
1113//
1114// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1115// compiled against the host bootclasspath.
Colin Cross9ae1b922018-06-26 17:59:05 -07001116func LibraryFactory() android.Module {
1117 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001118
Colin Crossce6734e2020-06-15 16:09:53 -07001119 module.addHostAndDeviceProperties()
Colin Cross2fe66872015-03-30 17:20:39 -07001120
Paul Duffin71b33cc2021-06-23 11:39:47 +01001121 module.initModuleAndImport(module)
Paul Duffin859fe962020-05-15 10:20:31 +01001122
Jiyong Park7f7766d2019-07-25 22:02:35 +09001123 android.InitApexModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09001124 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross9ae1b922018-06-26 17:59:05 -07001125 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001126}
1127
Colin Cross1b16b0e2019-02-12 14:41:32 -08001128// java_library_static is an obsolete alias for java_library.
1129func LibraryStaticFactory() android.Module {
1130 return LibraryFactory()
1131}
1132
1133// java_library_host builds and links sources into a `.jar` file for the host.
1134//
1135// A java_library_host has a single variant that produces a `.jar` file containing `.class` files that were
1136// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001137func LibraryHostFactory() android.Module {
1138 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001139
Colin Crossce6734e2020-06-15 16:09:53 -07001140 module.addHostProperties()
Colin Cross36242852017-06-23 15:06:31 -07001141
Colin Cross9ae1b922018-06-26 17:59:05 -07001142 module.Module.properties.Installable = proptools.BoolPtr(true)
1143
Jiyong Park7f7766d2019-07-25 22:02:35 +09001144 android.InitApexModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09001145 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -07001146 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001147}
1148
1149//
Colin Crossb628ea52018-08-14 16:42:33 -07001150// Java Tests
Colin Cross05638fc2018-04-09 18:40:24 -07001151//
1152
Dan Shi95d19422020-08-15 12:24:26 -07001153// Test option struct.
1154type TestOptions struct {
Zhenhuang Wang0ac5a432022-08-12 18:49:20 +08001155 android.CommonTestOptions
1156
Dan Shi95d19422020-08-15 12:24:26 -07001157 // a list of extra test configuration files that should be installed with the module.
1158 Extra_test_configs []string `android:"path,arch_variant"`
Cole Faust21680542022-12-07 18:18:37 -08001159
1160 // Extra <option> tags to add to the auto generated test xml file. The "key"
1161 // is optional in each of these.
1162 Tradefed_options []tradefed.Option
Dan Shiec731432023-05-26 04:21:44 +00001163
1164 // Extra <option> tags to add to the auto generated test xml file under the test runner, e.g., AndroidJunitTest.
1165 // The "key" is optional in each of these.
1166 Test_runner_options []tradefed.Option
Dan Shi95d19422020-08-15 12:24:26 -07001167}
1168
Colin Cross05638fc2018-04-09 18:40:24 -07001169type testProperties struct {
Colin Cross05638fc2018-04-09 18:40:24 -07001170 // list of compatibility suites (for example "cts", "vts") that the module should be
1171 // installed into.
1172 Test_suites []string `android:"arch_variant"`
Julien Despreze146e392018-08-02 15:00:46 -07001173
1174 // the name of the test configuration (for example "AndroidTest.xml") that should be
1175 // installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -08001176 Test_config *string `android:"path,arch_variant"`
Colin Crossd96ca352018-08-10 16:06:24 -07001177
Jack He33338892018-09-19 02:21:28 -07001178 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
1179 // should be installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -08001180 Test_config_template *string `android:"path,arch_variant"`
Jack He33338892018-09-19 02:21:28 -07001181
Colin Crossd96ca352018-08-10 16:06:24 -07001182 // list of files or filegroup modules that provide data that should be installed alongside
1183 // the test
Jiyong Park2b0e4902021-02-16 06:52:39 +09001184 Data []string `android:"path"`
Dan Shi6ffaaa82019-09-26 11:41:36 -07001185
1186 // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml
1187 // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true
1188 // explicitly.
1189 Auto_gen_config *bool
easoncylee5bcff5d2020-04-30 14:57:06 +08001190
1191 // Add parameterized mainline modules to auto generated test config. The options will be
1192 // handled by TradeFed to do downloading and installing the specified modules on the device.
1193 Test_mainline_modules []string
Dan Shi95d19422020-08-15 12:24:26 -07001194
1195 // Test options.
1196 Test_options TestOptions
Colin Crossf8d9c492021-01-26 11:01:43 -08001197
1198 // Names of modules containing JNI libraries that should be installed alongside the test.
1199 Jni_libs []string
Colin Crosscfb0f5e2021-09-24 15:47:17 -07001200
1201 // Install the test into a folder named for the module in all test suites.
1202 Per_testcase_directory *bool
Colin Cross05638fc2018-04-09 18:40:24 -07001203}
1204
Liz Kammerdd849a82020-06-12 16:38:45 -07001205type hostTestProperties struct {
1206 // list of native binary modules that should be installed alongside the test
1207 Data_native_bins []string `android:"arch_variant"`
Sam Delmericob3342ce2022-01-20 21:10:28 +00001208
1209 // list of device binary modules that should be installed alongside the test
Sam Delmericocc271e22022-06-01 15:45:02 +00001210 // This property only adds the first variant of the dependency
1211 Data_device_bins_first []string `android:"arch_variant"`
1212
1213 // list of device binary modules that should be installed alongside the test
1214 // This property adds 64bit AND 32bit variants of the dependency
1215 Data_device_bins_both []string `android:"arch_variant"`
1216
1217 // list of device binary modules that should be installed alongside the test
1218 // This property only adds 64bit variants of the dependency
1219 Data_device_bins_64 []string `android:"arch_variant"`
1220
1221 // list of device binary modules that should be installed alongside the test
1222 // This property adds 32bit variants of the dependency if available, or else
1223 // defaults to the 64bit variant
1224 Data_device_bins_prefer32 []string `android:"arch_variant"`
1225
1226 // list of device binary modules that should be installed alongside the test
1227 // This property only adds 32bit variants of the dependency
1228 Data_device_bins_32 []string `android:"arch_variant"`
Liz Kammerdd849a82020-06-12 16:38:45 -07001229}
1230
Paul Duffin42df1442019-03-20 12:45:53 +00001231type testHelperLibraryProperties struct {
1232 // list of compatibility suites (for example "cts", "vts") that the module should be
1233 // installed into.
1234 Test_suites []string `android:"arch_variant"`
Colin Crosscfb0f5e2021-09-24 15:47:17 -07001235
1236 // Install the test into a folder named for the module in all test suites.
1237 Per_testcase_directory *bool
Paul Duffin42df1442019-03-20 12:45:53 +00001238}
1239
Paul Duffin1b82e6a2019-12-03 18:06:47 +00001240type prebuiltTestProperties struct {
1241 // list of compatibility suites (for example "cts", "vts") that the module should be
1242 // installed into.
1243 Test_suites []string `android:"arch_variant"`
1244
1245 // the name of the test configuration (for example "AndroidTest.xml") that should be
1246 // installed with the module.
1247 Test_config *string `android:"path,arch_variant"`
1248}
1249
Colin Cross05638fc2018-04-09 18:40:24 -07001250type Test struct {
1251 Library
1252
1253 testProperties testProperties
Colin Cross303e21f2018-08-07 16:49:25 -07001254
Dan Shi95d19422020-08-15 12:24:26 -07001255 testConfig android.Path
1256 extraTestConfigs android.Paths
1257 data android.Paths
Colin Cross303e21f2018-08-07 16:49:25 -07001258}
1259
Liz Kammerdd849a82020-06-12 16:38:45 -07001260type TestHost struct {
1261 Test
1262
1263 testHostProperties hostTestProperties
1264}
1265
Paul Duffin42df1442019-03-20 12:45:53 +00001266type TestHelperLibrary struct {
1267 Library
1268
1269 testHelperLibraryProperties testHelperLibraryProperties
1270}
1271
Paul Duffin1b82e6a2019-12-03 18:06:47 +00001272type JavaTestImport struct {
1273 Import
1274
1275 prebuiltTestProperties prebuiltTestProperties
1276
1277 testConfig android.Path
Liz Kammerd6c31d22020-08-05 15:40:41 -07001278 dexJarFile android.Path
Paul Duffin1b82e6a2019-12-03 18:06:47 +00001279}
1280
Colin Cross24cc4be62021-11-03 14:09:41 -07001281func (j *Test) InstallInTestcases() bool {
1282 // Host java tests install into $(HOST_OUT_JAVA_LIBRARIES), and then are copied into
1283 // testcases by base_rules.mk.
1284 return !j.Host()
1285}
1286
1287func (j *TestHelperLibrary) InstallInTestcases() bool {
1288 return true
1289}
1290
1291func (j *JavaTestImport) InstallInTestcases() bool {
1292 return true
1293}
1294
Colin Crossf5f4ad32024-01-19 15:41:48 -08001295func (j *TestHost) IsNativeCoverageNeeded(ctx android.IncomingTransitionContext) bool {
Yu Liud8aa2002023-10-05 11:40:06 -07001296 return ctx.DeviceConfig().NativeCoverageEnabled()
1297}
1298
Sam Delmericocc271e22022-06-01 15:45:02 +00001299func (j *TestHost) addDataDeviceBinsDeps(ctx android.BottomUpMutatorContext) {
1300 if len(j.testHostProperties.Data_device_bins_first) > 0 {
1301 deviceVariations := ctx.Config().AndroidFirstDeviceTarget.Variations()
1302 ctx.AddFarVariationDependencies(deviceVariations, dataDeviceBinsTag, j.testHostProperties.Data_device_bins_first...)
1303 }
1304
1305 var maybeAndroid32Target *android.Target
1306 var maybeAndroid64Target *android.Target
1307 android32TargetList := android.FirstTarget(ctx.Config().Targets[android.Android], "lib32")
1308 android64TargetList := android.FirstTarget(ctx.Config().Targets[android.Android], "lib64")
1309 if len(android32TargetList) > 0 {
1310 maybeAndroid32Target = &android32TargetList[0]
1311 }
1312 if len(android64TargetList) > 0 {
1313 maybeAndroid64Target = &android64TargetList[0]
1314 }
1315
1316 if len(j.testHostProperties.Data_device_bins_both) > 0 {
1317 if maybeAndroid32Target == nil && maybeAndroid64Target == nil {
1318 ctx.PropertyErrorf("data_device_bins_both", "no device targets available. Targets: %q", ctx.Config().Targets)
1319 return
1320 }
1321 if maybeAndroid32Target != nil {
1322 ctx.AddFarVariationDependencies(
1323 maybeAndroid32Target.Variations(),
1324 dataDeviceBinsTag,
1325 j.testHostProperties.Data_device_bins_both...,
1326 )
1327 }
1328 if maybeAndroid64Target != nil {
1329 ctx.AddFarVariationDependencies(
1330 maybeAndroid64Target.Variations(),
1331 dataDeviceBinsTag,
1332 j.testHostProperties.Data_device_bins_both...,
1333 )
1334 }
1335 }
1336
1337 if len(j.testHostProperties.Data_device_bins_prefer32) > 0 {
1338 if maybeAndroid32Target != nil {
1339 ctx.AddFarVariationDependencies(
1340 maybeAndroid32Target.Variations(),
1341 dataDeviceBinsTag,
1342 j.testHostProperties.Data_device_bins_prefer32...,
1343 )
1344 } else {
1345 if maybeAndroid64Target == nil {
1346 ctx.PropertyErrorf("data_device_bins_prefer32", "no device targets available. Targets: %q", ctx.Config().Targets)
1347 return
1348 }
1349 ctx.AddFarVariationDependencies(
1350 maybeAndroid64Target.Variations(),
1351 dataDeviceBinsTag,
1352 j.testHostProperties.Data_device_bins_prefer32...,
1353 )
1354 }
1355 }
1356
1357 if len(j.testHostProperties.Data_device_bins_32) > 0 {
1358 if maybeAndroid32Target == nil {
1359 ctx.PropertyErrorf("data_device_bins_32", "cannot find 32bit device target. Targets: %q", ctx.Config().Targets)
1360 return
1361 }
1362 deviceVariations := maybeAndroid32Target.Variations()
1363 ctx.AddFarVariationDependencies(deviceVariations, dataDeviceBinsTag, j.testHostProperties.Data_device_bins_32...)
1364 }
1365
1366 if len(j.testHostProperties.Data_device_bins_64) > 0 {
1367 if maybeAndroid64Target == nil {
1368 ctx.PropertyErrorf("data_device_bins_64", "cannot find 64bit device target. Targets: %q", ctx.Config().Targets)
1369 return
1370 }
1371 deviceVariations := maybeAndroid64Target.Variations()
1372 ctx.AddFarVariationDependencies(deviceVariations, dataDeviceBinsTag, j.testHostProperties.Data_device_bins_64...)
1373 }
1374}
1375
Liz Kammerdd849a82020-06-12 16:38:45 -07001376func (j *TestHost) DepsMutator(ctx android.BottomUpMutatorContext) {
1377 if len(j.testHostProperties.Data_native_bins) > 0 {
1378 for _, target := range ctx.MultiTargets() {
1379 ctx.AddVariationDependencies(target.Variations(), dataNativeBinsTag, j.testHostProperties.Data_native_bins...)
1380 }
1381 }
1382
Colin Crossf8d9c492021-01-26 11:01:43 -08001383 if len(j.testProperties.Jni_libs) > 0 {
1384 for _, target := range ctx.MultiTargets() {
1385 sharedLibVariations := append(target.Variations(), blueprint.Variation{Mutator: "link", Variation: "shared"})
1386 ctx.AddFarVariationDependencies(sharedLibVariations, jniLibTag, j.testProperties.Jni_libs...)
1387 }
1388 }
1389
Sam Delmericocc271e22022-06-01 15:45:02 +00001390 j.addDataDeviceBinsDeps(ctx)
Liz Kammerdd849a82020-06-12 16:38:45 -07001391 j.deps(ctx)
1392}
1393
Yuexi Ma627263f2021-03-04 13:47:56 -08001394func (j *TestHost) AddExtraResource(p android.Path) {
1395 j.extraResources = append(j.extraResources, p)
1396}
1397
Sam Delmericocc271e22022-06-01 15:45:02 +00001398func (j *TestHost) dataDeviceBins() []string {
1399 ret := make([]string, 0,
1400 len(j.testHostProperties.Data_device_bins_first)+
1401 len(j.testHostProperties.Data_device_bins_both)+
1402 len(j.testHostProperties.Data_device_bins_prefer32)+
1403 len(j.testHostProperties.Data_device_bins_32)+
1404 len(j.testHostProperties.Data_device_bins_64),
1405 )
1406
1407 ret = append(ret, j.testHostProperties.Data_device_bins_first...)
1408 ret = append(ret, j.testHostProperties.Data_device_bins_both...)
1409 ret = append(ret, j.testHostProperties.Data_device_bins_prefer32...)
1410 ret = append(ret, j.testHostProperties.Data_device_bins_32...)
1411 ret = append(ret, j.testHostProperties.Data_device_bins_64...)
1412
1413 return ret
1414}
1415
Sam Delmericob3342ce2022-01-20 21:10:28 +00001416func (j *TestHost) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1417 var configs []tradefed.Config
Sam Delmericocc271e22022-06-01 15:45:02 +00001418 dataDeviceBins := j.dataDeviceBins()
1419 if len(dataDeviceBins) > 0 {
Sam Delmericob3342ce2022-01-20 21:10:28 +00001420 // add Tradefed configuration to push device bins to device for testing
1421 remoteDir := filepath.Join("/data/local/tests/unrestricted/", j.Name())
1422 options := []tradefed.Option{{Name: "cleanup", Value: "true"}}
Sam Delmericocc271e22022-06-01 15:45:02 +00001423 for _, bin := range dataDeviceBins {
Sam Delmericob3342ce2022-01-20 21:10:28 +00001424 fullPath := filepath.Join(remoteDir, bin)
1425 options = append(options, tradefed.Option{Name: "push-file", Key: bin, Value: fullPath})
1426 }
Sam Delmericocc271e22022-06-01 15:45:02 +00001427 configs = append(configs, tradefed.Object{
1428 Type: "target_preparer",
1429 Class: "com.android.tradefed.targetprep.PushFilePreparer",
1430 Options: options,
1431 })
Sam Delmericob3342ce2022-01-20 21:10:28 +00001432 }
1433
1434 j.Test.generateAndroidBuildActionsWithConfig(ctx, configs)
Colin Cross40213022023-12-13 15:19:49 -08001435 android.SetProvider(ctx, testing.TestModuleProviderKey, testing.TestModuleProviderData{})
Ronald Braunstein1a6e7c02024-03-14 21:14:39 +00001436 android.SetProvider(ctx, tradefed.BaseTestProviderKey, tradefed.BaseTestProviderData{
1437 InstalledFiles: j.data,
1438 OutputFile: j.outputFile,
1439 TestConfig: j.testConfig,
1440 RequiredModuleNames: j.RequiredModuleNames(),
1441 TestSuites: j.testProperties.Test_suites,
1442 IsHost: true,
1443 })
Sam Delmericob3342ce2022-01-20 21:10:28 +00001444}
1445
Colin Cross303e21f2018-08-07 16:49:25 -07001446func (j *Test) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Sam Delmericob3342ce2022-01-20 21:10:28 +00001447 j.generateAndroidBuildActionsWithConfig(ctx, nil)
Colin Cross40213022023-12-13 15:19:49 -08001448 android.SetProvider(ctx, testing.TestModuleProviderKey, testing.TestModuleProviderData{})
Sam Delmericob3342ce2022-01-20 21:10:28 +00001449}
1450
1451func (j *Test) generateAndroidBuildActionsWithConfig(ctx android.ModuleContext, configs []tradefed.Config) {
Julien Desprezb2166612021-03-05 18:08:36 +00001452 if j.testProperties.Test_options.Unit_test == nil && ctx.Host() {
1453 // TODO(b/): Clean temporary heuristic to avoid unexpected onboarding.
Julien Desprezf666b152021-03-15 13:07:53 -07001454 defaultUnitTest := !inList("tradefed", j.properties.Libs) && !inList("cts", j.testProperties.Test_suites)
Julien Desprezb2166612021-03-05 18:08:36 +00001455 j.testProperties.Test_options.Unit_test = proptools.BoolPtr(defaultUnitTest)
1456 }
Cole Faust21680542022-12-07 18:18:37 -08001457 j.testConfig = tradefed.AutoGenTestConfig(ctx, tradefed.AutoGenTestConfigOptions{
1458 TestConfigProp: j.testProperties.Test_config,
1459 TestConfigTemplateProp: j.testProperties.Test_config_template,
1460 TestSuites: j.testProperties.Test_suites,
1461 Config: configs,
1462 OptionsForAutogenerated: j.testProperties.Test_options.Tradefed_options,
Dan Shiec731432023-05-26 04:21:44 +00001463 TestRunnerOptions: j.testProperties.Test_options.Test_runner_options,
Cole Faust21680542022-12-07 18:18:37 -08001464 AutoGenConfig: j.testProperties.Auto_gen_config,
1465 UnitTest: j.testProperties.Test_options.Unit_test,
1466 DeviceTemplate: "${JavaTestConfigTemplate}",
1467 HostTemplate: "${JavaHostTestConfigTemplate}",
1468 HostUnitTestTemplate: "${JavaHostUnitTestConfigTemplate}",
1469 })
Liz Kammerdd849a82020-06-12 16:38:45 -07001470
Colin Cross8a497952019-03-05 22:25:09 -08001471 j.data = android.PathsForModuleSrc(ctx, j.testProperties.Data)
Colin Cross303e21f2018-08-07 16:49:25 -07001472
Dan Shi95d19422020-08-15 12:24:26 -07001473 j.extraTestConfigs = android.PathsForModuleSrc(ctx, j.testProperties.Test_options.Extra_test_configs)
1474
Liz Kammerdd849a82020-06-12 16:38:45 -07001475 ctx.VisitDirectDepsWithTag(dataNativeBinsTag, func(dep android.Module) {
1476 j.data = append(j.data, android.OutputFileForModule(ctx, dep, ""))
1477 })
1478
Sam Delmericob3342ce2022-01-20 21:10:28 +00001479 ctx.VisitDirectDepsWithTag(dataDeviceBinsTag, func(dep android.Module) {
1480 j.data = append(j.data, android.OutputFileForModule(ctx, dep, ""))
1481 })
1482
Colin Crossf8d9c492021-01-26 11:01:43 -08001483 ctx.VisitDirectDepsWithTag(jniLibTag, func(dep android.Module) {
Colin Cross313aa542023-12-13 13:47:44 -08001484 sharedLibInfo, _ := android.OtherModuleProvider(ctx, dep, cc.SharedLibraryInfoProvider)
Colin Crossf8d9c492021-01-26 11:01:43 -08001485 if sharedLibInfo.SharedLibrary != nil {
1486 // Copy to an intermediate output directory to append "lib[64]" to the path,
1487 // so that it's compatible with the default rpath values.
1488 var relPath string
1489 if sharedLibInfo.Target.Arch.ArchType.Multilib == "lib64" {
1490 relPath = filepath.Join("lib64", sharedLibInfo.SharedLibrary.Base())
1491 } else {
1492 relPath = filepath.Join("lib", sharedLibInfo.SharedLibrary.Base())
1493 }
1494 relocatedLib := android.PathForModuleOut(ctx, "relocated").Join(ctx, relPath)
1495 ctx.Build(pctx, android.BuildParams{
1496 Rule: android.Cp,
1497 Input: sharedLibInfo.SharedLibrary,
1498 Output: relocatedLib,
1499 })
1500 j.data = append(j.data, relocatedLib)
1501 } else {
1502 ctx.PropertyErrorf("jni_libs", "%q of type %q is not supported", dep.Name(), ctx.OtherModuleType(dep))
1503 }
1504 })
1505
Colin Cross303e21f2018-08-07 16:49:25 -07001506 j.Library.GenerateAndroidBuildActions(ctx)
Colin Cross05638fc2018-04-09 18:40:24 -07001507}
1508
Paul Duffin42df1442019-03-20 12:45:53 +00001509func (j *TestHelperLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1510 j.Library.GenerateAndroidBuildActions(ctx)
1511}
1512
Paul Duffin1b82e6a2019-12-03 18:06:47 +00001513func (j *JavaTestImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Cole Faust21680542022-12-07 18:18:37 -08001514 j.testConfig = tradefed.AutoGenTestConfig(ctx, tradefed.AutoGenTestConfigOptions{
1515 TestConfigProp: j.prebuiltTestProperties.Test_config,
1516 TestSuites: j.prebuiltTestProperties.Test_suites,
1517 DeviceTemplate: "${JavaTestConfigTemplate}",
1518 HostTemplate: "${JavaHostTestConfigTemplate}",
1519 HostUnitTestTemplate: "${JavaHostUnitTestConfigTemplate}",
1520 })
Paul Duffin1b82e6a2019-12-03 18:06:47 +00001521
1522 j.Import.GenerateAndroidBuildActions(ctx)
1523}
1524
1525type testSdkMemberType struct {
1526 android.SdkMemberTypeBase
1527}
1528
Paul Duffin296701e2021-07-14 10:29:36 +01001529func (mt *testSdkMemberType) AddDependencies(ctx android.SdkDependencyContext, dependencyTag blueprint.DependencyTag, names []string) {
1530 ctx.AddVariationDependencies(nil, dependencyTag, names...)
Paul Duffin1b82e6a2019-12-03 18:06:47 +00001531}
1532
1533func (mt *testSdkMemberType) IsInstance(module android.Module) bool {
1534 _, ok := module.(*Test)
1535 return ok
1536}
1537
Paul Duffin3a4eb502020-03-19 16:11:18 +00001538func (mt *testSdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
1539 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_test_import")
Paul Duffin14eb4672020-03-02 11:33:02 +00001540}
Paul Duffin1b82e6a2019-12-03 18:06:47 +00001541
Paul Duffin14eb4672020-03-02 11:33:02 +00001542func (mt *testSdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
1543 return &testSdkMemberProperties{}
1544}
1545
1546type testSdkMemberProperties struct {
1547 android.SdkMemberPropertiesBase
1548
Paul Duffina551a1c2020-03-17 21:04:24 +00001549 JarToExport android.Path
1550 TestConfig android.Path
Paul Duffin14eb4672020-03-02 11:33:02 +00001551}
1552
Paul Duffin3a4eb502020-03-19 16:11:18 +00001553func (p *testSdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
Paul Duffin14eb4672020-03-02 11:33:02 +00001554 test := variant.(*Test)
1555
1556 implementationJars := test.ImplementationJars()
Paul Duffin1b82e6a2019-12-03 18:06:47 +00001557 if len(implementationJars) != 1 {
Paul Duffin14eb4672020-03-02 11:33:02 +00001558 panic(fmt.Errorf("there must be only one implementation jar from %q", test.Name()))
Paul Duffin1b82e6a2019-12-03 18:06:47 +00001559 }
1560
Paul Duffina551a1c2020-03-17 21:04:24 +00001561 p.JarToExport = implementationJars[0]
1562 p.TestConfig = test.testConfig
Paul Duffin14eb4672020-03-02 11:33:02 +00001563}
Paul Duffin1b82e6a2019-12-03 18:06:47 +00001564
Paul Duffin3a4eb502020-03-19 16:11:18 +00001565func (p *testSdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffina551a1c2020-03-17 21:04:24 +00001566 builder := ctx.SnapshotBuilder()
Paul Duffin3a4eb502020-03-19 16:11:18 +00001567
Paul Duffina551a1c2020-03-17 21:04:24 +00001568 exportedJar := p.JarToExport
1569 if exportedJar != nil {
Paul Duffin13648912022-07-15 13:12:35 +00001570 snapshotRelativeJavaLibPath := sdkSnapshotFilePathForJar(ctx, p.OsPrefix(), ctx.Name())
Paul Duffina551a1c2020-03-17 21:04:24 +00001571 builder.CopyToSnapshot(exportedJar, snapshotRelativeJavaLibPath)
Paul Duffin14eb4672020-03-02 11:33:02 +00001572
1573 propertySet.AddProperty("jars", []string{snapshotRelativeJavaLibPath})
Paul Duffina551a1c2020-03-17 21:04:24 +00001574 }
1575
1576 testConfig := p.TestConfig
1577 if testConfig != nil {
1578 snapshotRelativeTestConfigPath := sdkSnapshotFilePathForMember(p.OsPrefix(), ctx.Name(), testConfigSuffix)
1579 builder.CopyToSnapshot(testConfig, snapshotRelativeTestConfigPath)
Paul Duffin14eb4672020-03-02 11:33:02 +00001580 propertySet.AddProperty("test_config", snapshotRelativeTestConfigPath)
1581 }
Paul Duffin1b82e6a2019-12-03 18:06:47 +00001582}
1583
Colin Cross1b16b0e2019-02-12 14:41:32 -08001584// java_test builds a and links sources into a `.jar` file for the device, and possibly for the host as well, and
1585// creates an `AndroidTest.xml` file to allow running the test with `atest` or a `TEST_MAPPING` file.
1586//
1587// By default, a java_test has a single variant that produces a `.jar` file containing `classes.dex` files that were
1588// compiled against the device bootclasspath.
1589//
1590// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1591// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -07001592func TestFactory() android.Module {
1593 module := &Test{}
1594
Colin Crossce6734e2020-06-15 16:09:53 -07001595 module.addHostAndDeviceProperties()
1596 module.AddProperties(&module.testProperties)
Colin Cross05638fc2018-04-09 18:40:24 -07001597
Colin Cross9ae1b922018-06-26 17:59:05 -07001598 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crosse3026872019-01-05 22:30:13 -08001599 module.Module.dexpreopter.isTest = true
Cole Faustd57e8b22022-08-11 11:59:04 -07001600 module.Module.linter.properties.Lint.Test = proptools.BoolPtr(true)
Colin Cross9ae1b922018-06-26 17:59:05 -07001601
Colin Cross05638fc2018-04-09 18:40:24 -07001602 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07001603 return module
1604}
1605
Paul Duffin42df1442019-03-20 12:45:53 +00001606// java_test_helper_library creates a java library and makes sure that it is added to the appropriate test suite.
1607func TestHelperLibraryFactory() android.Module {
1608 module := &TestHelperLibrary{}
1609
Colin Crossce6734e2020-06-15 16:09:53 -07001610 module.addHostAndDeviceProperties()
1611 module.AddProperties(&module.testHelperLibraryProperties)
Paul Duffin42df1442019-03-20 12:45:53 +00001612
Colin Cross9a4abed2019-04-24 13:19:28 -07001613 module.Module.properties.Installable = proptools.BoolPtr(true)
1614 module.Module.dexpreopter.isTest = true
Cole Faustd57e8b22022-08-11 11:59:04 -07001615 module.Module.linter.properties.Lint.Test = proptools.BoolPtr(true)
Colin Cross9a4abed2019-04-24 13:19:28 -07001616
Paul Duffin42df1442019-03-20 12:45:53 +00001617 InitJavaModule(module, android.HostAndDeviceSupported)
1618 return module
1619}
1620
Paul Duffin1b82e6a2019-12-03 18:06:47 +00001621// java_test_import imports one or more `.jar` files into the build graph as if they were built by a java_test module
1622// and makes sure that it is added to the appropriate test suite.
1623//
1624// By default, a java_test_import has a single variant that expects a `.jar` file containing `.class` files that were
1625// compiled against an Android classpath.
1626//
1627// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
1628// for host modules.
1629func JavaTestImportFactory() android.Module {
1630 module := &JavaTestImport{}
1631
1632 module.AddProperties(
1633 &module.Import.properties,
1634 &module.prebuiltTestProperties)
1635
1636 module.Import.properties.Installable = proptools.BoolPtr(true)
1637
1638 android.InitPrebuiltModule(module, &module.properties.Jars)
1639 android.InitApexModule(module)
Paul Duffin1b82e6a2019-12-03 18:06:47 +00001640 InitJavaModule(module, android.HostAndDeviceSupported)
1641 return module
1642}
1643
Colin Cross1b16b0e2019-02-12 14:41:32 -08001644// java_test_host builds a and links sources into a `.jar` file for the host, and creates an `AndroidTest.xml` file to
1645// allow running the test with `atest` or a `TEST_MAPPING` file.
1646//
1647// A java_test_host has a single variant that produces a `.jar` file containing `.class` files that were
1648// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -07001649func TestHostFactory() android.Module {
Liz Kammerdd849a82020-06-12 16:38:45 -07001650 module := &TestHost{}
Colin Cross05638fc2018-04-09 18:40:24 -07001651
Colin Crossce6734e2020-06-15 16:09:53 -07001652 module.addHostProperties()
1653 module.AddProperties(&module.testProperties)
Liz Kammerdd849a82020-06-12 16:38:45 -07001654 module.AddProperties(&module.testHostProperties)
Colin Cross05638fc2018-04-09 18:40:24 -07001655
Yuexi Ma627263f2021-03-04 13:47:56 -08001656 InitTestHost(
1657 module,
1658 proptools.BoolPtr(true),
1659 nil,
1660 nil)
Colin Cross9ae1b922018-06-26 17:59:05 -07001661
Liz Kammerdd849a82020-06-12 16:38:45 -07001662 InitJavaModuleMultiTargets(module, android.HostSupported)
Julien Desprezb2166612021-03-05 18:08:36 +00001663
Colin Cross05638fc2018-04-09 18:40:24 -07001664 return module
1665}
1666
Yuexi Ma627263f2021-03-04 13:47:56 -08001667func InitTestHost(th *TestHost, installable *bool, testSuites []string, autoGenConfig *bool) {
1668 th.properties.Installable = installable
1669 th.testProperties.Auto_gen_config = autoGenConfig
1670 th.testProperties.Test_suites = testSuites
1671}
1672
Colin Cross05638fc2018-04-09 18:40:24 -07001673//
Colin Cross2fe66872015-03-30 17:20:39 -07001674// Java Binaries (.jar file plus wrapper script)
1675//
1676
Colin Crossf506d872017-07-19 15:53:04 -07001677type binaryProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -07001678 // installable script to execute the resulting jar
Dan Willemsen8e6b3712021-09-20 23:11:24 -07001679 Wrapper *string `android:"path,arch_variant"`
Colin Cross094054a2018-10-17 15:10:48 -07001680
1681 // Name of the class containing main to be inserted into the manifest as Main-Class.
1682 Main_class *string
Colin Cross89226d92020-10-09 19:00:54 -07001683
1684 // Names of modules containing JNI libraries that should be installed alongside the host
1685 // variant of the binary.
Dan Willemsen8e6b3712021-09-20 23:11:24 -07001686 Jni_libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -07001687}
1688
Colin Crossf506d872017-07-19 15:53:04 -07001689type Binary struct {
1690 Library
Colin Cross2fe66872015-03-30 17:20:39 -07001691
Colin Crossf506d872017-07-19 15:53:04 -07001692 binaryProperties binaryProperties
Colin Cross10a03492017-08-10 17:09:43 -07001693
Colin Cross6b4a32d2017-12-05 13:42:45 -08001694 isWrapperVariant bool
1695
Colin Crossc3315992017-12-08 19:12:36 -08001696 wrapperFile android.Path
Colin Cross70dda7e2019-10-01 22:05:35 -07001697 binaryFile android.InstallPath
Colin Cross2fe66872015-03-30 17:20:39 -07001698}
1699
Alex Light24237172017-10-26 09:46:21 -07001700func (j *Binary) HostToolPath() android.OptionalPath {
1701 return android.OptionalPathForPath(j.binaryFile)
1702}
1703
Colin Crossf506d872017-07-19 15:53:04 -07001704func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jihoon Kang1bfb6f22023-07-01 00:13:47 +00001705 j.stem = proptools.StringDefault(j.overridableDeviceProperties.Stem, ctx.ModuleName())
1706
Colin Cross6b4a32d2017-12-05 13:42:45 -08001707 if ctx.Arch().ArchType == android.Common {
1708 // Compile the jar
Colin Cross094054a2018-10-17 15:10:48 -07001709 if j.binaryProperties.Main_class != nil {
1710 if j.properties.Manifest != nil {
1711 ctx.PropertyErrorf("main_class", "main_class cannot be used when manifest is set")
1712 }
1713 manifestFile := android.PathForModuleOut(ctx, "manifest.txt")
1714 GenerateMainClassManifest(ctx, manifestFile, String(j.binaryProperties.Main_class))
1715 j.overrideManifest = android.OptionalPathForPath(manifestFile)
1716 }
1717
Colin Cross6b4a32d2017-12-05 13:42:45 -08001718 j.Library.GenerateAndroidBuildActions(ctx)
Nan Zhang3c807db2017-11-03 14:53:31 -07001719 } else {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001720 // Handle the binary wrapper
1721 j.isWrapperVariant = true
1722
Colin Cross366938f2017-12-11 16:29:02 -08001723 if j.binaryProperties.Wrapper != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001724 j.wrapperFile = android.PathForModuleSrc(ctx, *j.binaryProperties.Wrapper)
Colin Cross6b4a32d2017-12-05 13:42:45 -08001725 } else {
Dan Willemsen8e6b3712021-09-20 23:11:24 -07001726 if ctx.Windows() {
1727 ctx.PropertyErrorf("wrapper", "wrapper is required for Windows")
1728 }
1729
Zi Wangca65b402022-10-10 13:45:06 -07001730 if ctx.Device() {
1731 // device binary should have a main_class property if it does not
1732 // have a specific wrapper, so that a default wrapper can
1733 // be generated for it.
1734 if j.binaryProperties.Main_class == nil {
1735 ctx.PropertyErrorf("main_class", "main_class property "+
1736 "is required for device binary if no default wrapper is assigned")
1737 } else {
1738 wrapper := android.PathForModuleOut(ctx, ctx.ModuleName()+".sh")
1739 jarName := j.Stem() + ".jar"
1740 partition := j.PartitionTag(ctx.DeviceConfig())
1741 ctx.Build(pctx, android.BuildParams{
1742 Rule: deviceBinaryWrapper,
1743 Output: wrapper,
1744 Args: map[string]string{
1745 "jar_name": jarName,
1746 "partition": partition,
1747 "main_class": String(j.binaryProperties.Main_class),
1748 },
1749 })
1750 j.wrapperFile = wrapper
1751 }
1752 } else {
1753 j.wrapperFile = android.PathForSource(ctx, "build/soong/scripts/jar-wrapper.sh")
1754 }
Colin Cross6b4a32d2017-12-05 13:42:45 -08001755 }
1756
Dan Willemsen8e6b3712021-09-20 23:11:24 -07001757 ext := ""
1758 if ctx.Windows() {
1759 ext = ".bat"
1760 }
1761
Colin Crossc179ea62020-10-09 10:54:15 -07001762 // The host installation rules make the installed wrapper depend on all the dependencies
Colin Cross89226d92020-10-09 19:00:54 -07001763 // of the wrapper variant, which will include the common variant's jar file and any JNI
1764 // libraries. This is verified by TestBinary.
Colin Cross6b4a32d2017-12-05 13:42:45 -08001765 j.binaryFile = ctx.InstallExecutable(android.PathForModuleInstall(ctx, "bin"),
Dan Willemsen8e6b3712021-09-20 23:11:24 -07001766 ctx.ModuleName()+ext, j.wrapperFile)
1767 }
Colin Cross2fe66872015-03-30 17:20:39 -07001768}
1769
Colin Crossf506d872017-07-19 15:53:04 -07001770func (j *Binary) DepsMutator(ctx android.BottomUpMutatorContext) {
Liz Kammer3bf97bd2022-04-26 09:38:20 -04001771 if ctx.Arch().ArchType == android.Common {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001772 j.deps(ctx)
Liz Kammer356f7d42021-01-26 09:18:53 -05001773 }
Liz Kammer3bf97bd2022-04-26 09:38:20 -04001774 if ctx.Arch().ArchType != android.Common {
Colin Crosse9fe2942020-11-10 18:12:15 -08001775 // These dependencies ensure the host installation rules will install the jar file and
1776 // the jni libraries when the wrapper is installed.
1777 ctx.AddVariationDependencies(nil, jniInstallTag, j.binaryProperties.Jni_libs...)
1778 ctx.AddVariationDependencies(
1779 []blueprint.Variation{{Mutator: "arch", Variation: android.CommonArch.String()}},
1780 binaryInstallTag, ctx.ModuleName())
Colin Cross6b4a32d2017-12-05 13:42:45 -08001781 }
Colin Cross46c9b8b2017-06-22 16:51:17 -07001782}
1783
Colin Cross1b16b0e2019-02-12 14:41:32 -08001784// java_binary builds a `.jar` file and a shell script that executes it for the device, and possibly for the host
1785// as well.
1786//
1787// By default, a java_binary has a single variant that produces a `.jar` file containing `classes.dex` files that were
1788// compiled against the device bootclasspath.
1789//
1790// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1791// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001792func BinaryFactory() android.Module {
1793 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001794
Colin Crossce6734e2020-06-15 16:09:53 -07001795 module.addHostAndDeviceProperties()
1796 module.AddProperties(&module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001797
Colin Cross9ae1b922018-06-26 17:59:05 -07001798 module.Module.properties.Installable = proptools.BoolPtr(true)
1799
Colin Cross6b4a32d2017-12-05 13:42:45 -08001800 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommonFirst)
1801 android.InitDefaultableModule(module)
Wei Libafb6d62021-12-10 03:14:59 -08001802
Colin Cross36242852017-06-23 15:06:31 -07001803 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001804}
1805
Colin Cross1b16b0e2019-02-12 14:41:32 -08001806// java_binary_host builds a `.jar` file and a shell script that executes it for the host.
1807//
1808// A java_binary_host has a single variant that produces a `.jar` file containing `.class` files that were
1809// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001810func BinaryHostFactory() android.Module {
1811 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001812
Colin Crossce6734e2020-06-15 16:09:53 -07001813 module.addHostProperties()
1814 module.AddProperties(&module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001815
Colin Cross9ae1b922018-06-26 17:59:05 -07001816 module.Module.properties.Installable = proptools.BoolPtr(true)
1817
Colin Cross6b4a32d2017-12-05 13:42:45 -08001818 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommonFirst)
1819 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001820 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001821}
1822
Jihoon Kang0ac87c22022-11-15 19:06:14 +00001823type JavaApiContribution struct {
1824 android.ModuleBase
1825 android.DefaultableModuleBase
Spandan Das2cc80ba2023-10-27 17:21:52 +00001826 embeddableInModuleAndImport
Jihoon Kang0ac87c22022-11-15 19:06:14 +00001827
1828 properties struct {
1829 // name of the API surface
1830 Api_surface *string
1831
1832 // relative path to the API signature text file
1833 Api_file *string `android:"path"`
1834 }
1835}
1836
1837func ApiContributionFactory() android.Module {
1838 module := &JavaApiContribution{}
1839 android.InitAndroidModule(module)
1840 android.InitDefaultableModule(module)
1841 module.AddProperties(&module.properties)
Spandan Das2cc80ba2023-10-27 17:21:52 +00001842 module.initModuleAndImport(module)
Jihoon Kang0ac87c22022-11-15 19:06:14 +00001843 return module
1844}
1845
1846type JavaApiImportInfo struct {
Jihoon Kang8fe19822023-09-14 06:27:36 +00001847 ApiFile android.Path
1848 ApiSurface string
Jihoon Kang0ac87c22022-11-15 19:06:14 +00001849}
1850
Colin Crossbc7d76c2023-12-12 16:39:03 -08001851var JavaApiImportProvider = blueprint.NewProvider[JavaApiImportInfo]()
Jihoon Kang0ac87c22022-11-15 19:06:14 +00001852
1853func (ap *JavaApiContribution) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jihoon Kang3198f3c2023-01-26 08:08:52 +00001854 var apiFile android.Path = nil
1855 if apiFileString := ap.properties.Api_file; apiFileString != nil {
1856 apiFile = android.PathForModuleSrc(ctx, String(apiFileString))
1857 }
1858
Colin Cross40213022023-12-13 15:19:49 -08001859 android.SetProvider(ctx, JavaApiImportProvider, JavaApiImportInfo{
Jihoon Kang8fe19822023-09-14 06:27:36 +00001860 ApiFile: apiFile,
1861 ApiSurface: proptools.String(ap.properties.Api_surface),
Jihoon Kang0ac87c22022-11-15 19:06:14 +00001862 })
1863}
1864
1865type ApiLibrary struct {
1866 android.ModuleBase
1867 android.DefaultableModuleBase
1868
Spandan Dascb368ea2023-03-22 04:27:05 +00001869 hiddenAPI
1870 dexer
Spandan Das2cc80ba2023-10-27 17:21:52 +00001871 embeddableInModuleAndImport
Spandan Dascb368ea2023-03-22 04:27:05 +00001872
Jihoon Kang0ac87c22022-11-15 19:06:14 +00001873 properties JavaApiLibraryProperties
1874
Jihoon Kang01e522c2023-03-14 01:09:34 +00001875 stubsSrcJar android.WritablePath
1876 stubsJar android.WritablePath
1877 stubsJarWithoutStaticLibs android.WritablePath
1878 extractedSrcJar android.WritablePath
Spandan Dascb368ea2023-03-22 04:27:05 +00001879 // .dex of stubs, used for hiddenapi processing
1880 dexJarFile OptionalDexJarPath
Jihoon Kang063ec002023-06-28 01:16:23 +00001881
1882 validationPaths android.Paths
Jihoon Kang5d701272024-02-15 21:53:49 +00001883
1884 stubsType StubsType
1885
1886 aconfigProtoFiles android.Paths
Jihoon Kang0ac87c22022-11-15 19:06:14 +00001887}
1888
1889type JavaApiLibraryProperties struct {
1890 // name of the API surface
1891 Api_surface *string
1892
Jihoon Kang60d4a092022-11-17 23:47:43 +00001893 // list of Java API contribution modules that consists this API surface
Spandan Dasc082eb82022-12-01 21:43:06 +00001894 // This is a list of Soong modules
Jihoon Kang60d4a092022-11-17 23:47:43 +00001895 Api_contributions []string
Jihoon Kang0ac87c22022-11-15 19:06:14 +00001896
1897 // List of flags to be passed to the javac compiler to generate jar file
1898 Javacflags []string
Jihoon Kang362aa9d2023-01-20 19:44:07 +00001899
1900 // List of shared java libs that this module has dependencies to and
1901 // should be passed as classpath in javac invocation
1902 Libs []string
Jihoon Kange30fff02023-02-14 20:18:20 +00001903
1904 // List of java libs that this module has static dependencies to and will be
Jihoon Kang01e522c2023-03-14 01:09:34 +00001905 // merge zipped after metalava invocation
Jihoon Kange30fff02023-02-14 20:18:20 +00001906 Static_libs []string
Jihoon Kang01e522c2023-03-14 01:09:34 +00001907
Jihoon Kangca198c22023-06-22 23:13:51 +00001908 // Java Api library to provide the full API surface stub jar file.
1909 // If this property is set, the stub jar of this module is created by
1910 // extracting the compiled class files provided by the
1911 // full_api_surface_stub module.
1912 Full_api_surface_stub *string
Jihoon Kang862da6f2023-08-01 06:28:51 +00001913
1914 // Version of previously released API file for compatibility check.
1915 Previous_api *string `android:"path"`
Jihoon Kang4ec24872023-10-05 17:26:09 +00001916
1917 // java_system_modules module providing the jar to be added to the
1918 // bootclasspath when compiling the stubs.
1919 // The jar will also be passed to metalava as a classpath to
1920 // generate compilable stubs.
1921 System_modules *string
Jihoon Kang063ec002023-06-28 01:16:23 +00001922
1923 // If true, the module runs validation on the API signature files provided
1924 // by the modules passed via api_contributions by checking if the files are
1925 // in sync with the source Java files. However, the environment variable
1926 // DISABLE_STUB_VALIDATION has precedence over this property.
1927 Enable_validation *bool
Jihoon Kang5d701272024-02-15 21:53:49 +00001928
1929 // Type of stubs the module should generate. Must be one of "everything", "runtime" or
1930 // "exportable". Defaults to "everything".
1931 // - "everything" stubs include all non-flagged apis and flagged apis, regardless of the state
1932 // of the flag.
1933 // - "runtime" stubs include all non-flagged apis and flagged apis that are ENABLED or
1934 // READ_WRITE, and all other flagged apis are stripped.
1935 // - "exportable" stubs include all non-flagged apis and flagged apis that are ENABLED and
1936 // READ_ONLY, and all other flagged apis are stripped.
1937 Stubs_type *string
1938
1939 // List of aconfig_declarations module names that the stubs generated in this module
1940 // depend on.
1941 Aconfig_declarations []string
Jihoon Kang0ac87c22022-11-15 19:06:14 +00001942}
1943
1944func ApiLibraryFactory() android.Module {
1945 module := &ApiLibrary{}
1946 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
Jihoon Kang0ac87c22022-11-15 19:06:14 +00001947 module.AddProperties(&module.properties)
Spandan Das2cc80ba2023-10-27 17:21:52 +00001948 module.initModuleAndImport(module)
Jihoon Kang1c51f502023-01-09 23:42:40 +00001949 android.InitDefaultableModule(module)
Jihoon Kang0ac87c22022-11-15 19:06:14 +00001950 return module
1951}
1952
1953func (al *ApiLibrary) ApiSurface() *string {
1954 return al.properties.Api_surface
1955}
1956
1957func (al *ApiLibrary) StubsJar() android.Path {
1958 return al.stubsJar
1959}
1960
1961func metalavaStubCmd(ctx android.ModuleContext, rule *android.RuleBuilder,
Jihoon Kang4ec24872023-10-05 17:26:09 +00001962 srcs android.Paths, homeDir android.WritablePath,
1963 classpath android.Paths) *android.RuleBuilderCommand {
Jihoon Kang0ac87c22022-11-15 19:06:14 +00001964 rule.Command().Text("rm -rf").Flag(homeDir.String())
1965 rule.Command().Text("mkdir -p").Flag(homeDir.String())
1966
1967 cmd := rule.Command()
1968 cmd.FlagWithArg("ANDROID_PREFS_ROOT=", homeDir.String())
1969
1970 if metalavaUseRbe(ctx) {
1971 rule.Remoteable(android.RemoteRuleSupports{RBE: true})
1972 execStrategy := ctx.Config().GetenvWithDefault("RBE_METALAVA_EXEC_STRATEGY", remoteexec.LocalExecStrategy)
1973 labels := map[string]string{"type": "tool", "name": "metalava"}
1974
1975 pool := ctx.Config().GetenvWithDefault("RBE_METALAVA_POOL", "java16")
1976 rule.Rewrapper(&remoteexec.REParams{
1977 Labels: labels,
1978 ExecStrategy: execStrategy,
1979 ToolchainInputs: []string{config.JavaCmd(ctx).String()},
1980 Platform: map[string]string{remoteexec.PoolKey: pool},
1981 })
1982 }
1983
1984 cmd.BuiltTool("metalava").ImplicitTool(ctx.Config().HostJavaToolPath(ctx, "metalava.jar")).
1985 Flag(config.JavacVmFlags).
1986 Flag("-J--add-opens=java.base/java.util=ALL-UNNAMED").
Jihoon Kang0ac87c22022-11-15 19:06:14 +00001987 FlagWithInputList("--source-files ", srcs, " ")
1988
MÃ¥rten Kongstadbd262442023-07-12 14:01:49 +02001989 cmd.Flag("--color").
Jihoon Kang0ac87c22022-11-15 19:06:14 +00001990 Flag("--quiet").
Jihoon Kang1bff0342023-01-17 20:40:22 +00001991 Flag("--include-annotations").
1992 // The flag makes nullability issues as warnings rather than errors by replacing
1993 // @Nullable/@NonNull in the listed packages APIs with @RecentlyNullable/@RecentlyNonNull,
1994 // and these packages are meant to have everything annotated
1995 // @RecentlyNullable/@RecentlyNonNull.
1996 FlagWithArg("--force-convert-to-warning-nullability-annotations ", "+*:-android.*:+android.icu.*:-dalvik.*").
Jihoon Kang0ac87c22022-11-15 19:06:14 +00001997 FlagWithArg("--repeat-errors-max ", "10").
1998 FlagWithArg("--hide ", "UnresolvedImport").
1999 FlagWithArg("--hide ", "InvalidNullabilityOverride").
2000 FlagWithArg("--hide ", "ChangedDefault")
2001
Jihoon Kang4ec24872023-10-05 17:26:09 +00002002 if len(classpath) == 0 {
2003 // The main purpose of the `--api-class-resolution api` option is to force metalava to ignore
2004 // classes on the classpath when an API file contains missing classes. However, as this command
2005 // does not specify `--classpath` this is not needed for that. However, this is also used as a
2006 // signal to the special metalava code for generating stubs from text files that it needs to add
2007 // some additional items into the API (e.g. default constructors).
2008 cmd.FlagWithArg("--api-class-resolution ", "api")
2009 } else {
2010 cmd.FlagWithArg("--api-class-resolution ", "api:classpath")
2011 cmd.FlagWithInputList("--classpath ", classpath, ":")
2012 }
Paul Duffin5b7035f2023-05-31 17:51:33 +01002013
Jihoon Kang0ac87c22022-11-15 19:06:14 +00002014 return cmd
2015}
2016
Jihoon Kang1bff0342023-01-17 20:40:22 +00002017func (al *ApiLibrary) HeaderJars() android.Paths {
2018 return android.Paths{al.stubsJar}
2019}
2020
2021func (al *ApiLibrary) OutputDirAndDeps() (android.Path, android.Paths) {
2022 return nil, nil
2023}
2024
Jihoon Kang0ac87c22022-11-15 19:06:14 +00002025func (al *ApiLibrary) stubsFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand, stubsDir android.OptionalPath) {
2026 if stubsDir.Valid() {
2027 cmd.FlagWithArg("--stubs ", stubsDir.String())
2028 }
2029}
2030
Jihoon Kang063ec002023-06-28 01:16:23 +00002031func (al *ApiLibrary) addValidation(ctx android.ModuleContext, cmd *android.RuleBuilderCommand, validationPaths android.Paths) {
2032 for _, validationPath := range validationPaths {
2033 cmd.Validation(validationPath)
2034 }
2035}
2036
Jihoon Kangca198c22023-06-22 23:13:51 +00002037// This method extracts the stub class files from the stub jar file provided
2038// from full_api_surface_stub module instead of compiling the srcjar generated from invoking metalava.
Jihoon Kang01e522c2023-03-14 01:09:34 +00002039// This method is used because metalava can generate compilable from-text stubs only when
Jihoon Kangca198c22023-06-22 23:13:51 +00002040// the codebase encompasses all classes listed in the input API text file, and a class can extend
Jihoon Kang01e522c2023-03-14 01:09:34 +00002041// a class that is not within the same API domain.
Jihoon Kangca198c22023-06-22 23:13:51 +00002042func (al *ApiLibrary) extractApiSrcs(ctx android.ModuleContext, rule *android.RuleBuilder, stubsDir android.OptionalPath, fullApiSurfaceStubJar android.Path) {
2043 classFilesList := android.PathForModuleOut(ctx, "metalava", "classes.txt")
Jihoon Kang01e522c2023-03-14 01:09:34 +00002044 unzippedSrcJarDir := android.PathForModuleOut(ctx, "metalava", "unzipDir")
2045
2046 rule.Command().
2047 BuiltTool("list_files").
2048 Text(stubsDir.String()).
Jihoon Kangca198c22023-06-22 23:13:51 +00002049 FlagWithOutput("--out ", classFilesList).
Jihoon Kang01e522c2023-03-14 01:09:34 +00002050 FlagWithArg("--extensions ", ".java").
Jihoon Kangca198c22023-06-22 23:13:51 +00002051 FlagWithArg("--root ", unzippedSrcJarDir.String()).
2052 Flag("--classes")
Jihoon Kang01e522c2023-03-14 01:09:34 +00002053
2054 rule.Command().
2055 Text("unzip").
2056 Flag("-q").
Jihoon Kangca198c22023-06-22 23:13:51 +00002057 Input(fullApiSurfaceStubJar).
Jihoon Kang01e522c2023-03-14 01:09:34 +00002058 FlagWithArg("-d ", unzippedSrcJarDir.String())
2059
2060 rule.Command().
2061 BuiltTool("soong_zip").
Jihoon Kangca198c22023-06-22 23:13:51 +00002062 Flag("-jar").
Jihoon Kang01e522c2023-03-14 01:09:34 +00002063 Flag("-write_if_changed").
Jihoon Kangca198c22023-06-22 23:13:51 +00002064 Flag("-ignore_missing_files").
Jihoon Kangd02a4362023-09-12 23:54:43 +00002065 Flag("-quiet").
Jihoon Kang01e522c2023-03-14 01:09:34 +00002066 FlagWithArg("-C ", unzippedSrcJarDir.String()).
Jihoon Kangca198c22023-06-22 23:13:51 +00002067 FlagWithInput("-l ", classFilesList).
2068 FlagWithOutput("-o ", al.stubsJarWithoutStaticLibs)
Jihoon Kang01e522c2023-03-14 01:09:34 +00002069}
Jihoon Kang0ac87c22022-11-15 19:06:14 +00002070
2071func (al *ApiLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
Jihoon Kang60d4a092022-11-17 23:47:43 +00002072 apiContributions := al.properties.Api_contributions
Jihoon Kang063ec002023-06-28 01:16:23 +00002073 addValidations := !ctx.Config().IsEnvTrue("DISABLE_STUB_VALIDATION") &&
Jihoon Kang4f04df92024-01-30 02:30:06 +00002074 !ctx.Config().IsEnvTrue("WITHOUT_CHECK_API") &&
Jihoon Kang063ec002023-06-28 01:16:23 +00002075 proptools.BoolDefault(al.properties.Enable_validation, true)
Jihoon Kang60d4a092022-11-17 23:47:43 +00002076 for _, apiContributionName := range apiContributions {
2077 ctx.AddDependency(ctx.Module(), javaApiContributionTag, apiContributionName)
Jihoon Kang063ec002023-06-28 01:16:23 +00002078
2079 // Add the java_api_contribution module generating droidstubs module
2080 // as dependency when validation adding conditions are met and
2081 // the java_api_contribution module name has ".api.contribution" suffix.
2082 // All droidstubs-generated modules possess the suffix in the name,
2083 // but there is no such guarantee for tests.
2084 if addValidations {
2085 if strings.HasSuffix(apiContributionName, ".api.contribution") {
2086 ctx.AddDependency(ctx.Module(), metalavaCurrentApiTimestampTag, strings.TrimSuffix(apiContributionName, ".api.contribution"))
2087 } else {
2088 ctx.ModuleErrorf("Validation is enabled for module %s but a "+
2089 "current timestamp provider is not found for the api "+
2090 "contribution %s",
2091 ctx.ModuleName(),
2092 apiContributionName,
2093 )
2094 }
2095 }
Jihoon Kang0ac87c22022-11-15 19:06:14 +00002096 }
Jihoon Kang362aa9d2023-01-20 19:44:07 +00002097 ctx.AddVariationDependencies(nil, libTag, al.properties.Libs...)
Jihoon Kange30fff02023-02-14 20:18:20 +00002098 ctx.AddVariationDependencies(nil, staticLibTag, al.properties.Static_libs...)
Jihoon Kangca198c22023-06-22 23:13:51 +00002099 if al.properties.Full_api_surface_stub != nil {
2100 ctx.AddVariationDependencies(nil, depApiSrcsTag, String(al.properties.Full_api_surface_stub))
Jihoon Kang01e522c2023-03-14 01:09:34 +00002101 }
Jihoon Kang4ec24872023-10-05 17:26:09 +00002102 if al.properties.System_modules != nil {
2103 ctx.AddVariationDependencies(nil, systemModulesTag, String(al.properties.System_modules))
2104 }
Jihoon Kang5d701272024-02-15 21:53:49 +00002105 for _, aconfigDeclarationsName := range al.properties.Aconfig_declarations {
2106 ctx.AddDependency(ctx.Module(), aconfigDeclarationTag, aconfigDeclarationsName)
2107 }
Jihoon Kang0ac87c22022-11-15 19:06:14 +00002108}
2109
Jihoon Kanga96a7b12023-09-20 23:43:32 +00002110// Map where key is the api scope name and value is the int value
2111// representing the order of the api scope, narrowest to the widest
2112var scopeOrderMap = allApiScopes.MapToIndex(
2113 func(s *apiScope) string { return s.name })
Jihoon Kang478ca5b2023-08-11 23:33:05 +00002114
Jihoon Kanga96a7b12023-09-20 23:43:32 +00002115func (al *ApiLibrary) sortApiFilesByApiScope(ctx android.ModuleContext, srcFilesInfo []JavaApiImportInfo) []JavaApiImportInfo {
2116 for _, srcFileInfo := range srcFilesInfo {
2117 if srcFileInfo.ApiSurface == "" {
2118 ctx.ModuleErrorf("Api surface not defined for the associated api file %s", srcFileInfo.ApiFile)
Jihoon Kang84473f52023-08-11 22:36:33 +00002119 }
2120 }
Jihoon Kanga96a7b12023-09-20 23:43:32 +00002121 sort.Slice(srcFilesInfo, func(i, j int) bool {
2122 return scopeOrderMap[srcFilesInfo[i].ApiSurface] < scopeOrderMap[srcFilesInfo[j].ApiSurface]
2123 })
Jihoon Kang8fe19822023-09-14 06:27:36 +00002124
Jihoon Kanga96a7b12023-09-20 23:43:32 +00002125 return srcFilesInfo
Jihoon Kang84473f52023-08-11 22:36:33 +00002126}
2127
Jihoon Kang5d701272024-02-15 21:53:49 +00002128var validstubsType = []StubsType{Everything, Runtime, Exportable}
2129
2130func (al *ApiLibrary) validateProperties(ctx android.ModuleContext) {
2131 if al.properties.Stubs_type == nil {
2132 ctx.ModuleErrorf("java_api_library module type must specify stubs_type property.")
2133 } else {
2134 al.stubsType = StringToStubsType(proptools.String(al.properties.Stubs_type))
2135 }
2136
2137 if !android.InList(al.stubsType, validstubsType) {
2138 ctx.PropertyErrorf("stubs_type", "%s is not a valid stubs_type property value. "+
2139 "Must be one of %s.", proptools.String(al.properties.Stubs_type), validstubsType)
2140 }
2141}
2142
Jihoon Kang0ac87c22022-11-15 19:06:14 +00002143func (al *ApiLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jihoon Kang5d701272024-02-15 21:53:49 +00002144 al.validateProperties(ctx)
Jihoon Kang0ac87c22022-11-15 19:06:14 +00002145
2146 rule := android.NewRuleBuilder(pctx, ctx)
2147
2148 rule.Sbox(android.PathForModuleOut(ctx, "metalava"),
2149 android.PathForModuleOut(ctx, "metalava.sbox.textproto")).
2150 SandboxInputs()
2151
Jihoon Kang063ec002023-06-28 01:16:23 +00002152 stubsDir := android.OptionalPathForPath(android.PathForModuleOut(ctx, "metalava", "stubsDir"))
Jihoon Kang0ac87c22022-11-15 19:06:14 +00002153 rule.Command().Text("rm -rf").Text(stubsDir.String())
2154 rule.Command().Text("mkdir -p").Text(stubsDir.String())
2155
2156 homeDir := android.PathForModuleOut(ctx, "metalava", "home")
2157
Jihoon Kang8fe19822023-09-14 06:27:36 +00002158 var srcFilesInfo []JavaApiImportInfo
Jihoon Kang362aa9d2023-01-20 19:44:07 +00002159 var classPaths android.Paths
Jihoon Kange30fff02023-02-14 20:18:20 +00002160 var staticLibs android.Paths
Jihoon Kangca198c22023-06-22 23:13:51 +00002161 var depApiSrcsStubsJar android.Path
Jihoon Kang4ec24872023-10-05 17:26:09 +00002162 var systemModulesPaths android.Paths
Jihoon Kang362aa9d2023-01-20 19:44:07 +00002163 ctx.VisitDirectDeps(func(dep android.Module) {
2164 tag := ctx.OtherModuleDependencyTag(dep)
2165 switch tag {
2166 case javaApiContributionTag:
Colin Cross313aa542023-12-13 13:47:44 -08002167 provider, _ := android.OtherModuleProvider(ctx, dep, JavaApiImportProvider)
Jihoon Kang8fe19822023-09-14 06:27:36 +00002168 if provider.ApiFile == nil && !ctx.Config().AllowMissingDependencies() {
Jihoon Kang3198f3c2023-01-26 08:08:52 +00002169 ctx.ModuleErrorf("Error: %s has an empty api file.", dep.Name())
2170 }
Jihoon Kang8fe19822023-09-14 06:27:36 +00002171 srcFilesInfo = append(srcFilesInfo, provider)
Jihoon Kang362aa9d2023-01-20 19:44:07 +00002172 case libTag:
Colin Cross313aa542023-12-13 13:47:44 -08002173 provider, _ := android.OtherModuleProvider(ctx, dep, JavaInfoProvider)
Jihoon Kang362aa9d2023-01-20 19:44:07 +00002174 classPaths = append(classPaths, provider.HeaderJars...)
Jihoon Kange30fff02023-02-14 20:18:20 +00002175 case staticLibTag:
Colin Cross313aa542023-12-13 13:47:44 -08002176 provider, _ := android.OtherModuleProvider(ctx, dep, JavaInfoProvider)
Jihoon Kange30fff02023-02-14 20:18:20 +00002177 staticLibs = append(staticLibs, provider.HeaderJars...)
Jihoon Kang01e522c2023-03-14 01:09:34 +00002178 case depApiSrcsTag:
Colin Cross313aa542023-12-13 13:47:44 -08002179 provider, _ := android.OtherModuleProvider(ctx, dep, JavaInfoProvider)
Jihoon Kangca198c22023-06-22 23:13:51 +00002180 depApiSrcsStubsJar = provider.HeaderJars[0]
Jihoon Kang4ec24872023-10-05 17:26:09 +00002181 case systemModulesTag:
2182 module := dep.(SystemModulesProvider)
2183 systemModulesPaths = append(systemModulesPaths, module.HeaderJars()...)
Jihoon Kang063ec002023-06-28 01:16:23 +00002184 case metalavaCurrentApiTimestampTag:
2185 if currentApiTimestampProvider, ok := dep.(currentApiTimestampProvider); ok {
2186 al.validationPaths = append(al.validationPaths, currentApiTimestampProvider.CurrentApiTimestamp())
2187 }
Jihoon Kang5d701272024-02-15 21:53:49 +00002188 case aconfigDeclarationTag:
2189 if provider, ok := android.OtherModuleProvider(ctx, dep, android.AconfigDeclarationsProviderKey); ok {
2190 al.aconfigProtoFiles = append(al.aconfigProtoFiles, provider.IntermediateCacheOutputPath)
Yu Liu67a28422024-03-05 00:36:31 +00002191 } else if provider, ok := android.OtherModuleProvider(ctx, dep, android.CodegenInfoProvider); ok {
Jihoon Kang5d701272024-02-15 21:53:49 +00002192 al.aconfigProtoFiles = append(al.aconfigProtoFiles, provider.IntermediateCacheOutputPaths...)
2193 } else {
2194 ctx.ModuleErrorf("Only aconfig_declarations and aconfig_declarations_group "+
2195 "module type is allowed for flags_packages property, but %s is neither "+
2196 "of these supported module types",
2197 dep.Name(),
2198 )
2199 }
Jihoon Kang362aa9d2023-01-20 19:44:07 +00002200 }
Jihoon Kang60d4a092022-11-17 23:47:43 +00002201 })
Jihoon Kang0ac87c22022-11-15 19:06:14 +00002202
Jihoon Kanga96a7b12023-09-20 23:43:32 +00002203 srcFilesInfo = al.sortApiFilesByApiScope(ctx, srcFilesInfo)
2204 var srcFiles android.Paths
2205 for _, srcFileInfo := range srcFilesInfo {
2206 srcFiles = append(srcFiles, android.PathForSource(ctx, srcFileInfo.ApiFile.String()))
Spandan Dasc082eb82022-12-01 21:43:06 +00002207 }
2208
Jihoon Kang160634c2023-05-25 05:28:29 +00002209 if srcFiles == nil && !ctx.Config().AllowMissingDependencies() {
Jihoon Kang01e522c2023-03-14 01:09:34 +00002210 ctx.ModuleErrorf("Error: %s has an empty api file.", ctx.ModuleName())
2211 }
2212
Jihoon Kang4ec24872023-10-05 17:26:09 +00002213 cmd := metalavaStubCmd(ctx, rule, srcFiles, homeDir, systemModulesPaths)
Jihoon Kang0ac87c22022-11-15 19:06:14 +00002214
2215 al.stubsFlags(ctx, cmd, stubsDir)
2216
Jihoon Kang862da6f2023-08-01 06:28:51 +00002217 migratingNullability := String(al.properties.Previous_api) != ""
2218 if migratingNullability {
2219 previousApi := android.PathForModuleSrc(ctx, String(al.properties.Previous_api))
2220 cmd.FlagWithInput("--migrate-nullness ", previousApi)
2221 }
2222
Jihoon Kang063ec002023-06-28 01:16:23 +00002223 al.addValidation(ctx, cmd, al.validationPaths)
2224
Jihoon Kang5d701272024-02-15 21:53:49 +00002225 generateRevertAnnotationArgs(ctx, cmd, al.stubsType, al.aconfigProtoFiles)
2226
Jihoon Kang0ac87c22022-11-15 19:06:14 +00002227 al.stubsSrcJar = android.PathForModuleOut(ctx, "metalava", ctx.ModuleName()+"-"+"stubs.srcjar")
Jihoon Kangca198c22023-06-22 23:13:51 +00002228 al.stubsJarWithoutStaticLibs = android.PathForModuleOut(ctx, "metalava", "stubs.jar")
2229 al.stubsJar = android.PathForModuleOut(ctx, ctx.ModuleName(), fmt.Sprintf("%s.jar", ctx.ModuleName()))
Jihoon Kang01e522c2023-03-14 01:09:34 +00002230
Jihoon Kangca198c22023-06-22 23:13:51 +00002231 if depApiSrcsStubsJar != nil {
2232 al.extractApiSrcs(ctx, rule, stubsDir, depApiSrcsStubsJar)
Jihoon Kang01e522c2023-03-14 01:09:34 +00002233 }
Jihoon Kangca198c22023-06-22 23:13:51 +00002234 rule.Command().
2235 BuiltTool("soong_zip").
2236 Flag("-write_if_changed").
2237 Flag("-jar").
2238 FlagWithOutput("-o ", al.stubsSrcJar).
2239 FlagWithArg("-C ", stubsDir.String()).
2240 FlagWithArg("-D ", stubsDir.String())
Jihoon Kang0ac87c22022-11-15 19:06:14 +00002241
Paul Duffin336b16a2023-08-15 23:10:13 +01002242 rule.Build("metalava", "metalava merged text")
Jihoon Kang01e522c2023-03-14 01:09:34 +00002243
Jihoon Kangca198c22023-06-22 23:13:51 +00002244 if depApiSrcsStubsJar == nil {
2245 var flags javaBuilderFlags
2246 flags.javaVersion = getStubsJavaVersion()
2247 flags.javacFlags = strings.Join(al.properties.Javacflags, " ")
2248 flags.classpath = classpath(classPaths)
Jihoon Kang4ec24872023-10-05 17:26:09 +00002249 flags.bootClasspath = classpath(systemModulesPaths)
Jihoon Kang0ac87c22022-11-15 19:06:14 +00002250
Vadim Spivak3c496f02023-06-08 06:14:59 +00002251 annoSrcJar := android.PathForModuleOut(ctx, ctx.ModuleName(), "anno.srcjar")
2252
Jihoon Kangca198c22023-06-22 23:13:51 +00002253 TransformJavaToClasses(ctx, al.stubsJarWithoutStaticLibs, 0, android.Paths{},
Vadim Spivak3c496f02023-06-08 06:14:59 +00002254 android.Paths{al.stubsSrcJar}, annoSrcJar, flags, android.Paths{})
Jihoon Kangca198c22023-06-22 23:13:51 +00002255 }
Jihoon Kang423d2292022-11-29 23:10:10 +00002256
Jihoon Kange30fff02023-02-14 20:18:20 +00002257 builder := android.NewRuleBuilder(pctx, ctx)
2258 builder.Command().
2259 BuiltTool("merge_zips").
2260 Output(al.stubsJar).
Jihoon Kang01e522c2023-03-14 01:09:34 +00002261 Inputs(android.Paths{al.stubsJarWithoutStaticLibs}).
Jihoon Kange30fff02023-02-14 20:18:20 +00002262 Inputs(staticLibs)
2263 builder.Build("merge_zips", "merge jar files")
2264
Spandan Dascb368ea2023-03-22 04:27:05 +00002265 // compile stubs to .dex for hiddenapi processing
2266 dexParams := &compileDexParams{
2267 flags: javaBuilderFlags{},
2268 sdkVersion: al.SdkVersion(ctx),
2269 minSdkVersion: al.MinSdkVersion(ctx),
2270 classesJar: al.stubsJar,
2271 jarName: ctx.ModuleName() + ".jar",
2272 }
2273 dexOutputFile := al.dexer.compileDex(ctx, dexParams)
2274 uncompressed := true
2275 al.initHiddenAPI(ctx, makeDexJarPathFromPath(dexOutputFile), al.stubsJar, &uncompressed)
2276 dexOutputFile = al.hiddenAPIEncodeDex(ctx, dexOutputFile)
2277 al.dexJarFile = makeDexJarPathFromPath(dexOutputFile)
2278
Jihoon Kang423d2292022-11-29 23:10:10 +00002279 ctx.Phony(ctx.ModuleName(), al.stubsJar)
Jihoon Kang362aa9d2023-01-20 19:44:07 +00002280
Colin Cross40213022023-12-13 15:19:49 -08002281 android.SetProvider(ctx, JavaInfoProvider, JavaInfo{
Jihoon Kang1bff0342023-01-17 20:40:22 +00002282 HeaderJars: android.PathsIfNonNil(al.stubsJar),
2283 ImplementationAndResourcesJars: android.PathsIfNonNil(al.stubsJar),
2284 ImplementationJars: android.PathsIfNonNil(al.stubsJar),
2285 AidlIncludeDirs: android.Paths{},
Jihoon Kangfe914ed2024-02-12 22:49:21 +00002286 StubsLinkType: Stubs,
Joe Onorato6fe59eb2023-07-16 13:20:33 -07002287 // No aconfig libraries on api libraries
Jihoon Kang362aa9d2023-01-20 19:44:07 +00002288 })
Jihoon Kang0ac87c22022-11-15 19:06:14 +00002289}
2290
Spandan Das59a4a2b2024-01-09 21:35:56 +00002291func (al *ApiLibrary) DexJarBuildPath(ctx android.ModuleErrorfContext) OptionalDexJarPath {
Spandan Dascb368ea2023-03-22 04:27:05 +00002292 return al.dexJarFile
2293}
2294
2295func (al *ApiLibrary) DexJarInstallPath() android.Path {
2296 return al.dexJarFile.Path()
2297}
2298
2299func (al *ApiLibrary) ClassLoaderContexts() dexpreopt.ClassLoaderContextMap {
2300 return nil
2301}
2302
2303// java_api_library constitutes the sdk, and does not build against one
2304func (al *ApiLibrary) SdkVersion(ctx android.EarlyModuleContext) android.SdkSpec {
2305 return android.SdkSpecNone
2306}
2307
2308// java_api_library is always at "current". Return FutureApiLevel
2309func (al *ApiLibrary) MinSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel {
2310 return android.FutureApiLevel
2311}
2312
2313// implement the following interfaces for hiddenapi processing
2314var _ hiddenAPIModule = (*ApiLibrary)(nil)
2315var _ UsesLibraryDependency = (*ApiLibrary)(nil)
2316
Colin Cross2fe66872015-03-30 17:20:39 -07002317//
2318// Java prebuilts
2319//
2320
Colin Cross74d73e22017-08-02 11:05:49 -07002321type ImportProperties struct {
Paul Duffina04c1072020-03-02 10:16:35 +00002322 Jars []string `android:"path,arch_variant"`
Colin Cross461bd1a2017-10-20 13:59:18 -07002323
Jaewoong Jung56e12db2021-04-02 00:38:25 +00002324 // The version of the SDK that the source prebuilt file was built against. Defaults to the
2325 // current version if not specified.
Nan Zhangea568a42017-11-08 21:20:04 -08002326 Sdk_version *string
Colin Cross535e2cf2017-10-20 17:57:49 -07002327
Jaewoong Jung56e12db2021-04-02 00:38:25 +00002328 // The minimum version of the SDK that this module supports. Defaults to sdk_version if not
2329 // specified.
2330 Min_sdk_version *string
2331
William Loh5a082f92022-05-17 20:21:50 +00002332 // The max sdk version placeholder used to replace maxSdkVersion attributes on permission
2333 // and uses-permission tags in manifest_fixer.
2334 Replace_max_sdk_version_placeholder *string
2335
Colin Cross535e2cf2017-10-20 17:57:49 -07002336 Installable *bool
Jiyong Park1be96912018-05-28 18:02:19 +09002337
Paul Duffin869de142021-07-15 14:14:41 +01002338 // If not empty, classes are restricted to the specified packages and their sub-packages.
Paul Duffin869de142021-07-15 14:14:41 +01002339 Permitted_packages []string
2340
Jiyong Park1be96912018-05-28 18:02:19 +09002341 // List of shared java libs that this module has dependencies to
2342 Libs []string
Colin Cross37f6d792018-07-12 12:28:41 -07002343
2344 // List of files to remove from the jar file(s)
2345 Exclude_files []string
2346
2347 // List of directories to remove from the jar file(s)
2348 Exclude_dirs []string
Nan Zhang4c819fb2018-08-27 18:31:46 -07002349
2350 // if set to true, run Jetifier against .jar file. Defaults to false.
Colin Cross1001a792019-03-21 22:21:39 -07002351 Jetifier *bool
Jiyong Park4c4c0242019-10-21 14:53:15 +09002352
2353 // set the name of the output
2354 Stem *string
Jiyong Park19604de2020-03-24 16:44:11 +09002355
2356 Aidl struct {
2357 // directories that should be added as include directories for any aidl sources of modules
2358 // that depend on this module, as well as to aidl for this module.
2359 Export_include_dirs []string
2360 }
Spandan Das3cf04632024-01-19 00:22:22 +00002361
2362 // Name of the source soong module that gets shadowed by this prebuilt
2363 // If unspecified, follows the naming convention that the source module of
2364 // the prebuilt is Name() without "prebuilt_" prefix
2365 Source_module_name *string
Spandan Das23956d12024-01-19 00:22:22 +00002366
2367 // Non-nil if this java_import module was dynamically created by a java_sdk_library_import
2368 // The name is the undecorated name of the java_sdk_library as it appears in the blueprint file
2369 // (without any prebuilt_ prefix)
2370 Created_by_java_sdk_library_name *string `blueprint:"mutated"`
Jihoon Kangfe914ed2024-02-12 22:49:21 +00002371
2372 // Property signifying whether the module provides stubs jar or not.
2373 Is_stubs_module *bool
Colin Cross74d73e22017-08-02 11:05:49 -07002374}
2375
2376type Import struct {
Colin Cross635c3b02016-05-18 15:37:25 -07002377 android.ModuleBase
Colin Cross48de9a42018-10-02 13:53:33 -07002378 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09002379 android.ApexModuleBase
Colin Crossec7a0422017-07-07 14:47:12 -07002380 prebuilt android.Prebuilt
Colin Cross2fe66872015-03-30 17:20:39 -07002381
Paul Duffin0d3c2e12020-05-17 08:34:50 +01002382 // Functionality common to Module and Import.
2383 embeddableInModuleAndImport
2384
Liz Kammerd6c31d22020-08-05 15:40:41 -07002385 hiddenAPI
2386 dexer
Bill Peckhamff89ffa2020-12-23 16:13:04 -08002387 dexpreopter
Liz Kammerd6c31d22020-08-05 15:40:41 -07002388
Colin Cross74d73e22017-08-02 11:05:49 -07002389 properties ImportProperties
2390
Liz Kammerd6c31d22020-08-05 15:40:41 -07002391 // output file containing classes.dex and resources
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002392 dexJarFile OptionalDexJarPath
Spandan Dasfae468e2023-12-12 23:23:53 +00002393 dexJarFileErr error
Jeongik Chad5fe8782021-07-08 01:13:11 +09002394 dexJarInstallFile android.Path
Liz Kammerd6c31d22020-08-05 15:40:41 -07002395
Colin Cross0a6e0072017-08-30 14:24:55 -07002396 combinedClasspathFile android.Path
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +01002397 classLoaderContexts dexpreopt.ClassLoaderContextMap
Jiyong Park19604de2020-03-24 16:44:11 +09002398 exportAidlIncludeDirs android.Paths
Colin Cross56a83212020-09-15 18:30:11 -07002399
2400 hideApexVariantFromMake bool
Jiyong Park92315372021-04-02 08:45:46 +09002401
2402 sdkVersion android.SdkSpec
Spandan Das8c9ae7e2023-03-03 21:20:36 +00002403 minSdkVersion android.ApiLevel
Jihoon Kangfe914ed2024-02-12 22:49:21 +00002404
2405 stubsLinkType StubsLinkType
Colin Cross2fe66872015-03-30 17:20:39 -07002406}
2407
Paul Duffin630b11e2021-07-15 13:35:26 +01002408var _ PermittedPackagesForUpdatableBootJars = (*Import)(nil)
2409
2410func (j *Import) PermittedPackagesForUpdatableBootJars() []string {
2411 return j.properties.Permitted_packages
2412}
2413
Jiyong Park92315372021-04-02 08:45:46 +09002414func (j *Import) SdkVersion(ctx android.EarlyModuleContext) android.SdkSpec {
2415 return android.SdkSpecFrom(ctx, String(j.properties.Sdk_version))
Liz Kammer2d2fd852020-08-12 14:42:30 -07002416}
2417
Jiyong Parkf1691d22021-03-29 20:11:58 +09002418func (j *Import) SystemModules() string {
Liz Kammerd6c31d22020-08-05 15:40:41 -07002419 return "none"
2420}
2421
Spandan Das8c9ae7e2023-03-03 21:20:36 +00002422func (j *Import) MinSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel {
Jaewoong Jung56e12db2021-04-02 00:38:25 +00002423 if j.properties.Min_sdk_version != nil {
Spandan Das8c9ae7e2023-03-03 21:20:36 +00002424 return android.ApiLevelFrom(ctx, *j.properties.Min_sdk_version)
Jaewoong Jung56e12db2021-04-02 00:38:25 +00002425 }
Spandan Das8c9ae7e2023-03-03 21:20:36 +00002426 return j.SdkVersion(ctx).ApiLevel
Colin Cross83bb3162018-06-25 15:48:06 -07002427}
2428
Spandan Dasa26eda72023-03-02 00:56:06 +00002429func (j *Import) ReplaceMaxSdkVersionPlaceholder(ctx android.EarlyModuleContext) android.ApiLevel {
William Loh5a082f92022-05-17 20:21:50 +00002430 if j.properties.Replace_max_sdk_version_placeholder != nil {
Spandan Dasa26eda72023-03-02 00:56:06 +00002431 return android.ApiLevelFrom(ctx, *j.properties.Replace_max_sdk_version_placeholder)
William Loh5a082f92022-05-17 20:21:50 +00002432 }
Spandan Dasa26eda72023-03-02 00:56:06 +00002433 // Default is PrivateApiLevel
2434 return android.SdkSpecPrivate.ApiLevel
William Loh5a082f92022-05-17 20:21:50 +00002435}
2436
Spandan Dasca70fc42023-03-01 23:38:49 +00002437func (j *Import) TargetSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel {
2438 return j.SdkVersion(ctx).ApiLevel
Artur Satayev480e25b2020-04-27 18:53:18 +01002439}
2440
Colin Cross74d73e22017-08-02 11:05:49 -07002441func (j *Import) Prebuilt() *android.Prebuilt {
Colin Crossec7a0422017-07-07 14:47:12 -07002442 return &j.prebuilt
2443}
2444
Colin Cross74d73e22017-08-02 11:05:49 -07002445func (j *Import) PrebuiltSrcs() []string {
2446 return j.properties.Jars
2447}
2448
Spandan Das3cf04632024-01-19 00:22:22 +00002449func (j *Import) BaseModuleName() string {
2450 return proptools.StringDefault(j.properties.Source_module_name, j.ModuleBase.Name())
2451}
2452
Colin Cross74d73e22017-08-02 11:05:49 -07002453func (j *Import) Name() string {
Colin Cross5ea9bcc2017-07-27 15:41:32 -07002454 return j.prebuilt.Name(j.ModuleBase.Name())
2455}
2456
Jiyong Park0b238752019-10-29 11:23:10 +09002457func (j *Import) Stem() string {
Spandan Das3cf04632024-01-19 00:22:22 +00002458 return proptools.StringDefault(j.properties.Stem, j.BaseModuleName())
Jiyong Park0b238752019-10-29 11:23:10 +09002459}
2460
Spandan Das23956d12024-01-19 00:22:22 +00002461func (j *Import) CreatedByJavaSdkLibraryName() *string {
2462 return j.properties.Created_by_java_sdk_library_name
2463}
2464
Jiyong Park618922e2020-01-08 13:35:43 +09002465func (a *Import) JacocoReportClassesFile() android.Path {
2466 return nil
2467}
2468
Bill Peckhama41a6962021-01-11 10:58:54 -08002469func (j *Import) LintDepSets() LintDepSets {
2470 return LintDepSets{}
2471}
2472
Jaewoong Jung476b9d62021-05-10 15:30:00 -07002473func (j *Import) getStrictUpdatabilityLinting() bool {
2474 return false
2475}
2476
2477func (j *Import) setStrictUpdatabilityLinting(bool) {
2478}
2479
Colin Cross74d73e22017-08-02 11:05:49 -07002480func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross42d48b72018-08-29 14:10:52 -07002481 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
Liz Kammerd6c31d22020-08-05 15:40:41 -07002482
2483 if ctx.Device() && Bool(j.dexProperties.Compile_dex) {
Jiyong Parkf1691d22021-03-29 20:11:58 +09002484 sdkDeps(ctx, android.SdkContext(j), j.dexer)
Liz Kammerd6c31d22020-08-05 15:40:41 -07002485 }
Colin Cross1e676be2016-10-12 14:38:15 -07002486}
2487
Sam Delmerico277795c2022-02-25 17:04:37 +00002488func (j *Import) commonBuildActions(ctx android.ModuleContext) {
Jiyong Park92315372021-04-02 08:45:46 +09002489 j.sdkVersion = j.SdkVersion(ctx)
2490 j.minSdkVersion = j.MinSdkVersion(ctx)
2491
Colin Crossff694a82023-12-13 15:54:49 -08002492 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
2493 if !apexInfo.IsForPlatform() {
Colin Cross56a83212020-09-15 18:30:11 -07002494 j.hideApexVariantFromMake = true
2495 }
2496
Dan Willemsen8e6b3712021-09-20 23:11:24 -07002497 if ctx.Windows() {
2498 j.HideFromMake()
2499 }
Jihoon Kangfe914ed2024-02-12 22:49:21 +00002500
2501 if proptools.Bool(j.properties.Is_stubs_module) {
2502 j.stubsLinkType = Stubs
2503 } else {
2504 j.stubsLinkType = Implementation
2505 }
Sam Delmerico277795c2022-02-25 17:04:37 +00002506}
2507
2508func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
2509 j.commonBuildActions(ctx)
Dan Willemsen8e6b3712021-09-20 23:11:24 -07002510
Colin Cross8a497952019-03-05 22:25:09 -08002511 jars := android.PathsForModuleSrc(ctx, j.properties.Jars)
Colin Crosse1d62a82015-04-03 16:53:05 -07002512
Jiyong Park0b238752019-10-29 11:23:10 +09002513 jarName := j.Stem() + ".jar"
Nan Zhang4c819fb2018-08-27 18:31:46 -07002514 outputFile := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07002515 TransformJarsToJar(ctx, outputFile, "for prebuilts", jars, android.OptionalPath{},
2516 false, j.properties.Exclude_files, j.properties.Exclude_dirs)
Colin Cross1001a792019-03-21 22:21:39 -07002517 if Bool(j.properties.Jetifier) {
Nan Zhang4c819fb2018-08-27 18:31:46 -07002518 inputFile := outputFile
2519 outputFile = android.PathForModuleOut(ctx, "jetifier", jarName)
2520 TransformJetifier(ctx, outputFile, inputFile)
2521 }
Colin Crosse9a275b2017-10-16 17:09:48 -07002522 j.combinedClasspathFile = outputFile
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +01002523 j.classLoaderContexts = make(dexpreopt.ClassLoaderContextMap)
Paul Duffin859fe962020-05-15 10:20:31 +01002524
Liz Kammerd6c31d22020-08-05 15:40:41 -07002525 var flags javaBuilderFlags
2526
Sam Delmerico9f9c0a22022-11-29 11:19:37 -05002527 j.collectTransitiveHeaderJars(ctx)
Jiyong Park1be96912018-05-28 18:02:19 +09002528 ctx.VisitDirectDeps(func(module android.Module) {
Jiyong Park1be96912018-05-28 18:02:19 +09002529 tag := ctx.OtherModuleDependencyTag(module)
Colin Cross313aa542023-12-13 13:47:44 -08002530 if dep, ok := android.OtherModuleProvider(ctx, module, JavaInfoProvider); ok {
Jiyong Park1be96912018-05-28 18:02:19 +09002531 switch tag {
Liz Kammeref28a4c2022-09-23 16:50:56 -04002532 case libTag, sdkLibTag:
Colin Cross9bb9bfb2022-03-17 11:12:32 -07002533 flags.classpath = append(flags.classpath, dep.HeaderJars...)
2534 flags.dexClasspath = append(flags.dexClasspath, dep.HeaderJars...)
2535 case staticLibTag:
Colin Crossdcf71b22021-02-01 13:59:03 -08002536 flags.classpath = append(flags.classpath, dep.HeaderJars...)
Liz Kammerd6c31d22020-08-05 15:40:41 -07002537 case bootClasspathTag:
Colin Crossdcf71b22021-02-01 13:59:03 -08002538 flags.bootClasspath = append(flags.bootClasspath, dep.HeaderJars...)
Jiyong Park1be96912018-05-28 18:02:19 +09002539 }
Colin Crossdcf71b22021-02-01 13:59:03 -08002540 } else if dep, ok := module.(SdkLibraryDependency); ok {
Jiyong Park1be96912018-05-28 18:02:19 +09002541 switch tag {
Liz Kammeref28a4c2022-09-23 16:50:56 -04002542 case libTag, sdkLibTag:
Jiyong Park92315372021-04-02 08:45:46 +09002543 flags.classpath = append(flags.classpath, dep.SdkHeaderJars(ctx, j.SdkVersion(ctx))...)
Jiyong Park1be96912018-05-28 18:02:19 +09002544 }
2545 }
Ulya Trafimovich65b03192020-12-03 16:50:22 +00002546
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00002547 addCLCFromDep(ctx, module, j.classLoaderContexts)
Jiyong Park1be96912018-05-28 18:02:19 +09002548 })
2549
Sam Delmerico277795c2022-02-25 17:04:37 +00002550 j.maybeInstall(ctx, jarName, outputFile)
Jiyong Park19604de2020-03-24 16:44:11 +09002551
2552 j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.properties.Aidl.Export_include_dirs)
Liz Kammerd6c31d22020-08-05 15:40:41 -07002553
Paul Duffin064b70c2020-11-02 17:32:38 +00002554 if ctx.Device() {
2555 // If this is a variant created for a prebuilt_apex then use the dex implementation jar
2556 // obtained from the associated deapexer module.
Colin Crossff694a82023-12-13 15:54:49 -08002557 ai, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
Paul Duffin064b70c2020-11-02 17:32:38 +00002558 if ai.ForPrebuiltApex {
Paul Duffin064b70c2020-11-02 17:32:38 +00002559 // Get the path of the dex implementation jar from the `deapexer` module.
Spandan Dasfae468e2023-12-12 23:23:53 +00002560 di, err := android.FindDeapexerProviderForModule(ctx)
2561 if err != nil {
2562 // An error was found, possibly due to multiple apexes in the tree that export this library
2563 // Defer the error till a client tries to call DexJarBuildPath
2564 j.dexJarFileErr = err
Spandan Das3a392012024-01-17 18:26:27 +00002565 j.initHiddenAPIError(err)
Spandan Dasfae468e2023-12-12 23:23:53 +00002566 return
Martin Stjernholm44825602021-09-17 01:44:12 +01002567 }
Spandan Das5be63332023-12-13 00:06:32 +00002568 dexJarFileApexRootRelative := ApexRootRelativePathToJavaLib(j.BaseModuleName())
Jiakai Zhang81e46812023-02-08 21:56:07 +08002569 if dexOutputPath := di.PrebuiltExportPath(dexJarFileApexRootRelative); dexOutputPath != nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002570 dexJarFile := makeDexJarPathFromPath(dexOutputPath)
2571 j.dexJarFile = dexJarFile
Spandan Das5be63332023-12-13 00:06:32 +00002572 installPath := android.PathForModuleInPartitionInstall(ctx, "apex", ai.ApexVariationName, ApexRootRelativePathToJavaLib(j.BaseModuleName()))
Jiakai Zhang5b24f722021-09-30 09:32:57 +00002573 j.dexJarInstallFile = installPath
Paul Duffin74d18d12021-05-14 14:18:47 +01002574
Spandan Dase21a8d42024-01-23 23:56:29 +00002575 j.dexpreopter.installPath = j.dexpreopter.getInstallPath(ctx, android.RemoveOptionalPrebuiltPrefix(ctx.ModuleName()), installPath)
Jiakai Zhang22450f22021-10-11 03:05:20 +00002576 setUncompressDex(ctx, &j.dexpreopter, &j.dexer)
Jiakai Zhang5b24f722021-09-30 09:32:57 +00002577 j.dexpreopter.uncompressedDex = *j.dexProperties.Uncompress_dex
Jiakai Zhang81e46812023-02-08 21:56:07 +08002578
2579 if profilePath := di.PrebuiltExportPath(dexJarFileApexRootRelative + ".prof"); profilePath != nil {
2580 j.dexpreopter.inputProfilePathOnHost = profilePath
2581 }
2582
Jiakai Zhang22450f22021-10-11 03:05:20 +00002583 // Initialize the hiddenapi structure.
2584 j.initHiddenAPI(ctx, dexJarFile, outputFile, j.dexProperties.Uncompress_dex)
Paul Duffin9d67ca62021-02-03 20:06:33 +00002585 } else {
Paul Duffin064b70c2020-11-02 17:32:38 +00002586 // This should never happen as a variant for a prebuilt_apex is only created if the
2587 // prebuilt_apex has been configured to export the java library dex file.
Martin Stjernholm44825602021-09-17 01:44:12 +01002588 ctx.ModuleErrorf("internal error: no dex implementation jar available from prebuilt APEX %s", di.ApexModuleName())
Paul Duffin064b70c2020-11-02 17:32:38 +00002589 }
2590 } else if Bool(j.dexProperties.Compile_dex) {
Jiyong Parkf1691d22021-03-29 20:11:58 +09002591 sdkDep := decodeSdkDep(ctx, android.SdkContext(j))
Paul Duffin064b70c2020-11-02 17:32:38 +00002592 if sdkDep.invalidVersion {
2593 ctx.AddMissingDependencies(sdkDep.bootclasspath)
2594 ctx.AddMissingDependencies(sdkDep.java9Classpath)
2595 } else if sdkDep.useFiles {
2596 // sdkDep.jar is actually equivalent to turbine header.jar.
2597 flags.classpath = append(flags.classpath, sdkDep.jars...)
2598 }
2599
2600 // Dex compilation
2601
Jiakai Zhang519c5c82021-09-16 06:15:39 +00002602 j.dexpreopter.installPath = j.dexpreopter.getInstallPath(
Spandan Dase21a8d42024-01-23 23:56:29 +00002603 ctx, android.RemoveOptionalPrebuiltPrefix(ctx.ModuleName()), android.PathForModuleInstall(ctx, "framework", jarName))
Jiakai Zhang22450f22021-10-11 03:05:20 +00002604 setUncompressDex(ctx, &j.dexpreopter, &j.dexer)
Paul Duffin064b70c2020-11-02 17:32:38 +00002605 j.dexpreopter.uncompressedDex = *j.dexProperties.Uncompress_dex
2606
Paul Duffin612e6102021-02-02 13:38:13 +00002607 var dexOutputFile android.OutputPath
Spandan Dasc404cc72023-02-23 18:05:05 +00002608 dexParams := &compileDexParams{
2609 flags: flags,
2610 sdkVersion: j.SdkVersion(ctx),
2611 minSdkVersion: j.MinSdkVersion(ctx),
2612 classesJar: outputFile,
2613 jarName: jarName,
2614 }
2615
2616 dexOutputFile = j.dexer.compileDex(ctx, dexParams)
Paul Duffin064b70c2020-11-02 17:32:38 +00002617 if ctx.Failed() {
2618 return
2619 }
2620
Paul Duffin74d18d12021-05-14 14:18:47 +01002621 // Initialize the hiddenapi structure.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002622 j.initHiddenAPI(ctx, makeDexJarPathFromPath(dexOutputFile), outputFile, j.dexProperties.Uncompress_dex)
Paul Duffinafaa47c2021-05-14 13:04:04 +01002623
2624 // Encode hidden API flags in dex file.
Paul Duffin1bbd0622021-05-14 15:52:25 +01002625 dexOutputFile = j.hiddenAPIEncodeDex(ctx, dexOutputFile)
Paul Duffin064b70c2020-11-02 17:32:38 +00002626
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002627 j.dexJarFile = makeDexJarPathFromPath(dexOutputFile)
Jeongik Chad5fe8782021-07-08 01:13:11 +09002628 j.dexJarInstallFile = android.PathForModuleInstall(ctx, "framework", jarName)
Liz Kammerd6c31d22020-08-05 15:40:41 -07002629 }
Liz Kammerd6c31d22020-08-05 15:40:41 -07002630 }
Colin Crossdcf71b22021-02-01 13:59:03 -08002631
Colin Cross40213022023-12-13 15:19:49 -08002632 android.SetProvider(ctx, JavaInfoProvider, JavaInfo{
Colin Crossdcf71b22021-02-01 13:59:03 -08002633 HeaderJars: android.PathsIfNonNil(j.combinedClasspathFile),
Sam Delmerico9f9c0a22022-11-29 11:19:37 -05002634 TransitiveLibsHeaderJars: j.transitiveLibsHeaderJars,
2635 TransitiveStaticLibsHeaderJars: j.transitiveStaticLibsHeaderJars,
Colin Crossdcf71b22021-02-01 13:59:03 -08002636 ImplementationAndResourcesJars: android.PathsIfNonNil(j.combinedClasspathFile),
2637 ImplementationJars: android.PathsIfNonNil(j.combinedClasspathFile),
2638 AidlIncludeDirs: j.exportAidlIncludeDirs,
Jihoon Kangfe914ed2024-02-12 22:49:21 +00002639 StubsLinkType: j.stubsLinkType,
Joe Onorato6fe59eb2023-07-16 13:20:33 -07002640 // TODO(b/289117800): LOCAL_ACONFIG_FILES for prebuilts
Colin Crossdcf71b22021-02-01 13:59:03 -08002641 })
Colin Cross2fe66872015-03-30 17:20:39 -07002642}
2643
Sam Delmerico277795c2022-02-25 17:04:37 +00002644func (j *Import) maybeInstall(ctx android.ModuleContext, jarName string, outputFile android.Path) {
2645 if !Bool(j.properties.Installable) {
2646 return
2647 }
2648
2649 var installDir android.InstallPath
2650 if ctx.InstallInTestcases() {
2651 var archDir string
2652 if !ctx.Host() {
2653 archDir = ctx.DeviceConfig().DeviceArch()
2654 }
2655 installDir = android.PathForModuleInstall(ctx, ctx.ModuleName(), archDir)
2656 } else {
2657 installDir = android.PathForModuleInstall(ctx, "framework")
2658 }
2659 ctx.InstallFile(installDir, jarName, outputFile)
2660}
2661
Paul Duffinaa55f742020-10-06 17:20:13 +01002662func (j *Import) OutputFiles(tag string) (android.Paths, error) {
2663 switch tag {
Saeid Farivar Asanjan128fe5c2020-10-15 17:54:40 +00002664 case "", ".jar":
Paul Duffinaa55f742020-10-06 17:20:13 +01002665 return android.Paths{j.combinedClasspathFile}, nil
2666 default:
2667 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
2668 }
2669}
2670
2671var _ android.OutputFileProducer = (*Import)(nil)
2672
Nan Zhanged19fc32017-10-19 13:06:22 -07002673func (j *Import) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08002674 if j.combinedClasspathFile == nil {
2675 return nil
2676 }
Colin Cross37f6d792018-07-12 12:28:41 -07002677 return android.Paths{j.combinedClasspathFile}
Nan Zhanged19fc32017-10-19 13:06:22 -07002678}
2679
Colin Cross331a1212018-08-15 20:40:52 -07002680func (j *Import) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08002681 if j.combinedClasspathFile == nil {
2682 return nil
2683 }
Colin Cross331a1212018-08-15 20:40:52 -07002684 return android.Paths{j.combinedClasspathFile}
2685}
2686
Spandan Das59a4a2b2024-01-09 21:35:56 +00002687func (j *Import) DexJarBuildPath(ctx android.ModuleErrorfContext) OptionalDexJarPath {
Spandan Dasfae468e2023-12-12 23:23:53 +00002688 if j.dexJarFileErr != nil {
Spandan Das59a4a2b2024-01-09 21:35:56 +00002689 ctx.ModuleErrorf(j.dexJarFileErr.Error())
Spandan Dasfae468e2023-12-12 23:23:53 +00002690 }
Liz Kammerd6c31d22020-08-05 15:40:41 -07002691 return j.dexJarFile
Colin Crossf24a22a2019-01-31 14:12:44 -08002692}
2693
Ulya Trafimovich9f3052c2020-06-09 14:31:19 +01002694func (j *Import) DexJarInstallPath() android.Path {
Jeongik Chad5fe8782021-07-08 01:13:11 +09002695 return j.dexJarInstallFile
Ulya Trafimovich9f3052c2020-06-09 14:31:19 +01002696}
2697
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +01002698func (j *Import) ClassLoaderContexts() dexpreopt.ClassLoaderContextMap {
2699 return j.classLoaderContexts
Jiyong Park1be96912018-05-28 18:02:19 +09002700}
2701
Jiyong Park45bf82e2020-12-15 22:29:02 +09002702var _ android.ApexModule = (*Import)(nil)
2703
2704// Implements android.ApexModule
Jiyong Park0f80c182020-01-31 02:49:53 +09002705func (j *Import) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
Paul Duffin0d3c2e12020-05-17 08:34:50 +01002706 return j.depIsInSameApex(ctx, dep)
Jiyong Park0f80c182020-01-31 02:49:53 +09002707}
2708
Jiyong Park45bf82e2020-12-15 22:29:02 +09002709// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07002710func (j *Import) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
2711 sdkVersion android.ApiLevel) error {
Spandan Das7fa982c2023-02-24 18:38:56 +00002712 sdkVersionSpec := j.SdkVersion(ctx)
Spandan Das8c9ae7e2023-03-03 21:20:36 +00002713 minSdkVersion := j.MinSdkVersion(ctx)
2714 if !minSdkVersion.Specified() {
Jaewoong Jung56e12db2021-04-02 00:38:25 +00002715 return fmt.Errorf("min_sdk_version is not specified")
2716 }
Spandan Das7fa982c2023-02-24 18:38:56 +00002717 // If the module is compiling against core (via sdk_version), skip comparison check.
2718 if sdkVersionSpec.Kind == android.SdkCore {
Jaewoong Jung56e12db2021-04-02 00:38:25 +00002719 return nil
2720 }
Spandan Das7fa982c2023-02-24 18:38:56 +00002721 if minSdkVersion.GreaterThan(sdkVersion) {
2722 return fmt.Errorf("newer SDK(%v)", minSdkVersion)
Jaewoong Jung56e12db2021-04-02 00:38:25 +00002723 }
Jooyung Han749dc692020-04-15 11:03:39 +09002724 return nil
2725}
2726
Paul Duffinfef55002021-06-17 14:56:05 +01002727// requiredFilesFromPrebuiltApexForImport returns information about the files that a java_import or
2728// java_sdk_library_import with the specified base module name requires to be exported from a
2729// prebuilt_apex/apex_set.
Jiakai Zhang81e46812023-02-08 21:56:07 +08002730func requiredFilesFromPrebuiltApexForImport(name string, d *dexpreopter) []string {
Spandan Das5be63332023-12-13 00:06:32 +00002731 dexJarFileApexRootRelative := ApexRootRelativePathToJavaLib(name)
Paul Duffinb4bbf2c2021-06-17 15:59:07 +01002732 // Add the dex implementation jar to the set of exported files.
Jiakai Zhang81e46812023-02-08 21:56:07 +08002733 files := []string{
2734 dexJarFileApexRootRelative,
Paul Duffinfef55002021-06-17 14:56:05 +01002735 }
Jiakai Zhang81e46812023-02-08 21:56:07 +08002736 if BoolDefault(d.importDexpreoptProperties.Dex_preopt.Profile_guided, false) {
2737 files = append(files, dexJarFileApexRootRelative+".prof")
2738 }
2739 return files
Paul Duffinfef55002021-06-17 14:56:05 +01002740}
2741
Spandan Das5be63332023-12-13 00:06:32 +00002742// ApexRootRelativePathToJavaLib returns the path, relative to the root of the apex's contents, for
Paul Duffinb4bbf2c2021-06-17 15:59:07 +01002743// the java library with the specified name.
Spandan Das5be63332023-12-13 00:06:32 +00002744func ApexRootRelativePathToJavaLib(name string) string {
Paul Duffinb4bbf2c2021-06-17 15:59:07 +01002745 return filepath.Join("javalib", name+".jar")
2746}
2747
Paul Duffinfef55002021-06-17 14:56:05 +01002748var _ android.RequiredFilesFromPrebuiltApex = (*Import)(nil)
2749
Paul Duffinb4bbf2c2021-06-17 15:59:07 +01002750func (j *Import) RequiredFilesFromPrebuiltApex(_ android.BaseModuleContext) []string {
Paul Duffinfef55002021-06-17 14:56:05 +01002751 name := j.BaseModuleName()
Jiakai Zhang81e46812023-02-08 21:56:07 +08002752 return requiredFilesFromPrebuiltApexForImport(name, &j.dexpreopter)
Paul Duffinfef55002021-06-17 14:56:05 +01002753}
2754
Spandan Das2ea84dd2024-01-25 22:12:50 +00002755func (j *Import) UseProfileGuidedDexpreopt() bool {
2756 return proptools.Bool(j.importDexpreoptProperties.Dex_preopt.Profile_guided)
2757}
2758
albaltai36ff7dc2018-12-25 14:35:23 +08002759// Add compile time check for interface implementation
2760var _ android.IDEInfo = (*Import)(nil)
2761var _ android.IDECustomizedModuleName = (*Import)(nil)
2762
Brandon Lee5d45c6f2018-08-15 15:35:38 -07002763// Collect information for opening IDE project files in java/jdeps.go.
Brandon Lee5d45c6f2018-08-15 15:35:38 -07002764
2765func (j *Import) IDEInfo(dpInfo *android.IdeInfo) {
2766 dpInfo.Jars = append(dpInfo.Jars, j.PrebuiltSrcs()...)
2767}
2768
2769func (j *Import) IDECustomizedModuleName() string {
2770 // TODO(b/113562217): Extract the base module name from the Import name, often the Import name
2771 // has a prefix "prebuilt_". Remove the prefix explicitly if needed until we find a better
2772 // solution to get the Import name.
Ulya Trafimovich497a0932021-07-14 16:35:33 +01002773 return android.RemoveOptionalPrebuiltPrefix(j.Name())
Brandon Lee5d45c6f2018-08-15 15:35:38 -07002774}
2775
Colin Cross74d73e22017-08-02 11:05:49 -07002776var _ android.PrebuiltInterface = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07002777
Bill Peckhamff89ffa2020-12-23 16:13:04 -08002778func (j *Import) IsInstallable() bool {
2779 return Bool(j.properties.Installable)
2780}
2781
Jiakai Zhang519c5c82021-09-16 06:15:39 +00002782var _ DexpreopterInterface = (*Import)(nil)
Bill Peckhamff89ffa2020-12-23 16:13:04 -08002783
Colin Cross1b16b0e2019-02-12 14:41:32 -08002784// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library module.
2785//
2786// By default, a java_import has a single variant that expects a `.jar` file containing `.class` files that were
2787// compiled against an Android classpath.
2788//
2789// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
2790// for host modules.
Colin Cross74d73e22017-08-02 11:05:49 -07002791func ImportFactory() android.Module {
2792 module := &Import{}
Colin Cross36242852017-06-23 15:06:31 -07002793
Liz Kammerd6c31d22020-08-05 15:40:41 -07002794 module.AddProperties(
2795 &module.properties,
2796 &module.dexer.dexProperties,
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08002797 &module.importDexpreoptProperties,
Liz Kammerd6c31d22020-08-05 15:40:41 -07002798 )
Colin Cross74d73e22017-08-02 11:05:49 -07002799
Paul Duffin71b33cc2021-06-23 11:39:47 +01002800 module.initModuleAndImport(module)
Paul Duffin859fe962020-05-15 10:20:31 +01002801
Liz Kammerd6c31d22020-08-05 15:40:41 -07002802 module.dexProperties.Optimize.EnabledByDefault = false
2803
Colin Cross74d73e22017-08-02 11:05:49 -07002804 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002805 android.InitApexModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09002806 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross36242852017-06-23 15:06:31 -07002807 return module
Colin Cross2fe66872015-03-30 17:20:39 -07002808}
2809
Colin Cross1b16b0e2019-02-12 14:41:32 -08002810// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library_host
2811// module.
2812//
2813// A java_import_host has a single variant that expects a `.jar` file containing `.class` files that were
2814// compiled against a host bootclasspath.
Colin Cross74d73e22017-08-02 11:05:49 -07002815func ImportFactoryHost() android.Module {
2816 module := &Import{}
2817
2818 module.AddProperties(&module.properties)
2819
2820 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002821 android.InitApexModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09002822 InitJavaModule(module, android.HostSupported)
Colin Cross74d73e22017-08-02 11:05:49 -07002823 return module
2824}
2825
Colin Cross42be7612019-02-21 18:12:14 -08002826// dex_import module
2827
2828type DexImportProperties struct {
Colin Cross5cfc70d2019-07-15 13:36:55 -07002829 Jars []string `android:"path"`
Jiyong Park4c4c0242019-10-21 14:53:15 +09002830
2831 // set the name of the output
2832 Stem *string
Colin Cross42be7612019-02-21 18:12:14 -08002833}
2834
2835type DexImport struct {
2836 android.ModuleBase
2837 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09002838 android.ApexModuleBase
Colin Cross42be7612019-02-21 18:12:14 -08002839 prebuilt android.Prebuilt
2840
2841 properties DexImportProperties
2842
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002843 dexJarFile OptionalDexJarPath
Colin Cross42be7612019-02-21 18:12:14 -08002844
2845 dexpreopter
Colin Cross56a83212020-09-15 18:30:11 -07002846
2847 hideApexVariantFromMake bool
Colin Cross42be7612019-02-21 18:12:14 -08002848}
2849
2850func (j *DexImport) Prebuilt() *android.Prebuilt {
2851 return &j.prebuilt
2852}
2853
2854func (j *DexImport) PrebuiltSrcs() []string {
2855 return j.properties.Jars
2856}
2857
2858func (j *DexImport) Name() string {
2859 return j.prebuilt.Name(j.ModuleBase.Name())
2860}
2861
Jiyong Park0b238752019-10-29 11:23:10 +09002862func (j *DexImport) Stem() string {
2863 return proptools.StringDefault(j.properties.Stem, j.ModuleBase.Name())
2864}
2865
Jiyong Park77acec62020-06-01 21:39:15 +09002866func (a *DexImport) JacocoReportClassesFile() android.Path {
2867 return nil
2868}
2869
Colin Cross08dca382020-07-21 20:31:17 -07002870func (a *DexImport) LintDepSets() LintDepSets {
2871 return LintDepSets{}
2872}
2873
Martin Stjernholm6d415272020-01-31 17:10:36 +00002874func (j *DexImport) IsInstallable() bool {
2875 return true
2876}
2877
Jaewoong Jung476b9d62021-05-10 15:30:00 -07002878func (j *DexImport) getStrictUpdatabilityLinting() bool {
2879 return false
2880}
2881
2882func (j *DexImport) setStrictUpdatabilityLinting(bool) {
2883}
2884
Colin Cross42be7612019-02-21 18:12:14 -08002885func (j *DexImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
2886 if len(j.properties.Jars) != 1 {
2887 ctx.PropertyErrorf("jars", "exactly one jar must be provided")
2888 }
2889
Colin Crossff694a82023-12-13 15:54:49 -08002890 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
Colin Cross56a83212020-09-15 18:30:11 -07002891 if !apexInfo.IsForPlatform() {
2892 j.hideApexVariantFromMake = true
2893 }
2894
Jiakai Zhang519c5c82021-09-16 06:15:39 +00002895 j.dexpreopter.installPath = j.dexpreopter.getInstallPath(
Spandan Dase21a8d42024-01-23 23:56:29 +00002896 ctx, android.RemoveOptionalPrebuiltPrefix(ctx.ModuleName()), android.PathForModuleInstall(ctx, "framework", j.Stem()+".jar"))
2897 j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, android.RemoveOptionalPrebuiltPrefix(ctx.ModuleName()), &j.dexpreopter)
Colin Cross42be7612019-02-21 18:12:14 -08002898
2899 inputJar := ctx.ExpandSource(j.properties.Jars[0], "jars")
2900 dexOutputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar")
2901
2902 if j.dexpreopter.uncompressedDex {
Colin Crossf1a035e2020-11-16 17:32:30 -08002903 rule := android.NewRuleBuilder(pctx, ctx)
Colin Cross42be7612019-02-21 18:12:14 -08002904
2905 temporary := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar.unaligned")
2906 rule.Temporary(temporary)
2907
2908 // use zip2zip to uncompress classes*.dex files
2909 rule.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -08002910 BuiltTool("zip2zip").
Colin Cross42be7612019-02-21 18:12:14 -08002911 FlagWithInput("-i ", inputJar).
2912 FlagWithOutput("-o ", temporary).
2913 FlagWithArg("-0 ", "'classes*.dex'")
2914
2915 // use zipalign to align uncompressed classes*.dex files
2916 rule.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -08002917 BuiltTool("zipalign").
Colin Cross42be7612019-02-21 18:12:14 -08002918 Flag("-f").
2919 Text("4").
2920 Input(temporary).
2921 Output(dexOutputFile)
2922
2923 rule.DeleteTemporaryFiles()
2924
Colin Crossf1a035e2020-11-16 17:32:30 -08002925 rule.Build("uncompress_dex", "uncompress dex")
Colin Cross42be7612019-02-21 18:12:14 -08002926 } else {
2927 ctx.Build(pctx, android.BuildParams{
2928 Rule: android.Cp,
2929 Input: inputJar,
2930 Output: dexOutputFile,
2931 })
2932 }
2933
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002934 j.dexJarFile = makeDexJarPathFromPath(dexOutputFile)
Colin Cross42be7612019-02-21 18:12:14 -08002935
Spandan Dase21a8d42024-01-23 23:56:29 +00002936 j.dexpreopt(ctx, android.RemoveOptionalPrebuiltPrefix(ctx.ModuleName()), dexOutputFile)
Colin Cross42be7612019-02-21 18:12:14 -08002937
Colin Cross56a83212020-09-15 18:30:11 -07002938 if apexInfo.IsForPlatform() {
Jiyong Park01bca752020-06-08 19:24:09 +09002939 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
2940 j.Stem()+".jar", dexOutputFile)
2941 }
Colin Cross42be7612019-02-21 18:12:14 -08002942}
2943
Spandan Das59a4a2b2024-01-09 21:35:56 +00002944func (j *DexImport) DexJarBuildPath(ctx android.ModuleErrorfContext) OptionalDexJarPath {
Colin Cross42be7612019-02-21 18:12:14 -08002945 return j.dexJarFile
2946}
2947
Jiyong Park45bf82e2020-12-15 22:29:02 +09002948var _ android.ApexModule = (*DexImport)(nil)
2949
2950// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07002951func (j *DexImport) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
2952 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09002953 // we don't check prebuilt modules for sdk_version
2954 return nil
2955}
2956
Colin Cross42be7612019-02-21 18:12:14 -08002957// dex_import imports a `.jar` file containing classes.dex files.
2958//
2959// A dex_import module cannot be used as a dependency of a java_* or android_* module, it can only be installed
2960// to the device.
2961func DexImportFactory() android.Module {
2962 module := &DexImport{}
2963
2964 module.AddProperties(&module.properties)
2965
2966 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002967 android.InitApexModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09002968 InitJavaModule(module, android.DeviceSupported)
Colin Cross42be7612019-02-21 18:12:14 -08002969 return module
2970}
2971
Colin Cross89536d42017-07-07 14:35:50 -07002972// Defaults
Colin Cross89536d42017-07-07 14:35:50 -07002973type Defaults struct {
2974 android.ModuleBase
2975 android.DefaultsModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09002976 android.ApexModuleBase
Colin Cross89536d42017-07-07 14:35:50 -07002977}
2978
Colin Cross1b16b0e2019-02-12 14:41:32 -08002979// java_defaults provides a set of properties that can be inherited by other java or android modules.
2980//
2981// A module can use the properties from a java_defaults module using `defaults: ["defaults_module_name"]`. Each
2982// property in the defaults module that exists in the depending module will be prepended to the depending module's
2983// value for that property.
2984//
2985// Example:
2986//
Sam Delmerico277795c2022-02-25 17:04:37 +00002987// java_defaults {
2988// name: "example_defaults",
2989// srcs: ["common/**/*.java"],
2990// javacflags: ["-Xlint:all"],
2991// aaptflags: ["--auto-add-overlay"],
2992// }
Colin Cross1b16b0e2019-02-12 14:41:32 -08002993//
Sam Delmerico277795c2022-02-25 17:04:37 +00002994// java_library {
2995// name: "example",
2996// defaults: ["example_defaults"],
2997// srcs: ["example/**/*.java"],
2998// }
Colin Cross1b16b0e2019-02-12 14:41:32 -08002999//
3000// is functionally identical to:
3001//
Sam Delmerico277795c2022-02-25 17:04:37 +00003002// java_library {
3003// name: "example",
3004// srcs: [
3005// "common/**/*.java",
3006// "example/**/*.java",
3007// ],
3008// javacflags: ["-Xlint:all"],
3009// }
Paul Duffin47357662019-12-05 14:07:14 +00003010func DefaultsFactory() android.Module {
Colin Cross89536d42017-07-07 14:35:50 -07003011 module := &Defaults{}
3012
Colin Cross89536d42017-07-07 14:35:50 -07003013 module.AddProperties(
Jaewoong Jungbc15e3a2021-03-10 17:02:43 -08003014 &CommonProperties{},
3015 &DeviceProperties{},
Jooyung Han01d80d82022-01-08 12:16:32 +09003016 &OverridableDeviceProperties{},
Liz Kammera7a64f32020-07-09 15:16:41 -07003017 &DexProperties{},
Colin Cross43f08db2018-11-12 10:13:39 -08003018 &DexpreoptProperties{},
Dan Willemsen6424d172018-03-08 13:27:59 -08003019 &android.ProtoProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07003020 &aaptProperties{},
3021 &androidLibraryProperties{},
3022 &appProperties{},
3023 &appTestProperties{},
Jaewoong Jung525443a2019-02-28 15:35:54 -08003024 &overridableAppProperties{},
Kun Niubd0fd202023-05-23 17:51:44 +00003025 &hostTestProperties{},
Roland Levillainb5b0ff32020-02-04 15:45:49 +00003026 &testProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07003027 &ImportProperties{},
3028 &AARImportProperties{},
3029 &sdkLibraryProperties{},
Paul Duffin1b1e8062020-05-08 13:44:43 +01003030 &commonToSdkLibraryAndImportProperties{},
Colin Cross42be7612019-02-21 18:12:14 -08003031 &DexImportProperties{},
Jooyung Han18020ea2019-11-13 10:50:48 +09003032 &android.ApexProperties{},
Jaewoong Jungbf135462020-04-26 15:10:51 -07003033 &RuntimeResourceOverlayProperties{},
Colin Cross014489c2020-06-02 20:09:13 -07003034 &LintProperties{},
Colin Crosscbce0b02021-02-09 10:38:30 -08003035 &appTestHelperAppProperties{},
Jihoon Kang1c51f502023-01-09 23:42:40 +00003036 &JavaApiLibraryProperties{},
Jihoon Kang9272dcc2024-01-12 00:08:30 +00003037 &bootclasspathFragmentProperties{},
3038 &SourceOnlyBootclasspathProperties{},
Colin Cross89536d42017-07-07 14:35:50 -07003039 )
3040
3041 android.InitDefaultsModule(module)
Colin Cross89536d42017-07-07 14:35:50 -07003042 return module
3043}
Nan Zhangea568a42017-11-08 21:20:04 -08003044
Sasha Smundak2a4549e2018-11-05 16:49:08 -08003045func kytheExtractJavaFactory() android.Singleton {
3046 return &kytheExtractJavaSingleton{}
3047}
3048
3049type kytheExtractJavaSingleton struct {
3050}
3051
3052func (ks *kytheExtractJavaSingleton) GenerateBuildActions(ctx android.SingletonContext) {
3053 var xrefTargets android.Paths
3054 ctx.VisitAllModules(func(module android.Module) {
3055 if javaModule, ok := module.(xref); ok {
3056 xrefTargets = append(xrefTargets, javaModule.XrefJavaFiles()...)
3057 }
3058 })
3059 // TODO(asmundak): perhaps emit a rule to output a warning if there were no xrefTargets
3060 if len(xrefTargets) > 0 {
Colin Crossc3d87d32020-06-04 13:25:17 -07003061 ctx.Phony("xref_java", xrefTargets...)
Sasha Smundak2a4549e2018-11-05 16:49:08 -08003062 }
3063}
3064
Nan Zhangea568a42017-11-08 21:20:04 -08003065var Bool = proptools.Bool
Colin Cross38b40df2018-04-10 16:14:46 -07003066var BoolDefault = proptools.BoolDefault
Nan Zhangea568a42017-11-08 21:20:04 -08003067var String = proptools.String
Sam Delmerico1717b3b2023-07-18 15:07:24 -04003068var inList = android.InList[string]
Ulya Trafimovich65b03192020-12-03 16:50:22 +00003069
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00003070// Add class loader context (CLC) of a given dependency to the current CLC.
3071func addCLCFromDep(ctx android.ModuleContext, depModule android.Module,
3072 clcMap dexpreopt.ClassLoaderContextMap) {
3073
3074 dep, ok := depModule.(UsesLibraryDependency)
3075 if !ok {
3076 return
3077 }
3078
Ulya Trafimovich840efb62021-07-15 14:34:40 +01003079 depName := android.RemoveOptionalPrebuiltPrefix(ctx.OtherModuleName(depModule))
3080
3081 var sdkLib *string
3082 if lib, ok := depModule.(SdkLibraryDependency); ok && lib.sharedLibrary() {
3083 // A shared SDK library. This should be added as a top-level CLC element.
3084 sdkLib = &depName
3085 } else if ulib, ok := depModule.(ProvidesUsesLib); ok {
3086 // A non-SDK library disguised as an SDK library by the means of `provides_uses_lib`
3087 // property. This should be handled in the same way as a shared SDK library.
3088 sdkLib = ulib.ProvidesUsesLib()
Ulya Trafimovich65b03192020-12-03 16:50:22 +00003089 }
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00003090
3091 depTag := ctx.OtherModuleDependencyTag(depModule)
Liz Kammeref28a4c2022-09-23 16:50:56 -04003092 if IsLibDepTag(depTag) {
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00003093 // Ok, propagate <uses-library> through non-static library dependencies.
Ulya Trafimovichf5d91bb2022-05-04 12:00:02 +01003094 } else if tag, ok := depTag.(usesLibraryDependencyTag); ok && tag.sdkVersion == dexpreopt.AnySdkVersion {
3095 // Ok, propagate <uses-library> through non-compatibility <uses-library> dependencies.
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00003096 } else if depTag == staticLibTag {
3097 // Propagate <uses-library> through static library dependencies, unless it is a component
3098 // library (such as stubs). Component libraries have a dependency on their SDK library,
3099 // which should not be pulled just because of a static component library.
Ulya Trafimovich840efb62021-07-15 14:34:40 +01003100 if sdkLib != nil {
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00003101 return
3102 }
3103 } else {
3104 // Don't propagate <uses-library> for other dependency tags.
3105 return
3106 }
3107
Ulya Trafimovich840efb62021-07-15 14:34:40 +01003108 // If this is an SDK (or SDK-like) library, then it should be added as a node in the CLC tree,
3109 // and its CLC should be added as subtree of that node. Otherwise the library is not a
3110 // <uses_library> and should not be added to CLC, but the transitive <uses-library> dependencies
3111 // from its CLC should be added to the current CLC.
3112 if sdkLib != nil {
Ulya Trafimovichf5d91bb2022-05-04 12:00:02 +01003113 clcMap.AddContext(ctx, dexpreopt.AnySdkVersion, *sdkLib, false,
Spandan Das59a4a2b2024-01-09 21:35:56 +00003114 dep.DexJarBuildPath(ctx).PathOrNil(), dep.DexJarInstallPath(), dep.ClassLoaderContexts())
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00003115 } else {
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00003116 clcMap.AddContextMap(dep.ClassLoaderContexts(), depName)
3117 }
Ulya Trafimovich65b03192020-12-03 16:50:22 +00003118}
Wei Libafb6d62021-12-10 03:14:59 -08003119
Jihoon Kangfdf32362023-09-12 00:36:43 +00003120type JavaApiContributionImport struct {
3121 JavaApiContribution
3122
Spandan Das23956d12024-01-19 00:22:22 +00003123 prebuilt android.Prebuilt
3124 prebuiltProperties javaApiContributionImportProperties
3125}
3126
3127type javaApiContributionImportProperties struct {
3128 // Name of the source soong module that gets shadowed by this prebuilt
3129 // If unspecified, follows the naming convention that the source module of
3130 // the prebuilt is Name() without "prebuilt_" prefix
3131 Source_module_name *string
3132
3133 // Non-nil if this java_import module was dynamically created by a java_sdk_library_import
3134 // The name is the undecorated name of the java_sdk_library as it appears in the blueprint file
3135 // (without any prebuilt_ prefix)
3136 Created_by_java_sdk_library_name *string `blueprint:"mutated"`
Jihoon Kangfdf32362023-09-12 00:36:43 +00003137}
3138
3139func ApiContributionImportFactory() android.Module {
3140 module := &JavaApiContributionImport{}
3141 android.InitAndroidModule(module)
3142 android.InitDefaultableModule(module)
3143 android.InitPrebuiltModule(module, &[]string{""})
Spandan Das23956d12024-01-19 00:22:22 +00003144 module.AddProperties(&module.properties, &module.prebuiltProperties)
Spandan Das2cc80ba2023-10-27 17:21:52 +00003145 module.AddProperties(&module.sdkLibraryComponentProperties)
Jihoon Kangfdf32362023-09-12 00:36:43 +00003146 return module
3147}
3148
3149func (module *JavaApiContributionImport) Prebuilt() *android.Prebuilt {
3150 return &module.prebuilt
3151}
3152
3153func (module *JavaApiContributionImport) Name() string {
3154 return module.prebuilt.Name(module.ModuleBase.Name())
3155}
3156
Spandan Das23956d12024-01-19 00:22:22 +00003157func (j *JavaApiContributionImport) BaseModuleName() string {
3158 return proptools.StringDefault(j.prebuiltProperties.Source_module_name, j.ModuleBase.Name())
3159}
3160
3161func (j *JavaApiContributionImport) CreatedByJavaSdkLibraryName() *string {
3162 return j.prebuiltProperties.Created_by_java_sdk_library_name
3163}
3164
Jihoon Kangfdf32362023-09-12 00:36:43 +00003165func (ap *JavaApiContributionImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
3166 ap.JavaApiContribution.GenerateAndroidBuildActions(ctx)
3167}