blob: f71952172d82ab07047d96606047dde0a014abda [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"
Colin Cross0ef08162019-05-01 15:50:51 -070026)
27
28func init() {
29 android.RegisterModuleType("android_robolectric_test", RobolectricTestFactory)
Colin Cross8eebb132020-01-29 20:07:03 -080030 android.RegisterModuleType("android_robolectric_runtimes", robolectricRuntimesFactory)
Colin Cross0ef08162019-05-01 15:50:51 -070031}
32
33var robolectricDefaultLibs = []string{
Colin Cross0ef08162019-05-01 15:50:51 -070034 "mockito-robolectric-prebuilt",
35 "truth-prebuilt",
Colin Cross8eebb132020-01-29 20:07:03 -080036 // TODO(ccross): this is not needed at link time
37 "junitxml",
Colin Cross0ef08162019-05-01 15:50:51 -070038}
39
Colin Cross2787e8e2021-03-05 11:16:20 -080040const robolectricCurrentLib = "Robolectric_all-target"
41const robolectricPrebuiltLibPattern = "platform-robolectric-%s-prebuilt"
42
Colin Cross3ec27ec2019-05-01 15:54:05 -070043var (
Colin Cross8eebb132020-01-29 20:07:03 -080044 roboCoverageLibsTag = dependencyTag{name: "roboCoverageLibs"}
45 roboRuntimesTag = dependencyTag{name: "roboRuntimes"}
Colin Cross3ec27ec2019-05-01 15:54:05 -070046)
47
Colin Cross0ef08162019-05-01 15:50:51 -070048type robolectricProperties struct {
49 // The name of the android_app module that the tests will run against.
50 Instrumentation_for *string
51
Colin Cross3ec27ec2019-05-01 15:54:05 -070052 // Additional libraries for which coverage data should be generated
53 Coverage_libs []string
54
Colin Cross0ef08162019-05-01 15:50:51 -070055 Test_options struct {
56 // Timeout in seconds when running the tests.
Colin Cross2f9a7c82019-05-30 11:16:26 -070057 Timeout *int64
Colin Crossd2d11772019-05-30 11:17:23 -070058
59 // Number of shards to use when running the tests.
60 Shards *int64
Colin Cross0ef08162019-05-01 15:50:51 -070061 }
Colin Cross2787e8e2021-03-05 11:16:20 -080062
63 // The version number of a robolectric prebuilt to use from prebuilts/misc/common/robolectric
64 // instead of the one built from source in external/robolectric-shadows.
65 Robolectric_prebuilt_version *string
Colin Cross0ef08162019-05-01 15:50:51 -070066}
67
68type robolectricTest struct {
69 Library
70
71 robolectricProperties robolectricProperties
Colin Cross8eebb132020-01-29 20:07:03 -080072 testProperties testProperties
Colin Cross0ef08162019-05-01 15:50:51 -070073
Colin Crossd2d11772019-05-30 11:17:23 -070074 libs []string
75 tests []string
Colin Cross3ec27ec2019-05-01 15:54:05 -070076
Colin Cross8eebb132020-01-29 20:07:03 -080077 manifest android.Path
78 resourceApk android.Path
79
80 combinedJar android.WritablePath
81
Colin Cross3ec27ec2019-05-01 15:54:05 -070082 roboSrcJar android.Path
Colin Cross8eebb132020-01-29 20:07:03 -080083
84 testConfig android.Path
85 data android.Paths
Colin Cross0c66bc62021-07-20 09:47:41 -070086
87 forceOSType android.OsType
88 forceArchType android.ArchType
Colin Cross0ef08162019-05-01 15:50:51 -070089}
90
Colin Cross8eebb132020-01-29 20:07:03 -080091func (r *robolectricTest) TestSuites() []string {
92 return r.testProperties.Test_suites
93}
94
95var _ android.TestSuiteModule = (*robolectricTest)(nil)
96
Colin Cross0ef08162019-05-01 15:50:51 -070097func (r *robolectricTest) DepsMutator(ctx android.BottomUpMutatorContext) {
98 r.Library.DepsMutator(ctx)
99
100 if r.robolectricProperties.Instrumentation_for != nil {
101 ctx.AddVariationDependencies(nil, instrumentationForTag, String(r.robolectricProperties.Instrumentation_for))
102 } else {
103 ctx.PropertyErrorf("instrumentation_for", "missing required instrumented module")
104 }
105
Colin Cross2787e8e2021-03-05 11:16:20 -0800106 if v := String(r.robolectricProperties.Robolectric_prebuilt_version); v != "" {
107 ctx.AddVariationDependencies(nil, libTag, fmt.Sprintf(robolectricPrebuiltLibPattern, v))
108 } else {
109 ctx.AddVariationDependencies(nil, libTag, robolectricCurrentLib)
110 }
111
Colin Cross0ef08162019-05-01 15:50:51 -0700112 ctx.AddVariationDependencies(nil, libTag, robolectricDefaultLibs...)
Colin Cross3ec27ec2019-05-01 15:54:05 -0700113
114 ctx.AddVariationDependencies(nil, roboCoverageLibsTag, r.robolectricProperties.Coverage_libs...)
Colin Cross8eebb132020-01-29 20:07:03 -0800115
Colin Cross5aa29a72020-09-14 19:54:47 -0700116 ctx.AddFarVariationDependencies(ctx.Config().BuildOSCommonTarget.Variations(),
117 roboRuntimesTag, "robolectric-android-all-prebuilts")
Colin Cross0ef08162019-05-01 15:50:51 -0700118}
119
120func (r *robolectricTest) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross0c66bc62021-07-20 09:47:41 -0700121 r.forceOSType = ctx.Config().BuildOS
122 r.forceArchType = ctx.Config().BuildArch
123
Colin Cross8eebb132020-01-29 20:07:03 -0800124 r.testConfig = tradefed.AutoGenRobolectricTestConfig(ctx, r.testProperties.Test_config,
125 r.testProperties.Test_config_template, r.testProperties.Test_suites,
126 r.testProperties.Auto_gen_config)
127 r.data = android.PathsForModuleSrc(ctx, r.testProperties.Data)
128
Colin Cross3ec27ec2019-05-01 15:54:05 -0700129 roboTestConfig := android.PathForModuleGen(ctx, "robolectric").
130 Join(ctx, "com/android/tools/test_config.properties")
131
132 // TODO: this inserts paths to built files into the test, it should really be inserting the contents.
133 instrumented := ctx.GetDirectDepsWithTag(instrumentationForTag)
134
135 if len(instrumented) != 1 {
136 panic(fmt.Errorf("expected exactly 1 instrumented dependency, got %d", len(instrumented)))
137 }
138
139 instrumentedApp, ok := instrumented[0].(*AndroidApp)
140 if !ok {
141 ctx.PropertyErrorf("instrumentation_for", "dependency must be an android_app")
142 }
143
Colin Cross8eebb132020-01-29 20:07:03 -0800144 r.manifest = instrumentedApp.mergedManifestFile
145 r.resourceApk = instrumentedApp.outputFile
146
Colin Cross3ec27ec2019-05-01 15:54:05 -0700147 generateRoboTestConfig(ctx, roboTestConfig, instrumentedApp)
148 r.extraResources = android.Paths{roboTestConfig}
149
Colin Cross0ef08162019-05-01 15:50:51 -0700150 r.Library.GenerateAndroidBuildActions(ctx)
151
Colin Cross3ec27ec2019-05-01 15:54:05 -0700152 roboSrcJar := android.PathForModuleGen(ctx, "robolectric", ctx.ModuleName()+".srcjar")
153 r.generateRoboSrcJar(ctx, roboSrcJar, instrumentedApp)
154 r.roboSrcJar = roboSrcJar
155
Colin Cross8eebb132020-01-29 20:07:03 -0800156 roboTestConfigJar := android.PathForModuleOut(ctx, "robolectric_samedir", "samedir_config.jar")
157 generateSameDirRoboTestConfigJar(ctx, roboTestConfigJar)
158
159 combinedJarJars := android.Paths{
160 // roboTestConfigJar comes first so that its com/android/tools/test_config.properties
161 // overrides the one from r.extraResources. The r.extraResources one can be removed
162 // once the Make test runner is removed.
163 roboTestConfigJar,
164 r.outputFile,
165 instrumentedApp.implementationAndResourcesJar,
Colin Cross0ef08162019-05-01 15:50:51 -0700166 }
Colin Crossd2d11772019-05-30 11:17:23 -0700167
Colin Cross8eebb132020-01-29 20:07:03 -0800168 for _, dep := range ctx.GetDirectDepsWithTag(libTag) {
Colin Crossdcf71b22021-02-01 13:59:03 -0800169 m := ctx.OtherModuleProvider(dep, JavaInfoProvider).(JavaInfo)
170 r.libs = append(r.libs, ctx.OtherModuleName(dep))
171 if !android.InList(ctx.OtherModuleName(dep), config.FrameworkLibraries) {
172 combinedJarJars = append(combinedJarJars, m.ImplementationAndResourcesJars...)
Colin Cross8eebb132020-01-29 20:07:03 -0800173 }
174 }
175
176 r.combinedJar = android.PathForModuleOut(ctx, "robolectric_combined", r.outputFile.Base())
177 TransformJarsToJar(ctx, r.combinedJar, "combine jars", combinedJarJars, android.OptionalPath{},
178 false, nil, nil)
179
Colin Crossd2d11772019-05-30 11:17:23 -0700180 // TODO: this could all be removed if tradefed was used as the test runner, it will find everything
181 // annotated as a test and run it.
182 for _, src := range r.compiledJavaSrcs {
183 s := src.Rel()
184 if !strings.HasSuffix(s, "Test.java") {
185 continue
186 } else if strings.HasSuffix(s, "/BaseRobolectricTest.java") {
187 continue
Ulya Trafimovich497a0932021-07-14 16:35:33 +0100188 } else {
Colin Crossd2d11772019-05-30 11:17:23 -0700189 s = strings.TrimPrefix(s, "src/")
190 }
191 r.tests = append(r.tests, s)
192 }
Colin Cross8eebb132020-01-29 20:07:03 -0800193
194 r.data = append(r.data, r.manifest, r.resourceApk)
195
196 runtimes := ctx.GetDirectDepWithTag("robolectric-android-all-prebuilts", roboRuntimesTag)
197
198 installPath := android.PathForModuleInstall(ctx, r.BaseModuleName())
199
200 installedResourceApk := ctx.InstallFile(installPath, ctx.ModuleName()+".apk", r.resourceApk)
201 installedManifest := ctx.InstallFile(installPath, ctx.ModuleName()+"-AndroidManifest.xml", r.manifest)
202 installedConfig := ctx.InstallFile(installPath, ctx.ModuleName()+".config", r.testConfig)
203
204 var installDeps android.Paths
205 for _, runtime := range runtimes.(*robolectricRuntimes).runtimes {
206 installDeps = append(installDeps, runtime)
207 }
208 installDeps = append(installDeps, installedResourceApk, installedManifest, installedConfig)
209
210 for _, data := range android.PathsForModuleSrc(ctx, r.testProperties.Data) {
211 installedData := ctx.InstallFile(installPath, data.Rel(), data)
212 installDeps = append(installDeps, installedData)
213 }
214
Colin Cross6301c3c2021-09-28 17:40:21 -0700215 r.installFile = ctx.InstallFile(installPath, ctx.ModuleName()+".jar", r.combinedJar, installDeps...)
Colin Crossd2d11772019-05-30 11:17:23 -0700216}
217
Colin Cross8eebb132020-01-29 20:07:03 -0800218func generateRoboTestConfig(ctx android.ModuleContext, outputFile android.WritablePath,
219 instrumentedApp *AndroidApp) {
Colin Crossf1a035e2020-11-16 17:32:30 -0800220 rule := android.NewRuleBuilder(pctx, ctx)
Colin Cross8eebb132020-01-29 20:07:03 -0800221
Colin Cross3ec27ec2019-05-01 15:54:05 -0700222 manifest := instrumentedApp.mergedManifestFile
223 resourceApk := instrumentedApp.outputFile
224
Colin Cross3ec27ec2019-05-01 15:54:05 -0700225 rule.Command().Text("rm -f").Output(outputFile)
226 rule.Command().
227 Textf(`echo "android_merged_manifest=%s" >>`, manifest.String()).Output(outputFile).Text("&&").
228 Textf(`echo "android_resource_apk=%s" >>`, resourceApk.String()).Output(outputFile).
229 // Make it depend on the files to which it points so the test file's timestamp is updated whenever the
230 // contents change
231 Implicit(manifest).
232 Implicit(resourceApk)
233
Colin Crossf1a035e2020-11-16 17:32:30 -0800234 rule.Build("generate_test_config", "generate test_config.properties")
Colin Cross3ec27ec2019-05-01 15:54:05 -0700235}
236
Colin Cross8eebb132020-01-29 20:07:03 -0800237func generateSameDirRoboTestConfigJar(ctx android.ModuleContext, outputFile android.ModuleOutPath) {
Colin Crossf1a035e2020-11-16 17:32:30 -0800238 rule := android.NewRuleBuilder(pctx, ctx)
Colin Cross8eebb132020-01-29 20:07:03 -0800239
240 outputDir := outputFile.InSameDir(ctx)
241 configFile := outputDir.Join(ctx, "com/android/tools/test_config.properties")
242 rule.Temporary(configFile)
243 rule.Command().Text("rm -f").Output(outputFile).Output(configFile)
244 rule.Command().Textf("mkdir -p $(dirname %s)", configFile.String())
245 rule.Command().
246 Text("(").
247 Textf(`echo "android_merged_manifest=%s-AndroidManifest.xml" &&`, ctx.ModuleName()).
248 Textf(`echo "android_resource_apk=%s.apk"`, ctx.ModuleName()).
249 Text(") >>").Output(configFile)
250 rule.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -0800251 BuiltTool("soong_zip").
Colin Cross8eebb132020-01-29 20:07:03 -0800252 FlagWithArg("-C ", outputDir.String()).
253 FlagWithInput("-f ", configFile).
254 FlagWithOutput("-o ", outputFile)
255
Colin Crossf1a035e2020-11-16 17:32:30 -0800256 rule.Build("generate_test_config_samedir", "generate test_config.properties")
Colin Cross8eebb132020-01-29 20:07:03 -0800257}
258
Colin Cross3ec27ec2019-05-01 15:54:05 -0700259func (r *robolectricTest) generateRoboSrcJar(ctx android.ModuleContext, outputFile android.WritablePath,
260 instrumentedApp *AndroidApp) {
261
262 srcJarArgs := copyOf(instrumentedApp.srcJarArgs)
263 srcJarDeps := append(android.Paths(nil), instrumentedApp.srcJarDeps...)
264
265 for _, m := range ctx.GetDirectDepsWithTag(roboCoverageLibsTag) {
Colin Crossdcf71b22021-02-01 13:59:03 -0800266 if ctx.OtherModuleHasProvider(m, JavaInfoProvider) {
267 dep := ctx.OtherModuleProvider(m, JavaInfoProvider).(JavaInfo)
268 srcJarArgs = append(srcJarArgs, dep.SrcJarArgs...)
269 srcJarDeps = append(srcJarDeps, dep.SrcJarDeps...)
Colin Cross3ec27ec2019-05-01 15:54:05 -0700270 }
271 }
272
273 TransformResourcesToJar(ctx, outputFile, srcJarArgs, srcJarDeps)
274}
275
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900276func (r *robolectricTest) AndroidMkEntries() []android.AndroidMkEntries {
277 entriesList := r.Library.AndroidMkEntries()
278 entries := &entriesList[0]
Colin Cross6301c3c2021-09-28 17:40:21 -0700279 entries.ExtraEntries = append(entries.ExtraEntries,
280 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
281 entries.SetBool("LOCAL_UNINSTALLABLE_MODULE", true)
282 })
Colin Cross0ef08162019-05-01 15:50:51 -0700283
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700284 entries.ExtraFooters = []android.AndroidMkExtraFootersFunc{
Jaewoong Jung02b11a62020-12-07 10:23:54 -0800285 func(w io.Writer, name, prefix, moduleDir string) {
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700286 if s := r.robolectricProperties.Test_options.Shards; s != nil && *s > 1 {
Colin Cross0a2f7192019-09-23 14:33:09 -0700287 numShards := int(*s)
288 shardSize := (len(r.tests) + numShards - 1) / numShards
289 shards := android.ShardStrings(r.tests, shardSize)
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700290 for i, shard := range shards {
291 r.writeTestRunner(w, name, "Run"+name+strconv.Itoa(i), shard)
292 }
Colin Cross0ef08162019-05-01 15:50:51 -0700293
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700294 // TODO: add rules to dist the outputs of the individual tests, or combine them together?
295 fmt.Fprintln(w, "")
296 fmt.Fprintln(w, ".PHONY:", "Run"+name)
297 fmt.Fprintln(w, "Run"+name, ": \\")
298 for i := range shards {
299 fmt.Fprintln(w, " ", "Run"+name+strconv.Itoa(i), "\\")
300 }
301 fmt.Fprintln(w, "")
302 } else {
303 r.writeTestRunner(w, name, "Run"+name, r.tests)
Colin Crossd2d11772019-05-30 11:17:23 -0700304 }
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700305 },
Colin Cross0ef08162019-05-01 15:50:51 -0700306 }
307
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900308 return entriesList
Colin Cross0ef08162019-05-01 15:50:51 -0700309}
310
Colin Crossd2d11772019-05-30 11:17:23 -0700311func (r *robolectricTest) writeTestRunner(w io.Writer, module, name string, tests []string) {
312 fmt.Fprintln(w, "")
313 fmt.Fprintln(w, "include $(CLEAR_VARS)")
314 fmt.Fprintln(w, "LOCAL_MODULE :=", name)
315 fmt.Fprintln(w, "LOCAL_JAVA_LIBRARIES :=", module)
316 fmt.Fprintln(w, "LOCAL_JAVA_LIBRARIES += ", strings.Join(r.libs, " "))
317 fmt.Fprintln(w, "LOCAL_TEST_PACKAGE :=", String(r.robolectricProperties.Instrumentation_for))
318 fmt.Fprintln(w, "LOCAL_INSTRUMENT_SRCJARS :=", r.roboSrcJar.String())
319 fmt.Fprintln(w, "LOCAL_ROBOTEST_FILES :=", strings.Join(tests, " "))
320 if t := r.robolectricProperties.Test_options.Timeout; t != nil {
321 fmt.Fprintln(w, "LOCAL_ROBOTEST_TIMEOUT :=", *t)
322 }
Colin Cross2787e8e2021-03-05 11:16:20 -0800323 if v := String(r.robolectricProperties.Robolectric_prebuilt_version); v != "" {
324 fmt.Fprintf(w, "-include prebuilts/misc/common/robolectric/%s/run_robotests.mk\n", v)
325 } else {
326 fmt.Fprintln(w, "-include external/robolectric-shadows/run_robotests.mk")
327 }
Colin Crossd2d11772019-05-30 11:17:23 -0700328}
329
Colin Cross0ef08162019-05-01 15:50:51 -0700330// An android_robolectric_test module compiles tests against the Robolectric framework that can run on the local host
331// instead of on a device. It also generates a rule with the name of the module prefixed with "Run" that can be
332// 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 -0700333//
334// The test runner considers any file listed in srcs whose name ends with Test.java to be a test class, unless
335// it is named BaseRobolectricTest.java. The path to the each source file must exactly match the package
336// name, or match the package name when the prefix "src/" is removed.
Colin Cross0ef08162019-05-01 15:50:51 -0700337func RobolectricTestFactory() android.Module {
338 module := &robolectricTest{}
339
Colin Crossce6734e2020-06-15 16:09:53 -0700340 module.addHostProperties()
Colin Cross0ef08162019-05-01 15:50:51 -0700341 module.AddProperties(
Colin Crosse323f3c2019-09-17 15:34:09 -0700342 &module.Module.deviceProperties,
Colin Cross8eebb132020-01-29 20:07:03 -0800343 &module.robolectricProperties,
344 &module.testProperties)
Colin Cross0ef08162019-05-01 15:50:51 -0700345
346 module.Module.dexpreopter.isTest = true
Colin Cross014489c2020-06-02 20:09:13 -0700347 module.Module.linter.test = true
Colin Cross0ef08162019-05-01 15:50:51 -0700348
Colin Cross8eebb132020-01-29 20:07:03 -0800349 module.testProperties.Test_suites = []string{"robolectric-tests"}
350
Colin Cross0ef08162019-05-01 15:50:51 -0700351 InitJavaModule(module, android.DeviceSupported)
352 return module
353}
Colin Cross8eebb132020-01-29 20:07:03 -0800354
Jiyong Park87788b52020-09-01 12:37:45 +0900355func (r *robolectricTest) InstallInTestcases() bool { return true }
356func (r *robolectricTest) InstallForceOS() (*android.OsType, *android.ArchType) {
Colin Cross0c66bc62021-07-20 09:47:41 -0700357 return &r.forceOSType, &r.forceArchType
Jiyong Park87788b52020-09-01 12:37:45 +0900358}
Colin Cross8eebb132020-01-29 20:07:03 -0800359
360func robolectricRuntimesFactory() android.Module {
361 module := &robolectricRuntimes{}
362 module.AddProperties(&module.props)
Colin Cross5aa29a72020-09-14 19:54:47 -0700363 android.InitAndroidArchModule(module, android.HostSupportedNoCross, android.MultilibCommon)
Colin Cross8eebb132020-01-29 20:07:03 -0800364 return module
365}
366
367type robolectricRuntimesProperties struct {
368 Jars []string `android:"path"`
369 Lib *string
370}
371
372type robolectricRuntimes struct {
373 android.ModuleBase
374
375 props robolectricRuntimesProperties
376
377 runtimes []android.InstallPath
Colin Cross0c66bc62021-07-20 09:47:41 -0700378
379 forceOSType android.OsType
380 forceArchType android.ArchType
Colin Cross8eebb132020-01-29 20:07:03 -0800381}
382
383func (r *robolectricRuntimes) TestSuites() []string {
384 return []string{"robolectric-tests"}
385}
386
387var _ android.TestSuiteModule = (*robolectricRuntimes)(nil)
388
389func (r *robolectricRuntimes) DepsMutator(ctx android.BottomUpMutatorContext) {
Jeongik Cha816a23a2020-07-08 01:09:23 +0900390 if !ctx.Config().AlwaysUsePrebuiltSdks() && r.props.Lib != nil {
Colin Cross8eebb132020-01-29 20:07:03 -0800391 ctx.AddVariationDependencies(nil, libTag, String(r.props.Lib))
392 }
393}
394
395func (r *robolectricRuntimes) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross5aa29a72020-09-14 19:54:47 -0700396 if ctx.Target().Os != ctx.Config().BuildOSCommonTarget.Os {
397 return
398 }
399
Colin Cross0c66bc62021-07-20 09:47:41 -0700400 r.forceOSType = ctx.Config().BuildOS
401 r.forceArchType = ctx.Config().BuildArch
402
Colin Cross8eebb132020-01-29 20:07:03 -0800403 files := android.PathsForModuleSrc(ctx, r.props.Jars)
404
405 androidAllDir := android.PathForModuleInstall(ctx, "android-all")
Colin Cross8eebb132020-01-29 20:07:03 -0800406 for _, from := range files {
407 installedRuntime := ctx.InstallFile(androidAllDir, from.Base(), from)
408 r.runtimes = append(r.runtimes, installedRuntime)
409 }
410
Jeongik Cha816a23a2020-07-08 01:09:23 +0900411 if !ctx.Config().AlwaysUsePrebuiltSdks() && r.props.Lib != nil {
Colin Cross8eebb132020-01-29 20:07:03 -0800412 runtimeFromSourceModule := ctx.GetDirectDepWithTag(String(r.props.Lib), libTag)
Jeongik Cha816a23a2020-07-08 01:09:23 +0900413 if runtimeFromSourceModule == nil {
414 if ctx.Config().AllowMissingDependencies() {
415 ctx.AddMissingDependencies([]string{String(r.props.Lib)})
416 } else {
417 ctx.PropertyErrorf("lib", "missing dependency %q", String(r.props.Lib))
418 }
419 return
420 }
Colin Cross8eebb132020-01-29 20:07:03 -0800421 runtimeFromSourceJar := android.OutputFileForModule(ctx, runtimeFromSourceModule, "")
422
Joseph Murphyc9648412021-07-20 13:58:15 -0700423 // "TREE" name is essential here because it hooks into the "TREE" name in
424 // Robolectric's SdkConfig.java that will always correspond to the NEWEST_SDK
425 // in Robolectric configs.
426 runtimeName := "android-all-current-robolectric-r0.jar"
Colin Cross8eebb132020-01-29 20:07:03 -0800427 installedRuntime := ctx.InstallFile(androidAllDir, runtimeName, runtimeFromSourceJar)
428 r.runtimes = append(r.runtimes, installedRuntime)
429 }
430}
431
Jiyong Park87788b52020-09-01 12:37:45 +0900432func (r *robolectricRuntimes) InstallInTestcases() bool { return true }
433func (r *robolectricRuntimes) InstallForceOS() (*android.OsType, *android.ArchType) {
Colin Cross0c66bc62021-07-20 09:47:41 -0700434 return &r.forceOSType, &r.forceArchType
Jiyong Park87788b52020-09-01 12:37:45 +0900435}