blob: 71ffdb175751cea4bc8539889959006e098539bf [file] [log] [blame]
Colin Cross0ef08162019-05-01 15:50:51 -07001// Copyright 2019 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package java
16
17import (
18 "fmt"
19 "io"
Colin Crossd2d11772019-05-30 11:17:23 -070020 "strconv"
Colin Cross0ef08162019-05-01 15:50:51 -070021 "strings"
22
23 "android/soong/android"
Colin Cross8eebb132020-01-29 20:07:03 -080024 "android/soong/java/config"
25 "android/soong/tradefed"
Cole Faustd57e8b22022-08-11 11:59:04 -070026 "github.com/google/blueprint/proptools"
Colin Cross0ef08162019-05-01 15:50:51 -070027)
28
29func init() {
30 android.RegisterModuleType("android_robolectric_test", RobolectricTestFactory)
Colin Cross8eebb132020-01-29 20:07:03 -080031 android.RegisterModuleType("android_robolectric_runtimes", robolectricRuntimesFactory)
Colin Cross0ef08162019-05-01 15:50:51 -070032}
33
34var robolectricDefaultLibs = []string{
Colin Cross0ef08162019-05-01 15:50:51 -070035 "mockito-robolectric-prebuilt",
36 "truth-prebuilt",
Colin Cross8eebb132020-01-29 20:07:03 -080037 // TODO(ccross): this is not needed at link time
38 "junitxml",
Colin Cross0ef08162019-05-01 15:50:51 -070039}
40
Colin Cross2787e8e2021-03-05 11:16:20 -080041const robolectricCurrentLib = "Robolectric_all-target"
42const robolectricPrebuiltLibPattern = "platform-robolectric-%s-prebuilt"
43
Colin Cross3ec27ec2019-05-01 15:54:05 -070044var (
Colin Cross8eebb132020-01-29 20:07:03 -080045 roboCoverageLibsTag = dependencyTag{name: "roboCoverageLibs"}
46 roboRuntimesTag = dependencyTag{name: "roboRuntimes"}
Colin Cross3ec27ec2019-05-01 15:54:05 -070047)
48
Colin Cross0ef08162019-05-01 15:50:51 -070049type robolectricProperties struct {
50 // The name of the android_app module that the tests will run against.
51 Instrumentation_for *string
52
Colin Cross3ec27ec2019-05-01 15:54:05 -070053 // Additional libraries for which coverage data should be generated
54 Coverage_libs []string
55
Colin Cross0ef08162019-05-01 15:50:51 -070056 Test_options struct {
57 // Timeout in seconds when running the tests.
Colin Cross2f9a7c82019-05-30 11:16:26 -070058 Timeout *int64
Colin Crossd2d11772019-05-30 11:17:23 -070059
60 // Number of shards to use when running the tests.
61 Shards *int64
Colin Cross0ef08162019-05-01 15:50:51 -070062 }
Colin Cross2787e8e2021-03-05 11:16:20 -080063
64 // The version number of a robolectric prebuilt to use from prebuilts/misc/common/robolectric
65 // instead of the one built from source in external/robolectric-shadows.
66 Robolectric_prebuilt_version *string
Colin Cross0ef08162019-05-01 15:50:51 -070067}
68
69type robolectricTest struct {
70 Library
71
72 robolectricProperties robolectricProperties
Colin Cross8eebb132020-01-29 20:07:03 -080073 testProperties testProperties
Colin Cross0ef08162019-05-01 15:50:51 -070074
Colin Crossd2d11772019-05-30 11:17:23 -070075 libs []string
76 tests []string
Colin Cross3ec27ec2019-05-01 15:54:05 -070077
Colin Cross8eebb132020-01-29 20:07:03 -080078 manifest android.Path
79 resourceApk android.Path
80
81 combinedJar android.WritablePath
82
Colin Cross3ec27ec2019-05-01 15:54:05 -070083 roboSrcJar android.Path
Colin Cross8eebb132020-01-29 20:07:03 -080084
85 testConfig android.Path
86 data android.Paths
Colin Cross0c66bc62021-07-20 09:47:41 -070087
88 forceOSType android.OsType
89 forceArchType android.ArchType
Colin Cross0ef08162019-05-01 15:50:51 -070090}
91
Colin Cross8eebb132020-01-29 20:07:03 -080092func (r *robolectricTest) TestSuites() []string {
93 return r.testProperties.Test_suites
94}
95
96var _ android.TestSuiteModule = (*robolectricTest)(nil)
97
Colin Cross0ef08162019-05-01 15:50:51 -070098func (r *robolectricTest) DepsMutator(ctx android.BottomUpMutatorContext) {
99 r.Library.DepsMutator(ctx)
100
101 if r.robolectricProperties.Instrumentation_for != nil {
102 ctx.AddVariationDependencies(nil, instrumentationForTag, String(r.robolectricProperties.Instrumentation_for))
103 } else {
104 ctx.PropertyErrorf("instrumentation_for", "missing required instrumented module")
105 }
106
Colin Cross2787e8e2021-03-05 11:16:20 -0800107 if v := String(r.robolectricProperties.Robolectric_prebuilt_version); v != "" {
108 ctx.AddVariationDependencies(nil, libTag, fmt.Sprintf(robolectricPrebuiltLibPattern, v))
109 } else {
110 ctx.AddVariationDependencies(nil, libTag, robolectricCurrentLib)
111 }
112
Colin Cross0ef08162019-05-01 15:50:51 -0700113 ctx.AddVariationDependencies(nil, libTag, robolectricDefaultLibs...)
Colin Cross3ec27ec2019-05-01 15:54:05 -0700114
115 ctx.AddVariationDependencies(nil, roboCoverageLibsTag, r.robolectricProperties.Coverage_libs...)
Colin Cross8eebb132020-01-29 20:07:03 -0800116
Colin Cross5aa29a72020-09-14 19:54:47 -0700117 ctx.AddFarVariationDependencies(ctx.Config().BuildOSCommonTarget.Variations(),
118 roboRuntimesTag, "robolectric-android-all-prebuilts")
Colin Cross0ef08162019-05-01 15:50:51 -0700119}
120
121func (r *robolectricTest) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross0c66bc62021-07-20 09:47:41 -0700122 r.forceOSType = ctx.Config().BuildOS
123 r.forceArchType = ctx.Config().BuildArch
124
Colin Cross8eebb132020-01-29 20:07:03 -0800125 r.testConfig = tradefed.AutoGenRobolectricTestConfig(ctx, r.testProperties.Test_config,
126 r.testProperties.Test_config_template, r.testProperties.Test_suites,
127 r.testProperties.Auto_gen_config)
128 r.data = android.PathsForModuleSrc(ctx, r.testProperties.Data)
129
Colin Cross3ec27ec2019-05-01 15:54:05 -0700130 roboTestConfig := android.PathForModuleGen(ctx, "robolectric").
131 Join(ctx, "com/android/tools/test_config.properties")
132
133 // TODO: this inserts paths to built files into the test, it should really be inserting the contents.
134 instrumented := ctx.GetDirectDepsWithTag(instrumentationForTag)
135
136 if len(instrumented) != 1 {
137 panic(fmt.Errorf("expected exactly 1 instrumented dependency, got %d", len(instrumented)))
138 }
139
140 instrumentedApp, ok := instrumented[0].(*AndroidApp)
141 if !ok {
142 ctx.PropertyErrorf("instrumentation_for", "dependency must be an android_app")
143 }
144
Colin Cross8eebb132020-01-29 20:07:03 -0800145 r.manifest = instrumentedApp.mergedManifestFile
146 r.resourceApk = instrumentedApp.outputFile
147
Colin Cross3ec27ec2019-05-01 15:54:05 -0700148 generateRoboTestConfig(ctx, roboTestConfig, instrumentedApp)
149 r.extraResources = android.Paths{roboTestConfig}
150
Colin Cross0ef08162019-05-01 15:50:51 -0700151 r.Library.GenerateAndroidBuildActions(ctx)
152
Colin Cross3ec27ec2019-05-01 15:54:05 -0700153 roboSrcJar := android.PathForModuleGen(ctx, "robolectric", ctx.ModuleName()+".srcjar")
154 r.generateRoboSrcJar(ctx, roboSrcJar, instrumentedApp)
155 r.roboSrcJar = roboSrcJar
156
Colin Cross8eebb132020-01-29 20:07:03 -0800157 roboTestConfigJar := android.PathForModuleOut(ctx, "robolectric_samedir", "samedir_config.jar")
158 generateSameDirRoboTestConfigJar(ctx, roboTestConfigJar)
159
160 combinedJarJars := android.Paths{
161 // roboTestConfigJar comes first so that its com/android/tools/test_config.properties
162 // overrides the one from r.extraResources. The r.extraResources one can be removed
163 // once the Make test runner is removed.
164 roboTestConfigJar,
165 r.outputFile,
166 instrumentedApp.implementationAndResourcesJar,
Colin Cross0ef08162019-05-01 15:50:51 -0700167 }
Colin Crossd2d11772019-05-30 11:17:23 -0700168
Colin Cross8eebb132020-01-29 20:07:03 -0800169 for _, dep := range ctx.GetDirectDepsWithTag(libTag) {
Colin Crossdcf71b22021-02-01 13:59:03 -0800170 m := ctx.OtherModuleProvider(dep, JavaInfoProvider).(JavaInfo)
171 r.libs = append(r.libs, ctx.OtherModuleName(dep))
172 if !android.InList(ctx.OtherModuleName(dep), config.FrameworkLibraries) {
173 combinedJarJars = append(combinedJarJars, m.ImplementationAndResourcesJars...)
Colin Cross8eebb132020-01-29 20:07:03 -0800174 }
175 }
176
177 r.combinedJar = android.PathForModuleOut(ctx, "robolectric_combined", r.outputFile.Base())
178 TransformJarsToJar(ctx, r.combinedJar, "combine jars", combinedJarJars, android.OptionalPath{},
179 false, nil, nil)
180
Colin Crossd2d11772019-05-30 11:17:23 -0700181 // TODO: this could all be removed if tradefed was used as the test runner, it will find everything
182 // annotated as a test and run it.
183 for _, src := range r.compiledJavaSrcs {
184 s := src.Rel()
185 if !strings.HasSuffix(s, "Test.java") {
186 continue
187 } else if strings.HasSuffix(s, "/BaseRobolectricTest.java") {
188 continue
Ulya Trafimovich497a0932021-07-14 16:35:33 +0100189 } else {
Colin Crossd2d11772019-05-30 11:17:23 -0700190 s = strings.TrimPrefix(s, "src/")
191 }
192 r.tests = append(r.tests, s)
193 }
Colin Cross8eebb132020-01-29 20:07:03 -0800194
195 r.data = append(r.data, r.manifest, r.resourceApk)
196
197 runtimes := ctx.GetDirectDepWithTag("robolectric-android-all-prebuilts", roboRuntimesTag)
198
199 installPath := android.PathForModuleInstall(ctx, r.BaseModuleName())
200
201 installedResourceApk := ctx.InstallFile(installPath, ctx.ModuleName()+".apk", r.resourceApk)
202 installedManifest := ctx.InstallFile(installPath, ctx.ModuleName()+"-AndroidManifest.xml", r.manifest)
203 installedConfig := ctx.InstallFile(installPath, ctx.ModuleName()+".config", r.testConfig)
204
205 var installDeps android.Paths
206 for _, runtime := range runtimes.(*robolectricRuntimes).runtimes {
207 installDeps = append(installDeps, runtime)
208 }
209 installDeps = append(installDeps, installedResourceApk, installedManifest, installedConfig)
210
211 for _, data := range android.PathsForModuleSrc(ctx, r.testProperties.Data) {
212 installedData := ctx.InstallFile(installPath, data.Rel(), data)
213 installDeps = append(installDeps, installedData)
214 }
215
Colin Cross6301c3c2021-09-28 17:40:21 -0700216 r.installFile = ctx.InstallFile(installPath, ctx.ModuleName()+".jar", r.combinedJar, installDeps...)
Colin Crossd2d11772019-05-30 11:17:23 -0700217}
218
Colin Cross8eebb132020-01-29 20:07:03 -0800219func generateRoboTestConfig(ctx android.ModuleContext, outputFile android.WritablePath,
220 instrumentedApp *AndroidApp) {
Colin Crossf1a035e2020-11-16 17:32:30 -0800221 rule := android.NewRuleBuilder(pctx, ctx)
Colin Cross8eebb132020-01-29 20:07:03 -0800222
Colin Cross3ec27ec2019-05-01 15:54:05 -0700223 manifest := instrumentedApp.mergedManifestFile
224 resourceApk := instrumentedApp.outputFile
225
Colin Cross3ec27ec2019-05-01 15:54:05 -0700226 rule.Command().Text("rm -f").Output(outputFile)
227 rule.Command().
228 Textf(`echo "android_merged_manifest=%s" >>`, manifest.String()).Output(outputFile).Text("&&").
229 Textf(`echo "android_resource_apk=%s" >>`, resourceApk.String()).Output(outputFile).
230 // Make it depend on the files to which it points so the test file's timestamp is updated whenever the
231 // contents change
232 Implicit(manifest).
233 Implicit(resourceApk)
234
Colin Crossf1a035e2020-11-16 17:32:30 -0800235 rule.Build("generate_test_config", "generate test_config.properties")
Colin Cross3ec27ec2019-05-01 15:54:05 -0700236}
237
Colin Cross8eebb132020-01-29 20:07:03 -0800238func generateSameDirRoboTestConfigJar(ctx android.ModuleContext, outputFile android.ModuleOutPath) {
Colin Crossf1a035e2020-11-16 17:32:30 -0800239 rule := android.NewRuleBuilder(pctx, ctx)
Colin Cross8eebb132020-01-29 20:07:03 -0800240
241 outputDir := outputFile.InSameDir(ctx)
242 configFile := outputDir.Join(ctx, "com/android/tools/test_config.properties")
243 rule.Temporary(configFile)
244 rule.Command().Text("rm -f").Output(outputFile).Output(configFile)
245 rule.Command().Textf("mkdir -p $(dirname %s)", configFile.String())
246 rule.Command().
247 Text("(").
248 Textf(`echo "android_merged_manifest=%s-AndroidManifest.xml" &&`, ctx.ModuleName()).
249 Textf(`echo "android_resource_apk=%s.apk"`, ctx.ModuleName()).
250 Text(") >>").Output(configFile)
251 rule.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -0800252 BuiltTool("soong_zip").
Colin Cross8eebb132020-01-29 20:07:03 -0800253 FlagWithArg("-C ", outputDir.String()).
254 FlagWithInput("-f ", configFile).
255 FlagWithOutput("-o ", outputFile)
256
Colin Crossf1a035e2020-11-16 17:32:30 -0800257 rule.Build("generate_test_config_samedir", "generate test_config.properties")
Colin Cross8eebb132020-01-29 20:07:03 -0800258}
259
Colin Cross3ec27ec2019-05-01 15:54:05 -0700260func (r *robolectricTest) generateRoboSrcJar(ctx android.ModuleContext, outputFile android.WritablePath,
261 instrumentedApp *AndroidApp) {
262
263 srcJarArgs := copyOf(instrumentedApp.srcJarArgs)
264 srcJarDeps := append(android.Paths(nil), instrumentedApp.srcJarDeps...)
265
266 for _, m := range ctx.GetDirectDepsWithTag(roboCoverageLibsTag) {
Colin Crossdcf71b22021-02-01 13:59:03 -0800267 if ctx.OtherModuleHasProvider(m, JavaInfoProvider) {
268 dep := ctx.OtherModuleProvider(m, JavaInfoProvider).(JavaInfo)
269 srcJarArgs = append(srcJarArgs, dep.SrcJarArgs...)
270 srcJarDeps = append(srcJarDeps, dep.SrcJarDeps...)
Colin Cross3ec27ec2019-05-01 15:54:05 -0700271 }
272 }
273
274 TransformResourcesToJar(ctx, outputFile, srcJarArgs, srcJarDeps)
275}
276
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900277func (r *robolectricTest) AndroidMkEntries() []android.AndroidMkEntries {
278 entriesList := r.Library.AndroidMkEntries()
279 entries := &entriesList[0]
Colin Cross6301c3c2021-09-28 17:40:21 -0700280 entries.ExtraEntries = append(entries.ExtraEntries,
281 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
282 entries.SetBool("LOCAL_UNINSTALLABLE_MODULE", true)
283 })
Colin Cross0ef08162019-05-01 15:50:51 -0700284
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700285 entries.ExtraFooters = []android.AndroidMkExtraFootersFunc{
Jaewoong Jung02b11a62020-12-07 10:23:54 -0800286 func(w io.Writer, name, prefix, moduleDir string) {
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700287 if s := r.robolectricProperties.Test_options.Shards; s != nil && *s > 1 {
Colin Cross0a2f7192019-09-23 14:33:09 -0700288 numShards := int(*s)
289 shardSize := (len(r.tests) + numShards - 1) / numShards
290 shards := android.ShardStrings(r.tests, shardSize)
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700291 for i, shard := range shards {
292 r.writeTestRunner(w, name, "Run"+name+strconv.Itoa(i), shard)
293 }
Colin Cross0ef08162019-05-01 15:50:51 -0700294
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700295 // TODO: add rules to dist the outputs of the individual tests, or combine them together?
296 fmt.Fprintln(w, "")
297 fmt.Fprintln(w, ".PHONY:", "Run"+name)
298 fmt.Fprintln(w, "Run"+name, ": \\")
299 for i := range shards {
300 fmt.Fprintln(w, " ", "Run"+name+strconv.Itoa(i), "\\")
301 }
302 fmt.Fprintln(w, "")
303 } else {
304 r.writeTestRunner(w, name, "Run"+name, r.tests)
Colin Crossd2d11772019-05-30 11:17:23 -0700305 }
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700306 },
Colin Cross0ef08162019-05-01 15:50:51 -0700307 }
308
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900309 return entriesList
Colin Cross0ef08162019-05-01 15:50:51 -0700310}
311
Colin Crossd2d11772019-05-30 11:17:23 -0700312func (r *robolectricTest) writeTestRunner(w io.Writer, module, name string, tests []string) {
313 fmt.Fprintln(w, "")
314 fmt.Fprintln(w, "include $(CLEAR_VARS)")
315 fmt.Fprintln(w, "LOCAL_MODULE :=", name)
316 fmt.Fprintln(w, "LOCAL_JAVA_LIBRARIES :=", module)
317 fmt.Fprintln(w, "LOCAL_JAVA_LIBRARIES += ", strings.Join(r.libs, " "))
318 fmt.Fprintln(w, "LOCAL_TEST_PACKAGE :=", String(r.robolectricProperties.Instrumentation_for))
319 fmt.Fprintln(w, "LOCAL_INSTRUMENT_SRCJARS :=", r.roboSrcJar.String())
320 fmt.Fprintln(w, "LOCAL_ROBOTEST_FILES :=", strings.Join(tests, " "))
321 if t := r.robolectricProperties.Test_options.Timeout; t != nil {
322 fmt.Fprintln(w, "LOCAL_ROBOTEST_TIMEOUT :=", *t)
323 }
Colin Cross2787e8e2021-03-05 11:16:20 -0800324 if v := String(r.robolectricProperties.Robolectric_prebuilt_version); v != "" {
325 fmt.Fprintf(w, "-include prebuilts/misc/common/robolectric/%s/run_robotests.mk\n", v)
326 } else {
327 fmt.Fprintln(w, "-include external/robolectric-shadows/run_robotests.mk")
328 }
Colin Crossd2d11772019-05-30 11:17:23 -0700329}
330
Colin Cross0ef08162019-05-01 15:50:51 -0700331// An android_robolectric_test module compiles tests against the Robolectric framework that can run on the local host
332// instead of on a device. It also generates a rule with the name of the module prefixed with "Run" that can be
333// used to run the tests. Running the tests with build rule will eventually be deprecated and replaced with atest.
Colin Crossd2d11772019-05-30 11:17:23 -0700334//
335// The test runner considers any file listed in srcs whose name ends with Test.java to be a test class, unless
336// it is named BaseRobolectricTest.java. The path to the each source file must exactly match the package
337// name, or match the package name when the prefix "src/" is removed.
Colin Cross0ef08162019-05-01 15:50:51 -0700338func RobolectricTestFactory() android.Module {
339 module := &robolectricTest{}
340
Colin Crossce6734e2020-06-15 16:09:53 -0700341 module.addHostProperties()
Colin Cross0ef08162019-05-01 15:50:51 -0700342 module.AddProperties(
Colin Crosse323f3c2019-09-17 15:34:09 -0700343 &module.Module.deviceProperties,
Colin Cross8eebb132020-01-29 20:07:03 -0800344 &module.robolectricProperties,
345 &module.testProperties)
Colin Cross0ef08162019-05-01 15:50:51 -0700346
347 module.Module.dexpreopter.isTest = true
Cole Faustd57e8b22022-08-11 11:59:04 -0700348 module.Module.linter.properties.Lint.Test = proptools.BoolPtr(true)
Colin Cross0ef08162019-05-01 15:50:51 -0700349
Colin Cross8eebb132020-01-29 20:07:03 -0800350 module.testProperties.Test_suites = []string{"robolectric-tests"}
351
Colin Cross0ef08162019-05-01 15:50:51 -0700352 InitJavaModule(module, android.DeviceSupported)
353 return module
354}
Colin Cross8eebb132020-01-29 20:07:03 -0800355
Jiyong Park87788b52020-09-01 12:37:45 +0900356func (r *robolectricTest) InstallInTestcases() bool { return true }
357func (r *robolectricTest) InstallForceOS() (*android.OsType, *android.ArchType) {
Colin Cross0c66bc62021-07-20 09:47:41 -0700358 return &r.forceOSType, &r.forceArchType
Jiyong Park87788b52020-09-01 12:37:45 +0900359}
Colin Cross8eebb132020-01-29 20:07:03 -0800360
361func robolectricRuntimesFactory() android.Module {
362 module := &robolectricRuntimes{}
363 module.AddProperties(&module.props)
Colin Cross5aa29a72020-09-14 19:54:47 -0700364 android.InitAndroidArchModule(module, android.HostSupportedNoCross, android.MultilibCommon)
Colin Cross8eebb132020-01-29 20:07:03 -0800365 return module
366}
367
368type robolectricRuntimesProperties struct {
369 Jars []string `android:"path"`
370 Lib *string
371}
372
373type robolectricRuntimes struct {
374 android.ModuleBase
375
376 props robolectricRuntimesProperties
377
378 runtimes []android.InstallPath
Colin Cross0c66bc62021-07-20 09:47:41 -0700379
380 forceOSType android.OsType
381 forceArchType android.ArchType
Colin Cross8eebb132020-01-29 20:07:03 -0800382}
383
384func (r *robolectricRuntimes) TestSuites() []string {
385 return []string{"robolectric-tests"}
386}
387
388var _ android.TestSuiteModule = (*robolectricRuntimes)(nil)
389
390func (r *robolectricRuntimes) DepsMutator(ctx android.BottomUpMutatorContext) {
Jeongik Cha816a23a2020-07-08 01:09:23 +0900391 if !ctx.Config().AlwaysUsePrebuiltSdks() && r.props.Lib != nil {
Colin Cross8eebb132020-01-29 20:07:03 -0800392 ctx.AddVariationDependencies(nil, libTag, String(r.props.Lib))
393 }
394}
395
396func (r *robolectricRuntimes) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross5aa29a72020-09-14 19:54:47 -0700397 if ctx.Target().Os != ctx.Config().BuildOSCommonTarget.Os {
398 return
399 }
400
Colin Cross0c66bc62021-07-20 09:47:41 -0700401 r.forceOSType = ctx.Config().BuildOS
402 r.forceArchType = ctx.Config().BuildArch
403
Colin Cross8eebb132020-01-29 20:07:03 -0800404 files := android.PathsForModuleSrc(ctx, r.props.Jars)
405
406 androidAllDir := android.PathForModuleInstall(ctx, "android-all")
Colin Cross8eebb132020-01-29 20:07:03 -0800407 for _, from := range files {
408 installedRuntime := ctx.InstallFile(androidAllDir, from.Base(), from)
409 r.runtimes = append(r.runtimes, installedRuntime)
410 }
411
Jeongik Cha816a23a2020-07-08 01:09:23 +0900412 if !ctx.Config().AlwaysUsePrebuiltSdks() && r.props.Lib != nil {
Colin Cross8eebb132020-01-29 20:07:03 -0800413 runtimeFromSourceModule := ctx.GetDirectDepWithTag(String(r.props.Lib), libTag)
Jeongik Cha816a23a2020-07-08 01:09:23 +0900414 if runtimeFromSourceModule == nil {
415 if ctx.Config().AllowMissingDependencies() {
416 ctx.AddMissingDependencies([]string{String(r.props.Lib)})
417 } else {
418 ctx.PropertyErrorf("lib", "missing dependency %q", String(r.props.Lib))
419 }
420 return
421 }
Colin Cross8eebb132020-01-29 20:07:03 -0800422 runtimeFromSourceJar := android.OutputFileForModule(ctx, runtimeFromSourceModule, "")
423
Joseph Murphyc9648412021-07-20 13:58:15 -0700424 // "TREE" name is essential here because it hooks into the "TREE" name in
425 // Robolectric's SdkConfig.java that will always correspond to the NEWEST_SDK
426 // in Robolectric configs.
427 runtimeName := "android-all-current-robolectric-r0.jar"
Colin Cross8eebb132020-01-29 20:07:03 -0800428 installedRuntime := ctx.InstallFile(androidAllDir, runtimeName, runtimeFromSourceJar)
429 r.runtimes = append(r.runtimes, installedRuntime)
430 }
431}
432
Jiyong Park87788b52020-09-01 12:37:45 +0900433func (r *robolectricRuntimes) InstallInTestcases() bool { return true }
434func (r *robolectricRuntimes) InstallForceOS() (*android.OsType, *android.ArchType) {
Colin Cross0c66bc62021-07-20 09:47:41 -0700435 return &r.forceOSType, &r.forceArchType
Jiyong Park87788b52020-09-01 12:37:45 +0900436}