blob: 804949435d4f577fdf666549fbeead913adddf6c [file] [log] [blame]
Colin Cross3bc7ffa2017-11-22 16:19:37 -08001// Copyright 2017 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 (
Colin Crossd09b0b62018-04-18 11:06:47 -070018 "fmt"
Colin Crossa4f08812018-10-02 22:03:40 -070019 "path/filepath"
Colin Cross3bc7ffa2017-11-22 16:19:37 -080020 "reflect"
Colin Crossb69301e2017-12-01 10:48:26 -080021 "sort"
Colin Crossd09b0b62018-04-18 11:06:47 -070022 "strings"
Colin Cross3bc7ffa2017-11-22 16:19:37 -080023 "testing"
Jaewoong Junga5e5abc2019-04-26 14:31:50 -070024
25 "github.com/google/blueprint/proptools"
26
27 "android/soong/android"
28 "android/soong/cc"
Ulya Trafimovich9023b022021-03-22 16:02:28 +000029 "android/soong/dexpreopt"
Colin Cross3bc7ffa2017-11-22 16:19:37 -080030)
31
Paul Duffin71ae5942021-03-22 15:36:52 +000032// testApp runs tests using the prepareForJavaTest
Paul Duffin0ed42d32021-03-13 02:19:32 +000033//
34// See testJava for an explanation as to how to stop using this deprecated method.
35//
36// deprecated
37func testApp(t *testing.T, bp string) *android.TestContext {
38 t.Helper()
Paul Duffin71ae5942021-03-22 15:36:52 +000039 result := prepareForJavaTest.RunTestWithBp(t, bp)
Paul Duffin0ed42d32021-03-13 02:19:32 +000040 return result.TestContext
41}
42
43func TestApp(t *testing.T) {
44 resourceFiles := []string{
Colin Cross3bc7ffa2017-11-22 16:19:37 -080045 "res/layout/layout.xml",
46 "res/values/strings.xml",
47 "res/values-en-rUS/strings.xml",
48 }
49
Paul Duffin0ed42d32021-03-13 02:19:32 +000050 compiledResourceFiles := []string{
Colin Cross3bc7ffa2017-11-22 16:19:37 -080051 "aapt2/res/layout_layout.xml.flat",
52 "aapt2/res/values_strings.arsc.flat",
53 "aapt2/res/values-en-rUS_strings.arsc.flat",
54 }
Colin Cross3bc7ffa2017-11-22 16:19:37 -080055
Colin Crossa97c5d32018-03-28 14:58:31 -070056 for _, moduleType := range []string{"android_app", "android_library"} {
57 t.Run(moduleType, func(t *testing.T) {
Paul Duffin71ae5942021-03-22 15:36:52 +000058 result := android.GroupFixturePreparers(
59 prepareForJavaTest,
Paul Duffin0ed42d32021-03-13 02:19:32 +000060 android.FixtureModifyMockFS(func(fs android.MockFS) {
61 for _, file := range resourceFiles {
62 fs[file] = nil
63 }
64 }),
65 ).RunTestWithBp(t, moduleType+` {
Colin Crossa97c5d32018-03-28 14:58:31 -070066 name: "foo",
67 srcs: ["a.java"],
Jeongik Cha538c0d02019-07-11 15:54:27 +090068 sdk_version: "current"
Colin Crossa97c5d32018-03-28 14:58:31 -070069 }
70 `)
Colin Cross3bc7ffa2017-11-22 16:19:37 -080071
Paul Duffin0ed42d32021-03-13 02:19:32 +000072 foo := result.ModuleForTests("foo", "android_common")
Colin Cross3bc7ffa2017-11-22 16:19:37 -080073
Colin Cross31656952018-05-24 16:11:20 -070074 var expectedLinkImplicits []string
75
76 manifestFixer := foo.Output("manifest_fixer/AndroidManifest.xml")
77 expectedLinkImplicits = append(expectedLinkImplicits, manifestFixer.Output.String())
Colin Cross3bc7ffa2017-11-22 16:19:37 -080078
Paul Duffin0ed42d32021-03-13 02:19:32 +000079 frameworkRes := result.ModuleForTests("framework-res", "android_common")
Colin Crossa97c5d32018-03-28 14:58:31 -070080 expectedLinkImplicits = append(expectedLinkImplicits,
81 frameworkRes.Output("package-res.apk").Output.String())
Colin Cross3bc7ffa2017-11-22 16:19:37 -080082
Colin Crossa97c5d32018-03-28 14:58:31 -070083 // Test the mapping from input files to compiled output file names
84 compile := foo.Output(compiledResourceFiles[0])
Paul Duffin0ed42d32021-03-13 02:19:32 +000085 android.AssertDeepEquals(t, "aapt2 compile inputs", resourceFiles, compile.Inputs.Strings())
Colin Crossb69301e2017-12-01 10:48:26 -080086
Colin Crossa97c5d32018-03-28 14:58:31 -070087 compiledResourceOutputs := compile.Outputs.Strings()
88 sort.Strings(compiledResourceOutputs)
Colin Crossb69301e2017-12-01 10:48:26 -080089
Colin Crossa97c5d32018-03-28 14:58:31 -070090 expectedLinkImplicits = append(expectedLinkImplicits, compiledResourceOutputs...)
Colin Cross3bc7ffa2017-11-22 16:19:37 -080091
Colin Crossa97c5d32018-03-28 14:58:31 -070092 list := foo.Output("aapt2/res.list")
93 expectedLinkImplicits = append(expectedLinkImplicits, list.Output.String())
Colin Cross3bc7ffa2017-11-22 16:19:37 -080094
Colin Crossa97c5d32018-03-28 14:58:31 -070095 // Check that the link rule uses
Paul Duffin0ed42d32021-03-13 02:19:32 +000096 res := result.ModuleForTests("foo", "android_common").Output("package-res.apk")
97 android.AssertDeepEquals(t, "aapt2 link implicits", expectedLinkImplicits, res.Implicits.Strings())
Colin Crossa97c5d32018-03-28 14:58:31 -070098 })
Colin Cross3bc7ffa2017-11-22 16:19:37 -080099 }
100}
Colin Cross890ff552017-11-30 20:13:19 -0800101
Colin Crosse560c4a2019-03-19 16:03:11 -0700102func TestAppSplits(t *testing.T) {
103 ctx := testApp(t, `
104 android_app {
105 name: "foo",
106 srcs: ["a.java"],
107 package_splits: ["v4", "v7,hdpi"],
Jeongik Cha538c0d02019-07-11 15:54:27 +0900108 sdk_version: "current"
Colin Crosse560c4a2019-03-19 16:03:11 -0700109 }`)
110
111 foo := ctx.ModuleForTests("foo", "android_common")
112
113 expectedOutputs := []string{
Paul Duffinfb0fe9f2021-03-22 17:31:52 +0000114 "out/soong/.intermediates/foo/android_common/foo.apk",
115 "out/soong/.intermediates/foo/android_common/foo_v4.apk",
116 "out/soong/.intermediates/foo/android_common/foo_v7_hdpi.apk",
Colin Crosse560c4a2019-03-19 16:03:11 -0700117 }
118 for _, expectedOutput := range expectedOutputs {
119 foo.Output(expectedOutput)
120 }
121
Colin Cross41955e82019-05-29 14:40:35 -0700122 outputFiles, err := foo.Module().(*AndroidApp).OutputFiles("")
123 if err != nil {
124 t.Fatal(err)
125 }
Paul Duffinfb0fe9f2021-03-22 17:31:52 +0000126 android.AssertPathsRelativeToTopEquals(t, `OutputFiles("")`, expectedOutputs, outputFiles)
Colin Crosse560c4a2019-03-19 16:03:11 -0700127}
128
Jeongik Cha538c0d02019-07-11 15:54:27 +0900129func TestPlatformAPIs(t *testing.T) {
130 testJava(t, `
131 android_app {
132 name: "foo",
133 srcs: ["a.java"],
134 platform_apis: true,
135 }
136 `)
137
138 testJava(t, `
139 android_app {
140 name: "foo",
141 srcs: ["a.java"],
142 sdk_version: "current",
143 }
144 `)
145
Spandan Das60999342021-11-16 04:15:33 +0000146 testJavaError(t, "This module has conflicting settings. sdk_version is empty, which means that this module is build against platform APIs. However platform_apis is not set to true", `
Jeongik Cha538c0d02019-07-11 15:54:27 +0900147 android_app {
148 name: "bar",
149 srcs: ["b.java"],
150 }
151 `)
152
Spandan Das60999342021-11-16 04:15:33 +0000153 testJavaError(t, "This module has conflicting settings. sdk_version is not empty, which means this module cannot use platform APIs. However platform_apis is set to true.", `
Jeongik Cha538c0d02019-07-11 15:54:27 +0900154 android_app {
155 name: "bar",
156 srcs: ["b.java"],
157 sdk_version: "system_current",
158 platform_apis: true,
159 }
160 `)
161}
162
Jeongik Chae403e9e2019-12-07 00:16:24 +0900163func TestAndroidAppLinkType(t *testing.T) {
164 testJava(t, `
165 android_app {
166 name: "foo",
167 srcs: ["a.java"],
168 libs: ["bar"],
169 static_libs: ["baz"],
170 platform_apis: true,
171 }
172
173 java_library {
174 name: "bar",
175 sdk_version: "current",
176 srcs: ["b.java"],
177 }
178
179 android_library {
180 name: "baz",
181 sdk_version: "system_current",
182 srcs: ["c.java"],
183 }
184 `)
185
Steven Moreland00298982020-11-17 21:44:36 +0000186 testJavaError(t, "consider adjusting sdk_version: OR platform_apis:", `
Jeongik Chae403e9e2019-12-07 00:16:24 +0900187 android_app {
188 name: "foo",
189 srcs: ["a.java"],
190 libs: ["bar"],
191 sdk_version: "current",
192 static_libs: ["baz"],
193 }
194
195 java_library {
196 name: "bar",
197 sdk_version: "current",
198 srcs: ["b.java"],
199 }
200
201 android_library {
202 name: "baz",
203 sdk_version: "system_current",
204 srcs: ["c.java"],
205 }
206 `)
207
208 testJava(t, `
209 android_app {
210 name: "foo",
211 srcs: ["a.java"],
212 libs: ["bar"],
213 sdk_version: "system_current",
214 static_libs: ["baz"],
215 }
216
217 java_library {
218 name: "bar",
219 sdk_version: "current",
220 srcs: ["b.java"],
221 }
222
223 android_library {
224 name: "baz",
225 sdk_version: "system_current",
226 srcs: ["c.java"],
227 }
228 `)
229
Steven Moreland00298982020-11-17 21:44:36 +0000230 testJavaError(t, "consider adjusting sdk_version: OR platform_apis:", `
Jeongik Chae403e9e2019-12-07 00:16:24 +0900231 android_app {
232 name: "foo",
233 srcs: ["a.java"],
234 libs: ["bar"],
235 sdk_version: "system_current",
236 static_libs: ["baz"],
237 }
238
239 java_library {
240 name: "bar",
241 sdk_version: "current",
242 srcs: ["b.java"],
243 }
244
245 android_library {
246 name: "baz",
247 srcs: ["c.java"],
248 }
249 `)
250}
251
Artur Satayev2db1c3f2020-04-08 19:09:30 +0100252func TestUpdatableApps(t *testing.T) {
253 testCases := []struct {
254 name string
255 bp string
256 expectedError string
257 }{
258 {
259 name: "Stable public SDK",
260 bp: `android_app {
261 name: "foo",
262 srcs: ["a.java"],
263 sdk_version: "29",
Artur Satayevf40fc852020-04-16 13:43:02 +0100264 min_sdk_version: "29",
Artur Satayev2db1c3f2020-04-08 19:09:30 +0100265 updatable: true,
266 }`,
267 },
268 {
269 name: "Stable system SDK",
270 bp: `android_app {
271 name: "foo",
272 srcs: ["a.java"],
273 sdk_version: "system_29",
Artur Satayevf40fc852020-04-16 13:43:02 +0100274 min_sdk_version: "29",
Artur Satayev2db1c3f2020-04-08 19:09:30 +0100275 updatable: true,
276 }`,
277 },
278 {
279 name: "Current public SDK",
280 bp: `android_app {
281 name: "foo",
282 srcs: ["a.java"],
283 sdk_version: "current",
Artur Satayevf40fc852020-04-16 13:43:02 +0100284 min_sdk_version: "29",
Artur Satayev2db1c3f2020-04-08 19:09:30 +0100285 updatable: true,
286 }`,
287 },
288 {
289 name: "Current system SDK",
290 bp: `android_app {
291 name: "foo",
292 srcs: ["a.java"],
293 sdk_version: "system_current",
Artur Satayevf40fc852020-04-16 13:43:02 +0100294 min_sdk_version: "29",
Artur Satayev2db1c3f2020-04-08 19:09:30 +0100295 updatable: true,
296 }`,
297 },
298 {
299 name: "Current module SDK",
300 bp: `android_app {
301 name: "foo",
302 srcs: ["a.java"],
303 sdk_version: "module_current",
Artur Satayevf40fc852020-04-16 13:43:02 +0100304 min_sdk_version: "29",
Artur Satayev2db1c3f2020-04-08 19:09:30 +0100305 updatable: true,
306 }`,
307 },
308 {
309 name: "Current core SDK",
310 bp: `android_app {
311 name: "foo",
312 srcs: ["a.java"],
313 sdk_version: "core_current",
Artur Satayevf40fc852020-04-16 13:43:02 +0100314 min_sdk_version: "29",
Artur Satayev2db1c3f2020-04-08 19:09:30 +0100315 updatable: true,
316 }`,
317 },
318 {
319 name: "No Platform APIs",
320 bp: `android_app {
321 name: "foo",
322 srcs: ["a.java"],
323 platform_apis: true,
Artur Satayevf40fc852020-04-16 13:43:02 +0100324 min_sdk_version: "29",
Artur Satayev2db1c3f2020-04-08 19:09:30 +0100325 updatable: true,
326 }`,
327 expectedError: "Updatable apps must use stable SDKs",
328 },
329 {
330 name: "No Core Platform APIs",
331 bp: `android_app {
332 name: "foo",
333 srcs: ["a.java"],
334 sdk_version: "core_platform",
Artur Satayevf40fc852020-04-16 13:43:02 +0100335 min_sdk_version: "29",
Artur Satayev2db1c3f2020-04-08 19:09:30 +0100336 updatable: true,
337 }`,
338 expectedError: "Updatable apps must use stable SDKs",
339 },
340 {
341 name: "No unspecified APIs",
342 bp: `android_app {
343 name: "foo",
344 srcs: ["a.java"],
345 updatable: true,
Artur Satayevf40fc852020-04-16 13:43:02 +0100346 min_sdk_version: "29",
Artur Satayev2db1c3f2020-04-08 19:09:30 +0100347 }`,
348 expectedError: "Updatable apps must use stable SDK",
349 },
Artur Satayevf40fc852020-04-16 13:43:02 +0100350 {
351 name: "Must specify min_sdk_version",
352 bp: `android_app {
353 name: "app_without_min_sdk_version",
354 srcs: ["a.java"],
355 sdk_version: "29",
356 updatable: true,
357 }`,
358 expectedError: "updatable apps must set min_sdk_version.",
359 },
Artur Satayev2db1c3f2020-04-08 19:09:30 +0100360 }
361
362 for _, test := range testCases {
363 t.Run(test.name, func(t *testing.T) {
Paul Duffincdb88a92021-03-14 00:36:50 +0000364 errorHandler := android.FixtureExpectsNoErrors
365 if test.expectedError != "" {
366 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(test.expectedError)
Artur Satayev2db1c3f2020-04-08 19:09:30 +0100367 }
Paul Duffin71ae5942021-03-22 15:36:52 +0000368 android.GroupFixturePreparers(
369 prepareForJavaTest, FixtureWithPrebuiltApis(map[string][]string{
Paul Duffin2645a292021-03-13 02:36:00 +0000370 "29": {"foo"},
371 })).
Paul Duffincdb88a92021-03-14 00:36:50 +0000372 ExtendWithErrorHandler(errorHandler).RunTestWithBp(t, test.bp)
Artur Satayev2db1c3f2020-04-08 19:09:30 +0100373 })
374 }
375}
376
Jooyung Han749dc692020-04-15 11:03:39 +0900377func TestUpdatableApps_TransitiveDepsShouldSetMinSdkVersion(t *testing.T) {
378 testJavaError(t, `module "bar".*: should support min_sdk_version\(29\)`, cc.GatherRequiredDepsForTest(android.Android)+`
379 android_app {
380 name: "foo",
381 srcs: ["a.java"],
382 updatable: true,
383 sdk_version: "current",
384 min_sdk_version: "29",
385 static_libs: ["bar"],
386 }
387
388 java_library {
389 name: "bar",
390 sdk_version: "current",
391 }
392 `)
393}
394
Jooyung Hanbbc3fb72020-04-29 14:01:06 +0900395func TestUpdatableApps_JniLibsShouldShouldSupportMinSdkVersion(t *testing.T) {
396 testJava(t, cc.GatherRequiredDepsForTest(android.Android)+`
397 android_app {
398 name: "foo",
399 srcs: ["a.java"],
400 updatable: true,
401 sdk_version: "current",
402 min_sdk_version: "current",
403 jni_libs: ["libjni"],
404 }
405
406 cc_library {
407 name: "libjni",
408 stl: "none",
409 system_shared_libs: [],
410 sdk_version: "current",
411 }
412 `)
413}
414
415func TestUpdatableApps_JniLibShouldBeBuiltAgainstMinSdkVersion(t *testing.T) {
416 bp := cc.GatherRequiredDepsForTest(android.Android) + `
417 android_app {
418 name: "foo",
419 srcs: ["a.java"],
420 updatable: true,
421 sdk_version: "current",
422 min_sdk_version: "29",
423 jni_libs: ["libjni"],
424 }
425
426 cc_library {
427 name: "libjni",
428 stl: "none",
429 system_shared_libs: [],
Spandan Das2e8c0442022-05-08 00:39:35 +0000430 sdk_version: "current",
431 min_sdk_version: "29",
Jooyung Hanbbc3fb72020-04-29 14:01:06 +0900432 }
Jooyung Hanbbc3fb72020-04-29 14:01:06 +0900433 `
434 fs := map[string][]byte{
435 "prebuilts/ndk/current/platforms/android-29/arch-arm64/usr/lib/crtbegin_so.o": nil,
436 "prebuilts/ndk/current/platforms/android-29/arch-arm64/usr/lib/crtend_so.o": nil,
437 "prebuilts/ndk/current/platforms/android-29/arch-arm/usr/lib/crtbegin_so.o": nil,
438 "prebuilts/ndk/current/platforms/android-29/arch-arm/usr/lib/crtend_so.o": nil,
439 }
440
Paul Duffinfb0fe9f2021-03-22 17:31:52 +0000441 ctx, _ := testJavaWithFS(t, bp, fs)
Jooyung Hanbbc3fb72020-04-29 14:01:06 +0900442
443 inputs := ctx.ModuleForTests("libjni", "android_arm64_armv8-a_sdk_shared").Description("link").Implicits
444 var crtbeginFound, crtendFound bool
Dan Albert92fe7402020-07-15 13:33:30 -0700445 expectedCrtBegin := ctx.ModuleForTests("crtbegin_so",
Colin Crossdea1d032022-12-06 14:50:08 -0800446 "android_arm64_armv8-a_sdk_29").Rule("noAddrSig").Output
Dan Albert92fe7402020-07-15 13:33:30 -0700447 expectedCrtEnd := ctx.ModuleForTests("crtend_so",
Colin Crossdea1d032022-12-06 14:50:08 -0800448 "android_arm64_armv8-a_sdk_29").Rule("noAddrSig").Output
Dan Albert92fe7402020-07-15 13:33:30 -0700449 implicits := []string{}
Jooyung Hanbbc3fb72020-04-29 14:01:06 +0900450 for _, input := range inputs {
Dan Albert92fe7402020-07-15 13:33:30 -0700451 implicits = append(implicits, input.String())
452 if strings.HasSuffix(input.String(), expectedCrtBegin.String()) {
Jooyung Hanbbc3fb72020-04-29 14:01:06 +0900453 crtbeginFound = true
Dan Albert92fe7402020-07-15 13:33:30 -0700454 } else if strings.HasSuffix(input.String(), expectedCrtEnd.String()) {
Jooyung Hanbbc3fb72020-04-29 14:01:06 +0900455 crtendFound = true
456 }
457 }
Dan Albert92fe7402020-07-15 13:33:30 -0700458 if !crtbeginFound {
459 t.Error(fmt.Sprintf(
460 "expected implicit with suffix %q, have the following implicits:\n%s",
461 expectedCrtBegin, strings.Join(implicits, "\n")))
462 }
463 if !crtendFound {
464 t.Error(fmt.Sprintf(
465 "expected implicit with suffix %q, have the following implicits:\n%s",
466 expectedCrtEnd, strings.Join(implicits, "\n")))
Jooyung Hanbbc3fb72020-04-29 14:01:06 +0900467 }
468}
469
470func TestUpdatableApps_ErrorIfJniLibDoesntSupportMinSdkVersion(t *testing.T) {
471 bp := cc.GatherRequiredDepsForTest(android.Android) + `
472 android_app {
473 name: "foo",
474 srcs: ["a.java"],
475 updatable: true,
476 sdk_version: "current",
477 min_sdk_version: "29", // this APK should support 29
478 jni_libs: ["libjni"],
479 }
480
481 cc_library {
482 name: "libjni",
483 stl: "none",
484 sdk_version: "current",
Spandan Das2e8c0442022-05-08 00:39:35 +0000485 min_sdk_version: "current",
Jooyung Hanbbc3fb72020-04-29 14:01:06 +0900486 }
487 `
Spandan Das2e8c0442022-05-08 00:39:35 +0000488 testJavaError(t, `"libjni" .*: min_sdk_version\(current\) is higher than min_sdk_version\(29\)`, bp)
Jooyung Hanbbc3fb72020-04-29 14:01:06 +0900489}
490
Spandan Das2e8c0442022-05-08 00:39:35 +0000491func TestUpdatableApps_ErrorIfDepMinSdkVersionIsHigher(t *testing.T) {
Jooyung Hanbbc3fb72020-04-29 14:01:06 +0900492 bp := cc.GatherRequiredDepsForTest(android.Android) + `
493 android_app {
494 name: "foo",
495 srcs: ["a.java"],
496 updatable: true,
497 sdk_version: "current",
498 min_sdk_version: "29", // this APK should support 29
499 jni_libs: ["libjni"],
500 }
501
502 cc_library {
503 name: "libjni",
504 stl: "none",
505 shared_libs: ["libbar"],
506 system_shared_libs: [],
507 sdk_version: "27",
Spandan Das2e8c0442022-05-08 00:39:35 +0000508 min_sdk_version: "27",
Jooyung Hanbbc3fb72020-04-29 14:01:06 +0900509 }
510
511 cc_library {
512 name: "libbar",
513 stl: "none",
514 system_shared_libs: [],
515 sdk_version: "current",
Spandan Das2e8c0442022-05-08 00:39:35 +0000516 min_sdk_version: "current",
Jooyung Hanbbc3fb72020-04-29 14:01:06 +0900517 }
518 `
519 testJavaError(t, `"libjni" .*: links "libbar" built against newer API version "current"`, bp)
520}
521
Colin Cross0ddae7f2019-02-07 15:30:01 -0800522func TestResourceDirs(t *testing.T) {
523 testCases := []struct {
524 name string
525 prop string
526 resources []string
527 }{
528 {
529 name: "no resource_dirs",
530 prop: "",
531 resources: []string{"res/res/values/strings.xml"},
532 },
533 {
534 name: "resource_dirs",
535 prop: `resource_dirs: ["res"]`,
536 resources: []string{"res/res/values/strings.xml"},
537 },
538 {
539 name: "empty resource_dirs",
540 prop: `resource_dirs: []`,
541 resources: nil,
542 },
543 }
544
Paul Duffinfb0fe9f2021-03-22 17:31:52 +0000545 fs := android.MockFS{
Colin Cross0ddae7f2019-02-07 15:30:01 -0800546 "res/res/values/strings.xml": nil,
547 }
548
549 bp := `
550 android_app {
551 name: "foo",
Jeongik Cha538c0d02019-07-11 15:54:27 +0900552 sdk_version: "current",
Colin Cross0ddae7f2019-02-07 15:30:01 -0800553 %s
554 }
555 `
556
557 for _, testCase := range testCases {
558 t.Run(testCase.name, func(t *testing.T) {
Paul Duffinfb0fe9f2021-03-22 17:31:52 +0000559 result := android.GroupFixturePreparers(
560 PrepareForTestWithJavaDefaultModules,
Paul Duffinfb0fe9f2021-03-22 17:31:52 +0000561 fs.AddToFixture(),
562 ).RunTestWithBp(t, fmt.Sprintf(bp, testCase.prop))
Colin Cross0ddae7f2019-02-07 15:30:01 -0800563
Paul Duffinfb0fe9f2021-03-22 17:31:52 +0000564 module := result.ModuleForTests("foo", "android_common")
Colin Cross0ddae7f2019-02-07 15:30:01 -0800565 resourceList := module.MaybeOutput("aapt2/res.list")
566
567 var resources []string
568 if resourceList.Rule != nil {
569 for _, compiledResource := range resourceList.Inputs.Strings() {
570 resources = append(resources, module.Output(compiledResource).Inputs.Strings()...)
571 }
572 }
573
Paul Duffinfb0fe9f2021-03-22 17:31:52 +0000574 android.AssertDeepEquals(t, "resource files", testCase.resources, resources)
Colin Cross0ddae7f2019-02-07 15:30:01 -0800575 })
576 }
577}
578
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800579func TestLibraryAssets(t *testing.T) {
580 bp := `
581 android_app {
582 name: "foo",
583 sdk_version: "current",
584 static_libs: ["lib1", "lib2", "lib3"],
585 }
586
587 android_library {
588 name: "lib1",
589 sdk_version: "current",
590 asset_dirs: ["assets_a"],
591 }
592
593 android_library {
594 name: "lib2",
595 sdk_version: "current",
596 }
597
598 android_library {
599 name: "lib3",
600 sdk_version: "current",
Colin Crossab8d1382023-07-14 17:23:41 +0000601 static_libs: ["lib4", "import"],
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800602 }
603
604 android_library {
605 name: "lib4",
606 sdk_version: "current",
607 asset_dirs: ["assets_b"],
608 }
Colin Crossab8d1382023-07-14 17:23:41 +0000609
Jiakai Zhangba82e282023-10-13 18:08:59 +0100610 android_library {
611 name: "lib5",
612 sdk_version: "current",
613 assets: [
614 "path/to/asset_file_1",
615 "path/to/asset_file_2",
616 ],
617 }
618
Colin Crossab8d1382023-07-14 17:23:41 +0000619 android_library_import {
620 name: "import",
621 sdk_version: "current",
622 aars: ["import.aar"],
623 }
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800624 `
625
626 testCases := []struct {
Jiakai Zhangba82e282023-10-13 18:08:59 +0100627 name string
628 assetFlag string
629 assetPackages []string
630 tmpAssetDirInputs []string
631 tmpAssetDirOutputs []string
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800632 }{
633 {
634 name: "foo",
Colin Crossab8d1382023-07-14 17:23:41 +0000635 // lib1 has its own assets. lib3 doesn't have any, but lib4 and import have assets.
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800636 assetPackages: []string{
Paul Duffinfb0fe9f2021-03-22 17:31:52 +0000637 "out/soong/.intermediates/foo/android_common/aapt2/package-res.apk",
638 "out/soong/.intermediates/lib1/android_common/assets.zip",
Colin Crossab8d1382023-07-14 17:23:41 +0000639 "out/soong/.intermediates/lib4/android_common/assets.zip",
640 "out/soong/.intermediates/import/android_common/assets.zip",
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800641 },
642 },
643 {
644 name: "lib1",
645 assetFlag: "-A assets_a",
646 },
647 {
648 name: "lib2",
649 },
650 {
651 name: "lib3",
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800652 },
653 {
654 name: "lib4",
655 assetFlag: "-A assets_b",
656 },
Jiakai Zhangba82e282023-10-13 18:08:59 +0100657 {
658 name: "lib5",
659 assetFlag: "-A out/soong/.intermediates/lib5/android_common/tmp_asset_dir",
660 tmpAssetDirInputs: []string{
661 "path/to/asset_file_1",
662 "path/to/asset_file_2",
663 },
664 tmpAssetDirOutputs: []string{
665 "out/soong/.intermediates/lib5/android_common/tmp_asset_dir/path/to/asset_file_1",
666 "out/soong/.intermediates/lib5/android_common/tmp_asset_dir/path/to/asset_file_2",
667 },
668 },
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800669 }
670 ctx := testApp(t, bp)
671
672 for _, test := range testCases {
673 t.Run(test.name, func(t *testing.T) {
674 m := ctx.ModuleForTests(test.name, "android_common")
675
676 // Check asset flag in aapt2 link flags
677 var aapt2link android.TestingBuildParams
678 if len(test.assetPackages) > 0 {
679 aapt2link = m.Output("aapt2/package-res.apk")
680 } else {
681 aapt2link = m.Output("package-res.apk")
682 }
Paul Duffina71a67a2021-03-29 00:42:57 +0100683 aapt2link = aapt2link
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800684 aapt2Flags := aapt2link.Args["flags"]
685 if test.assetFlag != "" {
Paul Duffinfb0fe9f2021-03-22 17:31:52 +0000686 android.AssertStringDoesContain(t, "asset flag", aapt2Flags, test.assetFlag)
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800687 } else {
Paul Duffinfb0fe9f2021-03-22 17:31:52 +0000688 android.AssertStringDoesNotContain(t, "aapt2 link flags", aapt2Flags, " -A ")
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800689 }
690
691 // Check asset merge rule.
692 if len(test.assetPackages) > 0 {
693 mergeAssets := m.Output("package-res.apk")
Paul Duffinfb0fe9f2021-03-22 17:31:52 +0000694 android.AssertPathsRelativeToTopEquals(t, "mergeAssets inputs", test.assetPackages, mergeAssets.Inputs)
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800695 }
Jiakai Zhangba82e282023-10-13 18:08:59 +0100696
697 if len(test.tmpAssetDirInputs) > 0 {
698 rule := m.Rule("tmp_asset_dir")
699 inputs := rule.Implicits
700 outputs := append(android.WritablePaths{rule.Output}, rule.ImplicitOutputs...).Paths()
701 android.AssertPathsRelativeToTopEquals(t, "tmp_asset_dir inputs", test.tmpAssetDirInputs, inputs)
702 android.AssertPathsRelativeToTopEquals(t, "tmp_asset_dir outputs", test.tmpAssetDirOutputs, outputs)
703 }
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800704 })
705 }
706}
707
Colin Crossb014f072021-02-26 14:54:36 -0800708func TestAppJavaResources(t *testing.T) {
709 bp := `
710 android_app {
711 name: "foo",
712 sdk_version: "current",
713 java_resources: ["resources/a"],
714 srcs: ["a.java"],
715 }
716
717 android_app {
718 name: "bar",
719 sdk_version: "current",
720 java_resources: ["resources/a"],
721 }
722 `
723
724 ctx := testApp(t, bp)
725
726 foo := ctx.ModuleForTests("foo", "android_common")
727 fooResources := foo.Output("res/foo.jar")
728 fooDexJar := foo.Output("dex-withres/foo.jar")
729 fooDexJarAligned := foo.Output("dex-withres-aligned/foo.jar")
730 fooApk := foo.Rule("combineApk")
731
732 if g, w := fooDexJar.Inputs.Strings(), fooResources.Output.String(); !android.InList(w, g) {
733 t.Errorf("expected resource jar %q in foo dex jar inputs %q", w, g)
734 }
735
736 if g, w := fooDexJarAligned.Input.String(), fooDexJar.Output.String(); g != w {
737 t.Errorf("expected dex jar %q in foo aligned dex jar inputs %q", w, g)
738 }
739
740 if g, w := fooApk.Inputs.Strings(), fooDexJarAligned.Output.String(); !android.InList(w, g) {
741 t.Errorf("expected aligned dex jar %q in foo apk inputs %q", w, g)
742 }
743
744 bar := ctx.ModuleForTests("bar", "android_common")
745 barResources := bar.Output("res/bar.jar")
746 barApk := bar.Rule("combineApk")
747
748 if g, w := barApk.Inputs.Strings(), barResources.Output.String(); !android.InList(w, g) {
749 t.Errorf("expected resources jar %q in bar apk inputs %q", w, g)
750 }
751}
752
Colin Crossff3ff7f2023-07-05 14:04:12 -0700753func TestAndroidResourceProcessor(t *testing.T) {
754 testCases := []struct {
Colin Cross8676c8c2023-10-12 15:58:57 -0700755 name string
756 appUsesRP bool
757 directLibUsesRP bool
758 transitiveLibUsesRP bool
759 sharedLibUsesRP bool
760 sharedTransitiveStaticLibUsesRP bool
761 sharedTransitiveSharedLibUsesRP bool
Colin Crossff3ff7f2023-07-05 14:04:12 -0700762
763 dontVerifyApp bool
764 appResources []string
765 appOverlays []string
766 appImports []string
767 appSrcJars []string
768 appClasspath []string
769 appCombined []string
770
771 dontVerifyDirect bool
772 directResources []string
773 directOverlays []string
774 directImports []string
775 directSrcJars []string
776 directClasspath []string
777 directCombined []string
778
779 dontVerifyTransitive bool
780 transitiveResources []string
781 transitiveOverlays []string
782 transitiveImports []string
783 transitiveSrcJars []string
784 transitiveClasspath []string
785 transitiveCombined []string
786
787 dontVerifyDirectImport bool
788 directImportResources []string
789 directImportOverlays []string
790 directImportImports []string
791
792 dontVerifyTransitiveImport bool
793 transitiveImportResources []string
794 transitiveImportOverlays []string
795 transitiveImportImports []string
Colin Cross8676c8c2023-10-12 15:58:57 -0700796
797 dontVerifyShared bool
798 sharedResources []string
799 sharedOverlays []string
800 sharedImports []string
801 sharedSrcJars []string
802 sharedClasspath []string
803 sharedCombined []string
Colin Crossff3ff7f2023-07-05 14:04:12 -0700804 }{
805 {
Colin Cross4eae06d2023-06-20 22:40:02 -0700806 // Test with all modules set to use_resource_processor: false (except android_library_import modules,
807 // which always use resource processor).
808 name: "legacy",
809 appUsesRP: false,
810 directLibUsesRP: false,
811 transitiveLibUsesRP: false,
Colin Crossff3ff7f2023-07-05 14:04:12 -0700812
813 appResources: nil,
814 appOverlays: []string{
815 "out/soong/.intermediates/transitive/android_common/package-res.apk",
Colin Crossab8d1382023-07-14 17:23:41 +0000816 "out/soong/.intermediates/transitive_import_dep/android_common/package-res.apk",
Colin Crossff3ff7f2023-07-05 14:04:12 -0700817 "out/soong/.intermediates/transitive_import/android_common/package-res.apk",
818 "out/soong/.intermediates/direct/android_common/package-res.apk",
Colin Crossab8d1382023-07-14 17:23:41 +0000819 "out/soong/.intermediates/direct_import_dep/android_common/package-res.apk",
Colin Crossff3ff7f2023-07-05 14:04:12 -0700820 "out/soong/.intermediates/direct_import/android_common/package-res.apk",
821 "out/soong/.intermediates/app/android_common/aapt2/app/res/values_strings.arsc.flat",
822 },
Colin Cross8676c8c2023-10-12 15:58:57 -0700823 appImports: []string{
824 "out/soong/.intermediates/shared/android_common/package-res.apk",
825 "out/soong/.intermediates/default/java/framework-res/android_common/package-res.apk",
826 },
Colin Crossff3ff7f2023-07-05 14:04:12 -0700827 appSrcJars: []string{"out/soong/.intermediates/app/android_common/gen/android/R.srcjar"},
828 appClasspath: []string{
829 "out/soong/.intermediates/default/java/android_stubs_current/android_common/turbine-combined/android_stubs_current.jar",
Colin Cross8676c8c2023-10-12 15:58:57 -0700830 "out/soong/.intermediates/shared/android_common/turbine-combined/shared.jar",
Colin Crossff3ff7f2023-07-05 14:04:12 -0700831 "out/soong/.intermediates/direct/android_common/turbine-combined/direct.jar",
Colin Cross9055e212024-03-23 04:43:41 +0000832 "out/soong/.intermediates/direct_import/android_common/turbine-combined/direct_import.jar",
Colin Crossff3ff7f2023-07-05 14:04:12 -0700833 },
834 appCombined: []string{
835 "out/soong/.intermediates/app/android_common/javac/app.jar",
836 "out/soong/.intermediates/direct/android_common/combined/direct.jar",
Colin Cross9055e212024-03-23 04:43:41 +0000837 "out/soong/.intermediates/direct_import/android_common/combined/direct_import.jar",
Colin Crossff3ff7f2023-07-05 14:04:12 -0700838 },
839
840 directResources: nil,
841 directOverlays: []string{
842 "out/soong/.intermediates/transitive/android_common/package-res.apk",
Colin Crossab8d1382023-07-14 17:23:41 +0000843 "out/soong/.intermediates/transitive_import_dep/android_common/package-res.apk",
Colin Crossff3ff7f2023-07-05 14:04:12 -0700844 "out/soong/.intermediates/transitive_import/android_common/package-res.apk",
845 "out/soong/.intermediates/direct/android_common/aapt2/direct/res/values_strings.arsc.flat",
846 },
847 directImports: []string{"out/soong/.intermediates/default/java/framework-res/android_common/package-res.apk"},
848 directSrcJars: []string{"out/soong/.intermediates/direct/android_common/gen/android/R.srcjar"},
849 directClasspath: []string{
850 "out/soong/.intermediates/default/java/android_stubs_current/android_common/turbine-combined/android_stubs_current.jar",
851 "out/soong/.intermediates/transitive/android_common/turbine-combined/transitive.jar",
Colin Cross9055e212024-03-23 04:43:41 +0000852 "out/soong/.intermediates/transitive_import/android_common/turbine-combined/transitive_import.jar",
Colin Crossff3ff7f2023-07-05 14:04:12 -0700853 },
854 directCombined: []string{
855 "out/soong/.intermediates/direct/android_common/javac/direct.jar",
856 "out/soong/.intermediates/transitive/android_common/javac/transitive.jar",
Colin Cross9055e212024-03-23 04:43:41 +0000857 "out/soong/.intermediates/transitive_import/android_common/combined/transitive_import.jar",
Colin Crossff3ff7f2023-07-05 14:04:12 -0700858 },
859
860 transitiveResources: []string{"out/soong/.intermediates/transitive/android_common/aapt2/transitive/res/values_strings.arsc.flat"},
861 transitiveOverlays: nil,
862 transitiveImports: []string{"out/soong/.intermediates/default/java/framework-res/android_common/package-res.apk"},
863 transitiveSrcJars: []string{"out/soong/.intermediates/transitive/android_common/gen/android/R.srcjar"},
864 transitiveClasspath: []string{"out/soong/.intermediates/default/java/android_stubs_current/android_common/turbine-combined/android_stubs_current.jar"},
865 transitiveCombined: nil,
866
Colin Cross8676c8c2023-10-12 15:58:57 -0700867 sharedResources: nil,
868 sharedOverlays: []string{
869 "out/soong/.intermediates/shared_transitive_static/android_common/package-res.apk",
870 "out/soong/.intermediates/shared/android_common/aapt2/shared/res/values_strings.arsc.flat",
871 },
872 sharedImports: []string{
873 "out/soong/.intermediates/shared_transitive_shared/android_common/package-res.apk",
874 "out/soong/.intermediates/default/java/framework-res/android_common/package-res.apk",
875 },
876 sharedSrcJars: []string{"out/soong/.intermediates/shared/android_common/gen/android/R.srcjar"},
877 sharedClasspath: []string{
878 "out/soong/.intermediates/default/java/android_stubs_current/android_common/turbine-combined/android_stubs_current.jar",
879 "out/soong/.intermediates/shared_transitive_shared/android_common/turbine-combined/shared_transitive_shared.jar",
880 "out/soong/.intermediates/shared_transitive_static/android_common/turbine-combined/shared_transitive_static.jar",
881 },
882 sharedCombined: []string{
883 "out/soong/.intermediates/shared/android_common/javac/shared.jar",
884 "out/soong/.intermediates/shared_transitive_static/android_common/javac/shared_transitive_static.jar",
885 },
886
Colin Crossff3ff7f2023-07-05 14:04:12 -0700887 directImportResources: nil,
Colin Cross4eae06d2023-06-20 22:40:02 -0700888 directImportOverlays: []string{"out/soong/.intermediates/direct_import/android_common/flat-res/gen_res.flata"},
889 directImportImports: []string{
890 "out/soong/.intermediates/default/java/framework-res/android_common/package-res.apk",
Colin Crossff3ff7f2023-07-05 14:04:12 -0700891 "out/soong/.intermediates/direct_import_dep/android_common/package-res.apk",
892 },
Colin Crossff3ff7f2023-07-05 14:04:12 -0700893
894 transitiveImportResources: nil,
Colin Cross4eae06d2023-06-20 22:40:02 -0700895 transitiveImportOverlays: []string{"out/soong/.intermediates/transitive_import/android_common/flat-res/gen_res.flata"},
896 transitiveImportImports: []string{
897 "out/soong/.intermediates/default/java/framework-res/android_common/package-res.apk",
Colin Crossff3ff7f2023-07-05 14:04:12 -0700898 "out/soong/.intermediates/transitive_import_dep/android_common/package-res.apk",
899 },
Colin Cross4eae06d2023-06-20 22:40:02 -0700900 },
901 {
902 // Test with all modules set to use_resource_processor: true.
Colin Cross8676c8c2023-10-12 15:58:57 -0700903 name: "resource_processor",
904 appUsesRP: true,
905 directLibUsesRP: true,
906 transitiveLibUsesRP: true,
907 sharedLibUsesRP: true,
908 sharedTransitiveSharedLibUsesRP: true,
909 sharedTransitiveStaticLibUsesRP: true,
Colin Cross4eae06d2023-06-20 22:40:02 -0700910
911 appResources: nil,
912 appOverlays: []string{
913 "out/soong/.intermediates/transitive/android_common/package-res.apk",
914 "out/soong/.intermediates/transitive_import_dep/android_common/package-res.apk",
915 "out/soong/.intermediates/transitive_import/android_common/package-res.apk",
916 "out/soong/.intermediates/direct/android_common/package-res.apk",
917 "out/soong/.intermediates/direct_import_dep/android_common/package-res.apk",
918 "out/soong/.intermediates/direct_import/android_common/package-res.apk",
919 "out/soong/.intermediates/app/android_common/aapt2/app/res/values_strings.arsc.flat",
920 },
Colin Cross8676c8c2023-10-12 15:58:57 -0700921 appImports: []string{
922 "out/soong/.intermediates/shared/android_common/package-res.apk",
923 "out/soong/.intermediates/default/java/framework-res/android_common/package-res.apk",
924 },
Colin Cross4eae06d2023-06-20 22:40:02 -0700925 appSrcJars: nil,
926 appClasspath: []string{
927 "out/soong/.intermediates/default/java/android_stubs_current/android_common/turbine-combined/android_stubs_current.jar",
928 "out/soong/.intermediates/app/android_common/busybox/R.jar",
Colin Cross8676c8c2023-10-12 15:58:57 -0700929 "out/soong/.intermediates/shared/android_common/turbine-combined/shared.jar",
Colin Cross4eae06d2023-06-20 22:40:02 -0700930 "out/soong/.intermediates/direct/android_common/turbine-combined/direct.jar",
Colin Cross9055e212024-03-23 04:43:41 +0000931 "out/soong/.intermediates/direct_import/android_common/turbine-combined/direct_import.jar",
Colin Cross4eae06d2023-06-20 22:40:02 -0700932 },
933 appCombined: []string{
Colin Cross4eae06d2023-06-20 22:40:02 -0700934 "out/soong/.intermediates/app/android_common/javac/app.jar",
Colin Crossfd620b22024-02-23 10:05:21 -0800935 "out/soong/.intermediates/app/android_common/busybox/R.jar",
Colin Cross4eae06d2023-06-20 22:40:02 -0700936 "out/soong/.intermediates/direct/android_common/combined/direct.jar",
Colin Cross9055e212024-03-23 04:43:41 +0000937 "out/soong/.intermediates/direct_import/android_common/combined/direct_import.jar",
Colin Cross4eae06d2023-06-20 22:40:02 -0700938 },
939
940 directResources: nil,
941 directOverlays: []string{"out/soong/.intermediates/direct/android_common/aapt2/direct/res/values_strings.arsc.flat"},
942 directImports: []string{
943 "out/soong/.intermediates/default/java/framework-res/android_common/package-res.apk",
944 "out/soong/.intermediates/transitive_import/android_common/package-res.apk",
945 "out/soong/.intermediates/transitive_import_dep/android_common/package-res.apk",
946 "out/soong/.intermediates/transitive/android_common/package-res.apk",
947 },
948 directSrcJars: nil,
949 directClasspath: []string{
950 "out/soong/.intermediates/default/java/android_stubs_current/android_common/turbine-combined/android_stubs_current.jar",
Colin Cross4eae06d2023-06-20 22:40:02 -0700951 "out/soong/.intermediates/direct/android_common/busybox/R.jar",
Colin Cross1d3f5902024-03-05 11:51:54 -0800952 "out/soong/.intermediates/transitive/android_common/busybox/R.jar",
953 "out/soong/.intermediates/transitive_import_dep/android_common/busybox/R.jar",
954 "out/soong/.intermediates/transitive_import/android_common/busybox/R.jar",
Colin Cross4eae06d2023-06-20 22:40:02 -0700955 "out/soong/.intermediates/transitive/android_common/turbine-combined/transitive.jar",
Colin Cross9055e212024-03-23 04:43:41 +0000956 "out/soong/.intermediates/transitive_import/android_common/turbine-combined/transitive_import.jar",
Colin Cross4eae06d2023-06-20 22:40:02 -0700957 },
958 directCombined: []string{
959 "out/soong/.intermediates/direct/android_common/javac/direct.jar",
960 "out/soong/.intermediates/transitive/android_common/javac/transitive.jar",
Colin Cross9055e212024-03-23 04:43:41 +0000961 "out/soong/.intermediates/transitive_import/android_common/combined/transitive_import.jar",
Colin Cross4eae06d2023-06-20 22:40:02 -0700962 },
963
964 transitiveResources: []string{"out/soong/.intermediates/transitive/android_common/aapt2/transitive/res/values_strings.arsc.flat"},
965 transitiveOverlays: nil,
966 transitiveImports: []string{"out/soong/.intermediates/default/java/framework-res/android_common/package-res.apk"},
967 transitiveSrcJars: nil,
968 transitiveClasspath: []string{
969 "out/soong/.intermediates/default/java/android_stubs_current/android_common/turbine-combined/android_stubs_current.jar",
970 "out/soong/.intermediates/transitive/android_common/busybox/R.jar",
971 },
972 transitiveCombined: nil,
973
Colin Cross8676c8c2023-10-12 15:58:57 -0700974 sharedResources: nil,
975 sharedOverlays: []string{"out/soong/.intermediates/shared/android_common/aapt2/shared/res/values_strings.arsc.flat"},
976 sharedImports: []string{
977 "out/soong/.intermediates/shared_transitive_shared/android_common/package-res.apk",
978 "out/soong/.intermediates/default/java/framework-res/android_common/package-res.apk",
979 "out/soong/.intermediates/shared_transitive_static/android_common/package-res.apk",
980 },
981 sharedSrcJars: nil,
982 sharedClasspath: []string{
983 "out/soong/.intermediates/default/java/android_stubs_current/android_common/turbine-combined/android_stubs_current.jar",
Colin Cross1d3f5902024-03-05 11:51:54 -0800984 "out/soong/.intermediates/shared/android_common/busybox/R.jar",
Colin Cross8676c8c2023-10-12 15:58:57 -0700985 "out/soong/.intermediates/shared_transitive_static/android_common/busybox/R.jar",
986 "out/soong/.intermediates/shared_transitive_shared/android_common/busybox/R.jar",
Colin Cross8676c8c2023-10-12 15:58:57 -0700987 "out/soong/.intermediates/shared_transitive_shared/android_common/turbine-combined/shared_transitive_shared.jar",
988 "out/soong/.intermediates/shared_transitive_static/android_common/turbine-combined/shared_transitive_static.jar",
989 },
990 sharedCombined: []string{
991 "out/soong/.intermediates/shared/android_common/javac/shared.jar",
992 "out/soong/.intermediates/shared_transitive_static/android_common/javac/shared_transitive_static.jar",
993 },
994
Colin Cross4eae06d2023-06-20 22:40:02 -0700995 directImportResources: nil,
996 directImportOverlays: []string{"out/soong/.intermediates/direct_import/android_common/flat-res/gen_res.flata"},
997 directImportImports: []string{
998 "out/soong/.intermediates/default/java/framework-res/android_common/package-res.apk",
999 "out/soong/.intermediates/direct_import_dep/android_common/package-res.apk",
1000 },
1001
1002 transitiveImportResources: nil,
1003 transitiveImportOverlays: []string{"out/soong/.intermediates/transitive_import/android_common/flat-res/gen_res.flata"},
1004 transitiveImportImports: []string{
1005 "out/soong/.intermediates/default/java/framework-res/android_common/package-res.apk",
1006 "out/soong/.intermediates/transitive_import_dep/android_common/package-res.apk",
1007 },
1008 }, {
1009 // Test an app building with resource processor enabled but with dependencies built without
1010 // resource processor.
1011 name: "app_resource_processor",
1012 appUsesRP: true,
1013 directLibUsesRP: false,
1014 transitiveLibUsesRP: false,
1015
1016 appResources: nil,
1017 appOverlays: []string{
1018 "out/soong/.intermediates/transitive/android_common/package-res.apk",
1019 "out/soong/.intermediates/transitive_import_dep/android_common/package-res.apk",
1020 "out/soong/.intermediates/transitive_import/android_common/package-res.apk",
1021 "out/soong/.intermediates/direct/android_common/package-res.apk",
1022 "out/soong/.intermediates/direct_import_dep/android_common/package-res.apk",
1023 "out/soong/.intermediates/direct_import/android_common/package-res.apk",
1024 "out/soong/.intermediates/app/android_common/aapt2/app/res/values_strings.arsc.flat",
1025 },
Colin Cross8676c8c2023-10-12 15:58:57 -07001026 appImports: []string{
1027 "out/soong/.intermediates/shared/android_common/package-res.apk",
1028 "out/soong/.intermediates/default/java/framework-res/android_common/package-res.apk",
1029 },
Colin Cross4eae06d2023-06-20 22:40:02 -07001030 appSrcJars: nil,
1031 appClasspath: []string{
1032 "out/soong/.intermediates/default/java/android_stubs_current/android_common/turbine-combined/android_stubs_current.jar",
1033 // R.jar has to come before direct.jar
1034 "out/soong/.intermediates/app/android_common/busybox/R.jar",
Colin Cross8676c8c2023-10-12 15:58:57 -07001035 "out/soong/.intermediates/shared/android_common/turbine-combined/shared.jar",
Colin Cross4eae06d2023-06-20 22:40:02 -07001036 "out/soong/.intermediates/direct/android_common/turbine-combined/direct.jar",
Colin Cross9055e212024-03-23 04:43:41 +00001037 "out/soong/.intermediates/direct_import/android_common/turbine-combined/direct_import.jar",
Colin Cross4eae06d2023-06-20 22:40:02 -07001038 },
1039 appCombined: []string{
Colin Cross4eae06d2023-06-20 22:40:02 -07001040 "out/soong/.intermediates/app/android_common/javac/app.jar",
Colin Crossfd620b22024-02-23 10:05:21 -08001041 "out/soong/.intermediates/app/android_common/busybox/R.jar",
Colin Cross4eae06d2023-06-20 22:40:02 -07001042 "out/soong/.intermediates/direct/android_common/combined/direct.jar",
Colin Cross9055e212024-03-23 04:43:41 +00001043 "out/soong/.intermediates/direct_import/android_common/combined/direct_import.jar",
Colin Cross4eae06d2023-06-20 22:40:02 -07001044 },
1045
1046 dontVerifyDirect: true,
1047 dontVerifyTransitive: true,
Colin Cross8676c8c2023-10-12 15:58:57 -07001048 dontVerifyShared: true,
Colin Cross4eae06d2023-06-20 22:40:02 -07001049 dontVerifyDirectImport: true,
1050 dontVerifyTransitiveImport: true,
1051 },
1052 {
1053 // Test an app building without resource processor enabled but with a dependency built with
1054 // resource processor.
1055 name: "app_dependency_lib_resource_processor",
1056 appUsesRP: false,
1057 directLibUsesRP: true,
1058 transitiveLibUsesRP: false,
1059
1060 appOverlays: []string{
1061 "out/soong/.intermediates/transitive/android_common/package-res.apk",
1062 "out/soong/.intermediates/transitive_import_dep/android_common/package-res.apk",
1063 "out/soong/.intermediates/transitive_import/android_common/package-res.apk",
1064 "out/soong/.intermediates/direct/android_common/package-res.apk",
1065 "out/soong/.intermediates/direct_import_dep/android_common/package-res.apk",
1066 "out/soong/.intermediates/direct_import/android_common/package-res.apk",
1067 "out/soong/.intermediates/app/android_common/aapt2/app/res/values_strings.arsc.flat",
1068 },
Colin Cross8676c8c2023-10-12 15:58:57 -07001069 appImports: []string{
1070 "out/soong/.intermediates/shared/android_common/package-res.apk",
1071 "out/soong/.intermediates/default/java/framework-res/android_common/package-res.apk",
1072 },
Colin Cross4eae06d2023-06-20 22:40:02 -07001073 appSrcJars: []string{"out/soong/.intermediates/app/android_common/gen/android/R.srcjar"},
1074 appClasspath: []string{
1075 "out/soong/.intermediates/default/java/android_stubs_current/android_common/turbine-combined/android_stubs_current.jar",
Colin Cross8676c8c2023-10-12 15:58:57 -07001076 "out/soong/.intermediates/shared/android_common/turbine-combined/shared.jar",
Colin Cross4eae06d2023-06-20 22:40:02 -07001077 "out/soong/.intermediates/direct/android_common/turbine-combined/direct.jar",
Colin Cross9055e212024-03-23 04:43:41 +00001078 "out/soong/.intermediates/direct_import/android_common/turbine-combined/direct_import.jar",
Colin Cross4eae06d2023-06-20 22:40:02 -07001079 },
1080 appCombined: []string{
1081 "out/soong/.intermediates/app/android_common/javac/app.jar",
1082 "out/soong/.intermediates/direct/android_common/combined/direct.jar",
Colin Cross9055e212024-03-23 04:43:41 +00001083 "out/soong/.intermediates/direct_import/android_common/combined/direct_import.jar",
Colin Cross4eae06d2023-06-20 22:40:02 -07001084 },
1085
1086 directResources: nil,
1087 directOverlays: []string{"out/soong/.intermediates/direct/android_common/aapt2/direct/res/values_strings.arsc.flat"},
1088 directImports: []string{
1089 "out/soong/.intermediates/default/java/framework-res/android_common/package-res.apk",
1090 "out/soong/.intermediates/transitive_import/android_common/package-res.apk",
1091 "out/soong/.intermediates/transitive_import_dep/android_common/package-res.apk",
1092 "out/soong/.intermediates/transitive/android_common/package-res.apk",
1093 },
1094 directSrcJars: nil,
1095 directClasspath: []string{
1096 "out/soong/.intermediates/default/java/android_stubs_current/android_common/turbine-combined/android_stubs_current.jar",
Colin Cross4eae06d2023-06-20 22:40:02 -07001097 "out/soong/.intermediates/direct/android_common/busybox/R.jar",
Colin Cross1d3f5902024-03-05 11:51:54 -08001098 "out/soong/.intermediates/transitive_import_dep/android_common/busybox/R.jar",
1099 "out/soong/.intermediates/transitive_import/android_common/busybox/R.jar",
Colin Cross4eae06d2023-06-20 22:40:02 -07001100 "out/soong/.intermediates/transitive/android_common/turbine-combined/transitive.jar",
Colin Cross9055e212024-03-23 04:43:41 +00001101 "out/soong/.intermediates/transitive_import/android_common/turbine-combined/transitive_import.jar",
Colin Cross4eae06d2023-06-20 22:40:02 -07001102 },
1103 directCombined: []string{
1104 "out/soong/.intermediates/direct/android_common/javac/direct.jar",
1105 "out/soong/.intermediates/transitive/android_common/javac/transitive.jar",
Colin Cross9055e212024-03-23 04:43:41 +00001106 "out/soong/.intermediates/transitive_import/android_common/combined/transitive_import.jar",
Colin Cross4eae06d2023-06-20 22:40:02 -07001107 },
1108
1109 dontVerifyTransitive: true,
Colin Cross8676c8c2023-10-12 15:58:57 -07001110 dontVerifyShared: true,
Colin Cross4eae06d2023-06-20 22:40:02 -07001111 dontVerifyDirectImport: true,
1112 dontVerifyTransitiveImport: true,
1113 },
1114 {
1115 // Test a library building without resource processor enabled but with a dependency built with
1116 // resource processor.
1117 name: "lib_dependency_lib_resource_processor",
1118 appUsesRP: false,
1119 directLibUsesRP: false,
1120 transitiveLibUsesRP: true,
1121
1122 appOverlays: []string{
1123 "out/soong/.intermediates/transitive/android_common/package-res.apk",
1124 "out/soong/.intermediates/transitive_import_dep/android_common/package-res.apk",
1125 "out/soong/.intermediates/transitive_import/android_common/package-res.apk",
1126 "out/soong/.intermediates/direct/android_common/package-res.apk",
1127 "out/soong/.intermediates/direct_import_dep/android_common/package-res.apk",
1128 "out/soong/.intermediates/direct_import/android_common/package-res.apk",
1129 "out/soong/.intermediates/app/android_common/aapt2/app/res/values_strings.arsc.flat",
1130 },
Colin Cross8676c8c2023-10-12 15:58:57 -07001131 appImports: []string{
1132 "out/soong/.intermediates/shared/android_common/package-res.apk",
1133 "out/soong/.intermediates/default/java/framework-res/android_common/package-res.apk",
1134 },
Colin Cross4eae06d2023-06-20 22:40:02 -07001135 appSrcJars: []string{"out/soong/.intermediates/app/android_common/gen/android/R.srcjar"},
1136 appClasspath: []string{
1137 "out/soong/.intermediates/default/java/android_stubs_current/android_common/turbine-combined/android_stubs_current.jar",
Colin Cross8676c8c2023-10-12 15:58:57 -07001138 "out/soong/.intermediates/shared/android_common/turbine-combined/shared.jar",
Colin Cross4eae06d2023-06-20 22:40:02 -07001139 "out/soong/.intermediates/direct/android_common/turbine-combined/direct.jar",
Colin Cross9055e212024-03-23 04:43:41 +00001140 "out/soong/.intermediates/direct_import/android_common/turbine-combined/direct_import.jar",
Colin Cross4eae06d2023-06-20 22:40:02 -07001141 },
1142 appCombined: []string{
1143 "out/soong/.intermediates/app/android_common/javac/app.jar",
1144 "out/soong/.intermediates/direct/android_common/combined/direct.jar",
Colin Cross9055e212024-03-23 04:43:41 +00001145 "out/soong/.intermediates/direct_import/android_common/combined/direct_import.jar",
Colin Cross4eae06d2023-06-20 22:40:02 -07001146 },
1147
1148 directResources: nil,
1149 directOverlays: []string{
1150 "out/soong/.intermediates/transitive/android_common/package-res.apk",
1151 "out/soong/.intermediates/transitive_import_dep/android_common/package-res.apk",
1152 "out/soong/.intermediates/transitive_import/android_common/package-res.apk",
1153 "out/soong/.intermediates/direct/android_common/aapt2/direct/res/values_strings.arsc.flat",
1154 },
1155 directImports: []string{"out/soong/.intermediates/default/java/framework-res/android_common/package-res.apk"},
1156 directSrcJars: []string{"out/soong/.intermediates/direct/android_common/gen/android/R.srcjar"},
1157 directClasspath: []string{
1158 "out/soong/.intermediates/default/java/android_stubs_current/android_common/turbine-combined/android_stubs_current.jar",
1159 "out/soong/.intermediates/transitive/android_common/turbine-combined/transitive.jar",
Colin Cross9055e212024-03-23 04:43:41 +00001160 "out/soong/.intermediates/transitive_import/android_common/turbine-combined/transitive_import.jar",
Colin Cross4eae06d2023-06-20 22:40:02 -07001161 },
1162 directCombined: []string{
1163 "out/soong/.intermediates/direct/android_common/javac/direct.jar",
1164 "out/soong/.intermediates/transitive/android_common/javac/transitive.jar",
Colin Cross9055e212024-03-23 04:43:41 +00001165 "out/soong/.intermediates/transitive_import/android_common/combined/transitive_import.jar",
Colin Cross4eae06d2023-06-20 22:40:02 -07001166 },
1167
1168 transitiveResources: []string{"out/soong/.intermediates/transitive/android_common/aapt2/transitive/res/values_strings.arsc.flat"},
1169 transitiveOverlays: nil,
1170 transitiveImports: []string{"out/soong/.intermediates/default/java/framework-res/android_common/package-res.apk"},
1171 transitiveSrcJars: nil,
1172 transitiveClasspath: []string{
1173 "out/soong/.intermediates/default/java/android_stubs_current/android_common/turbine-combined/android_stubs_current.jar",
1174 "out/soong/.intermediates/transitive/android_common/busybox/R.jar",
1175 },
1176 transitiveCombined: nil,
1177
Colin Cross8676c8c2023-10-12 15:58:57 -07001178 dontVerifyShared: true,
Colin Cross4eae06d2023-06-20 22:40:02 -07001179 dontVerifyDirectImport: true,
1180 dontVerifyTransitiveImport: true,
Colin Crossff3ff7f2023-07-05 14:04:12 -07001181 },
1182 }
1183
1184 for _, testCase := range testCases {
1185 t.Run(testCase.name, func(t *testing.T) {
1186 bp := fmt.Sprintf(`
1187 android_app {
1188 name: "app",
1189 sdk_version: "current",
1190 srcs: ["app/app.java"],
1191 resource_dirs: ["app/res"],
1192 manifest: "app/AndroidManifest.xml",
Colin Cross8676c8c2023-10-12 15:58:57 -07001193 libs: ["shared"],
Colin Crossff3ff7f2023-07-05 14:04:12 -07001194 static_libs: ["direct", "direct_import"],
Colin Cross4eae06d2023-06-20 22:40:02 -07001195 use_resource_processor: %v,
Colin Crossff3ff7f2023-07-05 14:04:12 -07001196 }
1197
1198 android_library {
1199 name: "direct",
1200 sdk_version: "current",
1201 srcs: ["direct/direct.java"],
1202 resource_dirs: ["direct/res"],
1203 manifest: "direct/AndroidManifest.xml",
1204 static_libs: ["transitive", "transitive_import"],
Colin Cross4eae06d2023-06-20 22:40:02 -07001205 use_resource_processor: %v,
Colin Crossff3ff7f2023-07-05 14:04:12 -07001206 }
1207
1208 android_library {
1209 name: "transitive",
1210 sdk_version: "current",
1211 srcs: ["transitive/transitive.java"],
1212 resource_dirs: ["transitive/res"],
1213 manifest: "transitive/AndroidManifest.xml",
Colin Cross4eae06d2023-06-20 22:40:02 -07001214 use_resource_processor: %v,
Colin Crossff3ff7f2023-07-05 14:04:12 -07001215 }
1216
Colin Cross8676c8c2023-10-12 15:58:57 -07001217 android_library {
1218 name: "shared",
1219 sdk_version: "current",
1220 srcs: ["shared/shared.java"],
1221 resource_dirs: ["shared/res"],
1222 manifest: "shared/AndroidManifest.xml",
1223 use_resource_processor: %v,
1224 libs: ["shared_transitive_shared"],
1225 static_libs: ["shared_transitive_static"],
1226 }
1227
1228 android_library {
1229 name: "shared_transitive_shared",
1230 sdk_version: "current",
1231 srcs: ["shared_transitive_shared/shared_transitive_shared.java"],
1232 resource_dirs: ["shared_transitive_shared/res"],
1233 manifest: "shared_transitive_shared/AndroidManifest.xml",
1234 use_resource_processor: %v,
1235 }
1236
1237 android_library {
1238 name: "shared_transitive_static",
1239 sdk_version: "current",
1240 srcs: ["shared_transitive_static/shared.java"],
1241 resource_dirs: ["shared_transitive_static/res"],
1242 manifest: "shared_transitive_static/AndroidManifest.xml",
1243 use_resource_processor: %v,
1244 }
1245
Colin Crossff3ff7f2023-07-05 14:04:12 -07001246 android_library_import {
1247 name: "direct_import",
1248 sdk_version: "current",
1249 aars: ["direct_import.aar"],
1250 static_libs: ["direct_import_dep"],
1251 }
1252
1253 android_library_import {
1254 name: "direct_import_dep",
1255 sdk_version: "current",
1256 aars: ["direct_import_dep.aar"],
1257 }
1258
1259 android_library_import {
1260 name: "transitive_import",
1261 sdk_version: "current",
1262 aars: ["transitive_import.aar"],
1263 static_libs: ["transitive_import_dep"],
1264 }
1265
1266 android_library_import {
1267 name: "transitive_import_dep",
1268 sdk_version: "current",
1269 aars: ["transitive_import_dep.aar"],
1270 }
Colin Cross8676c8c2023-10-12 15:58:57 -07001271 `, testCase.appUsesRP, testCase.directLibUsesRP, testCase.transitiveLibUsesRP,
1272 testCase.sharedLibUsesRP, testCase.sharedTransitiveSharedLibUsesRP, testCase.sharedTransitiveStaticLibUsesRP)
Colin Crossff3ff7f2023-07-05 14:04:12 -07001273
1274 fs := android.MockFS{
Colin Cross8676c8c2023-10-12 15:58:57 -07001275 "app/res/values/strings.xml": nil,
1276 "direct/res/values/strings.xml": nil,
1277 "transitive/res/values/strings.xml": nil,
1278 "shared/res/values/strings.xml": nil,
1279 "shared_transitive_static/res/values/strings.xml": nil,
1280 "shared_transitive_shared/res/values/strings.xml": nil,
Colin Crossff3ff7f2023-07-05 14:04:12 -07001281 }
1282
1283 result := android.GroupFixturePreparers(
1284 PrepareForTestWithJavaDefaultModules,
Colin Crossff3ff7f2023-07-05 14:04:12 -07001285 fs.AddToFixture(),
1286 ).RunTestWithBp(t, bp)
1287
1288 type aaptInfo struct {
1289 resources, overlays, imports, srcJars, classpath, combined android.Paths
1290 }
1291
1292 getAaptInfo := func(moduleName string) (aaptInfo aaptInfo) {
1293 mod := result.ModuleForTests(moduleName, "android_common")
1294 resourceListRule := mod.MaybeOutput("aapt2/res.list")
1295 overlayListRule := mod.MaybeOutput("aapt2/overlay.list")
1296 aaptRule := mod.Rule("aapt2Link")
1297 javacRule := mod.MaybeRule("javac")
1298 combinedRule := mod.MaybeOutput("combined/" + moduleName + ".jar")
1299
1300 aaptInfo.resources = resourceListRule.Inputs
1301 aaptInfo.overlays = overlayListRule.Inputs
1302
1303 aaptFlags := strings.Split(aaptRule.Args["flags"], " ")
1304 for i, flag := range aaptFlags {
1305 if flag == "-I" && i+1 < len(aaptFlags) {
1306 aaptInfo.imports = append(aaptInfo.imports, android.PathForTesting(aaptFlags[i+1]))
1307 }
1308 }
1309
1310 if len(javacRule.Args["srcJars"]) > 0 {
1311 aaptInfo.srcJars = android.PathsForTesting(strings.Split(javacRule.Args["srcJars"], " ")...)
1312 }
1313
1314 if len(javacRule.Args["classpath"]) > 0 {
1315 classpathArg := strings.TrimPrefix(javacRule.Args["classpath"], "-classpath ")
1316 aaptInfo.classpath = android.PathsForTesting(strings.Split(classpathArg, ":")...)
1317 }
1318
1319 aaptInfo.combined = combinedRule.Inputs
1320 return
1321 }
1322
1323 app := getAaptInfo("app")
1324 direct := getAaptInfo("direct")
1325 transitive := getAaptInfo("transitive")
Colin Cross8676c8c2023-10-12 15:58:57 -07001326 shared := getAaptInfo("shared")
Colin Crossff3ff7f2023-07-05 14:04:12 -07001327 directImport := getAaptInfo("direct_import")
1328 transitiveImport := getAaptInfo("transitive_import")
1329
1330 if !testCase.dontVerifyApp {
1331 android.AssertPathsRelativeToTopEquals(t, "app resources", testCase.appResources, app.resources)
1332 android.AssertPathsRelativeToTopEquals(t, "app overlays", testCase.appOverlays, app.overlays)
1333 android.AssertPathsRelativeToTopEquals(t, "app imports", testCase.appImports, app.imports)
1334 android.AssertPathsRelativeToTopEquals(t, "app srcjars", testCase.appSrcJars, app.srcJars)
1335 android.AssertPathsRelativeToTopEquals(t, "app classpath", testCase.appClasspath, app.classpath)
1336 android.AssertPathsRelativeToTopEquals(t, "app combined", testCase.appCombined, app.combined)
1337 }
1338
1339 if !testCase.dontVerifyDirect {
1340 android.AssertPathsRelativeToTopEquals(t, "direct resources", testCase.directResources, direct.resources)
1341 android.AssertPathsRelativeToTopEquals(t, "direct overlays", testCase.directOverlays, direct.overlays)
1342 android.AssertPathsRelativeToTopEquals(t, "direct imports", testCase.directImports, direct.imports)
1343 android.AssertPathsRelativeToTopEquals(t, "direct srcjars", testCase.directSrcJars, direct.srcJars)
1344 android.AssertPathsRelativeToTopEquals(t, "direct classpath", testCase.directClasspath, direct.classpath)
1345 android.AssertPathsRelativeToTopEquals(t, "direct combined", testCase.directCombined, direct.combined)
1346 }
1347
1348 if !testCase.dontVerifyTransitive {
1349 android.AssertPathsRelativeToTopEquals(t, "transitive resources", testCase.transitiveResources, transitive.resources)
1350 android.AssertPathsRelativeToTopEquals(t, "transitive overlays", testCase.transitiveOverlays, transitive.overlays)
1351 android.AssertPathsRelativeToTopEquals(t, "transitive imports", testCase.transitiveImports, transitive.imports)
1352 android.AssertPathsRelativeToTopEquals(t, "transitive srcjars", testCase.transitiveSrcJars, transitive.srcJars)
1353 android.AssertPathsRelativeToTopEquals(t, "transitive classpath", testCase.transitiveClasspath, transitive.classpath)
1354 android.AssertPathsRelativeToTopEquals(t, "transitive combined", testCase.transitiveCombined, transitive.combined)
1355 }
1356
Colin Cross8676c8c2023-10-12 15:58:57 -07001357 if !testCase.dontVerifyShared {
1358 android.AssertPathsRelativeToTopEquals(t, "shared resources", testCase.sharedResources, shared.resources)
1359 android.AssertPathsRelativeToTopEquals(t, "shared overlays", testCase.sharedOverlays, shared.overlays)
1360 android.AssertPathsRelativeToTopEquals(t, "shared imports", testCase.sharedImports, shared.imports)
1361 android.AssertPathsRelativeToTopEquals(t, "shared srcjars", testCase.sharedSrcJars, shared.srcJars)
1362 android.AssertPathsRelativeToTopEquals(t, "shared classpath", testCase.sharedClasspath, shared.classpath)
1363 android.AssertPathsRelativeToTopEquals(t, "shared combined", testCase.sharedCombined, shared.combined)
1364 }
1365
Colin Crossff3ff7f2023-07-05 14:04:12 -07001366 if !testCase.dontVerifyDirectImport {
1367 android.AssertPathsRelativeToTopEquals(t, "direct_import resources", testCase.directImportResources, directImport.resources)
1368 android.AssertPathsRelativeToTopEquals(t, "direct_import overlays", testCase.directImportOverlays, directImport.overlays)
1369 android.AssertPathsRelativeToTopEquals(t, "direct_import imports", testCase.directImportImports, directImport.imports)
1370 }
1371
1372 if !testCase.dontVerifyTransitiveImport {
1373 android.AssertPathsRelativeToTopEquals(t, "transitive_import resources", testCase.transitiveImportResources, transitiveImport.resources)
1374 android.AssertPathsRelativeToTopEquals(t, "transitive_import overlays", testCase.transitiveImportOverlays, transitiveImport.overlays)
1375 android.AssertPathsRelativeToTopEquals(t, "transitive_import imports", testCase.transitiveImportImports, transitiveImport.imports)
1376 }
1377 })
1378 }
1379}
1380
1381func TestAndroidResourceOverlays(t *testing.T) {
Colin Cross5c4791c2019-02-01 11:44:44 -08001382 testCases := []struct {
1383 name string
1384 enforceRROTargets []string
1385 enforceRROExcludedOverlays []string
Colin Crossbec85302019-02-13 13:15:46 -08001386 resourceFiles map[string][]string
Colin Cross5c4791c2019-02-01 11:44:44 -08001387 overlayFiles map[string][]string
1388 rroDirs map[string][]string
1389 }{
1390 {
1391 name: "no RRO",
1392 enforceRROTargets: nil,
1393 enforceRROExcludedOverlays: nil,
Colin Crossbec85302019-02-13 13:15:46 -08001394 resourceFiles: map[string][]string{
1395 "foo": nil,
1396 "bar": {"bar/res/res/values/strings.xml"},
1397 "lib": nil,
1398 "lib2": {"lib2/res/res/values/strings.xml"},
1399 },
Colin Cross5c4791c2019-02-01 11:44:44 -08001400 overlayFiles: map[string][]string{
Colin Crossbec85302019-02-13 13:15:46 -08001401 "foo": {
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00001402 "out/soong/.intermediates/lib2/android_common/package-res.apk",
1403 "out/soong/.intermediates/lib/android_common/package-res.apk",
1404 "out/soong/.intermediates/lib3/android_common/package-res.apk",
Colin Cross6ed7dea2019-01-31 14:44:30 -08001405 "foo/res/res/values/strings.xml",
Colin Cross5c4791c2019-02-01 11:44:44 -08001406 "device/vendor/blah/static_overlay/foo/res/values/strings.xml",
1407 "device/vendor/blah/overlay/foo/res/values/strings.xml",
Anton Hansson53c88442019-03-18 15:53:16 +00001408 "product/vendor/blah/overlay/foo/res/values/strings.xml",
Colin Cross5c4791c2019-02-01 11:44:44 -08001409 },
Colin Crossbec85302019-02-13 13:15:46 -08001410 "bar": {
Colin Cross5c4791c2019-02-01 11:44:44 -08001411 "device/vendor/blah/static_overlay/bar/res/values/strings.xml",
1412 "device/vendor/blah/overlay/bar/res/values/strings.xml",
1413 },
Colin Crossbec85302019-02-13 13:15:46 -08001414 "lib": {
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00001415 "out/soong/.intermediates/lib2/android_common/package-res.apk",
Colin Crossbec85302019-02-13 13:15:46 -08001416 "lib/res/res/values/strings.xml",
1417 "device/vendor/blah/overlay/lib/res/values/strings.xml",
1418 },
Colin Cross5c4791c2019-02-01 11:44:44 -08001419 },
1420 rroDirs: map[string][]string{
1421 "foo": nil,
1422 "bar": nil,
1423 },
1424 },
1425 {
1426 name: "enforce RRO on foo",
1427 enforceRROTargets: []string{"foo"},
1428 enforceRROExcludedOverlays: []string{"device/vendor/blah/static_overlay"},
Colin Crossbec85302019-02-13 13:15:46 -08001429 resourceFiles: map[string][]string{
1430 "foo": nil,
1431 "bar": {"bar/res/res/values/strings.xml"},
1432 "lib": nil,
1433 "lib2": {"lib2/res/res/values/strings.xml"},
1434 },
Colin Cross5c4791c2019-02-01 11:44:44 -08001435 overlayFiles: map[string][]string{
Colin Crossbec85302019-02-13 13:15:46 -08001436 "foo": {
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00001437 "out/soong/.intermediates/lib2/android_common/package-res.apk",
1438 "out/soong/.intermediates/lib/android_common/package-res.apk",
1439 "out/soong/.intermediates/lib3/android_common/package-res.apk",
Colin Cross6ed7dea2019-01-31 14:44:30 -08001440 "foo/res/res/values/strings.xml",
1441 "device/vendor/blah/static_overlay/foo/res/values/strings.xml",
1442 },
Colin Crossbec85302019-02-13 13:15:46 -08001443 "bar": {
Colin Cross5c4791c2019-02-01 11:44:44 -08001444 "device/vendor/blah/static_overlay/bar/res/values/strings.xml",
1445 "device/vendor/blah/overlay/bar/res/values/strings.xml",
1446 },
Colin Crossbec85302019-02-13 13:15:46 -08001447 "lib": {
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00001448 "out/soong/.intermediates/lib2/android_common/package-res.apk",
Colin Crossbec85302019-02-13 13:15:46 -08001449 "lib/res/res/values/strings.xml",
Colin Crossbec85302019-02-13 13:15:46 -08001450 },
Colin Cross5c4791c2019-02-01 11:44:44 -08001451 },
Colin Crossc1c37552019-01-31 11:42:41 -08001452
Colin Cross5c4791c2019-02-01 11:44:44 -08001453 rroDirs: map[string][]string{
Colin Crossbec85302019-02-13 13:15:46 -08001454 "foo": {
Anton Hansson53c88442019-03-18 15:53:16 +00001455 "device:device/vendor/blah/overlay/foo/res",
Anton Hansson53c88442019-03-18 15:53:16 +00001456 "product:product/vendor/blah/overlay/foo/res",
Jaewoong Jungc779cd42020-10-06 18:56:10 -07001457 "device:device/vendor/blah/overlay/lib/res",
Colin Crossc1c37552019-01-31 11:42:41 -08001458 },
Colin Cross5c4791c2019-02-01 11:44:44 -08001459 "bar": nil,
Jaewoong Jungc779cd42020-10-06 18:56:10 -07001460 "lib": {"device:device/vendor/blah/overlay/lib/res"},
Colin Cross5c4791c2019-02-01 11:44:44 -08001461 },
1462 },
1463 {
1464 name: "enforce RRO on all",
1465 enforceRROTargets: []string{"*"},
1466 enforceRROExcludedOverlays: []string{
1467 // Excluding specific apps/res directories also allowed.
1468 "device/vendor/blah/static_overlay/foo",
1469 "device/vendor/blah/static_overlay/bar/res",
1470 },
Colin Crossbec85302019-02-13 13:15:46 -08001471 resourceFiles: map[string][]string{
1472 "foo": nil,
1473 "bar": {"bar/res/res/values/strings.xml"},
1474 "lib": nil,
1475 "lib2": {"lib2/res/res/values/strings.xml"},
1476 },
Colin Cross5c4791c2019-02-01 11:44:44 -08001477 overlayFiles: map[string][]string{
Colin Crossbec85302019-02-13 13:15:46 -08001478 "foo": {
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00001479 "out/soong/.intermediates/lib2/android_common/package-res.apk",
1480 "out/soong/.intermediates/lib/android_common/package-res.apk",
1481 "out/soong/.intermediates/lib3/android_common/package-res.apk",
Colin Cross6ed7dea2019-01-31 14:44:30 -08001482 "foo/res/res/values/strings.xml",
1483 "device/vendor/blah/static_overlay/foo/res/values/strings.xml",
1484 },
Colin Crossbec85302019-02-13 13:15:46 -08001485 "bar": {"device/vendor/blah/static_overlay/bar/res/values/strings.xml"},
1486 "lib": {
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00001487 "out/soong/.intermediates/lib2/android_common/package-res.apk",
Colin Crossbec85302019-02-13 13:15:46 -08001488 "lib/res/res/values/strings.xml",
1489 },
Colin Cross5c4791c2019-02-01 11:44:44 -08001490 },
1491 rroDirs: map[string][]string{
Colin Crossbec85302019-02-13 13:15:46 -08001492 "foo": {
Anton Hansson53c88442019-03-18 15:53:16 +00001493 "device:device/vendor/blah/overlay/foo/res",
1494 "product:product/vendor/blah/overlay/foo/res",
1495 // Lib dep comes after the direct deps
1496 "device:device/vendor/blah/overlay/lib/res",
Colin Crossc1c37552019-01-31 11:42:41 -08001497 },
Anton Hansson53c88442019-03-18 15:53:16 +00001498 "bar": {"device:device/vendor/blah/overlay/bar/res"},
1499 "lib": {"device:device/vendor/blah/overlay/lib/res"},
Colin Cross5c4791c2019-02-01 11:44:44 -08001500 },
1501 },
1502 }
1503
Anton Hansson53c88442019-03-18 15:53:16 +00001504 deviceResourceOverlays := []string{
Colin Cross890ff552017-11-30 20:13:19 -08001505 "device/vendor/blah/overlay",
1506 "device/vendor/blah/overlay2",
1507 "device/vendor/blah/static_overlay",
1508 }
1509
Anton Hansson53c88442019-03-18 15:53:16 +00001510 productResourceOverlays := []string{
1511 "product/vendor/blah/overlay",
1512 }
1513
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00001514 fs := android.MockFS{
Colin Cross890ff552017-11-30 20:13:19 -08001515 "foo/res/res/values/strings.xml": nil,
1516 "bar/res/res/values/strings.xml": nil,
Colin Cross6ed7dea2019-01-31 14:44:30 -08001517 "lib/res/res/values/strings.xml": nil,
Colin Crossbec85302019-02-13 13:15:46 -08001518 "lib2/res/res/values/strings.xml": nil,
Colin Cross890ff552017-11-30 20:13:19 -08001519 "device/vendor/blah/overlay/foo/res/values/strings.xml": nil,
1520 "device/vendor/blah/overlay/bar/res/values/strings.xml": nil,
Colin Cross6ed7dea2019-01-31 14:44:30 -08001521 "device/vendor/blah/overlay/lib/res/values/strings.xml": nil,
Colin Cross890ff552017-11-30 20:13:19 -08001522 "device/vendor/blah/static_overlay/foo/res/values/strings.xml": nil,
1523 "device/vendor/blah/static_overlay/bar/res/values/strings.xml": nil,
1524 "device/vendor/blah/overlay2/res/values/strings.xml": nil,
Anton Hansson53c88442019-03-18 15:53:16 +00001525 "product/vendor/blah/overlay/foo/res/values/strings.xml": nil,
Colin Cross890ff552017-11-30 20:13:19 -08001526 }
1527
1528 bp := `
1529 android_app {
1530 name: "foo",
Jeongik Cha538c0d02019-07-11 15:54:27 +09001531 sdk_version: "current",
Colin Cross890ff552017-11-30 20:13:19 -08001532 resource_dirs: ["foo/res"],
Anton Hansson53c88442019-03-18 15:53:16 +00001533 static_libs: ["lib", "lib3"],
Colin Cross890ff552017-11-30 20:13:19 -08001534 }
1535
1536 android_app {
1537 name: "bar",
Jeongik Cha538c0d02019-07-11 15:54:27 +09001538 sdk_version: "current",
Colin Cross890ff552017-11-30 20:13:19 -08001539 resource_dirs: ["bar/res"],
1540 }
Colin Cross6ed7dea2019-01-31 14:44:30 -08001541
1542 android_library {
1543 name: "lib",
Jeongik Cha75b83b02019-11-01 15:28:00 +09001544 sdk_version: "current",
Colin Cross6ed7dea2019-01-31 14:44:30 -08001545 resource_dirs: ["lib/res"],
Colin Crossbec85302019-02-13 13:15:46 -08001546 static_libs: ["lib2"],
1547 }
1548
1549 android_library {
1550 name: "lib2",
Jeongik Cha75b83b02019-11-01 15:28:00 +09001551 sdk_version: "current",
Colin Crossbec85302019-02-13 13:15:46 -08001552 resource_dirs: ["lib2/res"],
Colin Cross6ed7dea2019-01-31 14:44:30 -08001553 }
Anton Hansson53c88442019-03-18 15:53:16 +00001554
1555 // This library has the same resources as lib (should not lead to dupe RROs)
1556 android_library {
1557 name: "lib3",
Jeongik Cha75b83b02019-11-01 15:28:00 +09001558 sdk_version: "current",
Anton Hansson53c88442019-03-18 15:53:16 +00001559 resource_dirs: ["lib/res"]
1560 }
Colin Cross890ff552017-11-30 20:13:19 -08001561 `
1562
Colin Cross5c4791c2019-02-01 11:44:44 -08001563 for _, testCase := range testCases {
Colin Cross890ff552017-11-30 20:13:19 -08001564 t.Run(testCase.name, func(t *testing.T) {
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00001565 result := android.GroupFixturePreparers(
1566 PrepareForTestWithJavaDefaultModules,
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00001567 fs.AddToFixture(),
1568 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1569 variables.DeviceResourceOverlays = deviceResourceOverlays
1570 variables.ProductResourceOverlays = productResourceOverlays
1571 if testCase.enforceRROTargets != nil {
1572 variables.EnforceRROTargets = testCase.enforceRROTargets
1573 }
1574 if testCase.enforceRROExcludedOverlays != nil {
1575 variables.EnforceRROExcludedOverlays = testCase.enforceRROExcludedOverlays
1576 }
1577 }),
1578 ).RunTestWithBp(t, bp)
Colin Cross890ff552017-11-30 20:13:19 -08001579
Colin Crossbec85302019-02-13 13:15:46 -08001580 resourceListToFiles := func(module android.TestingModule, list []string) (files []string) {
1581 for _, o := range list {
1582 res := module.MaybeOutput(o)
1583 if res.Rule != nil {
1584 // If the overlay is compiled as part of this module (i.e. a .arsc.flat file),
1585 // verify the inputs to the .arsc.flat rule.
1586 files = append(files, res.Inputs.Strings()...)
1587 } else {
1588 // Otherwise, verify the full path to the output of the other module
1589 files = append(files, o)
Anton Hansson94c93f32019-01-30 16:03:37 +00001590 }
Colin Cross890ff552017-11-30 20:13:19 -08001591 }
Colin Crossbec85302019-02-13 13:15:46 -08001592 return files
Colin Cross890ff552017-11-30 20:13:19 -08001593 }
1594
Colin Crossbec85302019-02-13 13:15:46 -08001595 getResources := func(moduleName string) (resourceFiles, overlayFiles, rroDirs []string) {
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00001596 module := result.ModuleForTests(moduleName, "android_common")
Colin Crossbec85302019-02-13 13:15:46 -08001597 resourceList := module.MaybeOutput("aapt2/res.list")
1598 if resourceList.Rule != nil {
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00001599 resourceFiles = resourceListToFiles(module, android.PathsRelativeToTop(resourceList.Inputs))
Anton Hansson0375a4f2019-01-24 14:39:19 +00001600 }
Colin Crossbec85302019-02-13 13:15:46 -08001601 overlayList := module.MaybeOutput("aapt2/overlay.list")
1602 if overlayList.Rule != nil {
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00001603 overlayFiles = resourceListToFiles(module, android.PathsRelativeToTop(overlayList.Inputs))
Colin Crossbec85302019-02-13 13:15:46 -08001604 }
1605
Colin Crossab8d1382023-07-14 17:23:41 +00001606 for _, d := range module.Module().(AndroidLibraryDependency).RRODirsDepSet().ToList() {
Anton Hansson53c88442019-03-18 15:53:16 +00001607 var prefix string
1608 if d.overlayType == device {
1609 prefix = "device:"
1610 } else if d.overlayType == product {
1611 prefix = "product:"
1612 } else {
1613 t.Fatalf("Unexpected overlayType %d", d.overlayType)
1614 }
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00001615 rroDirs = append(rroDirs, prefix+android.PathRelativeToTop(d.path))
Anton Hansson53c88442019-03-18 15:53:16 +00001616 }
Colin Crossbec85302019-02-13 13:15:46 -08001617
1618 return resourceFiles, overlayFiles, rroDirs
1619 }
1620
1621 modules := []string{"foo", "bar", "lib", "lib2"}
1622 for _, module := range modules {
1623 resourceFiles, overlayFiles, rroDirs := getResources(module)
1624
1625 if !reflect.DeepEqual(resourceFiles, testCase.resourceFiles[module]) {
1626 t.Errorf("expected %s resource files:\n %#v\n got:\n %#v",
1627 module, testCase.resourceFiles[module], resourceFiles)
1628 }
1629 if !reflect.DeepEqual(overlayFiles, testCase.overlayFiles[module]) {
1630 t.Errorf("expected %s overlay files:\n %#v\n got:\n %#v",
1631 module, testCase.overlayFiles[module], overlayFiles)
1632 }
1633 if !reflect.DeepEqual(rroDirs, testCase.rroDirs[module]) {
Anton Hansson0375a4f2019-01-24 14:39:19 +00001634 t.Errorf("expected %s rroDirs: %#v\n got:\n %#v",
Colin Crossbec85302019-02-13 13:15:46 -08001635 module, testCase.rroDirs[module], rroDirs)
Anton Hansson0375a4f2019-01-24 14:39:19 +00001636 }
Colin Cross890ff552017-11-30 20:13:19 -08001637 }
Colin Cross890ff552017-11-30 20:13:19 -08001638 })
1639 }
1640}
Colin Crossd09b0b62018-04-18 11:06:47 -07001641
Paul Duffincdb88a92021-03-14 00:36:50 +00001642func checkSdkVersion(t *testing.T, result *android.TestResult, expectedSdkVersion string) {
1643 foo := result.ModuleForTests("foo", "android_common")
Jeongik Cha219141c2020-08-06 23:00:37 +09001644 link := foo.Output("package-res.apk")
1645 linkFlags := strings.Split(link.Args["flags"], " ")
1646 min := android.IndexList("--min-sdk-version", linkFlags)
1647 target := android.IndexList("--target-sdk-version", linkFlags)
1648
1649 if min == -1 || target == -1 || min == len(linkFlags)-1 || target == len(linkFlags)-1 {
1650 t.Fatalf("missing --min-sdk-version or --target-sdk-version in link flags: %q", linkFlags)
1651 }
1652
1653 gotMinSdkVersion := linkFlags[min+1]
1654 gotTargetSdkVersion := linkFlags[target+1]
1655
Paul Duffincdb88a92021-03-14 00:36:50 +00001656 android.AssertStringEquals(t, "incorrect --min-sdk-version", expectedSdkVersion, gotMinSdkVersion)
Jeongik Cha219141c2020-08-06 23:00:37 +09001657
Paul Duffincdb88a92021-03-14 00:36:50 +00001658 android.AssertStringEquals(t, "incorrect --target-sdk-version", expectedSdkVersion, gotTargetSdkVersion)
Jeongik Cha219141c2020-08-06 23:00:37 +09001659}
1660
Colin Crossd09b0b62018-04-18 11:06:47 -07001661func TestAppSdkVersion(t *testing.T) {
1662 testCases := []struct {
1663 name string
1664 sdkVersion string
1665 platformSdkInt int
1666 platformSdkCodename string
1667 platformSdkFinal bool
Spandan Dasffb31af2023-03-01 19:46:18 +00001668 minSdkVersionBp string
Colin Crossd09b0b62018-04-18 11:06:47 -07001669 expectedMinSdkVersion string
Jeongik Cha538c0d02019-07-11 15:54:27 +09001670 platformApis bool
Dan Albert4f378d72020-07-23 17:32:15 -07001671 activeCodenames []string
Colin Crossd09b0b62018-04-18 11:06:47 -07001672 }{
1673 {
1674 name: "current final SDK",
1675 sdkVersion: "current",
1676 platformSdkInt: 27,
1677 platformSdkCodename: "REL",
1678 platformSdkFinal: true,
1679 expectedMinSdkVersion: "27",
1680 },
1681 {
1682 name: "current non-final SDK",
1683 sdkVersion: "current",
1684 platformSdkInt: 27,
1685 platformSdkCodename: "OMR1",
1686 platformSdkFinal: false,
1687 expectedMinSdkVersion: "OMR1",
Dan Albert4f378d72020-07-23 17:32:15 -07001688 activeCodenames: []string{"OMR1"},
Colin Crossd09b0b62018-04-18 11:06:47 -07001689 },
1690 {
1691 name: "default final SDK",
1692 sdkVersion: "",
Jeongik Cha538c0d02019-07-11 15:54:27 +09001693 platformApis: true,
Colin Crossd09b0b62018-04-18 11:06:47 -07001694 platformSdkInt: 27,
1695 platformSdkCodename: "REL",
1696 platformSdkFinal: true,
1697 expectedMinSdkVersion: "27",
1698 },
1699 {
1700 name: "default non-final SDK",
1701 sdkVersion: "",
Jeongik Cha538c0d02019-07-11 15:54:27 +09001702 platformApis: true,
Colin Crossd09b0b62018-04-18 11:06:47 -07001703 platformSdkInt: 27,
1704 platformSdkCodename: "OMR1",
1705 platformSdkFinal: false,
1706 expectedMinSdkVersion: "OMR1",
Dan Albert4f378d72020-07-23 17:32:15 -07001707 activeCodenames: []string{"OMR1"},
Colin Crossd09b0b62018-04-18 11:06:47 -07001708 },
1709 {
1710 name: "14",
1711 sdkVersion: "14",
1712 expectedMinSdkVersion: "14",
Dan Albert4f378d72020-07-23 17:32:15 -07001713 platformSdkCodename: "S",
1714 activeCodenames: []string{"S"},
Colin Crossd09b0b62018-04-18 11:06:47 -07001715 },
Spandan Dasffb31af2023-03-01 19:46:18 +00001716 {
1717 name: "two active SDKs",
1718 sdkVersion: "module_current",
1719 minSdkVersionBp: "UpsideDownCake",
1720 expectedMinSdkVersion: "UpsideDownCake", // And not VanillaIceCream
1721 platformSdkCodename: "VanillaIceCream",
1722 activeCodenames: []string{"UpsideDownCake", "VanillaIceCream"},
1723 },
Colin Crossd09b0b62018-04-18 11:06:47 -07001724 }
1725
1726 for _, moduleType := range []string{"android_app", "android_library"} {
1727 for _, test := range testCases {
1728 t.Run(moduleType+" "+test.name, func(t *testing.T) {
Jeongik Cha538c0d02019-07-11 15:54:27 +09001729 platformApiProp := ""
1730 if test.platformApis {
1731 platformApiProp = "platform_apis: true,"
1732 }
Spandan Dasffb31af2023-03-01 19:46:18 +00001733 minSdkVersionProp := ""
1734 if test.minSdkVersionBp != "" {
1735 minSdkVersionProp = fmt.Sprintf(` min_sdk_version: "%s",`, test.minSdkVersionBp)
1736 }
Colin Crossd09b0b62018-04-18 11:06:47 -07001737 bp := fmt.Sprintf(`%s {
1738 name: "foo",
1739 srcs: ["a.java"],
1740 sdk_version: "%s",
Jeongik Cha538c0d02019-07-11 15:54:27 +09001741 %s
Spandan Dasffb31af2023-03-01 19:46:18 +00001742 %s
1743 }`, moduleType, test.sdkVersion, platformApiProp, minSdkVersionProp)
Colin Crossd09b0b62018-04-18 11:06:47 -07001744
Paul Duffin71ae5942021-03-22 15:36:52 +00001745 result := android.GroupFixturePreparers(
1746 prepareForJavaTest,
Paul Duffincdb88a92021-03-14 00:36:50 +00001747 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1748 variables.Platform_sdk_version = &test.platformSdkInt
1749 variables.Platform_sdk_codename = &test.platformSdkCodename
1750 variables.Platform_version_active_codenames = test.activeCodenames
1751 variables.Platform_sdk_final = &test.platformSdkFinal
1752 }),
Paul Duffin2645a292021-03-13 02:36:00 +00001753 FixtureWithPrebuiltApis(map[string][]string{
1754 "14": {"foo"},
1755 }),
Paul Duffincdb88a92021-03-14 00:36:50 +00001756 ).RunTestWithBp(t, bp)
Colin Crossd09b0b62018-04-18 11:06:47 -07001757
Paul Duffincdb88a92021-03-14 00:36:50 +00001758 checkSdkVersion(t, result, test.expectedMinSdkVersion)
Colin Crossd09b0b62018-04-18 11:06:47 -07001759 })
1760 }
1761 }
1762}
Colin Crossa4f08812018-10-02 22:03:40 -07001763
Jeongik Cha219141c2020-08-06 23:00:37 +09001764func TestVendorAppSdkVersion(t *testing.T) {
1765 testCases := []struct {
1766 name string
1767 sdkVersion string
1768 platformSdkInt int
1769 platformSdkCodename string
1770 platformSdkFinal bool
1771 deviceCurrentApiLevelForVendorModules string
1772 expectedMinSdkVersion string
1773 }{
1774 {
1775 name: "current final SDK",
1776 sdkVersion: "current",
1777 platformSdkInt: 29,
1778 platformSdkCodename: "REL",
1779 platformSdkFinal: true,
1780 deviceCurrentApiLevelForVendorModules: "29",
1781 expectedMinSdkVersion: "29",
1782 },
1783 {
1784 name: "current final SDK",
1785 sdkVersion: "current",
1786 platformSdkInt: 29,
1787 platformSdkCodename: "REL",
1788 platformSdkFinal: true,
1789 deviceCurrentApiLevelForVendorModules: "28",
1790 expectedMinSdkVersion: "28",
1791 },
1792 {
1793 name: "current final SDK",
1794 sdkVersion: "current",
1795 platformSdkInt: 29,
1796 platformSdkCodename: "Q",
1797 platformSdkFinal: false,
Jeongik Cha219141c2020-08-06 23:00:37 +09001798 deviceCurrentApiLevelForVendorModules: "28",
1799 expectedMinSdkVersion: "28",
1800 },
1801 }
1802
1803 for _, moduleType := range []string{"android_app", "android_library"} {
1804 for _, sdkKind := range []string{"", "system_"} {
1805 for _, test := range testCases {
1806 t.Run(moduleType+" "+test.name, func(t *testing.T) {
1807 bp := fmt.Sprintf(`%s {
1808 name: "foo",
1809 srcs: ["a.java"],
1810 sdk_version: "%s%s",
1811 vendor: true,
1812 }`, moduleType, sdkKind, test.sdkVersion)
1813
Paul Duffin71ae5942021-03-22 15:36:52 +00001814 result := android.GroupFixturePreparers(
1815 prepareForJavaTest,
Paul Duffincdb88a92021-03-14 00:36:50 +00001816 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1817 variables.Platform_sdk_version = &test.platformSdkInt
1818 variables.Platform_sdk_codename = &test.platformSdkCodename
1819 variables.Platform_sdk_final = &test.platformSdkFinal
1820 variables.DeviceCurrentApiLevelForVendorModules = &test.deviceCurrentApiLevelForVendorModules
1821 variables.DeviceSystemSdkVersions = []string{"28", "29"}
1822 }),
Paul Duffin2645a292021-03-13 02:36:00 +00001823 FixtureWithPrebuiltApis(map[string][]string{
1824 "28": {"foo"},
1825 "29": {"foo"},
1826 "current": {"foo"},
1827 }),
Paul Duffincdb88a92021-03-14 00:36:50 +00001828 ).RunTestWithBp(t, bp)
1829
1830 checkSdkVersion(t, result, test.expectedMinSdkVersion)
Jeongik Cha219141c2020-08-06 23:00:37 +09001831 })
1832 }
1833 }
1834 }
1835}
1836
Paul Duffin50c217c2019-06-12 13:25:22 +01001837func TestJNIABI(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001838 ctx, _ := testJava(t, cc.GatherRequiredDepsForTest(android.Android)+`
Paul Duffin50c217c2019-06-12 13:25:22 +01001839 cc_library {
1840 name: "libjni",
1841 system_shared_libs: [],
Colin Crossc511bc52020-04-07 16:50:32 +00001842 sdk_version: "current",
Paul Duffin50c217c2019-06-12 13:25:22 +01001843 stl: "none",
1844 }
1845
1846 android_test {
1847 name: "test",
1848 sdk_version: "core_platform",
1849 jni_libs: ["libjni"],
1850 }
1851
1852 android_test {
1853 name: "test_first",
1854 sdk_version: "core_platform",
1855 compile_multilib: "first",
1856 jni_libs: ["libjni"],
1857 }
1858
1859 android_test {
1860 name: "test_both",
1861 sdk_version: "core_platform",
1862 compile_multilib: "both",
1863 jni_libs: ["libjni"],
1864 }
1865
1866 android_test {
1867 name: "test_32",
1868 sdk_version: "core_platform",
1869 compile_multilib: "32",
1870 jni_libs: ["libjni"],
1871 }
1872
1873 android_test {
1874 name: "test_64",
1875 sdk_version: "core_platform",
1876 compile_multilib: "64",
1877 jni_libs: ["libjni"],
1878 }
1879 `)
1880
1881 testCases := []struct {
1882 name string
1883 abis []string
1884 }{
1885 {"test", []string{"arm64-v8a"}},
1886 {"test_first", []string{"arm64-v8a"}},
1887 {"test_both", []string{"arm64-v8a", "armeabi-v7a"}},
1888 {"test_32", []string{"armeabi-v7a"}},
1889 {"test_64", []string{"arm64-v8a"}},
1890 }
1891
1892 for _, test := range testCases {
1893 t.Run(test.name, func(t *testing.T) {
1894 app := ctx.ModuleForTests(test.name, "android_common")
Colin Crossb3168ba2023-07-26 16:14:56 -07001895 jniLibZip := app.Output("jnilibs.zip")
Paul Duffin50c217c2019-06-12 13:25:22 +01001896 var abis []string
1897 args := strings.Fields(jniLibZip.Args["jarArgs"])
1898 for i := 0; i < len(args); i++ {
1899 if args[i] == "-P" {
1900 abis = append(abis, filepath.Base(args[i+1]))
1901 i++
1902 }
1903 }
1904 if !reflect.DeepEqual(abis, test.abis) {
1905 t.Errorf("want abis %v, got %v", test.abis, abis)
1906 }
1907 })
1908 }
1909}
1910
Jeongik Cha2cc570d2019-10-29 15:44:45 +09001911func TestAppSdkVersionByPartition(t *testing.T) {
1912 testJavaError(t, "sdk_version must have a value when the module is located at vendor or product", `
1913 android_app {
1914 name: "foo",
1915 srcs: ["a.java"],
1916 vendor: true,
1917 platform_apis: true,
1918 }
1919 `)
1920
1921 testJava(t, `
1922 android_app {
1923 name: "bar",
1924 srcs: ["b.java"],
1925 platform_apis: true,
1926 }
1927 `)
1928
1929 for _, enforce := range []bool{true, false} {
Jeongik Cha2cc570d2019-10-29 15:44:45 +09001930 bp := `
1931 android_app {
1932 name: "foo",
1933 srcs: ["a.java"],
1934 product_specific: true,
1935 platform_apis: true,
1936 }
1937 `
Colin Cross98be1bb2019-12-13 20:41:13 -08001938
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00001939 errorHandler := android.FixtureExpectsNoErrors
Jeongik Cha2cc570d2019-10-29 15:44:45 +09001940 if enforce {
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00001941 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern("sdk_version must have a value when the module is located at vendor or product")
Jeongik Cha2cc570d2019-10-29 15:44:45 +09001942 }
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00001943
1944 android.GroupFixturePreparers(
1945 PrepareForTestWithJavaDefaultModules,
1946 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1947 variables.EnforceProductPartitionInterface = proptools.BoolPtr(enforce)
1948 }),
1949 ).
1950 ExtendWithErrorHandler(errorHandler).
1951 RunTestWithBp(t, bp)
Jeongik Cha2cc570d2019-10-29 15:44:45 +09001952 }
1953}
1954
Paul Duffin50c217c2019-06-12 13:25:22 +01001955func TestJNIPackaging(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001956 ctx, _ := testJava(t, cc.GatherRequiredDepsForTest(android.Android)+`
Paul Duffin50c217c2019-06-12 13:25:22 +01001957 cc_library {
1958 name: "libjni",
1959 system_shared_libs: [],
1960 stl: "none",
Colin Cross094cde42020-02-15 10:38:00 -08001961 sdk_version: "current",
Paul Duffin50c217c2019-06-12 13:25:22 +01001962 }
1963
1964 android_app {
1965 name: "app",
1966 jni_libs: ["libjni"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09001967 sdk_version: "current",
Paul Duffin50c217c2019-06-12 13:25:22 +01001968 }
1969
1970 android_app {
1971 name: "app_noembed",
1972 jni_libs: ["libjni"],
1973 use_embedded_native_libs: false,
Jeongik Cha538c0d02019-07-11 15:54:27 +09001974 sdk_version: "current",
Paul Duffin50c217c2019-06-12 13:25:22 +01001975 }
1976
1977 android_app {
1978 name: "app_embed",
1979 jni_libs: ["libjni"],
1980 use_embedded_native_libs: true,
Jeongik Cha538c0d02019-07-11 15:54:27 +09001981 sdk_version: "current",
Paul Duffin50c217c2019-06-12 13:25:22 +01001982 }
1983
1984 android_test {
1985 name: "test",
Colin Crossc511bc52020-04-07 16:50:32 +00001986 sdk_version: "current",
Paul Duffin50c217c2019-06-12 13:25:22 +01001987 jni_libs: ["libjni"],
1988 }
1989
1990 android_test {
1991 name: "test_noembed",
Colin Crossc511bc52020-04-07 16:50:32 +00001992 sdk_version: "current",
Paul Duffin50c217c2019-06-12 13:25:22 +01001993 jni_libs: ["libjni"],
1994 use_embedded_native_libs: false,
1995 }
1996
1997 android_test_helper_app {
1998 name: "test_helper",
Colin Crossc511bc52020-04-07 16:50:32 +00001999 sdk_version: "current",
Paul Duffin50c217c2019-06-12 13:25:22 +01002000 jni_libs: ["libjni"],
2001 }
2002
2003 android_test_helper_app {
2004 name: "test_helper_noembed",
Colin Crossc511bc52020-04-07 16:50:32 +00002005 sdk_version: "current",
Paul Duffin50c217c2019-06-12 13:25:22 +01002006 jni_libs: ["libjni"],
2007 use_embedded_native_libs: false,
2008 }
2009 `)
2010
2011 testCases := []struct {
2012 name string
2013 packaged bool
2014 compressed bool
2015 }{
Jiyong Parkd044bb42024-05-15 02:09:54 +09002016 {"app", false, false},
2017 {"app_noembed", false, false},
Paul Duffin50c217c2019-06-12 13:25:22 +01002018 {"app_embed", true, false},
2019 {"test", true, false},
2020 {"test_noembed", true, true},
2021 {"test_helper", true, false},
2022 {"test_helper_noembed", true, true},
2023 }
2024
2025 for _, test := range testCases {
2026 t.Run(test.name, func(t *testing.T) {
2027 app := ctx.ModuleForTests(test.name, "android_common")
Colin Crossb3168ba2023-07-26 16:14:56 -07002028 jniLibZip := app.MaybeOutput("jnilibs.zip")
Paul Duffin50c217c2019-06-12 13:25:22 +01002029 if g, w := (jniLibZip.Rule != nil), test.packaged; g != w {
2030 t.Errorf("expected jni packaged %v, got %v", w, g)
2031 }
2032
2033 if jniLibZip.Rule != nil {
2034 if g, w := !strings.Contains(jniLibZip.Args["jarArgs"], "-L 0"), test.compressed; g != w {
2035 t.Errorf("expected jni compressed %v, got %v", w, g)
2036 }
Colin Crossc511bc52020-04-07 16:50:32 +00002037
2038 if !strings.Contains(jniLibZip.Implicits[0].String(), "_sdk_") {
2039 t.Errorf("expected input %q to use sdk variant", jniLibZip.Implicits[0].String())
2040 }
Paul Duffin50c217c2019-06-12 13:25:22 +01002041 }
2042 })
2043 }
Colin Cross47fa9d32019-03-26 10:51:39 -07002044}
2045
Colin Cross3c007702020-05-08 11:20:24 -07002046func TestJNISDK(t *testing.T) {
2047 ctx, _ := testJava(t, cc.GatherRequiredDepsForTest(android.Android)+`
2048 cc_library {
2049 name: "libjni",
2050 system_shared_libs: [],
2051 stl: "none",
2052 sdk_version: "current",
2053 }
2054
2055 android_test {
2056 name: "app_platform",
2057 jni_libs: ["libjni"],
2058 platform_apis: true,
2059 }
2060
2061 android_test {
2062 name: "app_sdk",
2063 jni_libs: ["libjni"],
2064 sdk_version: "current",
2065 }
2066
2067 android_test {
2068 name: "app_force_platform",
2069 jni_libs: ["libjni"],
2070 sdk_version: "current",
2071 jni_uses_platform_apis: true,
2072 }
2073
2074 android_test {
2075 name: "app_force_sdk",
2076 jni_libs: ["libjni"],
2077 platform_apis: true,
2078 jni_uses_sdk_apis: true,
2079 }
Colin Crossc2d24052020-05-13 11:05:02 -07002080
2081 cc_library {
2082 name: "libvendorjni",
2083 system_shared_libs: [],
2084 stl: "none",
2085 vendor: true,
2086 }
2087
2088 android_test {
2089 name: "app_vendor",
2090 jni_libs: ["libvendorjni"],
2091 sdk_version: "current",
2092 vendor: true,
2093 }
Colin Cross3c007702020-05-08 11:20:24 -07002094 `)
2095
2096 testCases := []struct {
Colin Crossc2d24052020-05-13 11:05:02 -07002097 name string
2098 sdkJNI bool
2099 vendorJNI bool
Colin Cross3c007702020-05-08 11:20:24 -07002100 }{
Colin Crossc2d24052020-05-13 11:05:02 -07002101 {name: "app_platform"},
2102 {name: "app_sdk", sdkJNI: true},
2103 {name: "app_force_platform"},
2104 {name: "app_force_sdk", sdkJNI: true},
2105 {name: "app_vendor", vendorJNI: true},
Colin Cross3c007702020-05-08 11:20:24 -07002106 }
2107
Colin Crossc2d24052020-05-13 11:05:02 -07002108 platformJNI := ctx.ModuleForTests("libjni", "android_arm64_armv8-a_shared").
2109 Output("libjni.so").Output.String()
2110 sdkJNI := ctx.ModuleForTests("libjni", "android_arm64_armv8-a_sdk_shared").
2111 Output("libjni.so").Output.String()
Kiyoung Kimb5fdb2e2024-01-03 14:24:34 +09002112 vendorJNI := ctx.ModuleForTests("libvendorjni", "android_vendor_arm64_armv8-a_shared").
Colin Crossc2d24052020-05-13 11:05:02 -07002113 Output("libvendorjni.so").Output.String()
2114
Colin Cross3c007702020-05-08 11:20:24 -07002115 for _, test := range testCases {
2116 t.Run(test.name, func(t *testing.T) {
2117 app := ctx.ModuleForTests(test.name, "android_common")
Colin Cross3c007702020-05-08 11:20:24 -07002118
Colin Crossb3168ba2023-07-26 16:14:56 -07002119 jniLibZip := app.MaybeOutput("jnilibs.zip")
Colin Cross3c007702020-05-08 11:20:24 -07002120 if len(jniLibZip.Implicits) != 1 {
2121 t.Fatalf("expected exactly one jni library, got %q", jniLibZip.Implicits.Strings())
2122 }
2123 gotJNI := jniLibZip.Implicits[0].String()
2124
2125 if test.sdkJNI {
2126 if gotJNI != sdkJNI {
2127 t.Errorf("expected SDK JNI library %q, got %q", sdkJNI, gotJNI)
2128 }
Colin Crossc2d24052020-05-13 11:05:02 -07002129 } else if test.vendorJNI {
2130 if gotJNI != vendorJNI {
2131 t.Errorf("expected platform JNI library %q, got %q", vendorJNI, gotJNI)
2132 }
Colin Cross3c007702020-05-08 11:20:24 -07002133 } else {
2134 if gotJNI != platformJNI {
2135 t.Errorf("expected platform JNI library %q, got %q", platformJNI, gotJNI)
2136 }
2137 }
2138 })
2139 }
2140
2141 t.Run("jni_uses_platform_apis_error", func(t *testing.T) {
2142 testJavaError(t, `jni_uses_platform_apis: can only be set for modules that set sdk_version`, `
2143 android_test {
2144 name: "app_platform",
2145 platform_apis: true,
2146 jni_uses_platform_apis: true,
2147 }
2148 `)
2149 })
2150
2151 t.Run("jni_uses_sdk_apis_error", func(t *testing.T) {
2152 testJavaError(t, `jni_uses_sdk_apis: can only be set for modules that do not set sdk_version`, `
2153 android_test {
2154 name: "app_sdk",
2155 sdk_version: "current",
2156 jni_uses_sdk_apis: true,
2157 }
2158 `)
2159 })
2160
2161}
2162
Jaewoong Jung2ad817c2019-01-18 14:27:16 -08002163func TestCertificates(t *testing.T) {
2164 testCases := []struct {
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -04002165 name string
2166 bp string
Colin Cross5caad2b2022-12-12 15:11:46 -08002167 allowMissingDependencies bool
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -04002168 certificateOverride string
2169 expectedCertSigningFlags string
2170 expectedCertificate string
Jaewoong Jung2ad817c2019-01-18 14:27:16 -08002171 }{
2172 {
2173 name: "default",
2174 bp: `
2175 android_app {
2176 name: "foo",
2177 srcs: ["a.java"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09002178 sdk_version: "current",
Jaewoong Jung2ad817c2019-01-18 14:27:16 -08002179 }
2180 `,
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -04002181 certificateOverride: "",
2182 expectedCertSigningFlags: "",
Colin Cross5caad2b2022-12-12 15:11:46 -08002183 expectedCertificate: "build/make/target/product/security/testkey",
Jaewoong Jung2ad817c2019-01-18 14:27:16 -08002184 },
2185 {
2186 name: "module certificate property",
2187 bp: `
2188 android_app {
2189 name: "foo",
2190 srcs: ["a.java"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09002191 certificate: ":new_certificate",
2192 sdk_version: "current",
Jaewoong Jung2ad817c2019-01-18 14:27:16 -08002193 }
2194
2195 android_app_certificate {
2196 name: "new_certificate",
Colin Cross3c007702020-05-08 11:20:24 -07002197 certificate: "cert/new_cert",
Jaewoong Jung2ad817c2019-01-18 14:27:16 -08002198 }
2199 `,
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -04002200 certificateOverride: "",
2201 expectedCertSigningFlags: "",
Colin Cross5caad2b2022-12-12 15:11:46 -08002202 expectedCertificate: "cert/new_cert",
Jaewoong Jung2ad817c2019-01-18 14:27:16 -08002203 },
2204 {
2205 name: "path certificate property",
2206 bp: `
2207 android_app {
2208 name: "foo",
2209 srcs: ["a.java"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09002210 certificate: "expiredkey",
2211 sdk_version: "current",
Jaewoong Jung2ad817c2019-01-18 14:27:16 -08002212 }
2213 `,
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -04002214 certificateOverride: "",
2215 expectedCertSigningFlags: "",
Colin Cross5caad2b2022-12-12 15:11:46 -08002216 expectedCertificate: "build/make/target/product/security/expiredkey",
Jaewoong Jung2ad817c2019-01-18 14:27:16 -08002217 },
2218 {
2219 name: "certificate overrides",
2220 bp: `
2221 android_app {
2222 name: "foo",
2223 srcs: ["a.java"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09002224 certificate: "expiredkey",
2225 sdk_version: "current",
Jaewoong Jung2ad817c2019-01-18 14:27:16 -08002226 }
2227
2228 android_app_certificate {
2229 name: "new_certificate",
Colin Cross3c007702020-05-08 11:20:24 -07002230 certificate: "cert/new_cert",
Jaewoong Jung2ad817c2019-01-18 14:27:16 -08002231 }
2232 `,
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -04002233 certificateOverride: "foo:new_certificate",
2234 expectedCertSigningFlags: "",
Colin Cross5caad2b2022-12-12 15:11:46 -08002235 expectedCertificate: "cert/new_cert",
Liz Kammere2b27f42020-05-07 13:24:05 -07002236 },
2237 {
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -04002238 name: "certificate signing flags",
Liz Kammere2b27f42020-05-07 13:24:05 -07002239 bp: `
2240 android_app {
2241 name: "foo",
2242 srcs: ["a.java"],
2243 certificate: ":new_certificate",
2244 lineage: "lineage.bin",
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -04002245 rotationMinSdkVersion: "32",
Liz Kammere2b27f42020-05-07 13:24:05 -07002246 sdk_version: "current",
2247 }
2248
2249 android_app_certificate {
2250 name: "new_certificate",
2251 certificate: "cert/new_cert",
2252 }
2253 `,
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -04002254 certificateOverride: "",
2255 expectedCertSigningFlags: "--lineage lineage.bin --rotation-min-sdk-version 32",
Colin Cross5caad2b2022-12-12 15:11:46 -08002256 expectedCertificate: "cert/new_cert",
Jaewoong Jung2ad817c2019-01-18 14:27:16 -08002257 },
Jaewoong Jung1c1b6e62021-03-09 15:02:31 -08002258 {
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -04002259 name: "cert signing flags from filegroup",
Jaewoong Jung1c1b6e62021-03-09 15:02:31 -08002260 bp: `
2261 android_app {
2262 name: "foo",
2263 srcs: ["a.java"],
2264 certificate: ":new_certificate",
2265 lineage: ":lineage_bin",
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -04002266 rotationMinSdkVersion: "32",
Jaewoong Jung1c1b6e62021-03-09 15:02:31 -08002267 sdk_version: "current",
2268 }
2269
2270 android_app_certificate {
2271 name: "new_certificate",
2272 certificate: "cert/new_cert",
2273 }
2274
2275 filegroup {
2276 name: "lineage_bin",
2277 srcs: ["lineage.bin"],
2278 }
2279 `,
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -04002280 certificateOverride: "",
2281 expectedCertSigningFlags: "--lineage lineage.bin --rotation-min-sdk-version 32",
Colin Cross5caad2b2022-12-12 15:11:46 -08002282 expectedCertificate: "cert/new_cert",
2283 },
2284 {
2285 name: "missing with AllowMissingDependencies",
2286 bp: `
2287 android_app {
2288 name: "foo",
2289 srcs: ["a.java"],
2290 certificate: ":new_certificate",
2291 sdk_version: "current",
2292 }
2293 `,
2294 expectedCertificate: "out/soong/.intermediates/foo/android_common/missing",
2295 allowMissingDependencies: true,
Jaewoong Jung1c1b6e62021-03-09 15:02:31 -08002296 },
Jaewoong Jung2ad817c2019-01-18 14:27:16 -08002297 }
2298
2299 for _, test := range testCases {
2300 t.Run(test.name, func(t *testing.T) {
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00002301 result := android.GroupFixturePreparers(
2302 PrepareForTestWithJavaDefaultModules,
2303 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
2304 if test.certificateOverride != "" {
2305 variables.CertificateOverrides = []string{test.certificateOverride}
2306 }
Colin Cross5caad2b2022-12-12 15:11:46 -08002307 if test.allowMissingDependencies {
2308 variables.Allow_missing_dependencies = proptools.BoolPtr(true)
2309 }
2310 }),
2311 android.FixtureModifyContext(func(ctx *android.TestContext) {
2312 ctx.SetAllowMissingDependencies(test.allowMissingDependencies)
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00002313 }),
2314 ).RunTestWithBp(t, test.bp)
Jaewoong Jung2ad817c2019-01-18 14:27:16 -08002315
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00002316 foo := result.ModuleForTests("foo", "android_common")
Jaewoong Jung2ad817c2019-01-18 14:27:16 -08002317
Colin Cross5caad2b2022-12-12 15:11:46 -08002318 certificate := foo.Module().(*AndroidApp).certificate
2319 android.AssertPathRelativeToTopEquals(t, "certificates key", test.expectedCertificate+".pk8", certificate.Key)
2320 // The sign_target_files_apks and check_target_files_signatures
2321 // tools require that certificates have a .x509.pem extension.
2322 android.AssertPathRelativeToTopEquals(t, "certificates pem", test.expectedCertificate+".x509.pem", certificate.Pem)
Liz Kammere2b27f42020-05-07 13:24:05 -07002323
Colin Cross5caad2b2022-12-12 15:11:46 -08002324 signapk := foo.Output("foo.apk")
2325 if signapk.Rule != android.ErrorRule {
2326 signCertificateFlags := signapk.Args["certificates"]
2327 expectedFlags := certificate.Pem.String() + " " + certificate.Key.String()
2328 android.AssertStringEquals(t, "certificates flags", expectedFlags, signCertificateFlags)
2329
2330 certSigningFlags := signapk.Args["flags"]
2331 android.AssertStringEquals(t, "cert signing flags", test.expectedCertSigningFlags, certSigningFlags)
2332 }
Jaewoong Jung2ad817c2019-01-18 14:27:16 -08002333 })
2334 }
2335}
Jaewoong Jung9d22a912019-01-23 16:27:47 -08002336
Songchun Fan688de9a2020-03-24 20:32:24 -07002337func TestRequestV4SigningFlag(t *testing.T) {
2338 testCases := []struct {
2339 name string
2340 bp string
2341 expected string
2342 }{
2343 {
2344 name: "default",
2345 bp: `
2346 android_app {
2347 name: "foo",
2348 srcs: ["a.java"],
2349 sdk_version: "current",
2350 }
2351 `,
2352 expected: "",
2353 },
2354 {
2355 name: "default",
2356 bp: `
2357 android_app {
2358 name: "foo",
2359 srcs: ["a.java"],
2360 sdk_version: "current",
2361 v4_signature: false,
2362 }
2363 `,
2364 expected: "",
2365 },
2366 {
2367 name: "module certificate property",
2368 bp: `
2369 android_app {
2370 name: "foo",
2371 srcs: ["a.java"],
2372 sdk_version: "current",
2373 v4_signature: true,
2374 }
2375 `,
2376 expected: "--enable-v4",
2377 },
2378 }
2379
2380 for _, test := range testCases {
2381 t.Run(test.name, func(t *testing.T) {
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00002382 result := android.GroupFixturePreparers(
2383 PrepareForTestWithJavaDefaultModules,
2384 ).RunTestWithBp(t, test.bp)
Songchun Fan688de9a2020-03-24 20:32:24 -07002385
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00002386 foo := result.ModuleForTests("foo", "android_common")
Songchun Fan688de9a2020-03-24 20:32:24 -07002387
2388 signapk := foo.Output("foo.apk")
2389 signFlags := signapk.Args["flags"]
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00002390 android.AssertStringEquals(t, "signing flags", test.expected, signFlags)
Songchun Fan688de9a2020-03-24 20:32:24 -07002391 })
2392 }
2393}
2394
Jaewoong Jung9d22a912019-01-23 16:27:47 -08002395func TestPackageNameOverride(t *testing.T) {
2396 testCases := []struct {
2397 name string
2398 bp string
2399 packageNameOverride string
2400 expected []string
2401 }{
2402 {
2403 name: "default",
2404 bp: `
2405 android_app {
2406 name: "foo",
2407 srcs: ["a.java"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09002408 sdk_version: "current",
Jaewoong Jung9d22a912019-01-23 16:27:47 -08002409 }
2410 `,
2411 packageNameOverride: "",
2412 expected: []string{
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00002413 "out/soong/.intermediates/foo/android_common/foo.apk",
2414 "out/soong/target/product/test_device/system/app/foo/foo.apk",
Jaewoong Jung9d22a912019-01-23 16:27:47 -08002415 },
2416 },
2417 {
Jooyung Han29e2f6d2022-01-08 12:13:59 +09002418 name: "overridden via PRODUCT_PACKAGE_NAME_OVERRIDES",
Jaewoong Jung9d22a912019-01-23 16:27:47 -08002419 bp: `
2420 android_app {
2421 name: "foo",
2422 srcs: ["a.java"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09002423 sdk_version: "current",
Jaewoong Jung9d22a912019-01-23 16:27:47 -08002424 }
2425 `,
2426 packageNameOverride: "foo:bar",
2427 expected: []string{
2428 // The package apk should be still be the original name for test dependencies.
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00002429 "out/soong/.intermediates/foo/android_common/bar.apk",
2430 "out/soong/target/product/test_device/system/app/bar/bar.apk",
Jaewoong Jung9d22a912019-01-23 16:27:47 -08002431 },
2432 },
Jooyung Han29e2f6d2022-01-08 12:13:59 +09002433 {
2434 name: "overridden via stem",
2435 bp: `
2436 android_app {
2437 name: "foo",
2438 srcs: ["a.java"],
2439 sdk_version: "current",
2440 stem: "bar",
2441 }
2442 `,
2443 packageNameOverride: "",
2444 expected: []string{
2445 "out/soong/.intermediates/foo/android_common/bar.apk",
2446 "out/soong/target/product/test_device/system/app/bar/bar.apk",
2447 },
2448 },
Jaewoong Jung9d22a912019-01-23 16:27:47 -08002449 }
2450
2451 for _, test := range testCases {
2452 t.Run(test.name, func(t *testing.T) {
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00002453 result := android.GroupFixturePreparers(
2454 PrepareForTestWithJavaDefaultModules,
2455 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
2456 if test.packageNameOverride != "" {
2457 variables.PackageNameOverrides = []string{test.packageNameOverride}
2458 }
2459 }),
2460 ).RunTestWithBp(t, test.bp)
Jaewoong Jung9d22a912019-01-23 16:27:47 -08002461
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00002462 foo := result.ModuleForTests("foo", "android_common")
2463
Lukacs T. Berkifbaf7252021-08-19 09:36:42 +02002464 outSoongDir := result.Config.SoongOutDir()
Jaewoong Jung9d22a912019-01-23 16:27:47 -08002465
2466 outputs := foo.AllOutputs()
2467 outputMap := make(map[string]bool)
2468 for _, o := range outputs {
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00002469 outputMap[android.StringPathRelativeToTop(outSoongDir, o)] = true
Jaewoong Jung9d22a912019-01-23 16:27:47 -08002470 }
2471 for _, e := range test.expected {
2472 if _, exist := outputMap[e]; !exist {
2473 t.Errorf("Can't find %q in output files.\nAll outputs:%v", e, outputs)
2474 }
2475 }
2476 })
2477 }
2478}
Jaewoong Jung4102e5d2019-02-27 16:26:28 -08002479
2480func TestInstrumentationTargetOverridden(t *testing.T) {
2481 bp := `
2482 android_app {
2483 name: "foo",
2484 srcs: ["a.java"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09002485 sdk_version: "current",
Jaewoong Jung4102e5d2019-02-27 16:26:28 -08002486 }
2487
2488 android_test {
2489 name: "bar",
2490 instrumentation_for: "foo",
Jeongik Cha538c0d02019-07-11 15:54:27 +09002491 sdk_version: "current",
Jaewoong Jung4102e5d2019-02-27 16:26:28 -08002492 }
2493 `
Jaewoong Jung4102e5d2019-02-27 16:26:28 -08002494
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00002495 result := android.GroupFixturePreparers(
2496 PrepareForTestWithJavaDefaultModules,
2497 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
2498 variables.ManifestPackageNameOverrides = []string{"foo:org.dandroid.bp"}
2499 }),
2500 ).RunTestWithBp(t, bp)
Jaewoong Jung4102e5d2019-02-27 16:26:28 -08002501
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00002502 bar := result.ModuleForTests("bar", "android_common")
Jaewoong Jung4102e5d2019-02-27 16:26:28 -08002503 res := bar.Output("package-res.apk")
2504 aapt2Flags := res.Args["flags"]
2505 e := "--rename-instrumentation-target-package org.dandroid.bp"
2506 if !strings.Contains(aapt2Flags, e) {
2507 t.Errorf("target package renaming flag, %q is missing in aapt2 link flags, %q", e, aapt2Flags)
2508 }
2509}
Jaewoong Jung525443a2019-02-28 15:35:54 -08002510
2511func TestOverrideAndroidApp(t *testing.T) {
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00002512 result := PrepareForTestWithJavaDefaultModules.RunTestWithBp(
2513 t, `
Jaewoong Jung525443a2019-02-28 15:35:54 -08002514 android_app {
2515 name: "foo",
2516 srcs: ["a.java"],
Jaewoong Junga641ee92019-03-27 11:17:14 -07002517 certificate: "expiredkey",
Jaewoong Jungb639a6a2019-05-10 15:16:29 -07002518 overrides: ["qux"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09002519 sdk_version: "current",
Jaewoong Jung525443a2019-02-28 15:35:54 -08002520 }
2521
2522 override_android_app {
2523 name: "bar",
2524 base: "foo",
2525 certificate: ":new_certificate",
Liz Kammere2b27f42020-05-07 13:24:05 -07002526 lineage: "lineage.bin",
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -04002527 rotationMinSdkVersion: "32",
Baligh Uddin5b16dfb2020-02-11 17:27:19 -08002528 logging_parent: "bah",
Jaewoong Jung525443a2019-02-28 15:35:54 -08002529 }
2530
2531 android_app_certificate {
2532 name: "new_certificate",
2533 certificate: "cert/new_cert",
2534 }
Jaewoong Jung6f373f62019-03-13 10:13:24 -07002535
2536 override_android_app {
2537 name: "baz",
2538 base: "foo",
2539 package_name: "org.dandroid.bp",
2540 }
Liz Kammer9f9fd022020-06-18 19:44:06 +00002541
2542 override_android_app {
2543 name: "baz_no_rename_resources",
2544 base: "foo",
2545 package_name: "org.dandroid.bp",
2546 rename_resources_package: false,
2547 }
2548
2549 android_app {
2550 name: "foo_no_rename_resources",
2551 srcs: ["a.java"],
2552 certificate: "expiredkey",
2553 overrides: ["qux"],
2554 rename_resources_package: false,
2555 sdk_version: "current",
2556 }
2557
2558 override_android_app {
2559 name: "baz_base_no_rename_resources",
2560 base: "foo_no_rename_resources",
2561 package_name: "org.dandroid.bp",
2562 }
2563
2564 override_android_app {
2565 name: "baz_override_base_rename_resources",
2566 base: "foo_no_rename_resources",
2567 package_name: "org.dandroid.bp",
2568 rename_resources_package: true,
2569 }
Jaewoong Jung525443a2019-02-28 15:35:54 -08002570 `)
2571
2572 expectedVariants := []struct {
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -04002573 name string
2574 moduleName string
2575 variantName string
2576 apkName string
2577 apkPath string
2578 certFlag string
2579 certSigningFlags string
2580 overrides []string
2581 packageFlag string
2582 renameResources bool
2583 logging_parent string
Jaewoong Jung525443a2019-02-28 15:35:54 -08002584 }{
2585 {
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -04002586 name: "foo",
2587 moduleName: "foo",
2588 variantName: "android_common",
2589 apkPath: "out/soong/target/product/test_device/system/app/foo/foo.apk",
2590 certFlag: "build/make/target/product/security/expiredkey.x509.pem build/make/target/product/security/expiredkey.pk8",
2591 certSigningFlags: "",
2592 overrides: []string{"qux"},
2593 packageFlag: "",
2594 renameResources: false,
2595 logging_parent: "",
Jaewoong Jung525443a2019-02-28 15:35:54 -08002596 },
2597 {
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -04002598 name: "foo",
2599 moduleName: "bar",
2600 variantName: "android_common_bar",
2601 apkPath: "out/soong/target/product/test_device/system/app/bar/bar.apk",
2602 certFlag: "cert/new_cert.x509.pem cert/new_cert.pk8",
2603 certSigningFlags: "--lineage lineage.bin --rotation-min-sdk-version 32",
2604 overrides: []string{"qux", "foo"},
2605 packageFlag: "",
2606 renameResources: false,
2607 logging_parent: "bah",
Jaewoong Jung6f373f62019-03-13 10:13:24 -07002608 },
2609 {
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -04002610 name: "foo",
2611 moduleName: "baz",
2612 variantName: "android_common_baz",
2613 apkPath: "out/soong/target/product/test_device/system/app/baz/baz.apk",
2614 certFlag: "build/make/target/product/security/expiredkey.x509.pem build/make/target/product/security/expiredkey.pk8",
2615 certSigningFlags: "",
2616 overrides: []string{"qux", "foo"},
2617 packageFlag: "org.dandroid.bp",
2618 renameResources: true,
2619 logging_parent: "",
Liz Kammer9f9fd022020-06-18 19:44:06 +00002620 },
2621 {
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -04002622 name: "foo",
2623 moduleName: "baz_no_rename_resources",
2624 variantName: "android_common_baz_no_rename_resources",
2625 apkPath: "out/soong/target/product/test_device/system/app/baz_no_rename_resources/baz_no_rename_resources.apk",
2626 certFlag: "build/make/target/product/security/expiredkey.x509.pem build/make/target/product/security/expiredkey.pk8",
2627 certSigningFlags: "",
2628 overrides: []string{"qux", "foo"},
2629 packageFlag: "org.dandroid.bp",
2630 renameResources: false,
2631 logging_parent: "",
Liz Kammer9f9fd022020-06-18 19:44:06 +00002632 },
2633 {
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -04002634 name: "foo_no_rename_resources",
2635 moduleName: "baz_base_no_rename_resources",
2636 variantName: "android_common_baz_base_no_rename_resources",
2637 apkPath: "out/soong/target/product/test_device/system/app/baz_base_no_rename_resources/baz_base_no_rename_resources.apk",
2638 certFlag: "build/make/target/product/security/expiredkey.x509.pem build/make/target/product/security/expiredkey.pk8",
2639 certSigningFlags: "",
2640 overrides: []string{"qux", "foo_no_rename_resources"},
2641 packageFlag: "org.dandroid.bp",
2642 renameResources: false,
2643 logging_parent: "",
Liz Kammer9f9fd022020-06-18 19:44:06 +00002644 },
2645 {
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -04002646 name: "foo_no_rename_resources",
2647 moduleName: "baz_override_base_rename_resources",
2648 variantName: "android_common_baz_override_base_rename_resources",
2649 apkPath: "out/soong/target/product/test_device/system/app/baz_override_base_rename_resources/baz_override_base_rename_resources.apk",
2650 certFlag: "build/make/target/product/security/expiredkey.x509.pem build/make/target/product/security/expiredkey.pk8",
2651 certSigningFlags: "",
2652 overrides: []string{"qux", "foo_no_rename_resources"},
2653 packageFlag: "org.dandroid.bp",
2654 renameResources: true,
2655 logging_parent: "",
Jaewoong Jung525443a2019-02-28 15:35:54 -08002656 },
2657 }
2658 for _, expected := range expectedVariants {
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00002659 variant := result.ModuleForTests(expected.name, expected.variantName)
Jaewoong Jung525443a2019-02-28 15:35:54 -08002660
2661 // Check the final apk name
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00002662 variant.Output(expected.apkPath)
Jaewoong Jung525443a2019-02-28 15:35:54 -08002663
2664 // Check the certificate paths
Jaewoong Jung5a498812019-11-07 14:14:38 -08002665 signapk := variant.Output(expected.moduleName + ".apk")
Liz Kammere2b27f42020-05-07 13:24:05 -07002666 certFlag := signapk.Args["certificates"]
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00002667 android.AssertStringEquals(t, "certificates flags", expected.certFlag, certFlag)
Liz Kammere2b27f42020-05-07 13:24:05 -07002668
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -04002669 // Check the cert signing flags
2670 certSigningFlags := signapk.Args["flags"]
2671 android.AssertStringEquals(t, "cert signing flags", expected.certSigningFlags, certSigningFlags)
Jaewoong Jung525443a2019-02-28 15:35:54 -08002672
Jaewoong Jung6f373f62019-03-13 10:13:24 -07002673 // Check if the overrides field values are correctly aggregated.
Jaewoong Jung525443a2019-02-28 15:35:54 -08002674 mod := variant.Module().(*AndroidApp)
zhidoua2ce78f2022-02-17 02:33:12 +00002675 android.AssertDeepEquals(t, "overrides property", expected.overrides, mod.overridableAppProperties.Overrides)
Jaewoong Jung6f373f62019-03-13 10:13:24 -07002676
Baligh Uddin5b16dfb2020-02-11 17:27:19 -08002677 // Test Overridable property: Logging_parent
2678 logging_parent := mod.aapt.LoggingParent
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00002679 android.AssertStringEquals(t, "overrides property value for logging parent", expected.logging_parent, logging_parent)
Baligh Uddin5b16dfb2020-02-11 17:27:19 -08002680
Liz Kammer1d5983b2020-05-19 19:15:37 +00002681 // Check the package renaming flag, if exists.
Jaewoong Jung6f373f62019-03-13 10:13:24 -07002682 res := variant.Output("package-res.apk")
2683 aapt2Flags := res.Args["flags"]
Liz Kammer9f9fd022020-06-18 19:44:06 +00002684 checkAapt2LinkFlag(t, aapt2Flags, "rename-manifest-package", expected.packageFlag)
2685 expectedPackage := expected.packageFlag
2686 if !expected.renameResources {
2687 expectedPackage = ""
Liz Kammer1d5983b2020-05-19 19:15:37 +00002688 }
Liz Kammer9f9fd022020-06-18 19:44:06 +00002689 checkAapt2LinkFlag(t, aapt2Flags, "rename-resources-package", expectedPackage)
Jaewoong Jung525443a2019-02-28 15:35:54 -08002690 }
2691}
Jaewoong Jungccbb3932019-04-15 09:48:31 -07002692
zhidoua2ce78f2022-02-17 02:33:12 +00002693func TestOverrideAndroidAppOverrides(t *testing.T) {
2694 ctx, _ := testJava(
2695 t, `
2696 android_app {
2697 name: "foo",
2698 srcs: ["a.java"],
2699 sdk_version: "current",
2700 overrides: ["qux"]
2701 }
2702
2703 android_app {
2704 name: "bar",
2705 srcs: ["b.java"],
2706 sdk_version: "current",
2707 overrides: ["foo"]
2708 }
2709
2710 override_android_app {
2711 name: "foo_override",
2712 base: "foo",
2713 overrides: ["bar"]
2714 }
2715 `)
2716
2717 expectedVariants := []struct {
2718 name string
2719 moduleName string
2720 variantName string
2721 overrides []string
2722 }{
2723 {
2724 name: "foo",
2725 moduleName: "foo",
2726 variantName: "android_common",
2727 overrides: []string{"qux"},
2728 },
2729 {
2730 name: "bar",
2731 moduleName: "bar",
2732 variantName: "android_common",
2733 overrides: []string{"foo"},
2734 },
2735 {
2736 name: "foo",
2737 moduleName: "foo_override",
2738 variantName: "android_common_foo_override",
2739 overrides: []string{"bar", "foo"},
2740 },
2741 }
2742 for _, expected := range expectedVariants {
2743 variant := ctx.ModuleForTests(expected.name, expected.variantName)
2744
2745 // Check if the overrides field values are correctly aggregated.
2746 mod := variant.Module().(*AndroidApp)
2747 android.AssertDeepEquals(t, "overrides property", expected.overrides, mod.overridableAppProperties.Overrides)
2748 }
2749}
2750
Colin Crossaaa0c1f2022-05-16 16:19:54 -07002751func TestOverrideAndroidAppWithPrebuilt(t *testing.T) {
2752 result := PrepareForTestWithJavaDefaultModules.RunTestWithBp(
2753 t, `
2754 android_app {
2755 name: "foo",
2756 srcs: ["a.java"],
2757 sdk_version: "current",
2758 }
2759
2760 override_android_app {
2761 name: "bar",
2762 base: "foo",
2763 }
2764
2765 android_app_import {
2766 name: "bar",
2767 prefer: true,
2768 apk: "bar.apk",
2769 presigned: true,
2770 }
2771 `)
2772
2773 // An app that has an override that also has a prebuilt should not be hidden.
2774 foo := result.ModuleForTests("foo", "android_common")
2775 if foo.Module().IsHideFromMake() {
2776 t.Errorf("expected foo to have HideFromMake false")
2777 }
2778
2779 // An override that also has a prebuilt should be hidden.
2780 barOverride := result.ModuleForTests("foo", "android_common_bar")
2781 if !barOverride.Module().IsHideFromMake() {
2782 t.Errorf("expected bar override variant of foo to have HideFromMake true")
2783 }
2784}
2785
Jooyung Han01d80d82022-01-08 12:16:32 +09002786func TestOverrideAndroidAppStem(t *testing.T) {
2787 ctx, _ := testJava(t, `
2788 android_app {
2789 name: "foo",
2790 srcs: ["a.java"],
2791 sdk_version: "current",
2792 }
2793 override_android_app {
2794 name: "bar",
2795 base: "foo",
2796 }
2797 override_android_app {
2798 name: "baz",
2799 base: "foo",
2800 stem: "baz_stem",
2801 }
2802 android_app {
2803 name: "foo2",
2804 srcs: ["a.java"],
2805 sdk_version: "current",
2806 stem: "foo2_stem",
2807 }
2808 override_android_app {
2809 name: "bar2",
2810 base: "foo2",
2811 }
2812 override_android_app {
2813 name: "baz2",
2814 base: "foo2",
2815 stem: "baz2_stem",
2816 }
2817 `)
2818 for _, expected := range []struct {
2819 moduleName string
2820 variantName string
2821 apkPath string
2822 }{
2823 {
2824 moduleName: "foo",
2825 variantName: "android_common",
2826 apkPath: "out/soong/target/product/test_device/system/app/foo/foo.apk",
2827 },
2828 {
2829 moduleName: "foo",
2830 variantName: "android_common_bar",
2831 apkPath: "out/soong/target/product/test_device/system/app/bar/bar.apk",
2832 },
2833 {
2834 moduleName: "foo",
2835 variantName: "android_common_baz",
2836 apkPath: "out/soong/target/product/test_device/system/app/baz_stem/baz_stem.apk",
2837 },
2838 {
2839 moduleName: "foo2",
2840 variantName: "android_common",
2841 apkPath: "out/soong/target/product/test_device/system/app/foo2_stem/foo2_stem.apk",
2842 },
2843 {
2844 moduleName: "foo2",
2845 variantName: "android_common_bar2",
2846 // Note that this may cause the duplicate output error.
2847 apkPath: "out/soong/target/product/test_device/system/app/foo2_stem/foo2_stem.apk",
2848 },
2849 {
2850 moduleName: "foo2",
2851 variantName: "android_common_baz2",
2852 apkPath: "out/soong/target/product/test_device/system/app/baz2_stem/baz2_stem.apk",
2853 },
2854 } {
2855 variant := ctx.ModuleForTests(expected.moduleName, expected.variantName)
2856 variant.Output(expected.apkPath)
2857 }
2858}
2859
Jaewoong Jungb639a6a2019-05-10 15:16:29 -07002860func TestOverrideAndroidAppDependency(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07002861 ctx, _ := testJava(t, `
Jaewoong Jungb639a6a2019-05-10 15:16:29 -07002862 android_app {
2863 name: "foo",
2864 srcs: ["a.java"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09002865 sdk_version: "current",
Jaewoong Jungb639a6a2019-05-10 15:16:29 -07002866 }
2867
2868 override_android_app {
2869 name: "bar",
2870 base: "foo",
2871 package_name: "org.dandroid.bp",
2872 }
2873
2874 android_test {
2875 name: "baz",
2876 srcs: ["b.java"],
2877 instrumentation_for: "foo",
2878 }
2879
2880 android_test {
2881 name: "qux",
2882 srcs: ["b.java"],
2883 instrumentation_for: "bar",
2884 }
2885 `)
2886
2887 // Verify baz, which depends on the overridden module foo, has the correct classpath javac arg.
Paul Duffina71a67a2021-03-29 00:42:57 +01002888 javac := ctx.ModuleForTests("baz", "android_common").Rule("javac")
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00002889 fooTurbine := "out/soong/.intermediates/foo/android_common/turbine-combined/foo.jar"
Jaewoong Jungb639a6a2019-05-10 15:16:29 -07002890 if !strings.Contains(javac.Args["classpath"], fooTurbine) {
2891 t.Errorf("baz classpath %v does not contain %q", javac.Args["classpath"], fooTurbine)
2892 }
2893
2894 // Verify qux, which depends on the overriding module bar, has the correct classpath javac arg.
Paul Duffina71a67a2021-03-29 00:42:57 +01002895 javac = ctx.ModuleForTests("qux", "android_common").Rule("javac")
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00002896 barTurbine := "out/soong/.intermediates/foo/android_common_bar/turbine-combined/foo.jar"
Jaewoong Jungb639a6a2019-05-10 15:16:29 -07002897 if !strings.Contains(javac.Args["classpath"], barTurbine) {
2898 t.Errorf("qux classpath %v does not contain %q", javac.Args["classpath"], barTurbine)
2899 }
2900}
2901
Jaewoong Jung26dedd32019-06-06 08:45:58 -07002902func TestOverrideAndroidTest(t *testing.T) {
2903 ctx, _ := testJava(t, `
2904 android_app {
2905 name: "foo",
2906 srcs: ["a.java"],
2907 package_name: "com.android.foo",
2908 sdk_version: "current",
2909 }
2910
2911 override_android_app {
2912 name: "bar",
2913 base: "foo",
2914 package_name: "com.android.bar",
2915 }
2916
2917 android_test {
2918 name: "foo_test",
2919 srcs: ["b.java"],
2920 instrumentation_for: "foo",
2921 }
2922
2923 override_android_test {
2924 name: "bar_test",
2925 base: "foo_test",
2926 package_name: "com.android.bar.test",
2927 instrumentation_for: "bar",
2928 instrumentation_target_package: "com.android.bar",
2929 }
2930 `)
2931
2932 expectedVariants := []struct {
2933 moduleName string
2934 variantName string
2935 apkPath string
2936 overrides []string
2937 targetVariant string
2938 packageFlag string
2939 targetPackageFlag string
2940 }{
2941 {
2942 variantName: "android_common",
Jaewoong Jung326a9412019-11-21 10:41:00 -08002943 apkPath: "/target/product/test_device/testcases/foo_test/arm64/foo_test.apk",
Jaewoong Jung26dedd32019-06-06 08:45:58 -07002944 overrides: nil,
2945 targetVariant: "android_common",
2946 packageFlag: "",
2947 targetPackageFlag: "",
2948 },
2949 {
2950 variantName: "android_common_bar_test",
Jaewoong Jung326a9412019-11-21 10:41:00 -08002951 apkPath: "/target/product/test_device/testcases/bar_test/arm64/bar_test.apk",
Jaewoong Jung26dedd32019-06-06 08:45:58 -07002952 overrides: []string{"foo_test"},
2953 targetVariant: "android_common_bar",
2954 packageFlag: "com.android.bar.test",
2955 targetPackageFlag: "com.android.bar",
2956 },
2957 }
2958 for _, expected := range expectedVariants {
2959 variant := ctx.ModuleForTests("foo_test", expected.variantName)
2960
2961 // Check the final apk name
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00002962 variant.Output("out/soong" + expected.apkPath)
Jaewoong Jung26dedd32019-06-06 08:45:58 -07002963
2964 // Check if the overrides field values are correctly aggregated.
2965 mod := variant.Module().(*AndroidTest)
zhidoua2ce78f2022-02-17 02:33:12 +00002966 if !reflect.DeepEqual(expected.overrides, mod.overridableAppProperties.Overrides) {
Jaewoong Jung26dedd32019-06-06 08:45:58 -07002967 t.Errorf("Incorrect overrides property value, expected: %q, got: %q",
zhidoua2ce78f2022-02-17 02:33:12 +00002968 expected.overrides, mod.overridableAppProperties.Overrides)
Jaewoong Jung26dedd32019-06-06 08:45:58 -07002969 }
2970
2971 // Check if javac classpath has the correct jar file path. This checks instrumentation_for overrides.
Paul Duffina71a67a2021-03-29 00:42:57 +01002972 javac := variant.Rule("javac")
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00002973 turbine := filepath.Join("out", "soong", ".intermediates", "foo", expected.targetVariant, "turbine-combined", "foo.jar")
Jaewoong Jung26dedd32019-06-06 08:45:58 -07002974 if !strings.Contains(javac.Args["classpath"], turbine) {
2975 t.Errorf("classpath %q does not contain %q", javac.Args["classpath"], turbine)
2976 }
2977
2978 // Check aapt2 flags.
2979 res := variant.Output("package-res.apk")
2980 aapt2Flags := res.Args["flags"]
2981 checkAapt2LinkFlag(t, aapt2Flags, "rename-manifest-package", expected.packageFlag)
Liz Kammer9f9fd022020-06-18 19:44:06 +00002982 checkAapt2LinkFlag(t, aapt2Flags, "rename-resources-package", expected.packageFlag)
Jaewoong Jung26dedd32019-06-06 08:45:58 -07002983 checkAapt2LinkFlag(t, aapt2Flags, "rename-instrumentation-target-package", expected.targetPackageFlag)
2984 }
2985}
2986
Jaewoong Jung39982342020-01-14 10:27:18 -08002987func TestAndroidTest_FixTestConfig(t *testing.T) {
2988 ctx, _ := testJava(t, `
2989 android_app {
2990 name: "foo",
2991 srcs: ["a.java"],
2992 package_name: "com.android.foo",
2993 sdk_version: "current",
2994 }
2995
2996 android_test {
2997 name: "foo_test",
2998 srcs: ["b.java"],
2999 instrumentation_for: "foo",
3000 }
3001
3002 android_test {
3003 name: "bar_test",
3004 srcs: ["b.java"],
3005 package_name: "com.android.bar.test",
3006 instrumentation_for: "foo",
Seth Moorec6f4b532023-02-02 13:22:26 -08003007 mainline_package_name: "com.android.bar",
Jaewoong Jung39982342020-01-14 10:27:18 -08003008 }
3009
3010 override_android_test {
3011 name: "baz_test",
3012 base: "foo_test",
3013 package_name: "com.android.baz.test",
Seth Moorec6f4b532023-02-02 13:22:26 -08003014 mainline_package_name: "com.android.baz",
Jaewoong Jung39982342020-01-14 10:27:18 -08003015 }
3016 `)
3017
3018 testCases := []struct {
3019 moduleName string
3020 variantName string
3021 expectedFlags []string
3022 }{
3023 {
3024 moduleName: "foo_test",
3025 variantName: "android_common",
3026 },
3027 {
3028 moduleName: "bar_test",
3029 variantName: "android_common",
3030 expectedFlags: []string{
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00003031 "--manifest out/soong/.intermediates/bar_test/android_common/manifest_fixer/AndroidManifest.xml",
Jaewoong Jung39982342020-01-14 10:27:18 -08003032 "--package-name com.android.bar.test",
Seth Moorec6f4b532023-02-02 13:22:26 -08003033 "--mainline-package-name com.android.bar",
Jaewoong Jung39982342020-01-14 10:27:18 -08003034 },
3035 },
3036 {
3037 moduleName: "foo_test",
3038 variantName: "android_common_baz_test",
3039 expectedFlags: []string{
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00003040 "--manifest out/soong/.intermediates/foo_test/android_common_baz_test/manifest_fixer/AndroidManifest.xml",
Jaewoong Jung39982342020-01-14 10:27:18 -08003041 "--package-name com.android.baz.test",
3042 "--test-file-name baz_test.apk",
Seth Moorec6f4b532023-02-02 13:22:26 -08003043 "out/soong/.intermediates/foo_test/android_common_baz_test/test_config_fixer/AndroidTest.xml",
3044 "--mainline-package-name com.android.baz",
Jaewoong Jung39982342020-01-14 10:27:18 -08003045 },
3046 },
3047 }
3048
3049 for _, test := range testCases {
3050 variant := ctx.ModuleForTests(test.moduleName, test.variantName)
Paul Duffina71a67a2021-03-29 00:42:57 +01003051 params := variant.MaybeOutput("test_config_fixer/AndroidTest.xml")
Jaewoong Jung39982342020-01-14 10:27:18 -08003052
3053 if len(test.expectedFlags) > 0 {
3054 if params.Rule == nil {
3055 t.Errorf("test_config_fixer was expected to run, but didn't")
3056 } else {
3057 for _, flag := range test.expectedFlags {
3058 if !strings.Contains(params.RuleParams.Command, flag) {
3059 t.Errorf("Flag %q was not found in command: %q", flag, params.RuleParams.Command)
3060 }
3061 }
3062 }
3063 } else {
3064 if params.Rule != nil {
3065 t.Errorf("test_config_fixer was not expected to run, but did: %q", params.RuleParams.Command)
3066 }
3067 }
Jaewoong Jung39982342020-01-14 10:27:18 -08003068 }
3069}
3070
Paul Duffin53a70a42022-01-11 14:35:55 +00003071func TestInstrumentationTargetPrebuilt(t *testing.T) {
3072 bp := `
3073 android_app_import {
3074 name: "foo",
3075 apk: "foo.apk",
3076 presigned: true,
3077 }
3078
3079 android_test {
3080 name: "bar",
3081 srcs: ["a.java"],
3082 instrumentation_for: "foo",
3083 sdk_version: "current",
3084 }
3085 `
3086
3087 android.GroupFixturePreparers(
3088 PrepareForTestWithJavaDefaultModules,
3089 ).ExtendWithErrorHandler(
3090 android.FixtureExpectsAtLeastOneErrorMatchingPattern(
3091 "instrumentation_for: dependency \"foo\" of type \"android_app_import\" does not provide JavaInfo so is unsuitable for use with this property")).
3092 RunTestWithBp(t, bp)
3093}
3094
Jaewoong Jungbc625cd2019-05-06 15:48:44 -07003095func TestStl(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07003096 ctx, _ := testJava(t, cc.GatherRequiredDepsForTest(android.Android)+`
Jaewoong Jungbc625cd2019-05-06 15:48:44 -07003097 cc_library {
3098 name: "libjni",
Peter Collingbournead84f972019-12-17 16:46:18 -08003099 sdk_version: "current",
3100 stl: "c++_shared",
Jaewoong Jungbc625cd2019-05-06 15:48:44 -07003101 }
3102
3103 android_test {
3104 name: "stl",
3105 jni_libs: ["libjni"],
3106 compile_multilib: "both",
3107 sdk_version: "current",
3108 stl: "c++_shared",
3109 }
3110
3111 android_test {
3112 name: "system",
3113 jni_libs: ["libjni"],
3114 compile_multilib: "both",
3115 sdk_version: "current",
3116 }
3117 `)
3118
3119 testCases := []struct {
3120 name string
3121 jnis []string
3122 }{
3123 {"stl",
3124 []string{
3125 "libjni.so",
Jaewoong Jung710756a2019-06-04 11:53:47 -07003126 "libc++_shared.so",
Jaewoong Jungbc625cd2019-05-06 15:48:44 -07003127 },
3128 },
3129 {"system",
3130 []string{
3131 "libjni.so",
3132 },
3133 },
3134 }
3135
3136 for _, test := range testCases {
3137 t.Run(test.name, func(t *testing.T) {
3138 app := ctx.ModuleForTests(test.name, "android_common")
Colin Crossb3168ba2023-07-26 16:14:56 -07003139 jniLibZip := app.Output("jnilibs.zip")
Jaewoong Jungbc625cd2019-05-06 15:48:44 -07003140 var jnis []string
3141 args := strings.Fields(jniLibZip.Args["jarArgs"])
3142 for i := 0; i < len(args); i++ {
3143 if args[i] == "-f" {
3144 jnis = append(jnis, args[i+1])
3145 i += 1
3146 }
3147 }
3148 jnisJoined := strings.Join(jnis, " ")
3149 for _, jni := range test.jnis {
3150 if !strings.Contains(jnisJoined, jni) {
3151 t.Errorf("missing jni %q in %q", jni, jnis)
3152 }
3153 }
3154 })
3155 }
3156}
Colin Cross50ddcc42019-05-16 12:28:22 -07003157
3158func TestUsesLibraries(t *testing.T) {
3159 bp := `
3160 java_sdk_library {
3161 name: "foo",
3162 srcs: ["a.java"],
3163 api_packages: ["foo"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09003164 sdk_version: "current",
Colin Cross50ddcc42019-05-16 12:28:22 -07003165 }
3166
3167 java_sdk_library {
Paul Duffin859fe962020-05-15 10:20:31 +01003168 name: "qux",
3169 srcs: ["a.java"],
3170 api_packages: ["qux"],
3171 sdk_version: "current",
3172 }
3173
3174 java_sdk_library {
3175 name: "quuz",
3176 srcs: ["a.java"],
3177 api_packages: ["quuz"],
3178 sdk_version: "current",
3179 }
3180
3181 java_sdk_library {
Ulya Trafimovich65b03192020-12-03 16:50:22 +00003182 name: "fred",
3183 srcs: ["a.java"],
3184 api_packages: ["fred"],
3185 sdk_version: "current",
3186 }
3187
3188 java_sdk_library {
Colin Cross50ddcc42019-05-16 12:28:22 -07003189 name: "bar",
3190 srcs: ["a.java"],
3191 api_packages: ["bar"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09003192 sdk_version: "current",
Colin Cross50ddcc42019-05-16 12:28:22 -07003193 }
3194
Ulya Trafimovich4b6d4c12020-08-19 14:58:01 +01003195 java_sdk_library {
3196 name: "runtime-library",
3197 srcs: ["a.java"],
3198 sdk_version: "current",
3199 }
3200
3201 java_library {
3202 name: "static-runtime-helper",
3203 srcs: ["a.java"],
3204 libs: ["runtime-library"],
3205 sdk_version: "current",
3206 }
3207
Ulya Trafimoviche4432872021-08-18 16:57:11 +01003208 java_library {
3209 name: "runtime-required-x",
3210 srcs: ["a.java"],
3211 installable: true,
3212 sdk_version: "current",
3213 }
3214
3215 java_library {
3216 name: "runtime-optional-x",
3217 srcs: ["a.java"],
3218 installable: true,
3219 sdk_version: "current",
3220 }
3221
3222 android_library {
3223 name: "static-x",
3224 uses_libs: ["runtime-required-x"],
3225 optional_uses_libs: ["runtime-optional-x"],
3226 sdk_version: "current",
3227 }
3228
3229 java_library {
3230 name: "runtime-required-y",
3231 srcs: ["a.java"],
3232 installable: true,
3233 sdk_version: "current",
3234 }
3235
3236 java_library {
3237 name: "runtime-optional-y",
3238 srcs: ["a.java"],
3239 installable: true,
3240 sdk_version: "current",
3241 }
3242
3243 java_library {
3244 name: "static-y",
3245 srcs: ["a.java"],
3246 uses_libs: ["runtime-required-y"],
Jiakai Zhang36937082024-04-15 11:15:50 +00003247 optional_uses_libs: [
3248 "runtime-optional-y",
3249 "missing-lib-a",
3250 ],
Ulya Trafimoviche4432872021-08-18 16:57:11 +01003251 sdk_version: "current",
3252 }
3253
Ulya Trafimovich861a8962021-02-26 14:49:07 +00003254 // A library that has to use "provides_uses_lib", because:
3255 // - it is not an SDK library
3256 // - its library name is different from its module name
3257 java_library {
3258 name: "non-sdk-lib",
3259 provides_uses_lib: "com.non.sdk.lib",
3260 installable: true,
3261 srcs: ["a.java"],
3262 }
3263
Colin Cross50ddcc42019-05-16 12:28:22 -07003264 android_app {
3265 name: "app",
3266 srcs: ["a.java"],
Ulya Trafimovich861a8962021-02-26 14:49:07 +00003267 libs: [
3268 "qux",
3269 "quuz.stubs"
3270 ],
Ulya Trafimovich65b03192020-12-03 16:50:22 +00003271 static_libs: [
3272 "static-runtime-helper",
3273 // statically linked component libraries should not pull their SDK libraries,
3274 // so "fred" should not be added to class loader context
3275 "fred.stubs",
Ulya Trafimoviche4432872021-08-18 16:57:11 +01003276 "static-x",
3277 "static-y",
Ulya Trafimovich65b03192020-12-03 16:50:22 +00003278 ],
Ulya Trafimovich861a8962021-02-26 14:49:07 +00003279 uses_libs: [
3280 "foo",
3281 "non-sdk-lib"
3282 ],
Jeongik Cha538c0d02019-07-11 15:54:27 +09003283 sdk_version: "current",
Colin Cross50ddcc42019-05-16 12:28:22 -07003284 optional_uses_libs: [
3285 "bar",
Jiakai Zhangf98da192024-04-15 11:15:41 +00003286 "missing-lib-b",
Colin Cross50ddcc42019-05-16 12:28:22 -07003287 ],
3288 }
3289
3290 android_app_import {
3291 name: "prebuilt",
3292 apk: "prebuilts/apk/app.apk",
3293 certificate: "platform",
Ulya Trafimovich861a8962021-02-26 14:49:07 +00003294 uses_libs: [
3295 "foo",
3296 "non-sdk-lib",
3297 "android.test.runner"
3298 ],
Colin Cross50ddcc42019-05-16 12:28:22 -07003299 optional_uses_libs: [
3300 "bar",
Jiakai Zhangf98da192024-04-15 11:15:41 +00003301 "missing-lib-b",
Colin Cross50ddcc42019-05-16 12:28:22 -07003302 ],
3303 }
3304 `
3305
Paul Duffin71ae5942021-03-22 15:36:52 +00003306 result := android.GroupFixturePreparers(
3307 prepareForJavaTest,
Paul Duffin2645a292021-03-13 02:36:00 +00003308 PrepareForTestWithJavaSdkLibraryFiles,
3309 FixtureWithLastReleaseApis("runtime-library", "foo", "quuz", "qux", "bar", "fred"),
Jiakai Zhang48203e32023-06-02 23:42:21 +01003310 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
3311 variables.BuildWarningBadOptionalUsesLibsAllowlist = []string{"app", "prebuilt"}
3312 }),
Paul Duffind234b412021-03-12 23:04:46 +00003313 ).RunTestWithBp(t, bp)
Colin Cross50ddcc42019-05-16 12:28:22 -07003314
Paul Duffind234b412021-03-12 23:04:46 +00003315 app := result.ModuleForTests("app", "android_common")
3316 prebuilt := result.ModuleForTests("prebuilt", "android_common")
Colin Cross50ddcc42019-05-16 12:28:22 -07003317
Paul Duffin859fe962020-05-15 10:20:31 +01003318 // Test that implicit dependencies on java_sdk_library instances are passed to the manifest.
Ulya Trafimovichf5d91bb2022-05-04 12:00:02 +01003319 // These also include explicit `uses_libs`/`optional_uses_libs` entries, as they may be
3320 // propagated from dependencies.
Ulya Trafimovich2eaf5c52021-02-26 12:05:11 +00003321 actualManifestFixerArgs := app.Output("manifest_fixer/AndroidManifest.xml").Args["args"]
Jiyong Parkd044bb42024-05-15 02:09:54 +09003322 expectManifestFixerArgs := `--extract-native-libs=true ` +
3323 `--uses-library foo ` +
Ulya Trafimovichf5d91bb2022-05-04 12:00:02 +01003324 `--uses-library com.non.sdk.lib ` +
Jiakai Zhangf98da192024-04-15 11:15:41 +00003325 `--uses-library qux ` +
3326 `--uses-library quuz ` +
Ulya Trafimovichf5d91bb2022-05-04 12:00:02 +01003327 `--uses-library runtime-library ` +
3328 `--uses-library runtime-required-x ` +
3329 `--uses-library runtime-required-y ` +
3330 `--optional-uses-library bar ` +
3331 `--optional-uses-library runtime-optional-x ` +
3332 `--optional-uses-library runtime-optional-y`
Gurpreet Singh75d65f32022-01-24 17:44:05 +00003333 android.AssertStringDoesContain(t, "manifest_fixer args", actualManifestFixerArgs, expectManifestFixerArgs)
Paul Duffin859fe962020-05-15 10:20:31 +01003334
Ulya Trafimovich2eaf5c52021-02-26 12:05:11 +00003335 // Test that all libraries are verified (library order matters).
3336 verifyCmd := app.Rule("verify_uses_libraries").RuleParams.Command
3337 verifyArgs := `--uses-library foo ` +
Ulya Trafimovicheea486a2021-02-26 11:38:21 +00003338 `--uses-library com.non.sdk.lib ` +
Ulya Trafimovich2eaf5c52021-02-26 12:05:11 +00003339 `--uses-library qux ` +
3340 `--uses-library quuz ` +
3341 `--uses-library runtime-library ` +
Ulya Trafimoviche4432872021-08-18 16:57:11 +01003342 `--uses-library runtime-required-x ` +
3343 `--uses-library runtime-required-y ` +
Ulya Trafimovich2eaf5c52021-02-26 12:05:11 +00003344 `--optional-uses-library bar ` +
Ulya Trafimoviche4432872021-08-18 16:57:11 +01003345 `--optional-uses-library runtime-optional-x ` +
Jiakai Zhangf98da192024-04-15 11:15:41 +00003346 `--optional-uses-library runtime-optional-y ` +
Jiakai Zhang36937082024-04-15 11:15:50 +00003347 `--missing-optional-uses-library missing-lib-b ` +
3348 `--missing-optional-uses-library missing-lib-a`
Paul Duffind234b412021-03-12 23:04:46 +00003349 android.AssertStringDoesContain(t, "verify cmd args", verifyCmd, verifyArgs)
Colin Cross50ddcc42019-05-16 12:28:22 -07003350
Ulya Trafimovich2eaf5c52021-02-26 12:05:11 +00003351 // Test that all libraries are verified for an APK (library order matters).
3352 verifyApkCmd := prebuilt.Rule("verify_uses_libraries").RuleParams.Command
Ulya Trafimovich0aba2522021-03-03 16:38:37 +00003353 verifyApkArgs := `--uses-library foo ` +
3354 `--uses-library com.non.sdk.lib ` +
3355 `--uses-library android.test.runner ` +
3356 `--optional-uses-library bar ` +
Jiakai Zhangf98da192024-04-15 11:15:41 +00003357 `--missing-optional-uses-library missing-lib-b `
Paul Duffind234b412021-03-12 23:04:46 +00003358 android.AssertStringDoesContain(t, "verify apk cmd args", verifyApkCmd, verifyApkArgs)
Colin Cross50ddcc42019-05-16 12:28:22 -07003359
Jiakai Zhanga4496782023-05-17 16:57:30 +01003360 // Test that necessary args are passed for constructing CLC in Ninja phase.
Ulya Trafimovich2eaf5c52021-02-26 12:05:11 +00003361 cmd := app.Rule("dexpreopt").RuleParams.Command
Jiakai Zhanga4496782023-05-17 16:57:30 +01003362 android.AssertStringDoesContain(t, "dexpreopt app cmd context", cmd, "--context-json=")
3363 android.AssertStringDoesContain(t, "dexpreopt app cmd product_packages", cmd,
Spandan Das2069c3f2023-12-06 19:40:24 +00003364 "--product-packages=out/soong/.intermediates/app/android_common/dexpreopt/app/product_packages.txt")
Colin Cross50ddcc42019-05-16 12:28:22 -07003365}
Jaewoong Jungc27ab662019-05-30 15:51:14 -07003366
Ulya Trafimovich9023b022021-03-22 16:02:28 +00003367func TestDexpreoptBcp(t *testing.T) {
3368 bp := `
3369 java_sdk_library {
3370 name: "foo",
3371 srcs: ["a.java"],
3372 api_packages: ["foo"],
3373 sdk_version: "current",
3374 }
3375
3376 java_sdk_library {
3377 name: "bar",
3378 srcs: ["a.java"],
3379 api_packages: ["bar"],
3380 permitted_packages: ["bar"],
3381 sdk_version: "current",
3382 }
3383
3384 android_app {
3385 name: "app",
3386 srcs: ["a.java"],
3387 sdk_version: "current",
3388 }
3389 `
3390
3391 testCases := []struct {
3392 name string
3393 with bool
3394 expect string
3395 }{
3396 {
3397 name: "with updatable bcp",
3398 with: true,
3399 expect: "/system/framework/foo.jar:/system/framework/bar.jar",
3400 },
3401 {
3402 name: "without updatable bcp",
3403 with: false,
3404 expect: "/system/framework/foo.jar",
3405 },
3406 }
3407
3408 for _, test := range testCases {
3409 t.Run(test.name, func(t *testing.T) {
3410 result := android.GroupFixturePreparers(
3411 prepareForJavaTest,
3412 PrepareForTestWithJavaSdkLibraryFiles,
3413 FixtureWithLastReleaseApis("runtime-library", "foo", "bar"),
3414 dexpreopt.FixtureSetBootJars("platform:foo"),
satayevd604b212021-07-21 14:23:52 +01003415 dexpreopt.FixtureSetApexBootJars("platform:bar"),
Ulya Trafimovich9023b022021-03-22 16:02:28 +00003416 dexpreopt.FixtureSetPreoptWithUpdatableBcp(test.with),
3417 ).RunTestWithBp(t, bp)
3418
3419 app := result.ModuleForTests("app", "android_common")
3420 cmd := app.Rule("dexpreopt").RuleParams.Command
3421 bcp := " -Xbootclasspath-locations:" + test.expect + " " // space at the end matters
3422 android.AssertStringDoesContain(t, "dexpreopt app bcp", cmd, bcp)
3423 })
3424 }
3425}
3426
Jaewoong Jungc27ab662019-05-30 15:51:14 -07003427func TestCodelessApp(t *testing.T) {
3428 testCases := []struct {
3429 name string
3430 bp string
3431 noCode bool
3432 }{
3433 {
3434 name: "normal",
3435 bp: `
3436 android_app {
3437 name: "foo",
3438 srcs: ["a.java"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09003439 sdk_version: "current",
Jaewoong Jungc27ab662019-05-30 15:51:14 -07003440 }
3441 `,
3442 noCode: false,
3443 },
3444 {
3445 name: "app without sources",
3446 bp: `
3447 android_app {
3448 name: "foo",
Jeongik Cha538c0d02019-07-11 15:54:27 +09003449 sdk_version: "current",
Jaewoong Jungc27ab662019-05-30 15:51:14 -07003450 }
3451 `,
3452 noCode: true,
3453 },
3454 {
3455 name: "app with libraries",
3456 bp: `
3457 android_app {
3458 name: "foo",
3459 static_libs: ["lib"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09003460 sdk_version: "current",
Jaewoong Jungc27ab662019-05-30 15:51:14 -07003461 }
3462
3463 java_library {
3464 name: "lib",
3465 srcs: ["a.java"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09003466 sdk_version: "current",
Jaewoong Jungc27ab662019-05-30 15:51:14 -07003467 }
3468 `,
3469 noCode: false,
3470 },
3471 {
3472 name: "app with sourceless libraries",
3473 bp: `
3474 android_app {
3475 name: "foo",
3476 static_libs: ["lib"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09003477 sdk_version: "current",
Jaewoong Jungc27ab662019-05-30 15:51:14 -07003478 }
3479
3480 java_library {
3481 name: "lib",
Jeongik Cha538c0d02019-07-11 15:54:27 +09003482 sdk_version: "current",
Jaewoong Jungc27ab662019-05-30 15:51:14 -07003483 }
3484 `,
3485 // TODO(jungjw): this should probably be true
3486 noCode: false,
3487 },
3488 }
3489
3490 for _, test := range testCases {
3491 t.Run(test.name, func(t *testing.T) {
3492 ctx := testApp(t, test.bp)
3493
3494 foo := ctx.ModuleForTests("foo", "android_common")
3495 manifestFixerArgs := foo.Output("manifest_fixer/AndroidManifest.xml").Args["args"]
3496 if strings.Contains(manifestFixerArgs, "--has-no-code") != test.noCode {
3497 t.Errorf("unexpected manifest_fixer args: %q", manifestFixerArgs)
3498 }
3499 })
3500 }
3501}
Jaewoong Jung5b425e22019-06-17 17:40:56 -07003502
Colin Cross53a87f52019-06-25 13:35:30 -07003503func TestUncompressDex(t *testing.T) {
3504 testCases := []struct {
3505 name string
3506 bp string
3507
3508 uncompressedPlatform bool
3509 uncompressedUnbundled bool
3510 }{
3511 {
3512 name: "normal",
3513 bp: `
3514 android_app {
3515 name: "foo",
3516 srcs: ["a.java"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09003517 sdk_version: "current",
Colin Cross53a87f52019-06-25 13:35:30 -07003518 }
3519 `,
3520 uncompressedPlatform: true,
3521 uncompressedUnbundled: false,
3522 },
3523 {
3524 name: "use_embedded_dex",
3525 bp: `
3526 android_app {
3527 name: "foo",
3528 use_embedded_dex: true,
3529 srcs: ["a.java"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09003530 sdk_version: "current",
Colin Cross53a87f52019-06-25 13:35:30 -07003531 }
3532 `,
3533 uncompressedPlatform: true,
3534 uncompressedUnbundled: true,
3535 },
3536 {
3537 name: "privileged",
3538 bp: `
3539 android_app {
3540 name: "foo",
3541 privileged: true,
3542 srcs: ["a.java"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09003543 sdk_version: "current",
Colin Cross53a87f52019-06-25 13:35:30 -07003544 }
3545 `,
3546 uncompressedPlatform: true,
3547 uncompressedUnbundled: true,
3548 },
David Srbeckye033cba2020-05-20 22:20:28 +01003549 {
3550 name: "normal_uncompress_dex_true",
3551 bp: `
3552 android_app {
3553 name: "foo",
3554 srcs: ["a.java"],
3555 sdk_version: "current",
3556 uncompress_dex: true,
3557 }
3558 `,
3559 uncompressedPlatform: true,
3560 uncompressedUnbundled: true,
3561 },
3562 {
3563 name: "normal_uncompress_dex_false",
3564 bp: `
3565 android_app {
3566 name: "foo",
3567 srcs: ["a.java"],
3568 sdk_version: "current",
3569 uncompress_dex: false,
3570 }
3571 `,
3572 uncompressedPlatform: false,
3573 uncompressedUnbundled: false,
3574 },
Colin Cross53a87f52019-06-25 13:35:30 -07003575 }
3576
3577 test := func(t *testing.T, bp string, want bool, unbundled bool) {
3578 t.Helper()
3579
Paul Duffin71ae5942021-03-22 15:36:52 +00003580 result := android.GroupFixturePreparers(
3581 prepareForJavaTest,
Paul Duffin2645a292021-03-13 02:36:00 +00003582 PrepareForTestWithPrebuiltsOfCurrentApi,
Paul Duffincdb88a92021-03-14 00:36:50 +00003583 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
3584 if unbundled {
3585 variables.Unbundled_build = proptools.BoolPtr(true)
3586 variables.Always_use_prebuilt_sdks = proptools.BoolPtr(true)
3587 }
3588 }),
3589 ).RunTestWithBp(t, bp)
Colin Cross53a87f52019-06-25 13:35:30 -07003590
Paul Duffincdb88a92021-03-14 00:36:50 +00003591 foo := result.ModuleForTests("foo", "android_common")
Colin Cross53a87f52019-06-25 13:35:30 -07003592 dex := foo.Rule("r8")
3593 uncompressedInDexJar := strings.Contains(dex.Args["zipFlags"], "-L 0")
3594 aligned := foo.MaybeRule("zipalign").Rule != nil
3595
Paul Duffincdb88a92021-03-14 00:36:50 +00003596 android.AssertBoolEquals(t, "uncompressed in dex", want, uncompressedInDexJar)
Colin Cross53a87f52019-06-25 13:35:30 -07003597
Paul Duffincdb88a92021-03-14 00:36:50 +00003598 android.AssertBoolEquals(t, "aligne", want, aligned)
Colin Cross53a87f52019-06-25 13:35:30 -07003599 }
3600
3601 for _, tt := range testCases {
3602 t.Run(tt.name, func(t *testing.T) {
3603 t.Run("platform", func(t *testing.T) {
3604 test(t, tt.bp, tt.uncompressedPlatform, false)
3605 })
3606 t.Run("unbundled", func(t *testing.T) {
3607 test(t, tt.bp, tt.uncompressedUnbundled, true)
3608 })
3609 })
3610 }
3611}
Jaewoong Jung26dedd32019-06-06 08:45:58 -07003612
3613func checkAapt2LinkFlag(t *testing.T, aapt2Flags, flagName, expectedValue string) {
3614 if expectedValue != "" {
3615 expectedFlag := "--" + flagName + " " + expectedValue
3616 if !strings.Contains(aapt2Flags, expectedFlag) {
3617 t.Errorf("%q is missing in aapt2 link flags, %q", expectedFlag, aapt2Flags)
3618 }
3619 } else {
3620 unexpectedFlag := "--" + flagName
3621 if strings.Contains(aapt2Flags, unexpectedFlag) {
3622 t.Errorf("unexpected flag, %q is found in aapt2 link flags, %q", unexpectedFlag, aapt2Flags)
3623 }
3624 }
3625}
Jaewoong Jung9befb0c2020-01-18 10:33:43 -08003626
Cole Faust9a631312020-10-22 21:05:24 +00003627func TestExportedProguardFlagFiles(t *testing.T) {
3628 ctx, _ := testJava(t, `
3629 android_app {
3630 name: "foo",
3631 sdk_version: "current",
Colin Crosscde55342024-03-27 14:11:51 -07003632 static_libs: [
3633 "lib1",
3634 "lib3",
3635 ],
Cole Faust9a631312020-10-22 21:05:24 +00003636 }
3637
3638 android_library {
3639 name: "lib1",
3640 sdk_version: "current",
3641 optimize: {
3642 proguard_flags_files: ["lib1proguard.cfg"],
Colin Crosscde55342024-03-27 14:11:51 -07003643 },
3644 static_libs: ["lib2"],
3645 }
3646
3647 android_library {
3648 name: "lib2",
3649 sdk_version: "current",
3650 optimize: {
3651 proguard_flags_files: ["lib2proguard.cfg"],
Cole Faust9a631312020-10-22 21:05:24 +00003652 }
3653 }
Colin Crosscde55342024-03-27 14:11:51 -07003654
3655 android_library_import {
3656 name: "lib3",
3657 sdk_version: "current",
3658 aars: ["lib3.aar"],
3659 static_libs: ["lib4"],
3660 }
3661
3662 android_library {
3663 name: "lib4",
3664 sdk_version: "current",
3665 optimize: {
3666 proguard_flags_files: ["lib4proguard.cfg"],
3667 }
3668 }
3669
3670
Cole Faust9a631312020-10-22 21:05:24 +00003671 `)
3672
3673 m := ctx.ModuleForTests("foo", "android_common")
Colin Crosscde55342024-03-27 14:11:51 -07003674 r8 := m.Rule("java.r8")
3675 implicits := r8.Implicits.RelativeToTop().Strings()
3676 android.AssertStringListContains(t, "r8 implicits", implicits, "lib1proguard.cfg")
3677 android.AssertStringListContains(t, "r8 implicits", implicits, "lib2proguard.cfg")
3678 android.AssertStringListContains(t, "r8 implicits", implicits, "lib4proguard.cfg")
3679 android.AssertStringListContains(t, "r8 implicits", implicits, "out/soong/.intermediates/lib3/android_common/aar/proguard.txt")
Cole Faust9a631312020-10-22 21:05:24 +00003680
Colin Crosscde55342024-03-27 14:11:51 -07003681 flags := r8.Args["r8Flags"]
3682 android.AssertStringDoesContain(t, "r8 flags", flags, "-include lib1proguard.cfg")
3683 android.AssertStringDoesContain(t, "r8 flags", flags, "-include lib2proguard.cfg")
3684 android.AssertStringDoesContain(t, "r8 flags", flags, "-include lib4proguard.cfg")
3685 android.AssertStringDoesContain(t, "r8 flags", flags, "-include out/soong/.intermediates/lib3/android_common/aar/proguard.txt")
Cole Faust9a631312020-10-22 21:05:24 +00003686}
Spandan Dasc8cb0c32021-12-08 17:00:38 +00003687
3688func TestTargetSdkVersionManifestFixer(t *testing.T) {
3689 platform_sdk_codename := "Tiramisu"
Spandan Dasfb6a1ee2023-04-27 16:08:26 +00003690 platform_sdk_version := 33
Spandan Dasc8cb0c32021-12-08 17:00:38 +00003691 testCases := []struct {
3692 name string
3693 targetSdkVersionInBp string
3694 targetSdkVersionExpected string
3695 unbundledBuild bool
Spandan Dasfb6a1ee2023-04-27 16:08:26 +00003696 platformSdkFinal bool
Spandan Dasc8cb0c32021-12-08 17:00:38 +00003697 }{
3698 {
3699 name: "Non-Unbundled build: Android.bp has targetSdkVersion",
3700 targetSdkVersionInBp: "30",
3701 targetSdkVersionExpected: "30",
3702 unbundledBuild: false,
3703 },
3704 {
3705 name: "Unbundled build: Android.bp has targetSdkVersion",
3706 targetSdkVersionInBp: "30",
3707 targetSdkVersionExpected: "30",
3708 unbundledBuild: true,
3709 },
3710 {
3711 name: "Non-Unbundled build: Android.bp has targetSdkVersion equal to platform_sdk_codename",
3712 targetSdkVersionInBp: platform_sdk_codename,
3713 targetSdkVersionExpected: platform_sdk_codename,
3714 unbundledBuild: false,
3715 },
3716 {
3717 name: "Unbundled build: Android.bp has targetSdkVersion equal to platform_sdk_codename",
3718 targetSdkVersionInBp: platform_sdk_codename,
3719 targetSdkVersionExpected: "10000",
3720 unbundledBuild: true,
3721 },
3722
3723 {
3724 name: "Non-Unbundled build: Android.bp has no targetSdkVersion",
3725 targetSdkVersionExpected: platform_sdk_codename,
3726 unbundledBuild: false,
3727 },
3728 {
3729 name: "Unbundled build: Android.bp has no targetSdkVersion",
3730 targetSdkVersionExpected: "10000",
3731 unbundledBuild: true,
3732 },
Spandan Dasfb6a1ee2023-04-27 16:08:26 +00003733 {
3734 name: "Bundled build in REL branches",
3735 targetSdkVersionExpected: "33",
3736 unbundledBuild: false,
3737 platformSdkFinal: true,
3738 },
Spandan Dasc8cb0c32021-12-08 17:00:38 +00003739 }
3740 for _, testCase := range testCases {
Spandan Dasca70fc42023-03-01 23:38:49 +00003741 targetSdkVersionTemplate := ""
3742 if testCase.targetSdkVersionInBp != "" {
3743 targetSdkVersionTemplate = fmt.Sprintf(`target_sdk_version: "%s",`, testCase.targetSdkVersionInBp)
3744 }
Spandan Dasc8cb0c32021-12-08 17:00:38 +00003745 bp := fmt.Sprintf(`
3746 android_app {
3747 name: "foo",
3748 sdk_version: "current",
Spandan Dasca70fc42023-03-01 23:38:49 +00003749 %s
Spandan Dasc8cb0c32021-12-08 17:00:38 +00003750 }
Spandan Dasca70fc42023-03-01 23:38:49 +00003751 `, targetSdkVersionTemplate)
Spandan Dasc8cb0c32021-12-08 17:00:38 +00003752 fixture := android.GroupFixturePreparers(
3753 prepareForJavaTest,
3754 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
Spandan Dasfb6a1ee2023-04-27 16:08:26 +00003755 if testCase.platformSdkFinal {
3756 variables.Platform_sdk_final = proptools.BoolPtr(true)
3757 }
Spandan Dasc8cb0c32021-12-08 17:00:38 +00003758 // explicitly set platform_sdk_codename to make the test deterministic
3759 variables.Platform_sdk_codename = &platform_sdk_codename
Spandan Dasfb6a1ee2023-04-27 16:08:26 +00003760 variables.Platform_sdk_version = &platform_sdk_version
Spandan Dasc8cb0c32021-12-08 17:00:38 +00003761 variables.Platform_version_active_codenames = []string{platform_sdk_codename}
3762 // create a non-empty list if unbundledBuild==true
3763 if testCase.unbundledBuild {
3764 variables.Unbundled_build_apps = []string{"apex_a", "apex_b"}
3765 }
3766 }),
3767 )
3768
3769 result := fixture.RunTestWithBp(t, bp)
3770 foo := result.ModuleForTests("foo", "android_common")
3771
Gurpreet Singh75d65f32022-01-24 17:44:05 +00003772 manifestFixerArgs := foo.Output("manifest_fixer/AndroidManifest.xml").Args["args"]
3773 android.AssertStringDoesContain(t, testCase.name, manifestFixerArgs, "--targetSdkVersion "+testCase.targetSdkVersionExpected)
Spandan Dasc8cb0c32021-12-08 17:00:38 +00003774 }
3775}
Colin Cross412436f2022-04-07 17:40:07 -07003776
Harshit Mahajan5b8b7302022-06-10 11:24:05 +00003777func TestDefaultAppTargetSdkVersionForUpdatableModules(t *testing.T) {
3778 platform_sdk_codename := "Tiramisu"
3779 platform_sdk_version := 33
3780 testCases := []struct {
3781 name string
3782 platform_sdk_final bool
3783 targetSdkVersionInBp *string
3784 targetSdkVersionExpected *string
3785 updatable bool
3786 }{
3787 {
3788 name: "Non-Updatable Module: Android.bp has older targetSdkVersion",
3789 targetSdkVersionInBp: proptools.StringPtr("29"),
3790 targetSdkVersionExpected: proptools.StringPtr("29"),
3791 updatable: false,
3792 },
3793 {
3794 name: "Updatable Module: Android.bp has older targetSdkVersion",
3795 targetSdkVersionInBp: proptools.StringPtr("30"),
3796 targetSdkVersionExpected: proptools.StringPtr("30"),
3797 updatable: true,
3798 },
3799 {
3800 name: "Updatable Module: Android.bp has no targetSdkVersion",
3801 targetSdkVersionExpected: proptools.StringPtr("10000"),
3802 updatable: true,
3803 },
3804 {
3805 name: "[SDK finalised] Non-Updatable Module: Android.bp has older targetSdkVersion",
3806 platform_sdk_final: true,
3807 targetSdkVersionInBp: proptools.StringPtr("30"),
3808 targetSdkVersionExpected: proptools.StringPtr("30"),
3809 updatable: false,
3810 },
3811 {
3812 name: "[SDK finalised] Updatable Module: Android.bp has older targetSdkVersion",
3813 platform_sdk_final: true,
3814 targetSdkVersionInBp: proptools.StringPtr("30"),
3815 targetSdkVersionExpected: proptools.StringPtr("30"),
3816 updatable: true,
3817 },
3818 {
3819 name: "[SDK finalised] Updatable Module: Android.bp has targetSdkVersion as platform sdk codename",
3820 platform_sdk_final: true,
3821 targetSdkVersionInBp: proptools.StringPtr(platform_sdk_codename),
3822 targetSdkVersionExpected: proptools.StringPtr("33"),
3823 updatable: true,
3824 },
3825 {
3826 name: "[SDK finalised] Updatable Module: Android.bp has no targetSdkVersion",
3827 platform_sdk_final: true,
3828 targetSdkVersionExpected: proptools.StringPtr("33"),
3829 updatable: true,
3830 },
3831 }
3832 for _, testCase := range testCases {
Spandan Dasca70fc42023-03-01 23:38:49 +00003833 targetSdkVersionTemplate := ""
3834 if testCase.targetSdkVersionInBp != nil {
3835 targetSdkVersionTemplate = fmt.Sprintf(`target_sdk_version: "%s",`, *testCase.targetSdkVersionInBp)
3836 }
Harshit Mahajan5b8b7302022-06-10 11:24:05 +00003837 bp := fmt.Sprintf(`
3838 android_app {
3839 name: "foo",
3840 sdk_version: "current",
3841 min_sdk_version: "29",
Spandan Dasca70fc42023-03-01 23:38:49 +00003842 %s
Harshit Mahajan5b8b7302022-06-10 11:24:05 +00003843 updatable: %t,
3844 enforce_default_target_sdk_version: %t
3845 }
Spandan Dasca70fc42023-03-01 23:38:49 +00003846 `, targetSdkVersionTemplate, testCase.updatable, testCase.updatable) // enforce default target sdk version if app is updatable
Harshit Mahajan5b8b7302022-06-10 11:24:05 +00003847
3848 fixture := android.GroupFixturePreparers(
3849 PrepareForTestWithJavaDefaultModules,
3850 android.PrepareForTestWithAllowMissingDependencies,
3851 android.PrepareForTestWithAndroidMk,
3852 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
3853 // explicitly set following platform variables to make the test deterministic
3854 variables.Platform_sdk_final = &testCase.platform_sdk_final
3855 variables.Platform_sdk_version = &platform_sdk_version
3856 variables.Platform_sdk_codename = &platform_sdk_codename
3857 variables.Platform_version_active_codenames = []string{platform_sdk_codename}
Harshit Mahajan18845d02023-01-11 18:39:12 +00003858 variables.Unbundled_build = proptools.BoolPtr(true)
Harshit Mahajan5b8b7302022-06-10 11:24:05 +00003859 variables.Unbundled_build_apps = []string{"sampleModule"}
3860 }),
3861 )
3862
3863 result := fixture.RunTestWithBp(t, bp)
3864 foo := result.ModuleForTests("foo", "android_common")
3865
3866 manifestFixerArgs := foo.Output("manifest_fixer/AndroidManifest.xml").Args["args"]
3867 android.AssertStringDoesContain(t, testCase.name, manifestFixerArgs, "--targetSdkVersion "+*testCase.targetSdkVersionExpected)
3868 }
3869}
3870
3871func TestEnforceDefaultAppTargetSdkVersionFlag(t *testing.T) {
3872 platform_sdk_codename := "Tiramisu"
3873 platform_sdk_version := 33
3874 testCases := []struct {
3875 name string
3876 enforceDefaultTargetSdkVersion bool
3877 expectedError string
3878 platform_sdk_final bool
3879 targetSdkVersionInBp string
3880 targetSdkVersionExpected string
3881 updatable bool
3882 }{
3883 {
3884 name: "Not enforcing Target SDK Version: Android.bp has older targetSdkVersion",
3885 enforceDefaultTargetSdkVersion: false,
3886 targetSdkVersionInBp: "29",
3887 targetSdkVersionExpected: "29",
3888 updatable: false,
3889 },
3890 {
3891 name: "[SDK finalised] Enforce Target SDK Version: Android.bp has current targetSdkVersion",
3892 enforceDefaultTargetSdkVersion: true,
3893 platform_sdk_final: true,
3894 targetSdkVersionInBp: "current",
3895 targetSdkVersionExpected: "33",
3896 updatable: true,
3897 },
3898 {
Harshit Mahajan8f202ad2023-01-09 20:45:55 +00003899 name: "Enforce Target SDK Version: Android.bp has current targetSdkVersion",
Harshit Mahajan5b8b7302022-06-10 11:24:05 +00003900 enforceDefaultTargetSdkVersion: true,
3901 platform_sdk_final: false,
3902 targetSdkVersionInBp: "current",
3903 targetSdkVersionExpected: "10000",
3904 updatable: false,
3905 },
3906 {
3907 name: "Not enforcing Target SDK Version for Updatable app",
3908 enforceDefaultTargetSdkVersion: false,
3909 expectedError: "Updatable apps must enforce default target sdk version",
3910 targetSdkVersionInBp: "29",
3911 targetSdkVersionExpected: "29",
3912 updatable: true,
3913 },
3914 }
3915 for _, testCase := range testCases {
3916 errExpected := testCase.expectedError != ""
3917 bp := fmt.Sprintf(`
3918 android_app {
3919 name: "foo",
3920 enforce_default_target_sdk_version: %t,
3921 sdk_version: "current",
3922 min_sdk_version: "29",
3923 target_sdk_version: "%v",
3924 updatable: %t
3925 }
3926 `, testCase.enforceDefaultTargetSdkVersion, testCase.targetSdkVersionInBp, testCase.updatable)
3927
3928 fixture := android.GroupFixturePreparers(
3929 PrepareForTestWithJavaDefaultModules,
3930 android.PrepareForTestWithAllowMissingDependencies,
3931 android.PrepareForTestWithAndroidMk,
3932 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
3933 // explicitly set following platform variables to make the test deterministic
3934 variables.Platform_sdk_final = &testCase.platform_sdk_final
3935 variables.Platform_sdk_version = &platform_sdk_version
3936 variables.Platform_sdk_codename = &platform_sdk_codename
Harshit Mahajan18845d02023-01-11 18:39:12 +00003937 variables.Unbundled_build = proptools.BoolPtr(true)
Harshit Mahajan5b8b7302022-06-10 11:24:05 +00003938 variables.Unbundled_build_apps = []string{"sampleModule"}
3939 }),
3940 )
3941
3942 errorHandler := android.FixtureExpectsNoErrors
3943 if errExpected {
3944 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(testCase.expectedError)
3945 }
3946 result := fixture.ExtendWithErrorHandler(errorHandler).RunTestWithBp(t, bp)
3947
3948 if !errExpected {
3949 foo := result.ModuleForTests("foo", "android_common")
3950 manifestFixerArgs := foo.Output("manifest_fixer/AndroidManifest.xml").Args["args"]
3951 android.AssertStringDoesContain(t, testCase.name, manifestFixerArgs, "--targetSdkVersion "+testCase.targetSdkVersionExpected)
3952 }
3953 }
3954}
3955
Harshit Mahajan8f202ad2023-01-09 20:45:55 +00003956func TestEnforceDefaultAppTargetSdkVersionFlagForTests(t *testing.T) {
3957 platform_sdk_codename := "Tiramisu"
3958 platform_sdk_version := 33
3959 testCases := []struct {
3960 name string
3961 enforceDefaultTargetSdkVersion bool
3962 expectedError string
3963 platform_sdk_final bool
3964 targetSdkVersionInBp string
3965 targetSdkVersionExpected string
3966 }{
3967 {
3968 name: "Not enforcing Target SDK Version: Android.bp has older targetSdkVersion",
3969 enforceDefaultTargetSdkVersion: false,
3970 targetSdkVersionInBp: "29",
3971 targetSdkVersionExpected: "29",
3972 },
3973 {
3974 name: "[SDK finalised] Enforce Target SDK Version: Android.bp has current targetSdkVersion",
3975 enforceDefaultTargetSdkVersion: true,
3976 platform_sdk_final: true,
3977 targetSdkVersionInBp: "current",
3978 targetSdkVersionExpected: "33",
3979 },
3980 {
3981 name: "Enforce Target SDK Version: Android.bp has current targetSdkVersion",
3982 enforceDefaultTargetSdkVersion: true,
3983 platform_sdk_final: false,
3984 targetSdkVersionInBp: "current",
3985 targetSdkVersionExpected: "10000",
3986 },
3987 }
3988 for _, testCase := range testCases {
3989 errExpected := testCase.expectedError != ""
3990 bp := fmt.Sprintf(`
3991 android_test {
3992 name: "foo",
3993 enforce_default_target_sdk_version: %t,
3994 min_sdk_version: "29",
3995 target_sdk_version: "%v",
3996 }
3997 `, testCase.enforceDefaultTargetSdkVersion, testCase.targetSdkVersionInBp)
3998
3999 fixture := android.GroupFixturePreparers(
4000 PrepareForTestWithJavaDefaultModules,
4001 android.PrepareForTestWithAllowMissingDependencies,
4002 android.PrepareForTestWithAndroidMk,
4003 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
4004 // explicitly set following platform variables to make the test deterministic
4005 variables.Platform_sdk_final = &testCase.platform_sdk_final
4006 variables.Platform_sdk_version = &platform_sdk_version
4007 variables.Platform_sdk_codename = &platform_sdk_codename
Harshit Mahajan18845d02023-01-11 18:39:12 +00004008 variables.Unbundled_build = proptools.BoolPtr(true)
Harshit Mahajan8f202ad2023-01-09 20:45:55 +00004009 variables.Unbundled_build_apps = []string{"sampleModule"}
4010 }),
4011 )
4012
4013 errorHandler := android.FixtureExpectsNoErrors
4014 if errExpected {
4015 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(testCase.expectedError)
4016 }
4017 result := fixture.ExtendWithErrorHandler(errorHandler).RunTestWithBp(t, bp)
4018
4019 if !errExpected {
4020 foo := result.ModuleForTests("foo", "android_common")
4021 manifestFixerArgs := foo.Output("manifest_fixer/AndroidManifest.xml").Args["args"]
4022 android.AssertStringDoesContain(t, testCase.name, manifestFixerArgs, "--targetSdkVersion "+testCase.targetSdkVersionExpected)
4023 }
4024 }
4025}
4026
Colin Cross412436f2022-04-07 17:40:07 -07004027func TestAppMissingCertificateAllowMissingDependencies(t *testing.T) {
4028 result := android.GroupFixturePreparers(
4029 PrepareForTestWithJavaDefaultModules,
4030 android.PrepareForTestWithAllowMissingDependencies,
4031 android.PrepareForTestWithAndroidMk,
4032 ).RunTestWithBp(t, `
4033 android_app {
4034 name: "foo",
4035 srcs: ["a.java"],
4036 certificate: ":missing_certificate",
4037 sdk_version: "current",
Zyan Wub7550aa2023-05-18 15:46:31 +08004038 }
4039
4040 android_app {
4041 name: "bar",
4042 srcs: ["a.java"],
4043 certificate: ":missing_certificate",
4044 product_specific: true,
4045 sdk_version: "current",
Colin Cross412436f2022-04-07 17:40:07 -07004046 }`)
4047
4048 foo := result.ModuleForTests("foo", "android_common")
4049 fooApk := foo.Output("foo.apk")
4050 if fooApk.Rule != android.ErrorRule {
4051 t.Fatalf("expected ErrorRule for foo.apk, got %s", fooApk.Rule.String())
4052 }
4053 android.AssertStringDoesContain(t, "expected error rule message", fooApk.Args["error"], "missing dependencies: missing_certificate\n")
4054}
Sam Delmerico82602492022-06-10 17:05:42 +00004055
4056func TestAppIncludesJniPackages(t *testing.T) {
4057 ctx := android.GroupFixturePreparers(
4058 PrepareForTestWithJavaDefaultModules,
4059 ).RunTestWithBp(t, `
4060 android_library_import {
4061 name: "aary-nodeps",
4062 aars: ["aary.aar"],
4063 extract_jni: true,
4064 }
4065
4066 android_library {
4067 name: "aary-lib",
4068 sdk_version: "current",
4069 min_sdk_version: "21",
4070 static_libs: ["aary-nodeps"],
4071 }
4072
4073 android_app {
4074 name: "aary-lib-dep",
4075 sdk_version: "current",
4076 min_sdk_version: "21",
4077 manifest: "AndroidManifest.xml",
4078 static_libs: ["aary-lib"],
4079 use_embedded_native_libs: true,
4080 }
4081
4082 android_app {
4083 name: "aary-import-dep",
4084 sdk_version: "current",
4085 min_sdk_version: "21",
4086 manifest: "AndroidManifest.xml",
4087 static_libs: ["aary-nodeps"],
4088 use_embedded_native_libs: true,
4089 }
4090
4091 android_app {
4092 name: "aary-no-use-embedded",
4093 sdk_version: "current",
4094 min_sdk_version: "21",
4095 manifest: "AndroidManifest.xml",
4096 static_libs: ["aary-nodeps"],
4097 }`)
4098
4099 testCases := []struct {
4100 name string
4101 hasPackage bool
4102 }{
4103 {
4104 name: "aary-import-dep",
4105 hasPackage: true,
4106 },
4107 {
4108 name: "aary-lib-dep",
4109 hasPackage: true,
4110 },
4111 {
4112 name: "aary-no-use-embedded",
Jiyong Parkd044bb42024-05-15 02:09:54 +09004113 hasPackage: false,
Sam Delmerico82602492022-06-10 17:05:42 +00004114 },
4115 }
4116
4117 for _, tc := range testCases {
4118 t.Run(tc.name, func(t *testing.T) {
4119 app := ctx.ModuleForTests(tc.name, "android_common")
4120
4121 outputFile := "jnilibs.zip"
4122 jniOutputLibZip := app.MaybeOutput(outputFile)
4123 if jniOutputLibZip.Rule == nil && !tc.hasPackage {
4124 return
4125 }
4126
4127 jniPackage := "arm64-v8a_jni.zip"
4128 inputs := jniOutputLibZip.Inputs
4129 foundPackage := false
4130 for i := 0; i < len(inputs); i++ {
4131 if strings.Contains(inputs[i].String(), jniPackage) {
4132 foundPackage = true
4133 }
4134 }
4135 if foundPackage != tc.hasPackage {
4136 t.Errorf("expected to find %v in %v inputs; inputs = %v", jniPackage, outputFile, inputs)
4137 }
4138 })
4139 }
4140}
Spandan Das9f7ae7f2022-07-25 00:34:18 +00004141
4142func TestTargetSdkVersionMtsTests(t *testing.T) {
4143 platformSdkCodename := "Tiramisu"
4144 android_test := "android_test"
4145 android_test_helper_app := "android_test_helper_app"
4146 bpTemplate := `
4147 %v {
4148 name: "mytest",
4149 target_sdk_version: "%v",
4150 test_suites: ["othersuite", "%v"],
4151 }
4152 `
4153 testCases := []struct {
4154 desc string
4155 moduleType string
4156 targetSdkVersionInBp string
4157 targetSdkVersionExpected string
4158 testSuites string
4159 }{
4160 {
4161 desc: "Non-MTS android_test_apps targeting current should not be upgraded to 10000",
4162 moduleType: android_test,
4163 targetSdkVersionInBp: "current",
4164 targetSdkVersionExpected: platformSdkCodename,
4165 testSuites: "non-mts-suite",
4166 },
4167 {
4168 desc: "MTS android_test_apps targeting released sdks should not be upgraded to 10000",
4169 moduleType: android_test,
4170 targetSdkVersionInBp: "29",
4171 targetSdkVersionExpected: "29",
4172 testSuites: "mts-suite",
4173 },
4174 {
4175 desc: "MTS android_test_apps targeting current should be upgraded to 10000",
4176 moduleType: android_test,
4177 targetSdkVersionInBp: "current",
4178 targetSdkVersionExpected: "10000",
4179 testSuites: "mts-suite",
4180 },
4181 {
4182 desc: "MTS android_test_helper_apps targeting current should be upgraded to 10000",
4183 moduleType: android_test_helper_app,
4184 targetSdkVersionInBp: "current",
4185 targetSdkVersionExpected: "10000",
4186 testSuites: "mts-suite",
4187 },
4188 }
4189 fixture := android.GroupFixturePreparers(
4190 prepareForJavaTest,
4191 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
4192 variables.Platform_sdk_codename = &platformSdkCodename
4193 variables.Platform_version_active_codenames = []string{platformSdkCodename}
4194 }),
4195 )
4196 for _, testCase := range testCases {
4197 result := fixture.RunTestWithBp(t, fmt.Sprintf(bpTemplate, testCase.moduleType, testCase.targetSdkVersionInBp, testCase.testSuites))
4198 mytest := result.ModuleForTests("mytest", "android_common")
4199 manifestFixerArgs := mytest.Output("manifest_fixer/AndroidManifest.xml").Args["args"]
4200 android.AssertStringDoesContain(t, testCase.desc, manifestFixerArgs, "--targetSdkVersion "+testCase.targetSdkVersionExpected)
4201 }
4202}
Andrei Onea580636b2022-08-17 16:53:46 +00004203
4204func TestPrivappAllowlist(t *testing.T) {
4205 testJavaError(t, "privileged must be set in order to use privapp_allowlist", `
4206 android_app {
4207 name: "foo",
4208 srcs: ["a.java"],
4209 privapp_allowlist: "perms.xml",
4210 }
4211 `)
4212
4213 result := PrepareForTestWithJavaDefaultModules.RunTestWithBp(
4214 t,
4215 `
4216 android_app {
4217 name: "foo",
4218 srcs: ["a.java"],
Sam Delmerico15809f82023-05-15 17:21:47 -04004219 privapp_allowlist: "privapp_allowlist_com.android.foo.xml",
Andrei Onea580636b2022-08-17 16:53:46 +00004220 privileged: true,
Andrei Onea580636b2022-08-17 16:53:46 +00004221 sdk_version: "current",
4222 }
4223 override_android_app {
4224 name: "bar",
4225 base: "foo",
4226 package_name: "com.google.android.foo",
4227 }
4228 `,
4229 )
4230 app := result.ModuleForTests("foo", "android_common")
4231 overrideApp := result.ModuleForTests("foo", "android_common_bar")
4232
Sam Delmerico15809f82023-05-15 17:21:47 -04004233 // verify that privapp allowlist is created for override apps
Andrei Onea580636b2022-08-17 16:53:46 +00004234 overrideApp.Output("out/soong/.intermediates/foo/android_common_bar/privapp_allowlist_com.google.android.foo.xml")
Sam Delmerico15809f82023-05-15 17:21:47 -04004235 expectedAllowlistInput := "privapp_allowlist_com.android.foo.xml"
4236 overrideActualAllowlistInput := overrideApp.Rule("modifyAllowlist").Input.String()
4237 if expectedAllowlistInput != overrideActualAllowlistInput {
4238 t.Errorf("expected override allowlist to be %q; got %q", expectedAllowlistInput, overrideActualAllowlistInput)
Andrei Onea580636b2022-08-17 16:53:46 +00004239 }
4240
4241 // verify that permissions are copied to device
Anton Hansson0e486a42023-06-01 16:38:35 +00004242 app.Output("out/soong/target/product/test_device/system/etc/permissions/foo.xml")
4243 overrideApp.Output("out/soong/target/product/test_device/system/etc/permissions/bar.xml")
Andrei Onea580636b2022-08-17 16:53:46 +00004244}
Sam Delmericob1daccd2023-05-25 14:45:30 -04004245
4246func TestPrivappAllowlistAndroidMk(t *testing.T) {
4247 result := android.GroupFixturePreparers(
4248 PrepareForTestWithJavaDefaultModules,
4249 android.PrepareForTestWithAndroidMk,
4250 ).RunTestWithBp(
4251 t,
4252 `
4253 android_app {
4254 name: "foo",
4255 srcs: ["a.java"],
4256 privapp_allowlist: "privapp_allowlist_com.android.foo.xml",
4257 privileged: true,
4258 sdk_version: "current",
4259 }
4260 override_android_app {
4261 name: "bar",
4262 base: "foo",
4263 package_name: "com.google.android.foo",
4264 }
4265 `,
4266 )
4267 baseApp := result.ModuleForTests("foo", "android_common")
4268 overrideApp := result.ModuleForTests("foo", "android_common_bar")
4269
4270 baseAndroidApp := baseApp.Module().(*AndroidApp)
4271 baseEntries := android.AndroidMkEntriesForTest(t, result.TestContext, baseAndroidApp)[0]
4272 android.AssertStringMatches(
4273 t,
4274 "androidmk has incorrect LOCAL_SOONG_INSTALLED_MODULE; expected to find foo.apk",
4275 baseEntries.EntryMap["LOCAL_SOONG_INSTALLED_MODULE"][0],
4276 "\\S+foo.apk",
4277 )
4278 android.AssertStringMatches(
4279 t,
4280 "androidmk has incorrect LOCAL_SOONG_INSTALL_PAIRS; expected to it to include foo.apk",
4281 baseEntries.EntryMap["LOCAL_SOONG_INSTALL_PAIRS"][0],
4282 "\\S+foo.apk",
4283 )
4284 android.AssertStringMatches(
4285 t,
4286 "androidmk has incorrect LOCAL_SOONG_INSTALL_PAIRS; expected to it to include app",
4287 baseEntries.EntryMap["LOCAL_SOONG_INSTALL_PAIRS"][0],
4288 "\\S+foo.apk:\\S+/target/product/test_device/system/priv-app/foo/foo.apk",
4289 )
4290 android.AssertStringMatches(
4291 t,
4292 "androidmk has incorrect LOCAL_SOONG_INSTALL_PAIRS; expected to it to include privapp_allowlist",
4293 baseEntries.EntryMap["LOCAL_SOONG_INSTALL_PAIRS"][0],
Anton Hansson0e486a42023-06-01 16:38:35 +00004294 "privapp_allowlist_com.android.foo.xml:\\S+/target/product/test_device/system/etc/permissions/foo.xml",
Sam Delmericob1daccd2023-05-25 14:45:30 -04004295 )
4296
4297 overrideAndroidApp := overrideApp.Module().(*AndroidApp)
4298 overrideEntries := android.AndroidMkEntriesForTest(t, result.TestContext, overrideAndroidApp)[0]
4299 android.AssertStringMatches(
4300 t,
4301 "androidmk has incorrect LOCAL_SOONG_INSTALLED_MODULE; expected to find bar.apk",
4302 overrideEntries.EntryMap["LOCAL_SOONG_INSTALLED_MODULE"][0],
4303 "\\S+bar.apk",
4304 )
4305 android.AssertStringMatches(
4306 t,
4307 "androidmk has incorrect LOCAL_SOONG_INSTALL_PAIRS; expected to it to include bar.apk",
4308 overrideEntries.EntryMap["LOCAL_SOONG_INSTALL_PAIRS"][0],
4309 "\\S+bar.apk",
4310 )
4311 android.AssertStringMatches(
4312 t,
4313 "androidmk has incorrect LOCAL_SOONG_INSTALL_PAIRS; expected to it to include app",
4314 overrideEntries.EntryMap["LOCAL_SOONG_INSTALL_PAIRS"][0],
4315 "\\S+bar.apk:\\S+/target/product/test_device/system/priv-app/bar/bar.apk",
4316 )
4317 android.AssertStringMatches(
4318 t,
4319 "androidmk has incorrect LOCAL_SOONG_INSTALL_PAIRS; expected to it to include privapp_allowlist",
4320 overrideEntries.EntryMap["LOCAL_SOONG_INSTALL_PAIRS"][0],
Anton Hansson0e486a42023-06-01 16:38:35 +00004321 "\\S+soong/.intermediates/foo/android_common_bar/privapp_allowlist_com.google.android.foo.xml:\\S+/target/product/test_device/system/etc/permissions/bar.xml",
Sam Delmericob1daccd2023-05-25 14:45:30 -04004322 )
4323}
Sam Delmerico0e0d96e2023-08-18 22:43:28 +00004324
Jihoon Kang84b25892023-12-01 22:01:06 +00004325func TestAppFlagsPackages(t *testing.T) {
4326 ctx := testApp(t, `
4327 android_app {
4328 name: "foo",
4329 srcs: ["a.java"],
4330 sdk_version: "current",
4331 flags_packages: [
4332 "bar",
4333 "baz",
4334 ],
4335 }
4336 aconfig_declarations {
4337 name: "bar",
Dennis Shen4e7773d2024-01-05 19:06:50 +00004338 package: "com.example.package.bar",
Yu Liu315a53c2024-04-24 16:41:57 +00004339 container: "com.android.foo",
Jihoon Kang84b25892023-12-01 22:01:06 +00004340 srcs: [
4341 "bar.aconfig",
4342 ],
4343 }
4344 aconfig_declarations {
4345 name: "baz",
Dennis Shen4e7773d2024-01-05 19:06:50 +00004346 package: "com.example.package.baz",
Yu Liu315a53c2024-04-24 16:41:57 +00004347 container: "com.android.foo",
Jihoon Kang84b25892023-12-01 22:01:06 +00004348 srcs: [
4349 "baz.aconfig",
4350 ],
4351 }
4352 `)
4353
4354 foo := ctx.ModuleForTests("foo", "android_common")
4355
4356 // android_app module depends on aconfig_declarations listed in flags_packages
4357 android.AssertBoolEquals(t, "foo expected to depend on bar", true,
4358 CheckModuleHasDependency(t, ctx, "foo", "android_common", "bar"))
4359
4360 android.AssertBoolEquals(t, "foo expected to depend on baz", true,
4361 CheckModuleHasDependency(t, ctx, "foo", "android_common", "baz"))
4362
4363 aapt2LinkRule := foo.Rule("android/soong/java.aapt2Link")
4364 linkInFlags := aapt2LinkRule.Args["inFlags"]
4365 android.AssertStringDoesContain(t,
4366 "aapt2 link command expected to pass feature flags arguments",
4367 linkInFlags,
4368 "--feature-flags @out/soong/.intermediates/bar/intermediate.txt --feature-flags @out/soong/.intermediates/baz/intermediate.txt",
4369 )
4370}
Spandan Das0727ba72024-02-13 16:37:43 +00004371
4372// Test that dexpreopt is disabled if an optional_uses_libs exists, but does not provide an implementation.
4373func TestNoDexpreoptOptionalUsesLibDoesNotHaveImpl(t *testing.T) {
4374 bp := `
4375 java_sdk_library_import {
4376 name: "sdklib_noimpl",
4377 public: {
4378 jars: ["stub.jar"],
4379 },
4380 }
4381 android_app {
4382 name: "app",
4383 srcs: ["a.java"],
4384 sdk_version: "current",
4385 optional_uses_libs: [
4386 "sdklib_noimpl",
4387 ],
4388 }
4389 `
4390 result := prepareForJavaTest.RunTestWithBp(t, bp)
4391 dexpreopt := result.ModuleForTests("app", "android_common").MaybeRule("dexpreopt").Rule
4392 android.AssertBoolEquals(t, "dexpreopt should be disabled if optional_uses_libs does not have an implementation", true, dexpreopt == nil)
4393}
yangbill2af0b6e2024-03-15 09:29:29 +00004394
Ronald Braunsteincdc66f42024-04-12 11:23:19 -07004395func TestTestOnlyApp(t *testing.T) {
4396 t.Parallel()
4397 ctx := android.GroupFixturePreparers(
4398 prepareForJavaTest,
4399 ).RunTestWithBp(t, `
4400 // These should be test-only
4401 android_test {
4402 name: "android-test",
4403 }
4404 android_test_helper_app {
4405 name: "helper-app",
4406 }
4407 override_android_test {
4408 name: "override-test",
4409 base: "android-app",
4410 }
4411 // And these should not be
4412 android_app {
4413 name: "android-app",
4414 srcs: ["b.java"],
4415 sdk_version: "current",
4416 }
4417 `)
4418
4419 expectedTestOnly := []string{
4420 "android-test",
4421 "helper-app",
4422 "override-test",
4423 }
4424
4425 expectedTopLevel := []string{
4426 "android-test",
4427 "override-test",
4428 }
4429
4430 assertTestOnlyAndTopLevel(t, ctx, expectedTestOnly, expectedTopLevel)
4431}
4432
yangbill2af0b6e2024-03-15 09:29:29 +00004433func TestAppStem(t *testing.T) {
4434 ctx := testApp(t, `
4435 android_app {
4436 name: "foo",
4437 srcs: ["a.java"],
4438 stem: "foo-new",
4439 sdk_version: "current",
4440 }`)
4441
4442 foo := ctx.ModuleForTests("foo", "android_common")
4443
4444 outputs := fmt.Sprint(foo.AllOutputs())
4445 if !strings.Contains(outputs, "foo-new.apk") {
4446 t.Errorf("Module output does not contain expected apk %s", "foo-new.apk")
4447 }
4448}
Spandan Dasb9c58352024-05-13 18:29:45 +00004449
4450func TestAppMinSdkVersionOverride(t *testing.T) {
4451 result := android.GroupFixturePreparers(
4452 PrepareForTestWithJavaDefaultModules,
4453 ).RunTestWithBp(t, `
4454 android_app {
4455 name: "com.android.foo",
4456 srcs: ["a.java"],
4457 sdk_version: "current",
4458 min_sdk_version: "31",
4459 updatable: true,
4460 }
4461 override_android_app {
4462 name: "com.android.go.foo",
4463 base: "com.android.foo",
4464 min_sdk_version: "33",
4465 }
4466 `)
4467 foo := result.ModuleForTests("com.android.foo", "android_common").Rule("manifestFixer")
4468 fooOverride := result.ModuleForTests("com.android.foo", "android_common_com.android.go.foo").Rule("manifestFixer")
4469
4470 android.AssertStringDoesContain(t,
4471 "com.android.foo: expected manifest fixer to set minSdkVersion to T",
4472 foo.BuildParams.Args["args"],
4473 "--minSdkVersion 31",
4474 )
4475 android.AssertStringDoesContain(t,
4476 "com.android.go.foo: expected manifest fixer to set minSdkVersion to T",
4477 fooOverride.BuildParams.Args["args"],
4478 "--minSdkVersion 33",
4479 )
4480
4481}