blob: 80be04612399bb3e66cae6e58648053ff7f09829 [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"
Rex Hoffmanebaac472022-11-14 04:06:48 +000026
27 "github.com/google/blueprint/proptools"
Colin Cross0ef08162019-05-01 15:50:51 -070028)
29
30func init() {
31 android.RegisterModuleType("android_robolectric_test", RobolectricTestFactory)
Colin Cross8eebb132020-01-29 20:07:03 -080032 android.RegisterModuleType("android_robolectric_runtimes", robolectricRuntimesFactory)
Colin Cross0ef08162019-05-01 15:50:51 -070033}
34
35var robolectricDefaultLibs = []string{
Colin Cross0ef08162019-05-01 15:50:51 -070036 "mockito-robolectric-prebuilt",
37 "truth-prebuilt",
Colin Cross8eebb132020-01-29 20:07:03 -080038 // TODO(ccross): this is not needed at link time
39 "junitxml",
Colin Cross0ef08162019-05-01 15:50:51 -070040}
41
Colin Cross2787e8e2021-03-05 11:16:20 -080042const robolectricCurrentLib = "Robolectric_all-target"
43const robolectricPrebuiltLibPattern = "platform-robolectric-%s-prebuilt"
44
Colin Cross3ec27ec2019-05-01 15:54:05 -070045var (
Colin Cross8eebb132020-01-29 20:07:03 -080046 roboCoverageLibsTag = dependencyTag{name: "roboCoverageLibs"}
47 roboRuntimesTag = dependencyTag{name: "roboRuntimes"}
Colin Cross3ec27ec2019-05-01 15:54:05 -070048)
49
Colin Cross0ef08162019-05-01 15:50:51 -070050type robolectricProperties struct {
51 // The name of the android_app module that the tests will run against.
52 Instrumentation_for *string
53
Colin Cross3ec27ec2019-05-01 15:54:05 -070054 // Additional libraries for which coverage data should be generated
55 Coverage_libs []string
56
Colin Cross0ef08162019-05-01 15:50:51 -070057 Test_options struct {
58 // Timeout in seconds when running the tests.
Colin Cross2f9a7c82019-05-30 11:16:26 -070059 Timeout *int64
Colin Crossd2d11772019-05-30 11:17:23 -070060
61 // Number of shards to use when running the tests.
62 Shards *int64
Colin Cross0ef08162019-05-01 15:50:51 -070063 }
Colin Cross2787e8e2021-03-05 11:16:20 -080064
65 // The version number of a robolectric prebuilt to use from prebuilts/misc/common/robolectric
66 // instead of the one built from source in external/robolectric-shadows.
67 Robolectric_prebuilt_version *string
Rex Hoffmanebaac472022-11-14 04:06:48 +000068
69 // Use /external/robolectric rather than /external/robolectric-shadows as the version of robolectri
70 // to use. /external/robolectric closely tracks github's master, and will fully replace /external/robolectric-shadows
71 Upstream *bool
Colin Cross0ef08162019-05-01 15:50:51 -070072}
73
74type robolectricTest struct {
75 Library
76
77 robolectricProperties robolectricProperties
Colin Cross8eebb132020-01-29 20:07:03 -080078 testProperties testProperties
Colin Cross0ef08162019-05-01 15:50:51 -070079
Colin Crossd2d11772019-05-30 11:17:23 -070080 libs []string
81 tests []string
Colin Cross3ec27ec2019-05-01 15:54:05 -070082
Colin Cross8eebb132020-01-29 20:07:03 -080083 manifest android.Path
84 resourceApk android.Path
85
86 combinedJar android.WritablePath
87
Colin Cross3ec27ec2019-05-01 15:54:05 -070088 roboSrcJar android.Path
Colin Cross8eebb132020-01-29 20:07:03 -080089
90 testConfig android.Path
91 data android.Paths
Colin Cross0c66bc62021-07-20 09:47:41 -070092
93 forceOSType android.OsType
94 forceArchType android.ArchType
Colin Cross0ef08162019-05-01 15:50:51 -070095}
96
Colin Cross8eebb132020-01-29 20:07:03 -080097func (r *robolectricTest) TestSuites() []string {
98 return r.testProperties.Test_suites
99}
100
101var _ android.TestSuiteModule = (*robolectricTest)(nil)
102
Colin Cross0ef08162019-05-01 15:50:51 -0700103func (r *robolectricTest) DepsMutator(ctx android.BottomUpMutatorContext) {
104 r.Library.DepsMutator(ctx)
105
106 if r.robolectricProperties.Instrumentation_for != nil {
107 ctx.AddVariationDependencies(nil, instrumentationForTag, String(r.robolectricProperties.Instrumentation_for))
108 } else {
109 ctx.PropertyErrorf("instrumentation_for", "missing required instrumented module")
110 }
111
Colin Cross2787e8e2021-03-05 11:16:20 -0800112 if v := String(r.robolectricProperties.Robolectric_prebuilt_version); v != "" {
113 ctx.AddVariationDependencies(nil, libTag, fmt.Sprintf(robolectricPrebuiltLibPattern, v))
114 } else {
Rex Hoffmanebaac472022-11-14 04:06:48 +0000115 if proptools.Bool(r.robolectricProperties.Upstream) {
116 ctx.AddVariationDependencies(nil, libTag, robolectricCurrentLib+"_upstream")
117 } else {
118 ctx.AddVariationDependencies(nil, libTag, robolectricCurrentLib)
119 }
Colin Cross2787e8e2021-03-05 11:16:20 -0800120 }
121
Colin Cross0ef08162019-05-01 15:50:51 -0700122 ctx.AddVariationDependencies(nil, libTag, robolectricDefaultLibs...)
Colin Cross3ec27ec2019-05-01 15:54:05 -0700123
124 ctx.AddVariationDependencies(nil, roboCoverageLibsTag, r.robolectricProperties.Coverage_libs...)
Colin Cross8eebb132020-01-29 20:07:03 -0800125
Colin Cross5aa29a72020-09-14 19:54:47 -0700126 ctx.AddFarVariationDependencies(ctx.Config().BuildOSCommonTarget.Variations(),
127 roboRuntimesTag, "robolectric-android-all-prebuilts")
Colin Cross0ef08162019-05-01 15:50:51 -0700128}
129
130func (r *robolectricTest) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross0c66bc62021-07-20 09:47:41 -0700131 r.forceOSType = ctx.Config().BuildOS
132 r.forceArchType = ctx.Config().BuildArch
133
Colin Cross8eebb132020-01-29 20:07:03 -0800134 r.testConfig = tradefed.AutoGenRobolectricTestConfig(ctx, r.testProperties.Test_config,
135 r.testProperties.Test_config_template, r.testProperties.Test_suites,
136 r.testProperties.Auto_gen_config)
137 r.data = android.PathsForModuleSrc(ctx, r.testProperties.Data)
138
Colin Cross3ec27ec2019-05-01 15:54:05 -0700139 roboTestConfig := android.PathForModuleGen(ctx, "robolectric").
140 Join(ctx, "com/android/tools/test_config.properties")
141
142 // TODO: this inserts paths to built files into the test, it should really be inserting the contents.
143 instrumented := ctx.GetDirectDepsWithTag(instrumentationForTag)
144
145 if len(instrumented) != 1 {
146 panic(fmt.Errorf("expected exactly 1 instrumented dependency, got %d", len(instrumented)))
147 }
148
149 instrumentedApp, ok := instrumented[0].(*AndroidApp)
150 if !ok {
151 ctx.PropertyErrorf("instrumentation_for", "dependency must be an android_app")
152 }
153
Colin Cross8eebb132020-01-29 20:07:03 -0800154 r.manifest = instrumentedApp.mergedManifestFile
155 r.resourceApk = instrumentedApp.outputFile
156
Colin Cross3ec27ec2019-05-01 15:54:05 -0700157 generateRoboTestConfig(ctx, roboTestConfig, instrumentedApp)
158 r.extraResources = android.Paths{roboTestConfig}
159
Colin Cross0ef08162019-05-01 15:50:51 -0700160 r.Library.GenerateAndroidBuildActions(ctx)
161
Colin Cross3ec27ec2019-05-01 15:54:05 -0700162 roboSrcJar := android.PathForModuleGen(ctx, "robolectric", ctx.ModuleName()+".srcjar")
163 r.generateRoboSrcJar(ctx, roboSrcJar, instrumentedApp)
164 r.roboSrcJar = roboSrcJar
165
Colin Cross8eebb132020-01-29 20:07:03 -0800166 roboTestConfigJar := android.PathForModuleOut(ctx, "robolectric_samedir", "samedir_config.jar")
167 generateSameDirRoboTestConfigJar(ctx, roboTestConfigJar)
168
169 combinedJarJars := android.Paths{
170 // roboTestConfigJar comes first so that its com/android/tools/test_config.properties
171 // overrides the one from r.extraResources. The r.extraResources one can be removed
172 // once the Make test runner is removed.
173 roboTestConfigJar,
174 r.outputFile,
175 instrumentedApp.implementationAndResourcesJar,
Colin Cross0ef08162019-05-01 15:50:51 -0700176 }
Colin Crossd2d11772019-05-30 11:17:23 -0700177
Colin Cross8eebb132020-01-29 20:07:03 -0800178 for _, dep := range ctx.GetDirectDepsWithTag(libTag) {
Colin Crossdcf71b22021-02-01 13:59:03 -0800179 m := ctx.OtherModuleProvider(dep, JavaInfoProvider).(JavaInfo)
180 r.libs = append(r.libs, ctx.OtherModuleName(dep))
181 if !android.InList(ctx.OtherModuleName(dep), config.FrameworkLibraries) {
182 combinedJarJars = append(combinedJarJars, m.ImplementationAndResourcesJars...)
Colin Cross8eebb132020-01-29 20:07:03 -0800183 }
184 }
185
186 r.combinedJar = android.PathForModuleOut(ctx, "robolectric_combined", r.outputFile.Base())
187 TransformJarsToJar(ctx, r.combinedJar, "combine jars", combinedJarJars, android.OptionalPath{},
188 false, nil, nil)
189
Colin Crossd2d11772019-05-30 11:17:23 -0700190 // TODO: this could all be removed if tradefed was used as the test runner, it will find everything
191 // annotated as a test and run it.
192 for _, src := range r.compiledJavaSrcs {
193 s := src.Rel()
194 if !strings.HasSuffix(s, "Test.java") {
195 continue
196 } else if strings.HasSuffix(s, "/BaseRobolectricTest.java") {
197 continue
Ulya Trafimovich497a0932021-07-14 16:35:33 +0100198 } else {
Colin Crossd2d11772019-05-30 11:17:23 -0700199 s = strings.TrimPrefix(s, "src/")
200 }
201 r.tests = append(r.tests, s)
202 }
Colin Cross8eebb132020-01-29 20:07:03 -0800203
204 r.data = append(r.data, r.manifest, r.resourceApk)
205
206 runtimes := ctx.GetDirectDepWithTag("robolectric-android-all-prebuilts", roboRuntimesTag)
207
208 installPath := android.PathForModuleInstall(ctx, r.BaseModuleName())
209
210 installedResourceApk := ctx.InstallFile(installPath, ctx.ModuleName()+".apk", r.resourceApk)
211 installedManifest := ctx.InstallFile(installPath, ctx.ModuleName()+"-AndroidManifest.xml", r.manifest)
212 installedConfig := ctx.InstallFile(installPath, ctx.ModuleName()+".config", r.testConfig)
213
214 var installDeps android.Paths
215 for _, runtime := range runtimes.(*robolectricRuntimes).runtimes {
216 installDeps = append(installDeps, runtime)
217 }
218 installDeps = append(installDeps, installedResourceApk, installedManifest, installedConfig)
219
220 for _, data := range android.PathsForModuleSrc(ctx, r.testProperties.Data) {
221 installedData := ctx.InstallFile(installPath, data.Rel(), data)
222 installDeps = append(installDeps, installedData)
223 }
224
Colin Cross6301c3c2021-09-28 17:40:21 -0700225 r.installFile = ctx.InstallFile(installPath, ctx.ModuleName()+".jar", r.combinedJar, installDeps...)
Colin Crossd2d11772019-05-30 11:17:23 -0700226}
227
Colin Cross8eebb132020-01-29 20:07:03 -0800228func generateRoboTestConfig(ctx android.ModuleContext, outputFile android.WritablePath,
229 instrumentedApp *AndroidApp) {
Colin Crossf1a035e2020-11-16 17:32:30 -0800230 rule := android.NewRuleBuilder(pctx, ctx)
Colin Cross8eebb132020-01-29 20:07:03 -0800231
Colin Cross3ec27ec2019-05-01 15:54:05 -0700232 manifest := instrumentedApp.mergedManifestFile
233 resourceApk := instrumentedApp.outputFile
234
Colin Cross3ec27ec2019-05-01 15:54:05 -0700235 rule.Command().Text("rm -f").Output(outputFile)
236 rule.Command().
237 Textf(`echo "android_merged_manifest=%s" >>`, manifest.String()).Output(outputFile).Text("&&").
238 Textf(`echo "android_resource_apk=%s" >>`, resourceApk.String()).Output(outputFile).
239 // Make it depend on the files to which it points so the test file's timestamp is updated whenever the
240 // contents change
241 Implicit(manifest).
242 Implicit(resourceApk)
243
Colin Crossf1a035e2020-11-16 17:32:30 -0800244 rule.Build("generate_test_config", "generate test_config.properties")
Colin Cross3ec27ec2019-05-01 15:54:05 -0700245}
246
Colin Cross8eebb132020-01-29 20:07:03 -0800247func generateSameDirRoboTestConfigJar(ctx android.ModuleContext, outputFile android.ModuleOutPath) {
Colin Crossf1a035e2020-11-16 17:32:30 -0800248 rule := android.NewRuleBuilder(pctx, ctx)
Colin Cross8eebb132020-01-29 20:07:03 -0800249
250 outputDir := outputFile.InSameDir(ctx)
251 configFile := outputDir.Join(ctx, "com/android/tools/test_config.properties")
252 rule.Temporary(configFile)
253 rule.Command().Text("rm -f").Output(outputFile).Output(configFile)
254 rule.Command().Textf("mkdir -p $(dirname %s)", configFile.String())
255 rule.Command().
256 Text("(").
257 Textf(`echo "android_merged_manifest=%s-AndroidManifest.xml" &&`, ctx.ModuleName()).
258 Textf(`echo "android_resource_apk=%s.apk"`, ctx.ModuleName()).
259 Text(") >>").Output(configFile)
260 rule.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -0800261 BuiltTool("soong_zip").
Colin Cross8eebb132020-01-29 20:07:03 -0800262 FlagWithArg("-C ", outputDir.String()).
263 FlagWithInput("-f ", configFile).
264 FlagWithOutput("-o ", outputFile)
265
Colin Crossf1a035e2020-11-16 17:32:30 -0800266 rule.Build("generate_test_config_samedir", "generate test_config.properties")
Colin Cross8eebb132020-01-29 20:07:03 -0800267}
268
Colin Cross3ec27ec2019-05-01 15:54:05 -0700269func (r *robolectricTest) generateRoboSrcJar(ctx android.ModuleContext, outputFile android.WritablePath,
270 instrumentedApp *AndroidApp) {
271
272 srcJarArgs := copyOf(instrumentedApp.srcJarArgs)
273 srcJarDeps := append(android.Paths(nil), instrumentedApp.srcJarDeps...)
274
275 for _, m := range ctx.GetDirectDepsWithTag(roboCoverageLibsTag) {
Colin Crossdcf71b22021-02-01 13:59:03 -0800276 if ctx.OtherModuleHasProvider(m, JavaInfoProvider) {
277 dep := ctx.OtherModuleProvider(m, JavaInfoProvider).(JavaInfo)
278 srcJarArgs = append(srcJarArgs, dep.SrcJarArgs...)
279 srcJarDeps = append(srcJarDeps, dep.SrcJarDeps...)
Colin Cross3ec27ec2019-05-01 15:54:05 -0700280 }
281 }
282
283 TransformResourcesToJar(ctx, outputFile, srcJarArgs, srcJarDeps)
284}
285
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900286func (r *robolectricTest) AndroidMkEntries() []android.AndroidMkEntries {
287 entriesList := r.Library.AndroidMkEntries()
288 entries := &entriesList[0]
Colin Cross6301c3c2021-09-28 17:40:21 -0700289 entries.ExtraEntries = append(entries.ExtraEntries,
290 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
291 entries.SetBool("LOCAL_UNINSTALLABLE_MODULE", true)
292 })
Colin Cross0ef08162019-05-01 15:50:51 -0700293
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700294 entries.ExtraFooters = []android.AndroidMkExtraFootersFunc{
Jaewoong Jung02b11a62020-12-07 10:23:54 -0800295 func(w io.Writer, name, prefix, moduleDir string) {
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700296 if s := r.robolectricProperties.Test_options.Shards; s != nil && *s > 1 {
Colin Cross0a2f7192019-09-23 14:33:09 -0700297 numShards := int(*s)
298 shardSize := (len(r.tests) + numShards - 1) / numShards
299 shards := android.ShardStrings(r.tests, shardSize)
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700300 for i, shard := range shards {
301 r.writeTestRunner(w, name, "Run"+name+strconv.Itoa(i), shard)
302 }
Colin Cross0ef08162019-05-01 15:50:51 -0700303
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700304 // TODO: add rules to dist the outputs of the individual tests, or combine them together?
305 fmt.Fprintln(w, "")
306 fmt.Fprintln(w, ".PHONY:", "Run"+name)
307 fmt.Fprintln(w, "Run"+name, ": \\")
308 for i := range shards {
309 fmt.Fprintln(w, " ", "Run"+name+strconv.Itoa(i), "\\")
310 }
311 fmt.Fprintln(w, "")
312 } else {
313 r.writeTestRunner(w, name, "Run"+name, r.tests)
Colin Crossd2d11772019-05-30 11:17:23 -0700314 }
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700315 },
Colin Cross0ef08162019-05-01 15:50:51 -0700316 }
317
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900318 return entriesList
Colin Cross0ef08162019-05-01 15:50:51 -0700319}
320
Colin Crossd2d11772019-05-30 11:17:23 -0700321func (r *robolectricTest) writeTestRunner(w io.Writer, module, name string, tests []string) {
322 fmt.Fprintln(w, "")
323 fmt.Fprintln(w, "include $(CLEAR_VARS)")
324 fmt.Fprintln(w, "LOCAL_MODULE :=", name)
325 fmt.Fprintln(w, "LOCAL_JAVA_LIBRARIES :=", module)
326 fmt.Fprintln(w, "LOCAL_JAVA_LIBRARIES += ", strings.Join(r.libs, " "))
327 fmt.Fprintln(w, "LOCAL_TEST_PACKAGE :=", String(r.robolectricProperties.Instrumentation_for))
328 fmt.Fprintln(w, "LOCAL_INSTRUMENT_SRCJARS :=", r.roboSrcJar.String())
329 fmt.Fprintln(w, "LOCAL_ROBOTEST_FILES :=", strings.Join(tests, " "))
330 if t := r.robolectricProperties.Test_options.Timeout; t != nil {
331 fmt.Fprintln(w, "LOCAL_ROBOTEST_TIMEOUT :=", *t)
332 }
Colin Cross2787e8e2021-03-05 11:16:20 -0800333 if v := String(r.robolectricProperties.Robolectric_prebuilt_version); v != "" {
334 fmt.Fprintf(w, "-include prebuilts/misc/common/robolectric/%s/run_robotests.mk\n", v)
335 } else {
336 fmt.Fprintln(w, "-include external/robolectric-shadows/run_robotests.mk")
337 }
Colin Crossd2d11772019-05-30 11:17:23 -0700338}
339
Colin Cross0ef08162019-05-01 15:50:51 -0700340// An android_robolectric_test module compiles tests against the Robolectric framework that can run on the local host
341// instead of on a device. It also generates a rule with the name of the module prefixed with "Run" that can be
342// 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 -0700343//
344// The test runner considers any file listed in srcs whose name ends with Test.java to be a test class, unless
345// it is named BaseRobolectricTest.java. The path to the each source file must exactly match the package
346// name, or match the package name when the prefix "src/" is removed.
Colin Cross0ef08162019-05-01 15:50:51 -0700347func RobolectricTestFactory() android.Module {
348 module := &robolectricTest{}
349
Colin Crossce6734e2020-06-15 16:09:53 -0700350 module.addHostProperties()
Colin Cross0ef08162019-05-01 15:50:51 -0700351 module.AddProperties(
Colin Crosse323f3c2019-09-17 15:34:09 -0700352 &module.Module.deviceProperties,
Colin Cross8eebb132020-01-29 20:07:03 -0800353 &module.robolectricProperties,
354 &module.testProperties)
Colin Cross0ef08162019-05-01 15:50:51 -0700355
356 module.Module.dexpreopter.isTest = true
Colin Cross014489c2020-06-02 20:09:13 -0700357 module.Module.linter.test = true
Colin Cross0ef08162019-05-01 15:50:51 -0700358
Colin Cross8eebb132020-01-29 20:07:03 -0800359 module.testProperties.Test_suites = []string{"robolectric-tests"}
360
Colin Cross0ef08162019-05-01 15:50:51 -0700361 InitJavaModule(module, android.DeviceSupported)
362 return module
363}
Colin Cross8eebb132020-01-29 20:07:03 -0800364
Jiyong Park87788b52020-09-01 12:37:45 +0900365func (r *robolectricTest) InstallInTestcases() bool { return true }
366func (r *robolectricTest) InstallForceOS() (*android.OsType, *android.ArchType) {
Colin Cross0c66bc62021-07-20 09:47:41 -0700367 return &r.forceOSType, &r.forceArchType
Jiyong Park87788b52020-09-01 12:37:45 +0900368}
Colin Cross8eebb132020-01-29 20:07:03 -0800369
370func robolectricRuntimesFactory() android.Module {
371 module := &robolectricRuntimes{}
372 module.AddProperties(&module.props)
Colin Cross5aa29a72020-09-14 19:54:47 -0700373 android.InitAndroidArchModule(module, android.HostSupportedNoCross, android.MultilibCommon)
Colin Cross8eebb132020-01-29 20:07:03 -0800374 return module
375}
376
377type robolectricRuntimesProperties struct {
378 Jars []string `android:"path"`
379 Lib *string
380}
381
382type robolectricRuntimes struct {
383 android.ModuleBase
384
385 props robolectricRuntimesProperties
386
387 runtimes []android.InstallPath
Colin Cross0c66bc62021-07-20 09:47:41 -0700388
389 forceOSType android.OsType
390 forceArchType android.ArchType
Colin Cross8eebb132020-01-29 20:07:03 -0800391}
392
393func (r *robolectricRuntimes) TestSuites() []string {
394 return []string{"robolectric-tests"}
395}
396
397var _ android.TestSuiteModule = (*robolectricRuntimes)(nil)
398
399func (r *robolectricRuntimes) DepsMutator(ctx android.BottomUpMutatorContext) {
Jeongik Cha816a23a2020-07-08 01:09:23 +0900400 if !ctx.Config().AlwaysUsePrebuiltSdks() && r.props.Lib != nil {
Colin Cross8eebb132020-01-29 20:07:03 -0800401 ctx.AddVariationDependencies(nil, libTag, String(r.props.Lib))
402 }
403}
404
405func (r *robolectricRuntimes) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross5aa29a72020-09-14 19:54:47 -0700406 if ctx.Target().Os != ctx.Config().BuildOSCommonTarget.Os {
407 return
408 }
409
Colin Cross0c66bc62021-07-20 09:47:41 -0700410 r.forceOSType = ctx.Config().BuildOS
411 r.forceArchType = ctx.Config().BuildArch
412
Colin Cross8eebb132020-01-29 20:07:03 -0800413 files := android.PathsForModuleSrc(ctx, r.props.Jars)
414
415 androidAllDir := android.PathForModuleInstall(ctx, "android-all")
Colin Cross8eebb132020-01-29 20:07:03 -0800416 for _, from := range files {
417 installedRuntime := ctx.InstallFile(androidAllDir, from.Base(), from)
418 r.runtimes = append(r.runtimes, installedRuntime)
419 }
420
Jeongik Cha816a23a2020-07-08 01:09:23 +0900421 if !ctx.Config().AlwaysUsePrebuiltSdks() && r.props.Lib != nil {
Colin Cross8eebb132020-01-29 20:07:03 -0800422 runtimeFromSourceModule := ctx.GetDirectDepWithTag(String(r.props.Lib), libTag)
Jeongik Cha816a23a2020-07-08 01:09:23 +0900423 if runtimeFromSourceModule == nil {
424 if ctx.Config().AllowMissingDependencies() {
425 ctx.AddMissingDependencies([]string{String(r.props.Lib)})
426 } else {
427 ctx.PropertyErrorf("lib", "missing dependency %q", String(r.props.Lib))
428 }
429 return
430 }
Colin Cross8eebb132020-01-29 20:07:03 -0800431 runtimeFromSourceJar := android.OutputFileForModule(ctx, runtimeFromSourceModule, "")
432
Joseph Murphyc9648412021-07-20 13:58:15 -0700433 // "TREE" name is essential here because it hooks into the "TREE" name in
434 // Robolectric's SdkConfig.java that will always correspond to the NEWEST_SDK
435 // in Robolectric configs.
436 runtimeName := "android-all-current-robolectric-r0.jar"
Colin Cross8eebb132020-01-29 20:07:03 -0800437 installedRuntime := ctx.InstallFile(androidAllDir, runtimeName, runtimeFromSourceJar)
438 r.runtimes = append(r.runtimes, installedRuntime)
439 }
440}
441
Jiyong Park87788b52020-09-01 12:37:45 +0900442func (r *robolectricRuntimes) InstallInTestcases() bool { return true }
443func (r *robolectricRuntimes) InstallForceOS() (*android.OsType, *android.ArchType) {
Colin Cross0c66bc62021-07-20 09:47:41 -0700444 return &r.forceOSType, &r.forceArchType
Jiyong Park87788b52020-09-01 12:37:45 +0900445}