blob: ca9d317042ac486f4dc6cbe3796eb1dbddd87bf0 [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 }{
2016 {"app", false, false},
2017 {"app_noembed", false, false},
2018 {"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"],
3247 optional_uses_libs: ["runtime-optional-y"],
3248 sdk_version: "current",
3249 }
3250
Ulya Trafimovich861a8962021-02-26 14:49:07 +00003251 // A library that has to use "provides_uses_lib", because:
3252 // - it is not an SDK library
3253 // - its library name is different from its module name
3254 java_library {
3255 name: "non-sdk-lib",
3256 provides_uses_lib: "com.non.sdk.lib",
3257 installable: true,
3258 srcs: ["a.java"],
3259 }
3260
Colin Cross50ddcc42019-05-16 12:28:22 -07003261 android_app {
3262 name: "app",
3263 srcs: ["a.java"],
Ulya Trafimovich861a8962021-02-26 14:49:07 +00003264 libs: [
3265 "qux",
3266 "quuz.stubs"
3267 ],
Ulya Trafimovich65b03192020-12-03 16:50:22 +00003268 static_libs: [
3269 "static-runtime-helper",
3270 // statically linked component libraries should not pull their SDK libraries,
3271 // so "fred" should not be added to class loader context
3272 "fred.stubs",
Ulya Trafimoviche4432872021-08-18 16:57:11 +01003273 "static-x",
3274 "static-y",
Ulya Trafimovich65b03192020-12-03 16:50:22 +00003275 ],
Ulya Trafimovich861a8962021-02-26 14:49:07 +00003276 uses_libs: [
3277 "foo",
3278 "non-sdk-lib"
3279 ],
Jeongik Cha538c0d02019-07-11 15:54:27 +09003280 sdk_version: "current",
Colin Cross50ddcc42019-05-16 12:28:22 -07003281 optional_uses_libs: [
3282 "bar",
3283 "baz",
3284 ],
3285 }
3286
3287 android_app_import {
3288 name: "prebuilt",
3289 apk: "prebuilts/apk/app.apk",
3290 certificate: "platform",
Ulya Trafimovich861a8962021-02-26 14:49:07 +00003291 uses_libs: [
3292 "foo",
3293 "non-sdk-lib",
3294 "android.test.runner"
3295 ],
Colin Cross50ddcc42019-05-16 12:28:22 -07003296 optional_uses_libs: [
3297 "bar",
3298 "baz",
3299 ],
3300 }
3301 `
3302
Paul Duffin71ae5942021-03-22 15:36:52 +00003303 result := android.GroupFixturePreparers(
3304 prepareForJavaTest,
Paul Duffin2645a292021-03-13 02:36:00 +00003305 PrepareForTestWithJavaSdkLibraryFiles,
3306 FixtureWithLastReleaseApis("runtime-library", "foo", "quuz", "qux", "bar", "fred"),
Jiakai Zhang48203e32023-06-02 23:42:21 +01003307 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
3308 variables.BuildWarningBadOptionalUsesLibsAllowlist = []string{"app", "prebuilt"}
3309 }),
Paul Duffind234b412021-03-12 23:04:46 +00003310 ).RunTestWithBp(t, bp)
Colin Cross50ddcc42019-05-16 12:28:22 -07003311
Paul Duffind234b412021-03-12 23:04:46 +00003312 app := result.ModuleForTests("app", "android_common")
3313 prebuilt := result.ModuleForTests("prebuilt", "android_common")
Colin Cross50ddcc42019-05-16 12:28:22 -07003314
Paul Duffin859fe962020-05-15 10:20:31 +01003315 // Test that implicit dependencies on java_sdk_library instances are passed to the manifest.
Ulya Trafimovichf5d91bb2022-05-04 12:00:02 +01003316 // These also include explicit `uses_libs`/`optional_uses_libs` entries, as they may be
3317 // propagated from dependencies.
Ulya Trafimovich2eaf5c52021-02-26 12:05:11 +00003318 actualManifestFixerArgs := app.Output("manifest_fixer/AndroidManifest.xml").Args["args"]
3319 expectManifestFixerArgs := `--extract-native-libs=true ` +
3320 `--uses-library qux ` +
3321 `--uses-library quuz ` +
Ulya Trafimovichf5d91bb2022-05-04 12:00:02 +01003322 `--uses-library foo ` +
3323 `--uses-library com.non.sdk.lib ` +
3324 `--uses-library runtime-library ` +
3325 `--uses-library runtime-required-x ` +
3326 `--uses-library runtime-required-y ` +
3327 `--optional-uses-library bar ` +
3328 `--optional-uses-library runtime-optional-x ` +
3329 `--optional-uses-library runtime-optional-y`
Gurpreet Singh75d65f32022-01-24 17:44:05 +00003330 android.AssertStringDoesContain(t, "manifest_fixer args", actualManifestFixerArgs, expectManifestFixerArgs)
Paul Duffin859fe962020-05-15 10:20:31 +01003331
Ulya Trafimovich2eaf5c52021-02-26 12:05:11 +00003332 // Test that all libraries are verified (library order matters).
3333 verifyCmd := app.Rule("verify_uses_libraries").RuleParams.Command
3334 verifyArgs := `--uses-library foo ` +
Ulya Trafimovicheea486a2021-02-26 11:38:21 +00003335 `--uses-library com.non.sdk.lib ` +
Ulya Trafimovich2eaf5c52021-02-26 12:05:11 +00003336 `--uses-library qux ` +
3337 `--uses-library quuz ` +
3338 `--uses-library runtime-library ` +
Ulya Trafimoviche4432872021-08-18 16:57:11 +01003339 `--uses-library runtime-required-x ` +
3340 `--uses-library runtime-required-y ` +
Ulya Trafimovich2eaf5c52021-02-26 12:05:11 +00003341 `--optional-uses-library bar ` +
Ulya Trafimoviche4432872021-08-18 16:57:11 +01003342 `--optional-uses-library baz ` +
3343 `--optional-uses-library runtime-optional-x ` +
3344 `--optional-uses-library runtime-optional-y `
Paul Duffind234b412021-03-12 23:04:46 +00003345 android.AssertStringDoesContain(t, "verify cmd args", verifyCmd, verifyArgs)
Colin Cross50ddcc42019-05-16 12:28:22 -07003346
Ulya Trafimovich2eaf5c52021-02-26 12:05:11 +00003347 // Test that all libraries are verified for an APK (library order matters).
3348 verifyApkCmd := prebuilt.Rule("verify_uses_libraries").RuleParams.Command
Ulya Trafimovich0aba2522021-03-03 16:38:37 +00003349 verifyApkArgs := `--uses-library foo ` +
3350 `--uses-library com.non.sdk.lib ` +
3351 `--uses-library android.test.runner ` +
3352 `--optional-uses-library bar ` +
3353 `--optional-uses-library baz `
Paul Duffind234b412021-03-12 23:04:46 +00003354 android.AssertStringDoesContain(t, "verify apk cmd args", verifyApkCmd, verifyApkArgs)
Colin Cross50ddcc42019-05-16 12:28:22 -07003355
Jiakai Zhanga4496782023-05-17 16:57:30 +01003356 // Test that necessary args are passed for constructing CLC in Ninja phase.
Ulya Trafimovich2eaf5c52021-02-26 12:05:11 +00003357 cmd := app.Rule("dexpreopt").RuleParams.Command
Jiakai Zhanga4496782023-05-17 16:57:30 +01003358 android.AssertStringDoesContain(t, "dexpreopt app cmd context", cmd, "--context-json=")
3359 android.AssertStringDoesContain(t, "dexpreopt app cmd product_packages", cmd,
Spandan Das2069c3f2023-12-06 19:40:24 +00003360 "--product-packages=out/soong/.intermediates/app/android_common/dexpreopt/app/product_packages.txt")
Colin Cross50ddcc42019-05-16 12:28:22 -07003361}
Jaewoong Jungc27ab662019-05-30 15:51:14 -07003362
Ulya Trafimovich9023b022021-03-22 16:02:28 +00003363func TestDexpreoptBcp(t *testing.T) {
3364 bp := `
3365 java_sdk_library {
3366 name: "foo",
3367 srcs: ["a.java"],
3368 api_packages: ["foo"],
3369 sdk_version: "current",
3370 }
3371
3372 java_sdk_library {
3373 name: "bar",
3374 srcs: ["a.java"],
3375 api_packages: ["bar"],
3376 permitted_packages: ["bar"],
3377 sdk_version: "current",
3378 }
3379
3380 android_app {
3381 name: "app",
3382 srcs: ["a.java"],
3383 sdk_version: "current",
3384 }
3385 `
3386
3387 testCases := []struct {
3388 name string
3389 with bool
3390 expect string
3391 }{
3392 {
3393 name: "with updatable bcp",
3394 with: true,
3395 expect: "/system/framework/foo.jar:/system/framework/bar.jar",
3396 },
3397 {
3398 name: "without updatable bcp",
3399 with: false,
3400 expect: "/system/framework/foo.jar",
3401 },
3402 }
3403
3404 for _, test := range testCases {
3405 t.Run(test.name, func(t *testing.T) {
3406 result := android.GroupFixturePreparers(
3407 prepareForJavaTest,
3408 PrepareForTestWithJavaSdkLibraryFiles,
3409 FixtureWithLastReleaseApis("runtime-library", "foo", "bar"),
3410 dexpreopt.FixtureSetBootJars("platform:foo"),
satayevd604b212021-07-21 14:23:52 +01003411 dexpreopt.FixtureSetApexBootJars("platform:bar"),
Ulya Trafimovich9023b022021-03-22 16:02:28 +00003412 dexpreopt.FixtureSetPreoptWithUpdatableBcp(test.with),
3413 ).RunTestWithBp(t, bp)
3414
3415 app := result.ModuleForTests("app", "android_common")
3416 cmd := app.Rule("dexpreopt").RuleParams.Command
3417 bcp := " -Xbootclasspath-locations:" + test.expect + " " // space at the end matters
3418 android.AssertStringDoesContain(t, "dexpreopt app bcp", cmd, bcp)
3419 })
3420 }
3421}
3422
Jaewoong Jungc27ab662019-05-30 15:51:14 -07003423func TestCodelessApp(t *testing.T) {
3424 testCases := []struct {
3425 name string
3426 bp string
3427 noCode bool
3428 }{
3429 {
3430 name: "normal",
3431 bp: `
3432 android_app {
3433 name: "foo",
3434 srcs: ["a.java"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09003435 sdk_version: "current",
Jaewoong Jungc27ab662019-05-30 15:51:14 -07003436 }
3437 `,
3438 noCode: false,
3439 },
3440 {
3441 name: "app without sources",
3442 bp: `
3443 android_app {
3444 name: "foo",
Jeongik Cha538c0d02019-07-11 15:54:27 +09003445 sdk_version: "current",
Jaewoong Jungc27ab662019-05-30 15:51:14 -07003446 }
3447 `,
3448 noCode: true,
3449 },
3450 {
3451 name: "app with libraries",
3452 bp: `
3453 android_app {
3454 name: "foo",
3455 static_libs: ["lib"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09003456 sdk_version: "current",
Jaewoong Jungc27ab662019-05-30 15:51:14 -07003457 }
3458
3459 java_library {
3460 name: "lib",
3461 srcs: ["a.java"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09003462 sdk_version: "current",
Jaewoong Jungc27ab662019-05-30 15:51:14 -07003463 }
3464 `,
3465 noCode: false,
3466 },
3467 {
3468 name: "app with sourceless libraries",
3469 bp: `
3470 android_app {
3471 name: "foo",
3472 static_libs: ["lib"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09003473 sdk_version: "current",
Jaewoong Jungc27ab662019-05-30 15:51:14 -07003474 }
3475
3476 java_library {
3477 name: "lib",
Jeongik Cha538c0d02019-07-11 15:54:27 +09003478 sdk_version: "current",
Jaewoong Jungc27ab662019-05-30 15:51:14 -07003479 }
3480 `,
3481 // TODO(jungjw): this should probably be true
3482 noCode: false,
3483 },
3484 }
3485
3486 for _, test := range testCases {
3487 t.Run(test.name, func(t *testing.T) {
3488 ctx := testApp(t, test.bp)
3489
3490 foo := ctx.ModuleForTests("foo", "android_common")
3491 manifestFixerArgs := foo.Output("manifest_fixer/AndroidManifest.xml").Args["args"]
3492 if strings.Contains(manifestFixerArgs, "--has-no-code") != test.noCode {
3493 t.Errorf("unexpected manifest_fixer args: %q", manifestFixerArgs)
3494 }
3495 })
3496 }
3497}
Jaewoong Jung5b425e22019-06-17 17:40:56 -07003498
Colin Cross53a87f52019-06-25 13:35:30 -07003499func TestUncompressDex(t *testing.T) {
3500 testCases := []struct {
3501 name string
3502 bp string
3503
3504 uncompressedPlatform bool
3505 uncompressedUnbundled bool
3506 }{
3507 {
3508 name: "normal",
3509 bp: `
3510 android_app {
3511 name: "foo",
3512 srcs: ["a.java"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09003513 sdk_version: "current",
Colin Cross53a87f52019-06-25 13:35:30 -07003514 }
3515 `,
3516 uncompressedPlatform: true,
3517 uncompressedUnbundled: false,
3518 },
3519 {
3520 name: "use_embedded_dex",
3521 bp: `
3522 android_app {
3523 name: "foo",
3524 use_embedded_dex: true,
3525 srcs: ["a.java"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09003526 sdk_version: "current",
Colin Cross53a87f52019-06-25 13:35:30 -07003527 }
3528 `,
3529 uncompressedPlatform: true,
3530 uncompressedUnbundled: true,
3531 },
3532 {
3533 name: "privileged",
3534 bp: `
3535 android_app {
3536 name: "foo",
3537 privileged: true,
3538 srcs: ["a.java"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09003539 sdk_version: "current",
Colin Cross53a87f52019-06-25 13:35:30 -07003540 }
3541 `,
3542 uncompressedPlatform: true,
3543 uncompressedUnbundled: true,
3544 },
David Srbeckye033cba2020-05-20 22:20:28 +01003545 {
3546 name: "normal_uncompress_dex_true",
3547 bp: `
3548 android_app {
3549 name: "foo",
3550 srcs: ["a.java"],
3551 sdk_version: "current",
3552 uncompress_dex: true,
3553 }
3554 `,
3555 uncompressedPlatform: true,
3556 uncompressedUnbundled: true,
3557 },
3558 {
3559 name: "normal_uncompress_dex_false",
3560 bp: `
3561 android_app {
3562 name: "foo",
3563 srcs: ["a.java"],
3564 sdk_version: "current",
3565 uncompress_dex: false,
3566 }
3567 `,
3568 uncompressedPlatform: false,
3569 uncompressedUnbundled: false,
3570 },
Colin Cross53a87f52019-06-25 13:35:30 -07003571 }
3572
3573 test := func(t *testing.T, bp string, want bool, unbundled bool) {
3574 t.Helper()
3575
Paul Duffin71ae5942021-03-22 15:36:52 +00003576 result := android.GroupFixturePreparers(
3577 prepareForJavaTest,
Paul Duffin2645a292021-03-13 02:36:00 +00003578 PrepareForTestWithPrebuiltsOfCurrentApi,
Paul Duffincdb88a92021-03-14 00:36:50 +00003579 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
3580 if unbundled {
3581 variables.Unbundled_build = proptools.BoolPtr(true)
3582 variables.Always_use_prebuilt_sdks = proptools.BoolPtr(true)
3583 }
3584 }),
3585 ).RunTestWithBp(t, bp)
Colin Cross53a87f52019-06-25 13:35:30 -07003586
Paul Duffincdb88a92021-03-14 00:36:50 +00003587 foo := result.ModuleForTests("foo", "android_common")
Colin Cross53a87f52019-06-25 13:35:30 -07003588 dex := foo.Rule("r8")
3589 uncompressedInDexJar := strings.Contains(dex.Args["zipFlags"], "-L 0")
3590 aligned := foo.MaybeRule("zipalign").Rule != nil
3591
Paul Duffincdb88a92021-03-14 00:36:50 +00003592 android.AssertBoolEquals(t, "uncompressed in dex", want, uncompressedInDexJar)
Colin Cross53a87f52019-06-25 13:35:30 -07003593
Paul Duffincdb88a92021-03-14 00:36:50 +00003594 android.AssertBoolEquals(t, "aligne", want, aligned)
Colin Cross53a87f52019-06-25 13:35:30 -07003595 }
3596
3597 for _, tt := range testCases {
3598 t.Run(tt.name, func(t *testing.T) {
3599 t.Run("platform", func(t *testing.T) {
3600 test(t, tt.bp, tt.uncompressedPlatform, false)
3601 })
3602 t.Run("unbundled", func(t *testing.T) {
3603 test(t, tt.bp, tt.uncompressedUnbundled, true)
3604 })
3605 })
3606 }
3607}
Jaewoong Jung26dedd32019-06-06 08:45:58 -07003608
3609func checkAapt2LinkFlag(t *testing.T, aapt2Flags, flagName, expectedValue string) {
3610 if expectedValue != "" {
3611 expectedFlag := "--" + flagName + " " + expectedValue
3612 if !strings.Contains(aapt2Flags, expectedFlag) {
3613 t.Errorf("%q is missing in aapt2 link flags, %q", expectedFlag, aapt2Flags)
3614 }
3615 } else {
3616 unexpectedFlag := "--" + flagName
3617 if strings.Contains(aapt2Flags, unexpectedFlag) {
3618 t.Errorf("unexpected flag, %q is found in aapt2 link flags, %q", unexpectedFlag, aapt2Flags)
3619 }
3620 }
3621}
Jaewoong Jung9befb0c2020-01-18 10:33:43 -08003622
Cole Faust9a631312020-10-22 21:05:24 +00003623func TestExportedProguardFlagFiles(t *testing.T) {
3624 ctx, _ := testJava(t, `
3625 android_app {
3626 name: "foo",
3627 sdk_version: "current",
3628 static_libs: ["lib1"],
3629 }
3630
3631 android_library {
3632 name: "lib1",
3633 sdk_version: "current",
3634 optimize: {
3635 proguard_flags_files: ["lib1proguard.cfg"],
3636 }
3637 }
3638 `)
3639
3640 m := ctx.ModuleForTests("foo", "android_common")
3641 hasLib1Proguard := false
3642 for _, s := range m.Rule("java.r8").Implicits.Strings() {
3643 if s == "lib1proguard.cfg" {
3644 hasLib1Proguard = true
3645 break
3646 }
3647 }
3648
3649 if !hasLib1Proguard {
3650 t.Errorf("App does not use library proguard config")
3651 }
3652}
Spandan Dasc8cb0c32021-12-08 17:00:38 +00003653
3654func TestTargetSdkVersionManifestFixer(t *testing.T) {
3655 platform_sdk_codename := "Tiramisu"
Spandan Dasfb6a1ee2023-04-27 16:08:26 +00003656 platform_sdk_version := 33
Spandan Dasc8cb0c32021-12-08 17:00:38 +00003657 testCases := []struct {
3658 name string
3659 targetSdkVersionInBp string
3660 targetSdkVersionExpected string
3661 unbundledBuild bool
Spandan Dasfb6a1ee2023-04-27 16:08:26 +00003662 platformSdkFinal bool
Spandan Dasc8cb0c32021-12-08 17:00:38 +00003663 }{
3664 {
3665 name: "Non-Unbundled build: Android.bp has targetSdkVersion",
3666 targetSdkVersionInBp: "30",
3667 targetSdkVersionExpected: "30",
3668 unbundledBuild: false,
3669 },
3670 {
3671 name: "Unbundled build: Android.bp has targetSdkVersion",
3672 targetSdkVersionInBp: "30",
3673 targetSdkVersionExpected: "30",
3674 unbundledBuild: true,
3675 },
3676 {
3677 name: "Non-Unbundled build: Android.bp has targetSdkVersion equal to platform_sdk_codename",
3678 targetSdkVersionInBp: platform_sdk_codename,
3679 targetSdkVersionExpected: platform_sdk_codename,
3680 unbundledBuild: false,
3681 },
3682 {
3683 name: "Unbundled build: Android.bp has targetSdkVersion equal to platform_sdk_codename",
3684 targetSdkVersionInBp: platform_sdk_codename,
3685 targetSdkVersionExpected: "10000",
3686 unbundledBuild: true,
3687 },
3688
3689 {
3690 name: "Non-Unbundled build: Android.bp has no targetSdkVersion",
3691 targetSdkVersionExpected: platform_sdk_codename,
3692 unbundledBuild: false,
3693 },
3694 {
3695 name: "Unbundled build: Android.bp has no targetSdkVersion",
3696 targetSdkVersionExpected: "10000",
3697 unbundledBuild: true,
3698 },
Spandan Dasfb6a1ee2023-04-27 16:08:26 +00003699 {
3700 name: "Bundled build in REL branches",
3701 targetSdkVersionExpected: "33",
3702 unbundledBuild: false,
3703 platformSdkFinal: true,
3704 },
Spandan Dasc8cb0c32021-12-08 17:00:38 +00003705 }
3706 for _, testCase := range testCases {
Spandan Dasca70fc42023-03-01 23:38:49 +00003707 targetSdkVersionTemplate := ""
3708 if testCase.targetSdkVersionInBp != "" {
3709 targetSdkVersionTemplate = fmt.Sprintf(`target_sdk_version: "%s",`, testCase.targetSdkVersionInBp)
3710 }
Spandan Dasc8cb0c32021-12-08 17:00:38 +00003711 bp := fmt.Sprintf(`
3712 android_app {
3713 name: "foo",
3714 sdk_version: "current",
Spandan Dasca70fc42023-03-01 23:38:49 +00003715 %s
Spandan Dasc8cb0c32021-12-08 17:00:38 +00003716 }
Spandan Dasca70fc42023-03-01 23:38:49 +00003717 `, targetSdkVersionTemplate)
Spandan Dasc8cb0c32021-12-08 17:00:38 +00003718 fixture := android.GroupFixturePreparers(
3719 prepareForJavaTest,
3720 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
Spandan Dasfb6a1ee2023-04-27 16:08:26 +00003721 if testCase.platformSdkFinal {
3722 variables.Platform_sdk_final = proptools.BoolPtr(true)
3723 }
Spandan Dasc8cb0c32021-12-08 17:00:38 +00003724 // explicitly set platform_sdk_codename to make the test deterministic
3725 variables.Platform_sdk_codename = &platform_sdk_codename
Spandan Dasfb6a1ee2023-04-27 16:08:26 +00003726 variables.Platform_sdk_version = &platform_sdk_version
Spandan Dasc8cb0c32021-12-08 17:00:38 +00003727 variables.Platform_version_active_codenames = []string{platform_sdk_codename}
3728 // create a non-empty list if unbundledBuild==true
3729 if testCase.unbundledBuild {
3730 variables.Unbundled_build_apps = []string{"apex_a", "apex_b"}
3731 }
3732 }),
3733 )
3734
3735 result := fixture.RunTestWithBp(t, bp)
3736 foo := result.ModuleForTests("foo", "android_common")
3737
Gurpreet Singh75d65f32022-01-24 17:44:05 +00003738 manifestFixerArgs := foo.Output("manifest_fixer/AndroidManifest.xml").Args["args"]
3739 android.AssertStringDoesContain(t, testCase.name, manifestFixerArgs, "--targetSdkVersion "+testCase.targetSdkVersionExpected)
Spandan Dasc8cb0c32021-12-08 17:00:38 +00003740 }
3741}
Colin Cross412436f2022-04-07 17:40:07 -07003742
Harshit Mahajan5b8b7302022-06-10 11:24:05 +00003743func TestDefaultAppTargetSdkVersionForUpdatableModules(t *testing.T) {
3744 platform_sdk_codename := "Tiramisu"
3745 platform_sdk_version := 33
3746 testCases := []struct {
3747 name string
3748 platform_sdk_final bool
3749 targetSdkVersionInBp *string
3750 targetSdkVersionExpected *string
3751 updatable bool
3752 }{
3753 {
3754 name: "Non-Updatable Module: Android.bp has older targetSdkVersion",
3755 targetSdkVersionInBp: proptools.StringPtr("29"),
3756 targetSdkVersionExpected: proptools.StringPtr("29"),
3757 updatable: false,
3758 },
3759 {
3760 name: "Updatable Module: Android.bp has older targetSdkVersion",
3761 targetSdkVersionInBp: proptools.StringPtr("30"),
3762 targetSdkVersionExpected: proptools.StringPtr("30"),
3763 updatable: true,
3764 },
3765 {
3766 name: "Updatable Module: Android.bp has no targetSdkVersion",
3767 targetSdkVersionExpected: proptools.StringPtr("10000"),
3768 updatable: true,
3769 },
3770 {
3771 name: "[SDK finalised] Non-Updatable Module: Android.bp has older targetSdkVersion",
3772 platform_sdk_final: true,
3773 targetSdkVersionInBp: proptools.StringPtr("30"),
3774 targetSdkVersionExpected: proptools.StringPtr("30"),
3775 updatable: false,
3776 },
3777 {
3778 name: "[SDK finalised] Updatable Module: Android.bp has older targetSdkVersion",
3779 platform_sdk_final: true,
3780 targetSdkVersionInBp: proptools.StringPtr("30"),
3781 targetSdkVersionExpected: proptools.StringPtr("30"),
3782 updatable: true,
3783 },
3784 {
3785 name: "[SDK finalised] Updatable Module: Android.bp has targetSdkVersion as platform sdk codename",
3786 platform_sdk_final: true,
3787 targetSdkVersionInBp: proptools.StringPtr(platform_sdk_codename),
3788 targetSdkVersionExpected: proptools.StringPtr("33"),
3789 updatable: true,
3790 },
3791 {
3792 name: "[SDK finalised] Updatable Module: Android.bp has no targetSdkVersion",
3793 platform_sdk_final: true,
3794 targetSdkVersionExpected: proptools.StringPtr("33"),
3795 updatable: true,
3796 },
3797 }
3798 for _, testCase := range testCases {
Spandan Dasca70fc42023-03-01 23:38:49 +00003799 targetSdkVersionTemplate := ""
3800 if testCase.targetSdkVersionInBp != nil {
3801 targetSdkVersionTemplate = fmt.Sprintf(`target_sdk_version: "%s",`, *testCase.targetSdkVersionInBp)
3802 }
Harshit Mahajan5b8b7302022-06-10 11:24:05 +00003803 bp := fmt.Sprintf(`
3804 android_app {
3805 name: "foo",
3806 sdk_version: "current",
3807 min_sdk_version: "29",
Spandan Dasca70fc42023-03-01 23:38:49 +00003808 %s
Harshit Mahajan5b8b7302022-06-10 11:24:05 +00003809 updatable: %t,
3810 enforce_default_target_sdk_version: %t
3811 }
Spandan Dasca70fc42023-03-01 23:38:49 +00003812 `, targetSdkVersionTemplate, testCase.updatable, testCase.updatable) // enforce default target sdk version if app is updatable
Harshit Mahajan5b8b7302022-06-10 11:24:05 +00003813
3814 fixture := android.GroupFixturePreparers(
3815 PrepareForTestWithJavaDefaultModules,
3816 android.PrepareForTestWithAllowMissingDependencies,
3817 android.PrepareForTestWithAndroidMk,
3818 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
3819 // explicitly set following platform variables to make the test deterministic
3820 variables.Platform_sdk_final = &testCase.platform_sdk_final
3821 variables.Platform_sdk_version = &platform_sdk_version
3822 variables.Platform_sdk_codename = &platform_sdk_codename
3823 variables.Platform_version_active_codenames = []string{platform_sdk_codename}
Harshit Mahajan18845d02023-01-11 18:39:12 +00003824 variables.Unbundled_build = proptools.BoolPtr(true)
Harshit Mahajan5b8b7302022-06-10 11:24:05 +00003825 variables.Unbundled_build_apps = []string{"sampleModule"}
3826 }),
3827 )
3828
3829 result := fixture.RunTestWithBp(t, bp)
3830 foo := result.ModuleForTests("foo", "android_common")
3831
3832 manifestFixerArgs := foo.Output("manifest_fixer/AndroidManifest.xml").Args["args"]
3833 android.AssertStringDoesContain(t, testCase.name, manifestFixerArgs, "--targetSdkVersion "+*testCase.targetSdkVersionExpected)
3834 }
3835}
3836
3837func TestEnforceDefaultAppTargetSdkVersionFlag(t *testing.T) {
3838 platform_sdk_codename := "Tiramisu"
3839 platform_sdk_version := 33
3840 testCases := []struct {
3841 name string
3842 enforceDefaultTargetSdkVersion bool
3843 expectedError string
3844 platform_sdk_final bool
3845 targetSdkVersionInBp string
3846 targetSdkVersionExpected string
3847 updatable bool
3848 }{
3849 {
3850 name: "Not enforcing Target SDK Version: Android.bp has older targetSdkVersion",
3851 enforceDefaultTargetSdkVersion: false,
3852 targetSdkVersionInBp: "29",
3853 targetSdkVersionExpected: "29",
3854 updatable: false,
3855 },
3856 {
3857 name: "[SDK finalised] Enforce Target SDK Version: Android.bp has current targetSdkVersion",
3858 enforceDefaultTargetSdkVersion: true,
3859 platform_sdk_final: true,
3860 targetSdkVersionInBp: "current",
3861 targetSdkVersionExpected: "33",
3862 updatable: true,
3863 },
3864 {
Harshit Mahajan8f202ad2023-01-09 20:45:55 +00003865 name: "Enforce Target SDK Version: Android.bp has current targetSdkVersion",
Harshit Mahajan5b8b7302022-06-10 11:24:05 +00003866 enforceDefaultTargetSdkVersion: true,
3867 platform_sdk_final: false,
3868 targetSdkVersionInBp: "current",
3869 targetSdkVersionExpected: "10000",
3870 updatable: false,
3871 },
3872 {
3873 name: "Not enforcing Target SDK Version for Updatable app",
3874 enforceDefaultTargetSdkVersion: false,
3875 expectedError: "Updatable apps must enforce default target sdk version",
3876 targetSdkVersionInBp: "29",
3877 targetSdkVersionExpected: "29",
3878 updatable: true,
3879 },
3880 }
3881 for _, testCase := range testCases {
3882 errExpected := testCase.expectedError != ""
3883 bp := fmt.Sprintf(`
3884 android_app {
3885 name: "foo",
3886 enforce_default_target_sdk_version: %t,
3887 sdk_version: "current",
3888 min_sdk_version: "29",
3889 target_sdk_version: "%v",
3890 updatable: %t
3891 }
3892 `, testCase.enforceDefaultTargetSdkVersion, testCase.targetSdkVersionInBp, testCase.updatable)
3893
3894 fixture := android.GroupFixturePreparers(
3895 PrepareForTestWithJavaDefaultModules,
3896 android.PrepareForTestWithAllowMissingDependencies,
3897 android.PrepareForTestWithAndroidMk,
3898 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
3899 // explicitly set following platform variables to make the test deterministic
3900 variables.Platform_sdk_final = &testCase.platform_sdk_final
3901 variables.Platform_sdk_version = &platform_sdk_version
3902 variables.Platform_sdk_codename = &platform_sdk_codename
Harshit Mahajan18845d02023-01-11 18:39:12 +00003903 variables.Unbundled_build = proptools.BoolPtr(true)
Harshit Mahajan5b8b7302022-06-10 11:24:05 +00003904 variables.Unbundled_build_apps = []string{"sampleModule"}
3905 }),
3906 )
3907
3908 errorHandler := android.FixtureExpectsNoErrors
3909 if errExpected {
3910 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(testCase.expectedError)
3911 }
3912 result := fixture.ExtendWithErrorHandler(errorHandler).RunTestWithBp(t, bp)
3913
3914 if !errExpected {
3915 foo := result.ModuleForTests("foo", "android_common")
3916 manifestFixerArgs := foo.Output("manifest_fixer/AndroidManifest.xml").Args["args"]
3917 android.AssertStringDoesContain(t, testCase.name, manifestFixerArgs, "--targetSdkVersion "+testCase.targetSdkVersionExpected)
3918 }
3919 }
3920}
3921
Harshit Mahajan8f202ad2023-01-09 20:45:55 +00003922func TestEnforceDefaultAppTargetSdkVersionFlagForTests(t *testing.T) {
3923 platform_sdk_codename := "Tiramisu"
3924 platform_sdk_version := 33
3925 testCases := []struct {
3926 name string
3927 enforceDefaultTargetSdkVersion bool
3928 expectedError string
3929 platform_sdk_final bool
3930 targetSdkVersionInBp string
3931 targetSdkVersionExpected string
3932 }{
3933 {
3934 name: "Not enforcing Target SDK Version: Android.bp has older targetSdkVersion",
3935 enforceDefaultTargetSdkVersion: false,
3936 targetSdkVersionInBp: "29",
3937 targetSdkVersionExpected: "29",
3938 },
3939 {
3940 name: "[SDK finalised] Enforce Target SDK Version: Android.bp has current targetSdkVersion",
3941 enforceDefaultTargetSdkVersion: true,
3942 platform_sdk_final: true,
3943 targetSdkVersionInBp: "current",
3944 targetSdkVersionExpected: "33",
3945 },
3946 {
3947 name: "Enforce Target SDK Version: Android.bp has current targetSdkVersion",
3948 enforceDefaultTargetSdkVersion: true,
3949 platform_sdk_final: false,
3950 targetSdkVersionInBp: "current",
3951 targetSdkVersionExpected: "10000",
3952 },
3953 }
3954 for _, testCase := range testCases {
3955 errExpected := testCase.expectedError != ""
3956 bp := fmt.Sprintf(`
3957 android_test {
3958 name: "foo",
3959 enforce_default_target_sdk_version: %t,
3960 min_sdk_version: "29",
3961 target_sdk_version: "%v",
3962 }
3963 `, testCase.enforceDefaultTargetSdkVersion, testCase.targetSdkVersionInBp)
3964
3965 fixture := android.GroupFixturePreparers(
3966 PrepareForTestWithJavaDefaultModules,
3967 android.PrepareForTestWithAllowMissingDependencies,
3968 android.PrepareForTestWithAndroidMk,
3969 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
3970 // explicitly set following platform variables to make the test deterministic
3971 variables.Platform_sdk_final = &testCase.platform_sdk_final
3972 variables.Platform_sdk_version = &platform_sdk_version
3973 variables.Platform_sdk_codename = &platform_sdk_codename
Harshit Mahajan18845d02023-01-11 18:39:12 +00003974 variables.Unbundled_build = proptools.BoolPtr(true)
Harshit Mahajan8f202ad2023-01-09 20:45:55 +00003975 variables.Unbundled_build_apps = []string{"sampleModule"}
3976 }),
3977 )
3978
3979 errorHandler := android.FixtureExpectsNoErrors
3980 if errExpected {
3981 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(testCase.expectedError)
3982 }
3983 result := fixture.ExtendWithErrorHandler(errorHandler).RunTestWithBp(t, bp)
3984
3985 if !errExpected {
3986 foo := result.ModuleForTests("foo", "android_common")
3987 manifestFixerArgs := foo.Output("manifest_fixer/AndroidManifest.xml").Args["args"]
3988 android.AssertStringDoesContain(t, testCase.name, manifestFixerArgs, "--targetSdkVersion "+testCase.targetSdkVersionExpected)
3989 }
3990 }
3991}
3992
Colin Cross412436f2022-04-07 17:40:07 -07003993func TestAppMissingCertificateAllowMissingDependencies(t *testing.T) {
3994 result := android.GroupFixturePreparers(
3995 PrepareForTestWithJavaDefaultModules,
3996 android.PrepareForTestWithAllowMissingDependencies,
3997 android.PrepareForTestWithAndroidMk,
3998 ).RunTestWithBp(t, `
3999 android_app {
4000 name: "foo",
4001 srcs: ["a.java"],
4002 certificate: ":missing_certificate",
4003 sdk_version: "current",
Zyan Wub7550aa2023-05-18 15:46:31 +08004004 }
4005
4006 android_app {
4007 name: "bar",
4008 srcs: ["a.java"],
4009 certificate: ":missing_certificate",
4010 product_specific: true,
4011 sdk_version: "current",
Colin Cross412436f2022-04-07 17:40:07 -07004012 }`)
4013
4014 foo := result.ModuleForTests("foo", "android_common")
4015 fooApk := foo.Output("foo.apk")
4016 if fooApk.Rule != android.ErrorRule {
4017 t.Fatalf("expected ErrorRule for foo.apk, got %s", fooApk.Rule.String())
4018 }
4019 android.AssertStringDoesContain(t, "expected error rule message", fooApk.Args["error"], "missing dependencies: missing_certificate\n")
4020}
Sam Delmerico82602492022-06-10 17:05:42 +00004021
4022func TestAppIncludesJniPackages(t *testing.T) {
4023 ctx := android.GroupFixturePreparers(
4024 PrepareForTestWithJavaDefaultModules,
4025 ).RunTestWithBp(t, `
4026 android_library_import {
4027 name: "aary-nodeps",
4028 aars: ["aary.aar"],
4029 extract_jni: true,
4030 }
4031
4032 android_library {
4033 name: "aary-lib",
4034 sdk_version: "current",
4035 min_sdk_version: "21",
4036 static_libs: ["aary-nodeps"],
4037 }
4038
4039 android_app {
4040 name: "aary-lib-dep",
4041 sdk_version: "current",
4042 min_sdk_version: "21",
4043 manifest: "AndroidManifest.xml",
4044 static_libs: ["aary-lib"],
4045 use_embedded_native_libs: true,
4046 }
4047
4048 android_app {
4049 name: "aary-import-dep",
4050 sdk_version: "current",
4051 min_sdk_version: "21",
4052 manifest: "AndroidManifest.xml",
4053 static_libs: ["aary-nodeps"],
4054 use_embedded_native_libs: true,
4055 }
4056
4057 android_app {
4058 name: "aary-no-use-embedded",
4059 sdk_version: "current",
4060 min_sdk_version: "21",
4061 manifest: "AndroidManifest.xml",
4062 static_libs: ["aary-nodeps"],
4063 }`)
4064
4065 testCases := []struct {
4066 name string
4067 hasPackage bool
4068 }{
4069 {
4070 name: "aary-import-dep",
4071 hasPackage: true,
4072 },
4073 {
4074 name: "aary-lib-dep",
4075 hasPackage: true,
4076 },
4077 {
4078 name: "aary-no-use-embedded",
4079 hasPackage: false,
4080 },
4081 }
4082
4083 for _, tc := range testCases {
4084 t.Run(tc.name, func(t *testing.T) {
4085 app := ctx.ModuleForTests(tc.name, "android_common")
4086
4087 outputFile := "jnilibs.zip"
4088 jniOutputLibZip := app.MaybeOutput(outputFile)
4089 if jniOutputLibZip.Rule == nil && !tc.hasPackage {
4090 return
4091 }
4092
4093 jniPackage := "arm64-v8a_jni.zip"
4094 inputs := jniOutputLibZip.Inputs
4095 foundPackage := false
4096 for i := 0; i < len(inputs); i++ {
4097 if strings.Contains(inputs[i].String(), jniPackage) {
4098 foundPackage = true
4099 }
4100 }
4101 if foundPackage != tc.hasPackage {
4102 t.Errorf("expected to find %v in %v inputs; inputs = %v", jniPackage, outputFile, inputs)
4103 }
4104 })
4105 }
4106}
Spandan Das9f7ae7f2022-07-25 00:34:18 +00004107
4108func TestTargetSdkVersionMtsTests(t *testing.T) {
4109 platformSdkCodename := "Tiramisu"
4110 android_test := "android_test"
4111 android_test_helper_app := "android_test_helper_app"
4112 bpTemplate := `
4113 %v {
4114 name: "mytest",
4115 target_sdk_version: "%v",
4116 test_suites: ["othersuite", "%v"],
4117 }
4118 `
4119 testCases := []struct {
4120 desc string
4121 moduleType string
4122 targetSdkVersionInBp string
4123 targetSdkVersionExpected string
4124 testSuites string
4125 }{
4126 {
4127 desc: "Non-MTS android_test_apps targeting current should not be upgraded to 10000",
4128 moduleType: android_test,
4129 targetSdkVersionInBp: "current",
4130 targetSdkVersionExpected: platformSdkCodename,
4131 testSuites: "non-mts-suite",
4132 },
4133 {
4134 desc: "MTS android_test_apps targeting released sdks should not be upgraded to 10000",
4135 moduleType: android_test,
4136 targetSdkVersionInBp: "29",
4137 targetSdkVersionExpected: "29",
4138 testSuites: "mts-suite",
4139 },
4140 {
4141 desc: "MTS android_test_apps targeting current should be upgraded to 10000",
4142 moduleType: android_test,
4143 targetSdkVersionInBp: "current",
4144 targetSdkVersionExpected: "10000",
4145 testSuites: "mts-suite",
4146 },
4147 {
4148 desc: "MTS android_test_helper_apps targeting current should be upgraded to 10000",
4149 moduleType: android_test_helper_app,
4150 targetSdkVersionInBp: "current",
4151 targetSdkVersionExpected: "10000",
4152 testSuites: "mts-suite",
4153 },
4154 }
4155 fixture := android.GroupFixturePreparers(
4156 prepareForJavaTest,
4157 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
4158 variables.Platform_sdk_codename = &platformSdkCodename
4159 variables.Platform_version_active_codenames = []string{platformSdkCodename}
4160 }),
4161 )
4162 for _, testCase := range testCases {
4163 result := fixture.RunTestWithBp(t, fmt.Sprintf(bpTemplate, testCase.moduleType, testCase.targetSdkVersionInBp, testCase.testSuites))
4164 mytest := result.ModuleForTests("mytest", "android_common")
4165 manifestFixerArgs := mytest.Output("manifest_fixer/AndroidManifest.xml").Args["args"]
4166 android.AssertStringDoesContain(t, testCase.desc, manifestFixerArgs, "--targetSdkVersion "+testCase.targetSdkVersionExpected)
4167 }
4168}
Andrei Onea580636b2022-08-17 16:53:46 +00004169
4170func TestPrivappAllowlist(t *testing.T) {
4171 testJavaError(t, "privileged must be set in order to use privapp_allowlist", `
4172 android_app {
4173 name: "foo",
4174 srcs: ["a.java"],
4175 privapp_allowlist: "perms.xml",
4176 }
4177 `)
4178
4179 result := PrepareForTestWithJavaDefaultModules.RunTestWithBp(
4180 t,
4181 `
4182 android_app {
4183 name: "foo",
4184 srcs: ["a.java"],
Sam Delmerico15809f82023-05-15 17:21:47 -04004185 privapp_allowlist: "privapp_allowlist_com.android.foo.xml",
Andrei Onea580636b2022-08-17 16:53:46 +00004186 privileged: true,
Andrei Onea580636b2022-08-17 16:53:46 +00004187 sdk_version: "current",
4188 }
4189 override_android_app {
4190 name: "bar",
4191 base: "foo",
4192 package_name: "com.google.android.foo",
4193 }
4194 `,
4195 )
4196 app := result.ModuleForTests("foo", "android_common")
4197 overrideApp := result.ModuleForTests("foo", "android_common_bar")
4198
Sam Delmerico15809f82023-05-15 17:21:47 -04004199 // verify that privapp allowlist is created for override apps
Andrei Onea580636b2022-08-17 16:53:46 +00004200 overrideApp.Output("out/soong/.intermediates/foo/android_common_bar/privapp_allowlist_com.google.android.foo.xml")
Sam Delmerico15809f82023-05-15 17:21:47 -04004201 expectedAllowlistInput := "privapp_allowlist_com.android.foo.xml"
4202 overrideActualAllowlistInput := overrideApp.Rule("modifyAllowlist").Input.String()
4203 if expectedAllowlistInput != overrideActualAllowlistInput {
4204 t.Errorf("expected override allowlist to be %q; got %q", expectedAllowlistInput, overrideActualAllowlistInput)
Andrei Onea580636b2022-08-17 16:53:46 +00004205 }
4206
4207 // verify that permissions are copied to device
Anton Hansson0e486a42023-06-01 16:38:35 +00004208 app.Output("out/soong/target/product/test_device/system/etc/permissions/foo.xml")
4209 overrideApp.Output("out/soong/target/product/test_device/system/etc/permissions/bar.xml")
Andrei Onea580636b2022-08-17 16:53:46 +00004210}
Sam Delmericob1daccd2023-05-25 14:45:30 -04004211
4212func TestPrivappAllowlistAndroidMk(t *testing.T) {
4213 result := android.GroupFixturePreparers(
4214 PrepareForTestWithJavaDefaultModules,
4215 android.PrepareForTestWithAndroidMk,
4216 ).RunTestWithBp(
4217 t,
4218 `
4219 android_app {
4220 name: "foo",
4221 srcs: ["a.java"],
4222 privapp_allowlist: "privapp_allowlist_com.android.foo.xml",
4223 privileged: true,
4224 sdk_version: "current",
4225 }
4226 override_android_app {
4227 name: "bar",
4228 base: "foo",
4229 package_name: "com.google.android.foo",
4230 }
4231 `,
4232 )
4233 baseApp := result.ModuleForTests("foo", "android_common")
4234 overrideApp := result.ModuleForTests("foo", "android_common_bar")
4235
4236 baseAndroidApp := baseApp.Module().(*AndroidApp)
4237 baseEntries := android.AndroidMkEntriesForTest(t, result.TestContext, baseAndroidApp)[0]
4238 android.AssertStringMatches(
4239 t,
4240 "androidmk has incorrect LOCAL_SOONG_INSTALLED_MODULE; expected to find foo.apk",
4241 baseEntries.EntryMap["LOCAL_SOONG_INSTALLED_MODULE"][0],
4242 "\\S+foo.apk",
4243 )
4244 android.AssertStringMatches(
4245 t,
4246 "androidmk has incorrect LOCAL_SOONG_INSTALL_PAIRS; expected to it to include foo.apk",
4247 baseEntries.EntryMap["LOCAL_SOONG_INSTALL_PAIRS"][0],
4248 "\\S+foo.apk",
4249 )
4250 android.AssertStringMatches(
4251 t,
4252 "androidmk has incorrect LOCAL_SOONG_INSTALL_PAIRS; expected to it to include app",
4253 baseEntries.EntryMap["LOCAL_SOONG_INSTALL_PAIRS"][0],
4254 "\\S+foo.apk:\\S+/target/product/test_device/system/priv-app/foo/foo.apk",
4255 )
4256 android.AssertStringMatches(
4257 t,
4258 "androidmk has incorrect LOCAL_SOONG_INSTALL_PAIRS; expected to it to include privapp_allowlist",
4259 baseEntries.EntryMap["LOCAL_SOONG_INSTALL_PAIRS"][0],
Anton Hansson0e486a42023-06-01 16:38:35 +00004260 "privapp_allowlist_com.android.foo.xml:\\S+/target/product/test_device/system/etc/permissions/foo.xml",
Sam Delmericob1daccd2023-05-25 14:45:30 -04004261 )
4262
4263 overrideAndroidApp := overrideApp.Module().(*AndroidApp)
4264 overrideEntries := android.AndroidMkEntriesForTest(t, result.TestContext, overrideAndroidApp)[0]
4265 android.AssertStringMatches(
4266 t,
4267 "androidmk has incorrect LOCAL_SOONG_INSTALLED_MODULE; expected to find bar.apk",
4268 overrideEntries.EntryMap["LOCAL_SOONG_INSTALLED_MODULE"][0],
4269 "\\S+bar.apk",
4270 )
4271 android.AssertStringMatches(
4272 t,
4273 "androidmk has incorrect LOCAL_SOONG_INSTALL_PAIRS; expected to it to include bar.apk",
4274 overrideEntries.EntryMap["LOCAL_SOONG_INSTALL_PAIRS"][0],
4275 "\\S+bar.apk",
4276 )
4277 android.AssertStringMatches(
4278 t,
4279 "androidmk has incorrect LOCAL_SOONG_INSTALL_PAIRS; expected to it to include app",
4280 overrideEntries.EntryMap["LOCAL_SOONG_INSTALL_PAIRS"][0],
4281 "\\S+bar.apk:\\S+/target/product/test_device/system/priv-app/bar/bar.apk",
4282 )
4283 android.AssertStringMatches(
4284 t,
4285 "androidmk has incorrect LOCAL_SOONG_INSTALL_PAIRS; expected to it to include privapp_allowlist",
4286 overrideEntries.EntryMap["LOCAL_SOONG_INSTALL_PAIRS"][0],
Anton Hansson0e486a42023-06-01 16:38:35 +00004287 "\\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 -04004288 )
4289}
Sam Delmerico0e0d96e2023-08-18 22:43:28 +00004290
4291func TestApexGlobalMinSdkVersionOverride(t *testing.T) {
4292 result := android.GroupFixturePreparers(
4293 PrepareForTestWithJavaDefaultModules,
4294 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
4295 variables.ApexGlobalMinSdkVersionOverride = proptools.StringPtr("Tiramisu")
4296 }),
4297 ).RunTestWithBp(t, `
4298 android_app {
4299 name: "com.android.bar",
4300 srcs: ["a.java"],
4301 sdk_version: "current",
4302 }
4303 android_app {
4304 name: "com.android.foo",
4305 srcs: ["a.java"],
4306 sdk_version: "current",
4307 min_sdk_version: "S",
4308 updatable: true,
4309 }
4310 override_android_app {
4311 name: "com.android.go.foo",
4312 base: "com.android.foo",
4313 }
4314 `)
4315 foo := result.ModuleForTests("com.android.foo", "android_common").Rule("manifestFixer")
4316 fooOverride := result.ModuleForTests("com.android.foo", "android_common_com.android.go.foo").Rule("manifestFixer")
4317 bar := result.ModuleForTests("com.android.bar", "android_common").Rule("manifestFixer")
4318
4319 android.AssertStringDoesContain(t,
4320 "expected manifest fixer to set com.android.bar minSdkVersion to S",
4321 bar.BuildParams.Args["args"],
4322 "--minSdkVersion S",
4323 )
4324 android.AssertStringDoesContain(t,
4325 "com.android.foo: expected manifest fixer to set minSdkVersion to T",
4326 foo.BuildParams.Args["args"],
4327 "--minSdkVersion T",
4328 )
4329 android.AssertStringDoesContain(t,
4330 "com.android.go.foo: expected manifest fixer to set minSdkVersion to T",
4331 fooOverride.BuildParams.Args["args"],
4332 "--minSdkVersion T",
4333 )
4334
4335}
Jihoon Kang84b25892023-12-01 22:01:06 +00004336
4337func TestAppFlagsPackages(t *testing.T) {
4338 ctx := testApp(t, `
4339 android_app {
4340 name: "foo",
4341 srcs: ["a.java"],
4342 sdk_version: "current",
4343 flags_packages: [
4344 "bar",
4345 "baz",
4346 ],
4347 }
4348 aconfig_declarations {
4349 name: "bar",
Dennis Shen4e7773d2024-01-05 19:06:50 +00004350 package: "com.example.package.bar",
Jihoon Kang84b25892023-12-01 22:01:06 +00004351 srcs: [
4352 "bar.aconfig",
4353 ],
4354 }
4355 aconfig_declarations {
4356 name: "baz",
Dennis Shen4e7773d2024-01-05 19:06:50 +00004357 package: "com.example.package.baz",
Jihoon Kang84b25892023-12-01 22:01:06 +00004358 srcs: [
4359 "baz.aconfig",
4360 ],
4361 }
4362 `)
4363
4364 foo := ctx.ModuleForTests("foo", "android_common")
4365
4366 // android_app module depends on aconfig_declarations listed in flags_packages
4367 android.AssertBoolEquals(t, "foo expected to depend on bar", true,
4368 CheckModuleHasDependency(t, ctx, "foo", "android_common", "bar"))
4369
4370 android.AssertBoolEquals(t, "foo expected to depend on baz", true,
4371 CheckModuleHasDependency(t, ctx, "foo", "android_common", "baz"))
4372
4373 aapt2LinkRule := foo.Rule("android/soong/java.aapt2Link")
4374 linkInFlags := aapt2LinkRule.Args["inFlags"]
4375 android.AssertStringDoesContain(t,
4376 "aapt2 link command expected to pass feature flags arguments",
4377 linkInFlags,
4378 "--feature-flags @out/soong/.intermediates/bar/intermediate.txt --feature-flags @out/soong/.intermediates/baz/intermediate.txt",
4379 )
4380}
Spandan Das0727ba72024-02-13 16:37:43 +00004381
4382// Test that dexpreopt is disabled if an optional_uses_libs exists, but does not provide an implementation.
4383func TestNoDexpreoptOptionalUsesLibDoesNotHaveImpl(t *testing.T) {
4384 bp := `
4385 java_sdk_library_import {
4386 name: "sdklib_noimpl",
4387 public: {
4388 jars: ["stub.jar"],
4389 },
4390 }
4391 android_app {
4392 name: "app",
4393 srcs: ["a.java"],
4394 sdk_version: "current",
4395 optional_uses_libs: [
4396 "sdklib_noimpl",
4397 ],
4398 }
4399 `
4400 result := prepareForJavaTest.RunTestWithBp(t, bp)
4401 dexpreopt := result.ModuleForTests("app", "android_common").MaybeRule("dexpreopt").Rule
4402 android.AssertBoolEquals(t, "dexpreopt should be disabled if optional_uses_libs does not have an implementation", true, dexpreopt == nil)
4403}
yangbill2af0b6e2024-03-15 09:29:29 +00004404
4405func TestAppStem(t *testing.T) {
4406 ctx := testApp(t, `
4407 android_app {
4408 name: "foo",
4409 srcs: ["a.java"],
4410 stem: "foo-new",
4411 sdk_version: "current",
4412 }`)
4413
4414 foo := ctx.ModuleForTests("foo", "android_common")
4415
4416 outputs := fmt.Sprint(foo.AllOutputs())
4417 if !strings.Contains(outputs, "foo-new.apk") {
4418 t.Errorf("Module output does not contain expected apk %s", "foo-new.apk")
4419 }
4420}