blob: 73cf092275cee391c467c8149f3624bbeeb8d624 [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"
Paul Duffinfb0fe9f2021-03-22 17:31:52 +000030 "android/soong/genrule"
Colin Cross3bc7ffa2017-11-22 16:19:37 -080031)
32
Paul Duffin71ae5942021-03-22 15:36:52 +000033// testApp runs tests using the prepareForJavaTest
Paul Duffin0ed42d32021-03-13 02:19:32 +000034//
35// See testJava for an explanation as to how to stop using this deprecated method.
36//
37// deprecated
38func testApp(t *testing.T, bp string) *android.TestContext {
39 t.Helper()
Paul Duffin71ae5942021-03-22 15:36:52 +000040 result := prepareForJavaTest.RunTestWithBp(t, bp)
Paul Duffin0ed42d32021-03-13 02:19:32 +000041 return result.TestContext
42}
43
44func TestApp(t *testing.T) {
45 resourceFiles := []string{
Colin Cross3bc7ffa2017-11-22 16:19:37 -080046 "res/layout/layout.xml",
47 "res/values/strings.xml",
48 "res/values-en-rUS/strings.xml",
49 }
50
Paul Duffin0ed42d32021-03-13 02:19:32 +000051 compiledResourceFiles := []string{
Colin Cross3bc7ffa2017-11-22 16:19:37 -080052 "aapt2/res/layout_layout.xml.flat",
53 "aapt2/res/values_strings.arsc.flat",
54 "aapt2/res/values-en-rUS_strings.arsc.flat",
55 }
Colin Cross3bc7ffa2017-11-22 16:19:37 -080056
Colin Crossa97c5d32018-03-28 14:58:31 -070057 for _, moduleType := range []string{"android_app", "android_library"} {
58 t.Run(moduleType, func(t *testing.T) {
Paul Duffin71ae5942021-03-22 15:36:52 +000059 result := android.GroupFixturePreparers(
60 prepareForJavaTest,
Paul Duffin0ed42d32021-03-13 02:19:32 +000061 android.FixtureModifyMockFS(func(fs android.MockFS) {
62 for _, file := range resourceFiles {
63 fs[file] = nil
64 }
65 }),
66 ).RunTestWithBp(t, moduleType+` {
Colin Crossa97c5d32018-03-28 14:58:31 -070067 name: "foo",
68 srcs: ["a.java"],
Jeongik Cha538c0d02019-07-11 15:54:27 +090069 sdk_version: "current"
Colin Crossa97c5d32018-03-28 14:58:31 -070070 }
71 `)
Colin Cross3bc7ffa2017-11-22 16:19:37 -080072
Paul Duffin0ed42d32021-03-13 02:19:32 +000073 foo := result.ModuleForTests("foo", "android_common")
Colin Cross3bc7ffa2017-11-22 16:19:37 -080074
Colin Cross31656952018-05-24 16:11:20 -070075 var expectedLinkImplicits []string
76
77 manifestFixer := foo.Output("manifest_fixer/AndroidManifest.xml")
78 expectedLinkImplicits = append(expectedLinkImplicits, manifestFixer.Output.String())
Colin Cross3bc7ffa2017-11-22 16:19:37 -080079
Paul Duffin0ed42d32021-03-13 02:19:32 +000080 frameworkRes := result.ModuleForTests("framework-res", "android_common")
Colin Crossa97c5d32018-03-28 14:58:31 -070081 expectedLinkImplicits = append(expectedLinkImplicits,
82 frameworkRes.Output("package-res.apk").Output.String())
Colin Cross3bc7ffa2017-11-22 16:19:37 -080083
Colin Crossa97c5d32018-03-28 14:58:31 -070084 // Test the mapping from input files to compiled output file names
85 compile := foo.Output(compiledResourceFiles[0])
Paul Duffin0ed42d32021-03-13 02:19:32 +000086 android.AssertDeepEquals(t, "aapt2 compile inputs", resourceFiles, compile.Inputs.Strings())
Colin Crossb69301e2017-12-01 10:48:26 -080087
Colin Crossa97c5d32018-03-28 14:58:31 -070088 compiledResourceOutputs := compile.Outputs.Strings()
89 sort.Strings(compiledResourceOutputs)
Colin Crossb69301e2017-12-01 10:48:26 -080090
Colin Crossa97c5d32018-03-28 14:58:31 -070091 expectedLinkImplicits = append(expectedLinkImplicits, compiledResourceOutputs...)
Colin Cross3bc7ffa2017-11-22 16:19:37 -080092
Colin Crossa97c5d32018-03-28 14:58:31 -070093 list := foo.Output("aapt2/res.list")
94 expectedLinkImplicits = append(expectedLinkImplicits, list.Output.String())
Colin Cross3bc7ffa2017-11-22 16:19:37 -080095
Colin Crossa97c5d32018-03-28 14:58:31 -070096 // Check that the link rule uses
Paul Duffin0ed42d32021-03-13 02:19:32 +000097 res := result.ModuleForTests("foo", "android_common").Output("package-res.apk")
98 android.AssertDeepEquals(t, "aapt2 link implicits", expectedLinkImplicits, res.Implicits.Strings())
Colin Crossa97c5d32018-03-28 14:58:31 -070099 })
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800100 }
101}
Colin Cross890ff552017-11-30 20:13:19 -0800102
Colin Crosse560c4a2019-03-19 16:03:11 -0700103func TestAppSplits(t *testing.T) {
104 ctx := testApp(t, `
105 android_app {
106 name: "foo",
107 srcs: ["a.java"],
108 package_splits: ["v4", "v7,hdpi"],
Jeongik Cha538c0d02019-07-11 15:54:27 +0900109 sdk_version: "current"
Colin Crosse560c4a2019-03-19 16:03:11 -0700110 }`)
111
112 foo := ctx.ModuleForTests("foo", "android_common")
113
114 expectedOutputs := []string{
Paul Duffinfb0fe9f2021-03-22 17:31:52 +0000115 "out/soong/.intermediates/foo/android_common/foo.apk",
116 "out/soong/.intermediates/foo/android_common/foo_v4.apk",
117 "out/soong/.intermediates/foo/android_common/foo_v7_hdpi.apk",
Colin Crosse560c4a2019-03-19 16:03:11 -0700118 }
119 for _, expectedOutput := range expectedOutputs {
120 foo.Output(expectedOutput)
121 }
122
Colin Cross41955e82019-05-29 14:40:35 -0700123 outputFiles, err := foo.Module().(*AndroidApp).OutputFiles("")
124 if err != nil {
125 t.Fatal(err)
126 }
Paul Duffinfb0fe9f2021-03-22 17:31:52 +0000127 android.AssertPathsRelativeToTopEquals(t, `OutputFiles("")`, expectedOutputs, outputFiles)
Colin Crosse560c4a2019-03-19 16:03:11 -0700128}
129
Jeongik Cha538c0d02019-07-11 15:54:27 +0900130func TestPlatformAPIs(t *testing.T) {
131 testJava(t, `
132 android_app {
133 name: "foo",
134 srcs: ["a.java"],
135 platform_apis: true,
136 }
137 `)
138
139 testJava(t, `
140 android_app {
141 name: "foo",
142 srcs: ["a.java"],
143 sdk_version: "current",
144 }
145 `)
146
Spandan Das60999342021-11-16 04:15:33 +0000147 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 +0900148 android_app {
149 name: "bar",
150 srcs: ["b.java"],
151 }
152 `)
153
Spandan Das60999342021-11-16 04:15:33 +0000154 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 +0900155 android_app {
156 name: "bar",
157 srcs: ["b.java"],
158 sdk_version: "system_current",
159 platform_apis: true,
160 }
161 `)
162}
163
Jeongik Chae403e9e2019-12-07 00:16:24 +0900164func TestAndroidAppLinkType(t *testing.T) {
165 testJava(t, `
166 android_app {
167 name: "foo",
168 srcs: ["a.java"],
169 libs: ["bar"],
170 static_libs: ["baz"],
171 platform_apis: true,
172 }
173
174 java_library {
175 name: "bar",
176 sdk_version: "current",
177 srcs: ["b.java"],
178 }
179
180 android_library {
181 name: "baz",
182 sdk_version: "system_current",
183 srcs: ["c.java"],
184 }
185 `)
186
Steven Moreland00298982020-11-17 21:44:36 +0000187 testJavaError(t, "consider adjusting sdk_version: OR platform_apis:", `
Jeongik Chae403e9e2019-12-07 00:16:24 +0900188 android_app {
189 name: "foo",
190 srcs: ["a.java"],
191 libs: ["bar"],
192 sdk_version: "current",
193 static_libs: ["baz"],
194 }
195
196 java_library {
197 name: "bar",
198 sdk_version: "current",
199 srcs: ["b.java"],
200 }
201
202 android_library {
203 name: "baz",
204 sdk_version: "system_current",
205 srcs: ["c.java"],
206 }
207 `)
208
209 testJava(t, `
210 android_app {
211 name: "foo",
212 srcs: ["a.java"],
213 libs: ["bar"],
214 sdk_version: "system_current",
215 static_libs: ["baz"],
216 }
217
218 java_library {
219 name: "bar",
220 sdk_version: "current",
221 srcs: ["b.java"],
222 }
223
224 android_library {
225 name: "baz",
226 sdk_version: "system_current",
227 srcs: ["c.java"],
228 }
229 `)
230
Steven Moreland00298982020-11-17 21:44:36 +0000231 testJavaError(t, "consider adjusting sdk_version: OR platform_apis:", `
Jeongik Chae403e9e2019-12-07 00:16:24 +0900232 android_app {
233 name: "foo",
234 srcs: ["a.java"],
235 libs: ["bar"],
236 sdk_version: "system_current",
237 static_libs: ["baz"],
238 }
239
240 java_library {
241 name: "bar",
242 sdk_version: "current",
243 srcs: ["b.java"],
244 }
245
246 android_library {
247 name: "baz",
248 srcs: ["c.java"],
249 }
250 `)
251}
252
Artur Satayev2db1c3f2020-04-08 19:09:30 +0100253func TestUpdatableApps(t *testing.T) {
254 testCases := []struct {
255 name string
256 bp string
257 expectedError string
258 }{
259 {
260 name: "Stable public SDK",
261 bp: `android_app {
262 name: "foo",
263 srcs: ["a.java"],
264 sdk_version: "29",
Artur Satayevf40fc852020-04-16 13:43:02 +0100265 min_sdk_version: "29",
Artur Satayev2db1c3f2020-04-08 19:09:30 +0100266 updatable: true,
267 }`,
268 },
269 {
270 name: "Stable system SDK",
271 bp: `android_app {
272 name: "foo",
273 srcs: ["a.java"],
274 sdk_version: "system_29",
Artur Satayevf40fc852020-04-16 13:43:02 +0100275 min_sdk_version: "29",
Artur Satayev2db1c3f2020-04-08 19:09:30 +0100276 updatable: true,
277 }`,
278 },
279 {
280 name: "Current public SDK",
281 bp: `android_app {
282 name: "foo",
283 srcs: ["a.java"],
284 sdk_version: "current",
Artur Satayevf40fc852020-04-16 13:43:02 +0100285 min_sdk_version: "29",
Artur Satayev2db1c3f2020-04-08 19:09:30 +0100286 updatable: true,
287 }`,
288 },
289 {
290 name: "Current system SDK",
291 bp: `android_app {
292 name: "foo",
293 srcs: ["a.java"],
294 sdk_version: "system_current",
Artur Satayevf40fc852020-04-16 13:43:02 +0100295 min_sdk_version: "29",
Artur Satayev2db1c3f2020-04-08 19:09:30 +0100296 updatable: true,
297 }`,
298 },
299 {
300 name: "Current module SDK",
301 bp: `android_app {
302 name: "foo",
303 srcs: ["a.java"],
304 sdk_version: "module_current",
Artur Satayevf40fc852020-04-16 13:43:02 +0100305 min_sdk_version: "29",
Artur Satayev2db1c3f2020-04-08 19:09:30 +0100306 updatable: true,
307 }`,
308 },
309 {
310 name: "Current core SDK",
311 bp: `android_app {
312 name: "foo",
313 srcs: ["a.java"],
314 sdk_version: "core_current",
Artur Satayevf40fc852020-04-16 13:43:02 +0100315 min_sdk_version: "29",
Artur Satayev2db1c3f2020-04-08 19:09:30 +0100316 updatable: true,
317 }`,
318 },
319 {
320 name: "No Platform APIs",
321 bp: `android_app {
322 name: "foo",
323 srcs: ["a.java"],
324 platform_apis: true,
Artur Satayevf40fc852020-04-16 13:43:02 +0100325 min_sdk_version: "29",
Artur Satayev2db1c3f2020-04-08 19:09:30 +0100326 updatable: true,
327 }`,
328 expectedError: "Updatable apps must use stable SDKs",
329 },
330 {
331 name: "No Core Platform APIs",
332 bp: `android_app {
333 name: "foo",
334 srcs: ["a.java"],
335 sdk_version: "core_platform",
Artur Satayevf40fc852020-04-16 13:43:02 +0100336 min_sdk_version: "29",
Artur Satayev2db1c3f2020-04-08 19:09:30 +0100337 updatable: true,
338 }`,
339 expectedError: "Updatable apps must use stable SDKs",
340 },
341 {
342 name: "No unspecified APIs",
343 bp: `android_app {
344 name: "foo",
345 srcs: ["a.java"],
346 updatable: true,
Artur Satayevf40fc852020-04-16 13:43:02 +0100347 min_sdk_version: "29",
Artur Satayev2db1c3f2020-04-08 19:09:30 +0100348 }`,
349 expectedError: "Updatable apps must use stable SDK",
350 },
Artur Satayevf40fc852020-04-16 13:43:02 +0100351 {
352 name: "Must specify min_sdk_version",
353 bp: `android_app {
354 name: "app_without_min_sdk_version",
355 srcs: ["a.java"],
356 sdk_version: "29",
357 updatable: true,
358 }`,
359 expectedError: "updatable apps must set min_sdk_version.",
360 },
Artur Satayev2db1c3f2020-04-08 19:09:30 +0100361 }
362
363 for _, test := range testCases {
364 t.Run(test.name, func(t *testing.T) {
Paul Duffincdb88a92021-03-14 00:36:50 +0000365 errorHandler := android.FixtureExpectsNoErrors
366 if test.expectedError != "" {
367 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(test.expectedError)
Artur Satayev2db1c3f2020-04-08 19:09:30 +0100368 }
Paul Duffin71ae5942021-03-22 15:36:52 +0000369 android.GroupFixturePreparers(
370 prepareForJavaTest, FixtureWithPrebuiltApis(map[string][]string{
Paul Duffin2645a292021-03-13 02:36:00 +0000371 "29": {"foo"},
372 })).
Paul Duffincdb88a92021-03-14 00:36:50 +0000373 ExtendWithErrorHandler(errorHandler).RunTestWithBp(t, test.bp)
Artur Satayev2db1c3f2020-04-08 19:09:30 +0100374 })
375 }
376}
377
Jooyung Han749dc692020-04-15 11:03:39 +0900378func TestUpdatableApps_TransitiveDepsShouldSetMinSdkVersion(t *testing.T) {
379 testJavaError(t, `module "bar".*: should support min_sdk_version\(29\)`, cc.GatherRequiredDepsForTest(android.Android)+`
380 android_app {
381 name: "foo",
382 srcs: ["a.java"],
383 updatable: true,
384 sdk_version: "current",
385 min_sdk_version: "29",
386 static_libs: ["bar"],
387 }
388
389 java_library {
390 name: "bar",
391 sdk_version: "current",
392 }
393 `)
394}
395
Jooyung Hanbbc3fb72020-04-29 14:01:06 +0900396func TestUpdatableApps_JniLibsShouldShouldSupportMinSdkVersion(t *testing.T) {
397 testJava(t, cc.GatherRequiredDepsForTest(android.Android)+`
398 android_app {
399 name: "foo",
400 srcs: ["a.java"],
401 updatable: true,
402 sdk_version: "current",
403 min_sdk_version: "current",
404 jni_libs: ["libjni"],
405 }
406
407 cc_library {
408 name: "libjni",
409 stl: "none",
410 system_shared_libs: [],
411 sdk_version: "current",
412 }
413 `)
414}
415
416func TestUpdatableApps_JniLibShouldBeBuiltAgainstMinSdkVersion(t *testing.T) {
417 bp := cc.GatherRequiredDepsForTest(android.Android) + `
418 android_app {
419 name: "foo",
420 srcs: ["a.java"],
421 updatable: true,
422 sdk_version: "current",
423 min_sdk_version: "29",
424 jni_libs: ["libjni"],
425 }
426
427 cc_library {
428 name: "libjni",
429 stl: "none",
430 system_shared_libs: [],
431 sdk_version: "29",
432 }
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",
446 "android_arm64_armv8-a_sdk_29").Rule("partialLd").Output
447 expectedCrtEnd := ctx.ModuleForTests("crtend_so",
448 "android_arm64_armv8-a_sdk_29").Rule("partialLd").Output
449 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",
485 }
486 `
487 testJavaError(t, `"libjni" .*: sdk_version\(current\) is higher than min_sdk_version\(29\)`, bp)
488}
489
490func TestUpdatableApps_ErrorIfDepSdkVersionIsHigher(t *testing.T) {
491 bp := cc.GatherRequiredDepsForTest(android.Android) + `
492 android_app {
493 name: "foo",
494 srcs: ["a.java"],
495 updatable: true,
496 sdk_version: "current",
497 min_sdk_version: "29", // this APK should support 29
498 jni_libs: ["libjni"],
499 }
500
501 cc_library {
502 name: "libjni",
503 stl: "none",
504 shared_libs: ["libbar"],
505 system_shared_libs: [],
506 sdk_version: "27",
507 }
508
509 cc_library {
510 name: "libbar",
511 stl: "none",
512 system_shared_libs: [],
513 sdk_version: "current",
514 }
515 `
516 testJavaError(t, `"libjni" .*: links "libbar" built against newer API version "current"`, bp)
517}
518
Colin Cross0ddae7f2019-02-07 15:30:01 -0800519func TestResourceDirs(t *testing.T) {
520 testCases := []struct {
521 name string
522 prop string
523 resources []string
524 }{
525 {
526 name: "no resource_dirs",
527 prop: "",
528 resources: []string{"res/res/values/strings.xml"},
529 },
530 {
531 name: "resource_dirs",
532 prop: `resource_dirs: ["res"]`,
533 resources: []string{"res/res/values/strings.xml"},
534 },
535 {
536 name: "empty resource_dirs",
537 prop: `resource_dirs: []`,
538 resources: nil,
539 },
540 }
541
Paul Duffinfb0fe9f2021-03-22 17:31:52 +0000542 fs := android.MockFS{
Colin Cross0ddae7f2019-02-07 15:30:01 -0800543 "res/res/values/strings.xml": nil,
544 }
545
546 bp := `
547 android_app {
548 name: "foo",
Jeongik Cha538c0d02019-07-11 15:54:27 +0900549 sdk_version: "current",
Colin Cross0ddae7f2019-02-07 15:30:01 -0800550 %s
551 }
552 `
553
554 for _, testCase := range testCases {
555 t.Run(testCase.name, func(t *testing.T) {
Paul Duffinfb0fe9f2021-03-22 17:31:52 +0000556 result := android.GroupFixturePreparers(
557 PrepareForTestWithJavaDefaultModules,
558 PrepareForTestWithOverlayBuildComponents,
559 fs.AddToFixture(),
560 ).RunTestWithBp(t, fmt.Sprintf(bp, testCase.prop))
Colin Cross0ddae7f2019-02-07 15:30:01 -0800561
Paul Duffinfb0fe9f2021-03-22 17:31:52 +0000562 module := result.ModuleForTests("foo", "android_common")
Colin Cross0ddae7f2019-02-07 15:30:01 -0800563 resourceList := module.MaybeOutput("aapt2/res.list")
564
565 var resources []string
566 if resourceList.Rule != nil {
567 for _, compiledResource := range resourceList.Inputs.Strings() {
568 resources = append(resources, module.Output(compiledResource).Inputs.Strings()...)
569 }
570 }
571
Paul Duffinfb0fe9f2021-03-22 17:31:52 +0000572 android.AssertDeepEquals(t, "resource files", testCase.resources, resources)
Colin Cross0ddae7f2019-02-07 15:30:01 -0800573 })
574 }
575}
576
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800577func TestLibraryAssets(t *testing.T) {
578 bp := `
579 android_app {
580 name: "foo",
581 sdk_version: "current",
582 static_libs: ["lib1", "lib2", "lib3"],
583 }
584
585 android_library {
586 name: "lib1",
587 sdk_version: "current",
588 asset_dirs: ["assets_a"],
589 }
590
591 android_library {
592 name: "lib2",
593 sdk_version: "current",
594 }
595
596 android_library {
597 name: "lib3",
598 sdk_version: "current",
599 static_libs: ["lib4"],
600 }
601
602 android_library {
603 name: "lib4",
604 sdk_version: "current",
605 asset_dirs: ["assets_b"],
606 }
607 `
608
609 testCases := []struct {
610 name string
611 assetFlag string
612 assetPackages []string
613 }{
614 {
615 name: "foo",
616 // lib1 has its own asset. lib3 doesn't have any, but provides lib4's transitively.
617 assetPackages: []string{
Paul Duffinfb0fe9f2021-03-22 17:31:52 +0000618 "out/soong/.intermediates/foo/android_common/aapt2/package-res.apk",
619 "out/soong/.intermediates/lib1/android_common/assets.zip",
620 "out/soong/.intermediates/lib3/android_common/assets.zip",
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800621 },
622 },
623 {
624 name: "lib1",
625 assetFlag: "-A assets_a",
626 },
627 {
628 name: "lib2",
629 },
630 {
631 name: "lib3",
632 assetPackages: []string{
Paul Duffinfb0fe9f2021-03-22 17:31:52 +0000633 "out/soong/.intermediates/lib3/android_common/aapt2/package-res.apk",
634 "out/soong/.intermediates/lib4/android_common/assets.zip",
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800635 },
636 },
637 {
638 name: "lib4",
639 assetFlag: "-A assets_b",
640 },
641 }
642 ctx := testApp(t, bp)
643
644 for _, test := range testCases {
645 t.Run(test.name, func(t *testing.T) {
646 m := ctx.ModuleForTests(test.name, "android_common")
647
648 // Check asset flag in aapt2 link flags
649 var aapt2link android.TestingBuildParams
650 if len(test.assetPackages) > 0 {
651 aapt2link = m.Output("aapt2/package-res.apk")
652 } else {
653 aapt2link = m.Output("package-res.apk")
654 }
Paul Duffina71a67a2021-03-29 00:42:57 +0100655 aapt2link = aapt2link
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800656 aapt2Flags := aapt2link.Args["flags"]
657 if test.assetFlag != "" {
Paul Duffinfb0fe9f2021-03-22 17:31:52 +0000658 android.AssertStringDoesContain(t, "asset flag", aapt2Flags, test.assetFlag)
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800659 } else {
Paul Duffinfb0fe9f2021-03-22 17:31:52 +0000660 android.AssertStringDoesNotContain(t, "aapt2 link flags", aapt2Flags, " -A ")
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800661 }
662
663 // Check asset merge rule.
664 if len(test.assetPackages) > 0 {
665 mergeAssets := m.Output("package-res.apk")
Paul Duffinfb0fe9f2021-03-22 17:31:52 +0000666 android.AssertPathsRelativeToTopEquals(t, "mergeAssets inputs", test.assetPackages, mergeAssets.Inputs)
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800667 }
668 })
669 }
670}
671
Colin Crossb014f072021-02-26 14:54:36 -0800672func TestAppJavaResources(t *testing.T) {
673 bp := `
674 android_app {
675 name: "foo",
676 sdk_version: "current",
677 java_resources: ["resources/a"],
678 srcs: ["a.java"],
679 }
680
681 android_app {
682 name: "bar",
683 sdk_version: "current",
684 java_resources: ["resources/a"],
685 }
686 `
687
688 ctx := testApp(t, bp)
689
690 foo := ctx.ModuleForTests("foo", "android_common")
691 fooResources := foo.Output("res/foo.jar")
692 fooDexJar := foo.Output("dex-withres/foo.jar")
693 fooDexJarAligned := foo.Output("dex-withres-aligned/foo.jar")
694 fooApk := foo.Rule("combineApk")
695
696 if g, w := fooDexJar.Inputs.Strings(), fooResources.Output.String(); !android.InList(w, g) {
697 t.Errorf("expected resource jar %q in foo dex jar inputs %q", w, g)
698 }
699
700 if g, w := fooDexJarAligned.Input.String(), fooDexJar.Output.String(); g != w {
701 t.Errorf("expected dex jar %q in foo aligned dex jar inputs %q", w, g)
702 }
703
704 if g, w := fooApk.Inputs.Strings(), fooDexJarAligned.Output.String(); !android.InList(w, g) {
705 t.Errorf("expected aligned dex jar %q in foo apk inputs %q", w, g)
706 }
707
708 bar := ctx.ModuleForTests("bar", "android_common")
709 barResources := bar.Output("res/bar.jar")
710 barApk := bar.Rule("combineApk")
711
712 if g, w := barApk.Inputs.Strings(), barResources.Output.String(); !android.InList(w, g) {
713 t.Errorf("expected resources jar %q in bar apk inputs %q", w, g)
714 }
715}
716
Colin Crossbec85302019-02-13 13:15:46 -0800717func TestAndroidResources(t *testing.T) {
Colin Cross5c4791c2019-02-01 11:44:44 -0800718 testCases := []struct {
719 name string
720 enforceRROTargets []string
721 enforceRROExcludedOverlays []string
Colin Crossbec85302019-02-13 13:15:46 -0800722 resourceFiles map[string][]string
Colin Cross5c4791c2019-02-01 11:44:44 -0800723 overlayFiles map[string][]string
724 rroDirs map[string][]string
725 }{
726 {
727 name: "no RRO",
728 enforceRROTargets: nil,
729 enforceRROExcludedOverlays: nil,
Colin Crossbec85302019-02-13 13:15:46 -0800730 resourceFiles: map[string][]string{
731 "foo": nil,
732 "bar": {"bar/res/res/values/strings.xml"},
733 "lib": nil,
734 "lib2": {"lib2/res/res/values/strings.xml"},
735 },
Colin Cross5c4791c2019-02-01 11:44:44 -0800736 overlayFiles: map[string][]string{
Colin Crossbec85302019-02-13 13:15:46 -0800737 "foo": {
Paul Duffinfb0fe9f2021-03-22 17:31:52 +0000738 "out/soong/.intermediates/lib2/android_common/package-res.apk",
739 "out/soong/.intermediates/lib/android_common/package-res.apk",
740 "out/soong/.intermediates/lib3/android_common/package-res.apk",
Colin Cross6ed7dea2019-01-31 14:44:30 -0800741 "foo/res/res/values/strings.xml",
Colin Cross5c4791c2019-02-01 11:44:44 -0800742 "device/vendor/blah/static_overlay/foo/res/values/strings.xml",
743 "device/vendor/blah/overlay/foo/res/values/strings.xml",
Anton Hansson53c88442019-03-18 15:53:16 +0000744 "product/vendor/blah/overlay/foo/res/values/strings.xml",
Colin Cross5c4791c2019-02-01 11:44:44 -0800745 },
Colin Crossbec85302019-02-13 13:15:46 -0800746 "bar": {
Colin Cross5c4791c2019-02-01 11:44:44 -0800747 "device/vendor/blah/static_overlay/bar/res/values/strings.xml",
748 "device/vendor/blah/overlay/bar/res/values/strings.xml",
749 },
Colin Crossbec85302019-02-13 13:15:46 -0800750 "lib": {
Paul Duffinfb0fe9f2021-03-22 17:31:52 +0000751 "out/soong/.intermediates/lib2/android_common/package-res.apk",
Colin Crossbec85302019-02-13 13:15:46 -0800752 "lib/res/res/values/strings.xml",
753 "device/vendor/blah/overlay/lib/res/values/strings.xml",
754 },
Colin Cross5c4791c2019-02-01 11:44:44 -0800755 },
756 rroDirs: map[string][]string{
757 "foo": nil,
758 "bar": nil,
759 },
760 },
761 {
762 name: "enforce RRO on foo",
763 enforceRROTargets: []string{"foo"},
764 enforceRROExcludedOverlays: []string{"device/vendor/blah/static_overlay"},
Colin Crossbec85302019-02-13 13:15:46 -0800765 resourceFiles: map[string][]string{
766 "foo": nil,
767 "bar": {"bar/res/res/values/strings.xml"},
768 "lib": nil,
769 "lib2": {"lib2/res/res/values/strings.xml"},
770 },
Colin Cross5c4791c2019-02-01 11:44:44 -0800771 overlayFiles: map[string][]string{
Colin Crossbec85302019-02-13 13:15:46 -0800772 "foo": {
Paul Duffinfb0fe9f2021-03-22 17:31:52 +0000773 "out/soong/.intermediates/lib2/android_common/package-res.apk",
774 "out/soong/.intermediates/lib/android_common/package-res.apk",
775 "out/soong/.intermediates/lib3/android_common/package-res.apk",
Colin Cross6ed7dea2019-01-31 14:44:30 -0800776 "foo/res/res/values/strings.xml",
777 "device/vendor/blah/static_overlay/foo/res/values/strings.xml",
778 },
Colin Crossbec85302019-02-13 13:15:46 -0800779 "bar": {
Colin Cross5c4791c2019-02-01 11:44:44 -0800780 "device/vendor/blah/static_overlay/bar/res/values/strings.xml",
781 "device/vendor/blah/overlay/bar/res/values/strings.xml",
782 },
Colin Crossbec85302019-02-13 13:15:46 -0800783 "lib": {
Paul Duffinfb0fe9f2021-03-22 17:31:52 +0000784 "out/soong/.intermediates/lib2/android_common/package-res.apk",
Colin Crossbec85302019-02-13 13:15:46 -0800785 "lib/res/res/values/strings.xml",
Colin Crossbec85302019-02-13 13:15:46 -0800786 },
Colin Cross5c4791c2019-02-01 11:44:44 -0800787 },
Colin Crossc1c37552019-01-31 11:42:41 -0800788
Colin Cross5c4791c2019-02-01 11:44:44 -0800789 rroDirs: map[string][]string{
Colin Crossbec85302019-02-13 13:15:46 -0800790 "foo": {
Anton Hansson53c88442019-03-18 15:53:16 +0000791 "device:device/vendor/blah/overlay/foo/res",
Anton Hansson53c88442019-03-18 15:53:16 +0000792 "product:product/vendor/blah/overlay/foo/res",
Jaewoong Jungc779cd42020-10-06 18:56:10 -0700793 "device:device/vendor/blah/overlay/lib/res",
Colin Crossc1c37552019-01-31 11:42:41 -0800794 },
Colin Cross5c4791c2019-02-01 11:44:44 -0800795 "bar": nil,
Jaewoong Jungc779cd42020-10-06 18:56:10 -0700796 "lib": {"device:device/vendor/blah/overlay/lib/res"},
Colin Cross5c4791c2019-02-01 11:44:44 -0800797 },
798 },
799 {
800 name: "enforce RRO on all",
801 enforceRROTargets: []string{"*"},
802 enforceRROExcludedOverlays: []string{
803 // Excluding specific apps/res directories also allowed.
804 "device/vendor/blah/static_overlay/foo",
805 "device/vendor/blah/static_overlay/bar/res",
806 },
Colin Crossbec85302019-02-13 13:15:46 -0800807 resourceFiles: map[string][]string{
808 "foo": nil,
809 "bar": {"bar/res/res/values/strings.xml"},
810 "lib": nil,
811 "lib2": {"lib2/res/res/values/strings.xml"},
812 },
Colin Cross5c4791c2019-02-01 11:44:44 -0800813 overlayFiles: map[string][]string{
Colin Crossbec85302019-02-13 13:15:46 -0800814 "foo": {
Paul Duffinfb0fe9f2021-03-22 17:31:52 +0000815 "out/soong/.intermediates/lib2/android_common/package-res.apk",
816 "out/soong/.intermediates/lib/android_common/package-res.apk",
817 "out/soong/.intermediates/lib3/android_common/package-res.apk",
Colin Cross6ed7dea2019-01-31 14:44:30 -0800818 "foo/res/res/values/strings.xml",
819 "device/vendor/blah/static_overlay/foo/res/values/strings.xml",
820 },
Colin Crossbec85302019-02-13 13:15:46 -0800821 "bar": {"device/vendor/blah/static_overlay/bar/res/values/strings.xml"},
822 "lib": {
Paul Duffinfb0fe9f2021-03-22 17:31:52 +0000823 "out/soong/.intermediates/lib2/android_common/package-res.apk",
Colin Crossbec85302019-02-13 13:15:46 -0800824 "lib/res/res/values/strings.xml",
825 },
Colin Cross5c4791c2019-02-01 11:44:44 -0800826 },
827 rroDirs: map[string][]string{
Colin Crossbec85302019-02-13 13:15:46 -0800828 "foo": {
Anton Hansson53c88442019-03-18 15:53:16 +0000829 "device:device/vendor/blah/overlay/foo/res",
830 "product:product/vendor/blah/overlay/foo/res",
831 // Lib dep comes after the direct deps
832 "device:device/vendor/blah/overlay/lib/res",
Colin Crossc1c37552019-01-31 11:42:41 -0800833 },
Anton Hansson53c88442019-03-18 15:53:16 +0000834 "bar": {"device:device/vendor/blah/overlay/bar/res"},
835 "lib": {"device:device/vendor/blah/overlay/lib/res"},
Colin Cross5c4791c2019-02-01 11:44:44 -0800836 },
837 },
838 }
839
Anton Hansson53c88442019-03-18 15:53:16 +0000840 deviceResourceOverlays := []string{
Colin Cross890ff552017-11-30 20:13:19 -0800841 "device/vendor/blah/overlay",
842 "device/vendor/blah/overlay2",
843 "device/vendor/blah/static_overlay",
844 }
845
Anton Hansson53c88442019-03-18 15:53:16 +0000846 productResourceOverlays := []string{
847 "product/vendor/blah/overlay",
848 }
849
Paul Duffinfb0fe9f2021-03-22 17:31:52 +0000850 fs := android.MockFS{
Colin Cross890ff552017-11-30 20:13:19 -0800851 "foo/res/res/values/strings.xml": nil,
852 "bar/res/res/values/strings.xml": nil,
Colin Cross6ed7dea2019-01-31 14:44:30 -0800853 "lib/res/res/values/strings.xml": nil,
Colin Crossbec85302019-02-13 13:15:46 -0800854 "lib2/res/res/values/strings.xml": nil,
Colin Cross890ff552017-11-30 20:13:19 -0800855 "device/vendor/blah/overlay/foo/res/values/strings.xml": nil,
856 "device/vendor/blah/overlay/bar/res/values/strings.xml": nil,
Colin Cross6ed7dea2019-01-31 14:44:30 -0800857 "device/vendor/blah/overlay/lib/res/values/strings.xml": nil,
Colin Cross890ff552017-11-30 20:13:19 -0800858 "device/vendor/blah/static_overlay/foo/res/values/strings.xml": nil,
859 "device/vendor/blah/static_overlay/bar/res/values/strings.xml": nil,
860 "device/vendor/blah/overlay2/res/values/strings.xml": nil,
Anton Hansson53c88442019-03-18 15:53:16 +0000861 "product/vendor/blah/overlay/foo/res/values/strings.xml": nil,
Colin Cross890ff552017-11-30 20:13:19 -0800862 }
863
864 bp := `
865 android_app {
866 name: "foo",
Jeongik Cha538c0d02019-07-11 15:54:27 +0900867 sdk_version: "current",
Colin Cross890ff552017-11-30 20:13:19 -0800868 resource_dirs: ["foo/res"],
Anton Hansson53c88442019-03-18 15:53:16 +0000869 static_libs: ["lib", "lib3"],
Colin Cross890ff552017-11-30 20:13:19 -0800870 }
871
872 android_app {
873 name: "bar",
Jeongik Cha538c0d02019-07-11 15:54:27 +0900874 sdk_version: "current",
Colin Cross890ff552017-11-30 20:13:19 -0800875 resource_dirs: ["bar/res"],
876 }
Colin Cross6ed7dea2019-01-31 14:44:30 -0800877
878 android_library {
879 name: "lib",
Jeongik Cha75b83b02019-11-01 15:28:00 +0900880 sdk_version: "current",
Colin Cross6ed7dea2019-01-31 14:44:30 -0800881 resource_dirs: ["lib/res"],
Colin Crossbec85302019-02-13 13:15:46 -0800882 static_libs: ["lib2"],
883 }
884
885 android_library {
886 name: "lib2",
Jeongik Cha75b83b02019-11-01 15:28:00 +0900887 sdk_version: "current",
Colin Crossbec85302019-02-13 13:15:46 -0800888 resource_dirs: ["lib2/res"],
Colin Cross6ed7dea2019-01-31 14:44:30 -0800889 }
Anton Hansson53c88442019-03-18 15:53:16 +0000890
891 // This library has the same resources as lib (should not lead to dupe RROs)
892 android_library {
893 name: "lib3",
Jeongik Cha75b83b02019-11-01 15:28:00 +0900894 sdk_version: "current",
Anton Hansson53c88442019-03-18 15:53:16 +0000895 resource_dirs: ["lib/res"]
896 }
Colin Cross890ff552017-11-30 20:13:19 -0800897 `
898
Colin Cross5c4791c2019-02-01 11:44:44 -0800899 for _, testCase := range testCases {
Colin Cross890ff552017-11-30 20:13:19 -0800900 t.Run(testCase.name, func(t *testing.T) {
Paul Duffinfb0fe9f2021-03-22 17:31:52 +0000901 result := android.GroupFixturePreparers(
902 PrepareForTestWithJavaDefaultModules,
903 PrepareForTestWithOverlayBuildComponents,
904 fs.AddToFixture(),
905 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
906 variables.DeviceResourceOverlays = deviceResourceOverlays
907 variables.ProductResourceOverlays = productResourceOverlays
908 if testCase.enforceRROTargets != nil {
909 variables.EnforceRROTargets = testCase.enforceRROTargets
910 }
911 if testCase.enforceRROExcludedOverlays != nil {
912 variables.EnforceRROExcludedOverlays = testCase.enforceRROExcludedOverlays
913 }
914 }),
915 ).RunTestWithBp(t, bp)
Colin Cross890ff552017-11-30 20:13:19 -0800916
Colin Crossbec85302019-02-13 13:15:46 -0800917 resourceListToFiles := func(module android.TestingModule, list []string) (files []string) {
918 for _, o := range list {
919 res := module.MaybeOutput(o)
920 if res.Rule != nil {
921 // If the overlay is compiled as part of this module (i.e. a .arsc.flat file),
922 // verify the inputs to the .arsc.flat rule.
923 files = append(files, res.Inputs.Strings()...)
924 } else {
925 // Otherwise, verify the full path to the output of the other module
926 files = append(files, o)
Anton Hansson94c93f32019-01-30 16:03:37 +0000927 }
Colin Cross890ff552017-11-30 20:13:19 -0800928 }
Colin Crossbec85302019-02-13 13:15:46 -0800929 return files
Colin Cross890ff552017-11-30 20:13:19 -0800930 }
931
Colin Crossbec85302019-02-13 13:15:46 -0800932 getResources := func(moduleName string) (resourceFiles, overlayFiles, rroDirs []string) {
Paul Duffinfb0fe9f2021-03-22 17:31:52 +0000933 module := result.ModuleForTests(moduleName, "android_common")
Colin Crossbec85302019-02-13 13:15:46 -0800934 resourceList := module.MaybeOutput("aapt2/res.list")
935 if resourceList.Rule != nil {
Paul Duffinfb0fe9f2021-03-22 17:31:52 +0000936 resourceFiles = resourceListToFiles(module, android.PathsRelativeToTop(resourceList.Inputs))
Anton Hansson0375a4f2019-01-24 14:39:19 +0000937 }
Colin Crossbec85302019-02-13 13:15:46 -0800938 overlayList := module.MaybeOutput("aapt2/overlay.list")
939 if overlayList.Rule != nil {
Paul Duffinfb0fe9f2021-03-22 17:31:52 +0000940 overlayFiles = resourceListToFiles(module, android.PathsRelativeToTop(overlayList.Inputs))
Colin Crossbec85302019-02-13 13:15:46 -0800941 }
942
Anton Hansson53c88442019-03-18 15:53:16 +0000943 for _, d := range module.Module().(AndroidLibraryDependency).ExportedRRODirs() {
944 var prefix string
945 if d.overlayType == device {
946 prefix = "device:"
947 } else if d.overlayType == product {
948 prefix = "product:"
949 } else {
950 t.Fatalf("Unexpected overlayType %d", d.overlayType)
951 }
Paul Duffinfb0fe9f2021-03-22 17:31:52 +0000952 rroDirs = append(rroDirs, prefix+android.PathRelativeToTop(d.path))
Anton Hansson53c88442019-03-18 15:53:16 +0000953 }
Colin Crossbec85302019-02-13 13:15:46 -0800954
955 return resourceFiles, overlayFiles, rroDirs
956 }
957
958 modules := []string{"foo", "bar", "lib", "lib2"}
959 for _, module := range modules {
960 resourceFiles, overlayFiles, rroDirs := getResources(module)
961
962 if !reflect.DeepEqual(resourceFiles, testCase.resourceFiles[module]) {
963 t.Errorf("expected %s resource files:\n %#v\n got:\n %#v",
964 module, testCase.resourceFiles[module], resourceFiles)
965 }
966 if !reflect.DeepEqual(overlayFiles, testCase.overlayFiles[module]) {
967 t.Errorf("expected %s overlay files:\n %#v\n got:\n %#v",
968 module, testCase.overlayFiles[module], overlayFiles)
969 }
970 if !reflect.DeepEqual(rroDirs, testCase.rroDirs[module]) {
Anton Hansson0375a4f2019-01-24 14:39:19 +0000971 t.Errorf("expected %s rroDirs: %#v\n got:\n %#v",
Colin Crossbec85302019-02-13 13:15:46 -0800972 module, testCase.rroDirs[module], rroDirs)
Anton Hansson0375a4f2019-01-24 14:39:19 +0000973 }
Colin Cross890ff552017-11-30 20:13:19 -0800974 }
Colin Cross890ff552017-11-30 20:13:19 -0800975 })
976 }
977}
Colin Crossd09b0b62018-04-18 11:06:47 -0700978
Paul Duffincdb88a92021-03-14 00:36:50 +0000979func checkSdkVersion(t *testing.T, result *android.TestResult, expectedSdkVersion string) {
980 foo := result.ModuleForTests("foo", "android_common")
Jeongik Cha219141c2020-08-06 23:00:37 +0900981 link := foo.Output("package-res.apk")
982 linkFlags := strings.Split(link.Args["flags"], " ")
983 min := android.IndexList("--min-sdk-version", linkFlags)
984 target := android.IndexList("--target-sdk-version", linkFlags)
985
986 if min == -1 || target == -1 || min == len(linkFlags)-1 || target == len(linkFlags)-1 {
987 t.Fatalf("missing --min-sdk-version or --target-sdk-version in link flags: %q", linkFlags)
988 }
989
990 gotMinSdkVersion := linkFlags[min+1]
991 gotTargetSdkVersion := linkFlags[target+1]
992
Paul Duffincdb88a92021-03-14 00:36:50 +0000993 android.AssertStringEquals(t, "incorrect --min-sdk-version", expectedSdkVersion, gotMinSdkVersion)
Jeongik Cha219141c2020-08-06 23:00:37 +0900994
Paul Duffincdb88a92021-03-14 00:36:50 +0000995 android.AssertStringEquals(t, "incorrect --target-sdk-version", expectedSdkVersion, gotTargetSdkVersion)
Jeongik Cha219141c2020-08-06 23:00:37 +0900996}
997
Colin Crossd09b0b62018-04-18 11:06:47 -0700998func TestAppSdkVersion(t *testing.T) {
999 testCases := []struct {
1000 name string
1001 sdkVersion string
1002 platformSdkInt int
1003 platformSdkCodename string
1004 platformSdkFinal bool
1005 expectedMinSdkVersion string
Jeongik Cha538c0d02019-07-11 15:54:27 +09001006 platformApis bool
Dan Albert4f378d72020-07-23 17:32:15 -07001007 activeCodenames []string
Colin Crossd09b0b62018-04-18 11:06:47 -07001008 }{
1009 {
1010 name: "current final SDK",
1011 sdkVersion: "current",
1012 platformSdkInt: 27,
1013 platformSdkCodename: "REL",
1014 platformSdkFinal: true,
1015 expectedMinSdkVersion: "27",
1016 },
1017 {
1018 name: "current non-final SDK",
1019 sdkVersion: "current",
1020 platformSdkInt: 27,
1021 platformSdkCodename: "OMR1",
1022 platformSdkFinal: false,
1023 expectedMinSdkVersion: "OMR1",
Dan Albert4f378d72020-07-23 17:32:15 -07001024 activeCodenames: []string{"OMR1"},
Colin Crossd09b0b62018-04-18 11:06:47 -07001025 },
1026 {
1027 name: "default final SDK",
1028 sdkVersion: "",
Jeongik Cha538c0d02019-07-11 15:54:27 +09001029 platformApis: true,
Colin Crossd09b0b62018-04-18 11:06:47 -07001030 platformSdkInt: 27,
1031 platformSdkCodename: "REL",
1032 platformSdkFinal: true,
1033 expectedMinSdkVersion: "27",
1034 },
1035 {
1036 name: "default non-final SDK",
1037 sdkVersion: "",
Jeongik Cha538c0d02019-07-11 15:54:27 +09001038 platformApis: true,
Colin Crossd09b0b62018-04-18 11:06:47 -07001039 platformSdkInt: 27,
1040 platformSdkCodename: "OMR1",
1041 platformSdkFinal: false,
1042 expectedMinSdkVersion: "OMR1",
Dan Albert4f378d72020-07-23 17:32:15 -07001043 activeCodenames: []string{"OMR1"},
Colin Crossd09b0b62018-04-18 11:06:47 -07001044 },
1045 {
1046 name: "14",
1047 sdkVersion: "14",
1048 expectedMinSdkVersion: "14",
Dan Albert4f378d72020-07-23 17:32:15 -07001049 platformSdkCodename: "S",
1050 activeCodenames: []string{"S"},
Colin Crossd09b0b62018-04-18 11:06:47 -07001051 },
1052 }
1053
1054 for _, moduleType := range []string{"android_app", "android_library"} {
1055 for _, test := range testCases {
1056 t.Run(moduleType+" "+test.name, func(t *testing.T) {
Jeongik Cha538c0d02019-07-11 15:54:27 +09001057 platformApiProp := ""
1058 if test.platformApis {
1059 platformApiProp = "platform_apis: true,"
1060 }
Colin Crossd09b0b62018-04-18 11:06:47 -07001061 bp := fmt.Sprintf(`%s {
1062 name: "foo",
1063 srcs: ["a.java"],
1064 sdk_version: "%s",
Jeongik Cha538c0d02019-07-11 15:54:27 +09001065 %s
1066 }`, moduleType, test.sdkVersion, platformApiProp)
Colin Crossd09b0b62018-04-18 11:06:47 -07001067
Paul Duffin71ae5942021-03-22 15:36:52 +00001068 result := android.GroupFixturePreparers(
1069 prepareForJavaTest,
Paul Duffincdb88a92021-03-14 00:36:50 +00001070 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1071 variables.Platform_sdk_version = &test.platformSdkInt
1072 variables.Platform_sdk_codename = &test.platformSdkCodename
1073 variables.Platform_version_active_codenames = test.activeCodenames
1074 variables.Platform_sdk_final = &test.platformSdkFinal
1075 }),
Paul Duffin2645a292021-03-13 02:36:00 +00001076 FixtureWithPrebuiltApis(map[string][]string{
1077 "14": {"foo"},
1078 }),
Paul Duffincdb88a92021-03-14 00:36:50 +00001079 ).RunTestWithBp(t, bp)
Colin Crossd09b0b62018-04-18 11:06:47 -07001080
Paul Duffincdb88a92021-03-14 00:36:50 +00001081 checkSdkVersion(t, result, test.expectedMinSdkVersion)
Colin Crossd09b0b62018-04-18 11:06:47 -07001082 })
1083 }
1084 }
1085}
Colin Crossa4f08812018-10-02 22:03:40 -07001086
Jeongik Cha219141c2020-08-06 23:00:37 +09001087func TestVendorAppSdkVersion(t *testing.T) {
1088 testCases := []struct {
1089 name string
1090 sdkVersion string
1091 platformSdkInt int
1092 platformSdkCodename string
1093 platformSdkFinal bool
1094 deviceCurrentApiLevelForVendorModules string
1095 expectedMinSdkVersion string
1096 }{
1097 {
1098 name: "current final SDK",
1099 sdkVersion: "current",
1100 platformSdkInt: 29,
1101 platformSdkCodename: "REL",
1102 platformSdkFinal: true,
1103 deviceCurrentApiLevelForVendorModules: "29",
1104 expectedMinSdkVersion: "29",
1105 },
1106 {
1107 name: "current final SDK",
1108 sdkVersion: "current",
1109 platformSdkInt: 29,
1110 platformSdkCodename: "REL",
1111 platformSdkFinal: true,
1112 deviceCurrentApiLevelForVendorModules: "28",
1113 expectedMinSdkVersion: "28",
1114 },
1115 {
1116 name: "current final SDK",
1117 sdkVersion: "current",
1118 platformSdkInt: 29,
1119 platformSdkCodename: "Q",
1120 platformSdkFinal: false,
Jeongik Cha219141c2020-08-06 23:00:37 +09001121 deviceCurrentApiLevelForVendorModules: "28",
1122 expectedMinSdkVersion: "28",
1123 },
1124 }
1125
1126 for _, moduleType := range []string{"android_app", "android_library"} {
1127 for _, sdkKind := range []string{"", "system_"} {
1128 for _, test := range testCases {
1129 t.Run(moduleType+" "+test.name, func(t *testing.T) {
1130 bp := fmt.Sprintf(`%s {
1131 name: "foo",
1132 srcs: ["a.java"],
1133 sdk_version: "%s%s",
1134 vendor: true,
1135 }`, moduleType, sdkKind, test.sdkVersion)
1136
Paul Duffin71ae5942021-03-22 15:36:52 +00001137 result := android.GroupFixturePreparers(
1138 prepareForJavaTest,
Paul Duffincdb88a92021-03-14 00:36:50 +00001139 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1140 variables.Platform_sdk_version = &test.platformSdkInt
1141 variables.Platform_sdk_codename = &test.platformSdkCodename
1142 variables.Platform_sdk_final = &test.platformSdkFinal
1143 variables.DeviceCurrentApiLevelForVendorModules = &test.deviceCurrentApiLevelForVendorModules
1144 variables.DeviceSystemSdkVersions = []string{"28", "29"}
1145 }),
Paul Duffin2645a292021-03-13 02:36:00 +00001146 FixtureWithPrebuiltApis(map[string][]string{
1147 "28": {"foo"},
1148 "29": {"foo"},
1149 "current": {"foo"},
1150 }),
Paul Duffincdb88a92021-03-14 00:36:50 +00001151 ).RunTestWithBp(t, bp)
1152
1153 checkSdkVersion(t, result, test.expectedMinSdkVersion)
Jeongik Cha219141c2020-08-06 23:00:37 +09001154 })
1155 }
1156 }
1157 }
1158}
1159
Paul Duffin50c217c2019-06-12 13:25:22 +01001160func TestJNIABI(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001161 ctx, _ := testJava(t, cc.GatherRequiredDepsForTest(android.Android)+`
Paul Duffin50c217c2019-06-12 13:25:22 +01001162 cc_library {
1163 name: "libjni",
1164 system_shared_libs: [],
Colin Crossc511bc52020-04-07 16:50:32 +00001165 sdk_version: "current",
Paul Duffin50c217c2019-06-12 13:25:22 +01001166 stl: "none",
1167 }
1168
1169 android_test {
1170 name: "test",
1171 sdk_version: "core_platform",
1172 jni_libs: ["libjni"],
1173 }
1174
1175 android_test {
1176 name: "test_first",
1177 sdk_version: "core_platform",
1178 compile_multilib: "first",
1179 jni_libs: ["libjni"],
1180 }
1181
1182 android_test {
1183 name: "test_both",
1184 sdk_version: "core_platform",
1185 compile_multilib: "both",
1186 jni_libs: ["libjni"],
1187 }
1188
1189 android_test {
1190 name: "test_32",
1191 sdk_version: "core_platform",
1192 compile_multilib: "32",
1193 jni_libs: ["libjni"],
1194 }
1195
1196 android_test {
1197 name: "test_64",
1198 sdk_version: "core_platform",
1199 compile_multilib: "64",
1200 jni_libs: ["libjni"],
1201 }
1202 `)
1203
1204 testCases := []struct {
1205 name string
1206 abis []string
1207 }{
1208 {"test", []string{"arm64-v8a"}},
1209 {"test_first", []string{"arm64-v8a"}},
1210 {"test_both", []string{"arm64-v8a", "armeabi-v7a"}},
1211 {"test_32", []string{"armeabi-v7a"}},
1212 {"test_64", []string{"arm64-v8a"}},
1213 }
1214
1215 for _, test := range testCases {
1216 t.Run(test.name, func(t *testing.T) {
1217 app := ctx.ModuleForTests(test.name, "android_common")
1218 jniLibZip := app.Output("jnilibs.zip")
1219 var abis []string
1220 args := strings.Fields(jniLibZip.Args["jarArgs"])
1221 for i := 0; i < len(args); i++ {
1222 if args[i] == "-P" {
1223 abis = append(abis, filepath.Base(args[i+1]))
1224 i++
1225 }
1226 }
1227 if !reflect.DeepEqual(abis, test.abis) {
1228 t.Errorf("want abis %v, got %v", test.abis, abis)
1229 }
1230 })
1231 }
1232}
1233
Jeongik Cha2cc570d2019-10-29 15:44:45 +09001234func TestAppSdkVersionByPartition(t *testing.T) {
1235 testJavaError(t, "sdk_version must have a value when the module is located at vendor or product", `
1236 android_app {
1237 name: "foo",
1238 srcs: ["a.java"],
1239 vendor: true,
1240 platform_apis: true,
1241 }
1242 `)
1243
1244 testJava(t, `
1245 android_app {
1246 name: "bar",
1247 srcs: ["b.java"],
1248 platform_apis: true,
1249 }
1250 `)
1251
1252 for _, enforce := range []bool{true, false} {
Jeongik Cha2cc570d2019-10-29 15:44:45 +09001253 bp := `
1254 android_app {
1255 name: "foo",
1256 srcs: ["a.java"],
1257 product_specific: true,
1258 platform_apis: true,
1259 }
1260 `
Colin Cross98be1bb2019-12-13 20:41:13 -08001261
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00001262 errorHandler := android.FixtureExpectsNoErrors
Jeongik Cha2cc570d2019-10-29 15:44:45 +09001263 if enforce {
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00001264 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 +09001265 }
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00001266
1267 android.GroupFixturePreparers(
1268 PrepareForTestWithJavaDefaultModules,
1269 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1270 variables.EnforceProductPartitionInterface = proptools.BoolPtr(enforce)
1271 }),
1272 ).
1273 ExtendWithErrorHandler(errorHandler).
1274 RunTestWithBp(t, bp)
Jeongik Cha2cc570d2019-10-29 15:44:45 +09001275 }
1276}
1277
Paul Duffin50c217c2019-06-12 13:25:22 +01001278func TestJNIPackaging(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001279 ctx, _ := testJava(t, cc.GatherRequiredDepsForTest(android.Android)+`
Paul Duffin50c217c2019-06-12 13:25:22 +01001280 cc_library {
1281 name: "libjni",
1282 system_shared_libs: [],
1283 stl: "none",
Colin Cross094cde42020-02-15 10:38:00 -08001284 sdk_version: "current",
Paul Duffin50c217c2019-06-12 13:25:22 +01001285 }
1286
1287 android_app {
1288 name: "app",
1289 jni_libs: ["libjni"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09001290 sdk_version: "current",
Paul Duffin50c217c2019-06-12 13:25:22 +01001291 }
1292
1293 android_app {
1294 name: "app_noembed",
1295 jni_libs: ["libjni"],
1296 use_embedded_native_libs: false,
Jeongik Cha538c0d02019-07-11 15:54:27 +09001297 sdk_version: "current",
Paul Duffin50c217c2019-06-12 13:25:22 +01001298 }
1299
1300 android_app {
1301 name: "app_embed",
1302 jni_libs: ["libjni"],
1303 use_embedded_native_libs: true,
Jeongik Cha538c0d02019-07-11 15:54:27 +09001304 sdk_version: "current",
Paul Duffin50c217c2019-06-12 13:25:22 +01001305 }
1306
1307 android_test {
1308 name: "test",
Colin Crossc511bc52020-04-07 16:50:32 +00001309 sdk_version: "current",
Paul Duffin50c217c2019-06-12 13:25:22 +01001310 jni_libs: ["libjni"],
1311 }
1312
1313 android_test {
1314 name: "test_noembed",
Colin Crossc511bc52020-04-07 16:50:32 +00001315 sdk_version: "current",
Paul Duffin50c217c2019-06-12 13:25:22 +01001316 jni_libs: ["libjni"],
1317 use_embedded_native_libs: false,
1318 }
1319
1320 android_test_helper_app {
1321 name: "test_helper",
Colin Crossc511bc52020-04-07 16:50:32 +00001322 sdk_version: "current",
Paul Duffin50c217c2019-06-12 13:25:22 +01001323 jni_libs: ["libjni"],
1324 }
1325
1326 android_test_helper_app {
1327 name: "test_helper_noembed",
Colin Crossc511bc52020-04-07 16:50:32 +00001328 sdk_version: "current",
Paul Duffin50c217c2019-06-12 13:25:22 +01001329 jni_libs: ["libjni"],
1330 use_embedded_native_libs: false,
1331 }
1332 `)
1333
1334 testCases := []struct {
1335 name string
1336 packaged bool
1337 compressed bool
1338 }{
1339 {"app", false, false},
1340 {"app_noembed", false, false},
1341 {"app_embed", true, false},
1342 {"test", true, false},
1343 {"test_noembed", true, true},
1344 {"test_helper", true, false},
1345 {"test_helper_noembed", true, true},
1346 }
1347
1348 for _, test := range testCases {
1349 t.Run(test.name, func(t *testing.T) {
1350 app := ctx.ModuleForTests(test.name, "android_common")
1351 jniLibZip := app.MaybeOutput("jnilibs.zip")
1352 if g, w := (jniLibZip.Rule != nil), test.packaged; g != w {
1353 t.Errorf("expected jni packaged %v, got %v", w, g)
1354 }
1355
1356 if jniLibZip.Rule != nil {
1357 if g, w := !strings.Contains(jniLibZip.Args["jarArgs"], "-L 0"), test.compressed; g != w {
1358 t.Errorf("expected jni compressed %v, got %v", w, g)
1359 }
Colin Crossc511bc52020-04-07 16:50:32 +00001360
1361 if !strings.Contains(jniLibZip.Implicits[0].String(), "_sdk_") {
1362 t.Errorf("expected input %q to use sdk variant", jniLibZip.Implicits[0].String())
1363 }
Paul Duffin50c217c2019-06-12 13:25:22 +01001364 }
1365 })
1366 }
Colin Cross47fa9d32019-03-26 10:51:39 -07001367}
1368
Colin Cross3c007702020-05-08 11:20:24 -07001369func TestJNISDK(t *testing.T) {
1370 ctx, _ := testJava(t, cc.GatherRequiredDepsForTest(android.Android)+`
1371 cc_library {
1372 name: "libjni",
1373 system_shared_libs: [],
1374 stl: "none",
1375 sdk_version: "current",
1376 }
1377
1378 android_test {
1379 name: "app_platform",
1380 jni_libs: ["libjni"],
1381 platform_apis: true,
1382 }
1383
1384 android_test {
1385 name: "app_sdk",
1386 jni_libs: ["libjni"],
1387 sdk_version: "current",
1388 }
1389
1390 android_test {
1391 name: "app_force_platform",
1392 jni_libs: ["libjni"],
1393 sdk_version: "current",
1394 jni_uses_platform_apis: true,
1395 }
1396
1397 android_test {
1398 name: "app_force_sdk",
1399 jni_libs: ["libjni"],
1400 platform_apis: true,
1401 jni_uses_sdk_apis: true,
1402 }
Colin Crossc2d24052020-05-13 11:05:02 -07001403
1404 cc_library {
1405 name: "libvendorjni",
1406 system_shared_libs: [],
1407 stl: "none",
1408 vendor: true,
1409 }
1410
1411 android_test {
1412 name: "app_vendor",
1413 jni_libs: ["libvendorjni"],
1414 sdk_version: "current",
1415 vendor: true,
1416 }
Colin Cross3c007702020-05-08 11:20:24 -07001417 `)
1418
1419 testCases := []struct {
Colin Crossc2d24052020-05-13 11:05:02 -07001420 name string
1421 sdkJNI bool
1422 vendorJNI bool
Colin Cross3c007702020-05-08 11:20:24 -07001423 }{
Colin Crossc2d24052020-05-13 11:05:02 -07001424 {name: "app_platform"},
1425 {name: "app_sdk", sdkJNI: true},
1426 {name: "app_force_platform"},
1427 {name: "app_force_sdk", sdkJNI: true},
1428 {name: "app_vendor", vendorJNI: true},
Colin Cross3c007702020-05-08 11:20:24 -07001429 }
1430
Colin Crossc2d24052020-05-13 11:05:02 -07001431 platformJNI := ctx.ModuleForTests("libjni", "android_arm64_armv8-a_shared").
1432 Output("libjni.so").Output.String()
1433 sdkJNI := ctx.ModuleForTests("libjni", "android_arm64_armv8-a_sdk_shared").
1434 Output("libjni.so").Output.String()
1435 vendorJNI := ctx.ModuleForTests("libvendorjni", "android_arm64_armv8-a_shared").
1436 Output("libvendorjni.so").Output.String()
1437
Colin Cross3c007702020-05-08 11:20:24 -07001438 for _, test := range testCases {
1439 t.Run(test.name, func(t *testing.T) {
1440 app := ctx.ModuleForTests(test.name, "android_common")
Colin Cross3c007702020-05-08 11:20:24 -07001441
1442 jniLibZip := app.MaybeOutput("jnilibs.zip")
1443 if len(jniLibZip.Implicits) != 1 {
1444 t.Fatalf("expected exactly one jni library, got %q", jniLibZip.Implicits.Strings())
1445 }
1446 gotJNI := jniLibZip.Implicits[0].String()
1447
1448 if test.sdkJNI {
1449 if gotJNI != sdkJNI {
1450 t.Errorf("expected SDK JNI library %q, got %q", sdkJNI, gotJNI)
1451 }
Colin Crossc2d24052020-05-13 11:05:02 -07001452 } else if test.vendorJNI {
1453 if gotJNI != vendorJNI {
1454 t.Errorf("expected platform JNI library %q, got %q", vendorJNI, gotJNI)
1455 }
Colin Cross3c007702020-05-08 11:20:24 -07001456 } else {
1457 if gotJNI != platformJNI {
1458 t.Errorf("expected platform JNI library %q, got %q", platformJNI, gotJNI)
1459 }
1460 }
1461 })
1462 }
1463
1464 t.Run("jni_uses_platform_apis_error", func(t *testing.T) {
1465 testJavaError(t, `jni_uses_platform_apis: can only be set for modules that set sdk_version`, `
1466 android_test {
1467 name: "app_platform",
1468 platform_apis: true,
1469 jni_uses_platform_apis: true,
1470 }
1471 `)
1472 })
1473
1474 t.Run("jni_uses_sdk_apis_error", func(t *testing.T) {
1475 testJavaError(t, `jni_uses_sdk_apis: can only be set for modules that do not set sdk_version`, `
1476 android_test {
1477 name: "app_sdk",
1478 sdk_version: "current",
1479 jni_uses_sdk_apis: true,
1480 }
1481 `)
1482 })
1483
1484}
1485
Jaewoong Jung2ad817c2019-01-18 14:27:16 -08001486func TestCertificates(t *testing.T) {
1487 testCases := []struct {
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -04001488 name string
1489 bp string
1490 certificateOverride string
1491 expectedCertSigningFlags string
1492 expectedCertificate string
Jaewoong Jung2ad817c2019-01-18 14:27:16 -08001493 }{
1494 {
1495 name: "default",
1496 bp: `
1497 android_app {
1498 name: "foo",
1499 srcs: ["a.java"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09001500 sdk_version: "current",
Jaewoong Jung2ad817c2019-01-18 14:27:16 -08001501 }
1502 `,
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -04001503 certificateOverride: "",
1504 expectedCertSigningFlags: "",
1505 expectedCertificate: "build/make/target/product/security/testkey.x509.pem build/make/target/product/security/testkey.pk8",
Jaewoong Jung2ad817c2019-01-18 14:27:16 -08001506 },
1507 {
1508 name: "module certificate property",
1509 bp: `
1510 android_app {
1511 name: "foo",
1512 srcs: ["a.java"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09001513 certificate: ":new_certificate",
1514 sdk_version: "current",
Jaewoong Jung2ad817c2019-01-18 14:27:16 -08001515 }
1516
1517 android_app_certificate {
1518 name: "new_certificate",
Colin Cross3c007702020-05-08 11:20:24 -07001519 certificate: "cert/new_cert",
Jaewoong Jung2ad817c2019-01-18 14:27:16 -08001520 }
1521 `,
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -04001522 certificateOverride: "",
1523 expectedCertSigningFlags: "",
1524 expectedCertificate: "cert/new_cert.x509.pem cert/new_cert.pk8",
Jaewoong Jung2ad817c2019-01-18 14:27:16 -08001525 },
1526 {
1527 name: "path certificate property",
1528 bp: `
1529 android_app {
1530 name: "foo",
1531 srcs: ["a.java"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09001532 certificate: "expiredkey",
1533 sdk_version: "current",
Jaewoong Jung2ad817c2019-01-18 14:27:16 -08001534 }
1535 `,
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -04001536 certificateOverride: "",
1537 expectedCertSigningFlags: "",
1538 expectedCertificate: "build/make/target/product/security/expiredkey.x509.pem build/make/target/product/security/expiredkey.pk8",
Jaewoong Jung2ad817c2019-01-18 14:27:16 -08001539 },
1540 {
1541 name: "certificate overrides",
1542 bp: `
1543 android_app {
1544 name: "foo",
1545 srcs: ["a.java"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09001546 certificate: "expiredkey",
1547 sdk_version: "current",
Jaewoong Jung2ad817c2019-01-18 14:27:16 -08001548 }
1549
1550 android_app_certificate {
1551 name: "new_certificate",
Colin Cross3c007702020-05-08 11:20:24 -07001552 certificate: "cert/new_cert",
Jaewoong Jung2ad817c2019-01-18 14:27:16 -08001553 }
1554 `,
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -04001555 certificateOverride: "foo:new_certificate",
1556 expectedCertSigningFlags: "",
1557 expectedCertificate: "cert/new_cert.x509.pem cert/new_cert.pk8",
Liz Kammere2b27f42020-05-07 13:24:05 -07001558 },
1559 {
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -04001560 name: "certificate signing flags",
Liz Kammere2b27f42020-05-07 13:24:05 -07001561 bp: `
1562 android_app {
1563 name: "foo",
1564 srcs: ["a.java"],
1565 certificate: ":new_certificate",
1566 lineage: "lineage.bin",
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -04001567 rotationMinSdkVersion: "32",
Liz Kammere2b27f42020-05-07 13:24:05 -07001568 sdk_version: "current",
1569 }
1570
1571 android_app_certificate {
1572 name: "new_certificate",
1573 certificate: "cert/new_cert",
1574 }
1575 `,
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -04001576 certificateOverride: "",
1577 expectedCertSigningFlags: "--lineage lineage.bin --rotation-min-sdk-version 32",
1578 expectedCertificate: "cert/new_cert.x509.pem cert/new_cert.pk8",
Jaewoong Jung2ad817c2019-01-18 14:27:16 -08001579 },
Jaewoong Jung1c1b6e62021-03-09 15:02:31 -08001580 {
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -04001581 name: "cert signing flags from filegroup",
Jaewoong Jung1c1b6e62021-03-09 15:02:31 -08001582 bp: `
1583 android_app {
1584 name: "foo",
1585 srcs: ["a.java"],
1586 certificate: ":new_certificate",
1587 lineage: ":lineage_bin",
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -04001588 rotationMinSdkVersion: "32",
Jaewoong Jung1c1b6e62021-03-09 15:02:31 -08001589 sdk_version: "current",
1590 }
1591
1592 android_app_certificate {
1593 name: "new_certificate",
1594 certificate: "cert/new_cert",
1595 }
1596
1597 filegroup {
1598 name: "lineage_bin",
1599 srcs: ["lineage.bin"],
1600 }
1601 `,
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -04001602 certificateOverride: "",
1603 expectedCertSigningFlags: "--lineage lineage.bin --rotation-min-sdk-version 32",
1604 expectedCertificate: "cert/new_cert.x509.pem cert/new_cert.pk8",
Jaewoong Jung1c1b6e62021-03-09 15:02:31 -08001605 },
Jaewoong Jung2ad817c2019-01-18 14:27:16 -08001606 }
1607
1608 for _, test := range testCases {
1609 t.Run(test.name, func(t *testing.T) {
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00001610 result := android.GroupFixturePreparers(
1611 PrepareForTestWithJavaDefaultModules,
1612 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1613 if test.certificateOverride != "" {
1614 variables.CertificateOverrides = []string{test.certificateOverride}
1615 }
1616 }),
1617 ).RunTestWithBp(t, test.bp)
Jaewoong Jung2ad817c2019-01-18 14:27:16 -08001618
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00001619 foo := result.ModuleForTests("foo", "android_common")
Jaewoong Jung2ad817c2019-01-18 14:27:16 -08001620
1621 signapk := foo.Output("foo.apk")
Liz Kammere2b27f42020-05-07 13:24:05 -07001622 signCertificateFlags := signapk.Args["certificates"]
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00001623 android.AssertStringEquals(t, "certificates flags", test.expectedCertificate, signCertificateFlags)
Liz Kammere2b27f42020-05-07 13:24:05 -07001624
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -04001625 certSigningFlags := signapk.Args["flags"]
1626 android.AssertStringEquals(t, "cert signing flags", test.expectedCertSigningFlags, certSigningFlags)
Jaewoong Jung2ad817c2019-01-18 14:27:16 -08001627 })
1628 }
1629}
Jaewoong Jung9d22a912019-01-23 16:27:47 -08001630
Songchun Fan688de9a2020-03-24 20:32:24 -07001631func TestRequestV4SigningFlag(t *testing.T) {
1632 testCases := []struct {
1633 name string
1634 bp string
1635 expected string
1636 }{
1637 {
1638 name: "default",
1639 bp: `
1640 android_app {
1641 name: "foo",
1642 srcs: ["a.java"],
1643 sdk_version: "current",
1644 }
1645 `,
1646 expected: "",
1647 },
1648 {
1649 name: "default",
1650 bp: `
1651 android_app {
1652 name: "foo",
1653 srcs: ["a.java"],
1654 sdk_version: "current",
1655 v4_signature: false,
1656 }
1657 `,
1658 expected: "",
1659 },
1660 {
1661 name: "module certificate property",
1662 bp: `
1663 android_app {
1664 name: "foo",
1665 srcs: ["a.java"],
1666 sdk_version: "current",
1667 v4_signature: true,
1668 }
1669 `,
1670 expected: "--enable-v4",
1671 },
1672 }
1673
1674 for _, test := range testCases {
1675 t.Run(test.name, func(t *testing.T) {
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00001676 result := android.GroupFixturePreparers(
1677 PrepareForTestWithJavaDefaultModules,
1678 ).RunTestWithBp(t, test.bp)
Songchun Fan688de9a2020-03-24 20:32:24 -07001679
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00001680 foo := result.ModuleForTests("foo", "android_common")
Songchun Fan688de9a2020-03-24 20:32:24 -07001681
1682 signapk := foo.Output("foo.apk")
1683 signFlags := signapk.Args["flags"]
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00001684 android.AssertStringEquals(t, "signing flags", test.expected, signFlags)
Songchun Fan688de9a2020-03-24 20:32:24 -07001685 })
1686 }
1687}
1688
Jaewoong Jung9d22a912019-01-23 16:27:47 -08001689func TestPackageNameOverride(t *testing.T) {
1690 testCases := []struct {
1691 name string
1692 bp string
1693 packageNameOverride string
1694 expected []string
1695 }{
1696 {
1697 name: "default",
1698 bp: `
1699 android_app {
1700 name: "foo",
1701 srcs: ["a.java"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09001702 sdk_version: "current",
Jaewoong Jung9d22a912019-01-23 16:27:47 -08001703 }
1704 `,
1705 packageNameOverride: "",
1706 expected: []string{
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00001707 "out/soong/.intermediates/foo/android_common/foo.apk",
1708 "out/soong/target/product/test_device/system/app/foo/foo.apk",
Jaewoong Jung9d22a912019-01-23 16:27:47 -08001709 },
1710 },
1711 {
Jooyung Han29e2f6d2022-01-08 12:13:59 +09001712 name: "overridden via PRODUCT_PACKAGE_NAME_OVERRIDES",
Jaewoong Jung9d22a912019-01-23 16:27:47 -08001713 bp: `
1714 android_app {
1715 name: "foo",
1716 srcs: ["a.java"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09001717 sdk_version: "current",
Jaewoong Jung9d22a912019-01-23 16:27:47 -08001718 }
1719 `,
1720 packageNameOverride: "foo:bar",
1721 expected: []string{
1722 // The package apk should be still be the original name for test dependencies.
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00001723 "out/soong/.intermediates/foo/android_common/bar.apk",
1724 "out/soong/target/product/test_device/system/app/bar/bar.apk",
Jaewoong Jung9d22a912019-01-23 16:27:47 -08001725 },
1726 },
Jooyung Han29e2f6d2022-01-08 12:13:59 +09001727 {
1728 name: "overridden via stem",
1729 bp: `
1730 android_app {
1731 name: "foo",
1732 srcs: ["a.java"],
1733 sdk_version: "current",
1734 stem: "bar",
1735 }
1736 `,
1737 packageNameOverride: "",
1738 expected: []string{
1739 "out/soong/.intermediates/foo/android_common/bar.apk",
1740 "out/soong/target/product/test_device/system/app/bar/bar.apk",
1741 },
1742 },
Jaewoong Jung9d22a912019-01-23 16:27:47 -08001743 }
1744
1745 for _, test := range testCases {
1746 t.Run(test.name, func(t *testing.T) {
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00001747 result := android.GroupFixturePreparers(
1748 PrepareForTestWithJavaDefaultModules,
1749 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1750 if test.packageNameOverride != "" {
1751 variables.PackageNameOverrides = []string{test.packageNameOverride}
1752 }
1753 }),
1754 ).RunTestWithBp(t, test.bp)
Jaewoong Jung9d22a912019-01-23 16:27:47 -08001755
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00001756 foo := result.ModuleForTests("foo", "android_common")
1757
Lukacs T. Berkifbaf7252021-08-19 09:36:42 +02001758 outSoongDir := result.Config.SoongOutDir()
Jaewoong Jung9d22a912019-01-23 16:27:47 -08001759
1760 outputs := foo.AllOutputs()
1761 outputMap := make(map[string]bool)
1762 for _, o := range outputs {
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00001763 outputMap[android.StringPathRelativeToTop(outSoongDir, o)] = true
Jaewoong Jung9d22a912019-01-23 16:27:47 -08001764 }
1765 for _, e := range test.expected {
1766 if _, exist := outputMap[e]; !exist {
1767 t.Errorf("Can't find %q in output files.\nAll outputs:%v", e, outputs)
1768 }
1769 }
1770 })
1771 }
1772}
Jaewoong Jung4102e5d2019-02-27 16:26:28 -08001773
1774func TestInstrumentationTargetOverridden(t *testing.T) {
1775 bp := `
1776 android_app {
1777 name: "foo",
1778 srcs: ["a.java"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09001779 sdk_version: "current",
Jaewoong Jung4102e5d2019-02-27 16:26:28 -08001780 }
1781
1782 android_test {
1783 name: "bar",
1784 instrumentation_for: "foo",
Jeongik Cha538c0d02019-07-11 15:54:27 +09001785 sdk_version: "current",
Jaewoong Jung4102e5d2019-02-27 16:26:28 -08001786 }
1787 `
Jaewoong Jung4102e5d2019-02-27 16:26:28 -08001788
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00001789 result := android.GroupFixturePreparers(
1790 PrepareForTestWithJavaDefaultModules,
1791 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1792 variables.ManifestPackageNameOverrides = []string{"foo:org.dandroid.bp"}
1793 }),
1794 ).RunTestWithBp(t, bp)
Jaewoong Jung4102e5d2019-02-27 16:26:28 -08001795
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00001796 bar := result.ModuleForTests("bar", "android_common")
Jaewoong Jung4102e5d2019-02-27 16:26:28 -08001797 res := bar.Output("package-res.apk")
1798 aapt2Flags := res.Args["flags"]
1799 e := "--rename-instrumentation-target-package org.dandroid.bp"
1800 if !strings.Contains(aapt2Flags, e) {
1801 t.Errorf("target package renaming flag, %q is missing in aapt2 link flags, %q", e, aapt2Flags)
1802 }
1803}
Jaewoong Jung525443a2019-02-28 15:35:54 -08001804
1805func TestOverrideAndroidApp(t *testing.T) {
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00001806 result := PrepareForTestWithJavaDefaultModules.RunTestWithBp(
1807 t, `
Jaewoong Jung525443a2019-02-28 15:35:54 -08001808 android_app {
1809 name: "foo",
1810 srcs: ["a.java"],
Jaewoong Junga641ee92019-03-27 11:17:14 -07001811 certificate: "expiredkey",
Jaewoong Jungb639a6a2019-05-10 15:16:29 -07001812 overrides: ["qux"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09001813 sdk_version: "current",
Jaewoong Jung525443a2019-02-28 15:35:54 -08001814 }
1815
1816 override_android_app {
1817 name: "bar",
1818 base: "foo",
1819 certificate: ":new_certificate",
Liz Kammere2b27f42020-05-07 13:24:05 -07001820 lineage: "lineage.bin",
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -04001821 rotationMinSdkVersion: "32",
Baligh Uddin5b16dfb2020-02-11 17:27:19 -08001822 logging_parent: "bah",
Jaewoong Jung525443a2019-02-28 15:35:54 -08001823 }
1824
1825 android_app_certificate {
1826 name: "new_certificate",
1827 certificate: "cert/new_cert",
1828 }
Jaewoong Jung6f373f62019-03-13 10:13:24 -07001829
1830 override_android_app {
1831 name: "baz",
1832 base: "foo",
1833 package_name: "org.dandroid.bp",
1834 }
Liz Kammer9f9fd022020-06-18 19:44:06 +00001835
1836 override_android_app {
1837 name: "baz_no_rename_resources",
1838 base: "foo",
1839 package_name: "org.dandroid.bp",
1840 rename_resources_package: false,
1841 }
1842
1843 android_app {
1844 name: "foo_no_rename_resources",
1845 srcs: ["a.java"],
1846 certificate: "expiredkey",
1847 overrides: ["qux"],
1848 rename_resources_package: false,
1849 sdk_version: "current",
1850 }
1851
1852 override_android_app {
1853 name: "baz_base_no_rename_resources",
1854 base: "foo_no_rename_resources",
1855 package_name: "org.dandroid.bp",
1856 }
1857
1858 override_android_app {
1859 name: "baz_override_base_rename_resources",
1860 base: "foo_no_rename_resources",
1861 package_name: "org.dandroid.bp",
1862 rename_resources_package: true,
1863 }
Jaewoong Jung525443a2019-02-28 15:35:54 -08001864 `)
1865
1866 expectedVariants := []struct {
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -04001867 name string
1868 moduleName string
1869 variantName string
1870 apkName string
1871 apkPath string
1872 certFlag string
1873 certSigningFlags string
1874 overrides []string
1875 packageFlag string
1876 renameResources bool
1877 logging_parent string
Jaewoong Jung525443a2019-02-28 15:35:54 -08001878 }{
1879 {
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -04001880 name: "foo",
1881 moduleName: "foo",
1882 variantName: "android_common",
1883 apkPath: "out/soong/target/product/test_device/system/app/foo/foo.apk",
1884 certFlag: "build/make/target/product/security/expiredkey.x509.pem build/make/target/product/security/expiredkey.pk8",
1885 certSigningFlags: "",
1886 overrides: []string{"qux"},
1887 packageFlag: "",
1888 renameResources: false,
1889 logging_parent: "",
Jaewoong Jung525443a2019-02-28 15:35:54 -08001890 },
1891 {
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -04001892 name: "foo",
1893 moduleName: "bar",
1894 variantName: "android_common_bar",
1895 apkPath: "out/soong/target/product/test_device/system/app/bar/bar.apk",
1896 certFlag: "cert/new_cert.x509.pem cert/new_cert.pk8",
1897 certSigningFlags: "--lineage lineage.bin --rotation-min-sdk-version 32",
1898 overrides: []string{"qux", "foo"},
1899 packageFlag: "",
1900 renameResources: false,
1901 logging_parent: "bah",
Jaewoong Jung6f373f62019-03-13 10:13:24 -07001902 },
1903 {
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -04001904 name: "foo",
1905 moduleName: "baz",
1906 variantName: "android_common_baz",
1907 apkPath: "out/soong/target/product/test_device/system/app/baz/baz.apk",
1908 certFlag: "build/make/target/product/security/expiredkey.x509.pem build/make/target/product/security/expiredkey.pk8",
1909 certSigningFlags: "",
1910 overrides: []string{"qux", "foo"},
1911 packageFlag: "org.dandroid.bp",
1912 renameResources: true,
1913 logging_parent: "",
Liz Kammer9f9fd022020-06-18 19:44:06 +00001914 },
1915 {
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -04001916 name: "foo",
1917 moduleName: "baz_no_rename_resources",
1918 variantName: "android_common_baz_no_rename_resources",
1919 apkPath: "out/soong/target/product/test_device/system/app/baz_no_rename_resources/baz_no_rename_resources.apk",
1920 certFlag: "build/make/target/product/security/expiredkey.x509.pem build/make/target/product/security/expiredkey.pk8",
1921 certSigningFlags: "",
1922 overrides: []string{"qux", "foo"},
1923 packageFlag: "org.dandroid.bp",
1924 renameResources: false,
1925 logging_parent: "",
Liz Kammer9f9fd022020-06-18 19:44:06 +00001926 },
1927 {
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -04001928 name: "foo_no_rename_resources",
1929 moduleName: "baz_base_no_rename_resources",
1930 variantName: "android_common_baz_base_no_rename_resources",
1931 apkPath: "out/soong/target/product/test_device/system/app/baz_base_no_rename_resources/baz_base_no_rename_resources.apk",
1932 certFlag: "build/make/target/product/security/expiredkey.x509.pem build/make/target/product/security/expiredkey.pk8",
1933 certSigningFlags: "",
1934 overrides: []string{"qux", "foo_no_rename_resources"},
1935 packageFlag: "org.dandroid.bp",
1936 renameResources: false,
1937 logging_parent: "",
Liz Kammer9f9fd022020-06-18 19:44:06 +00001938 },
1939 {
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -04001940 name: "foo_no_rename_resources",
1941 moduleName: "baz_override_base_rename_resources",
1942 variantName: "android_common_baz_override_base_rename_resources",
1943 apkPath: "out/soong/target/product/test_device/system/app/baz_override_base_rename_resources/baz_override_base_rename_resources.apk",
1944 certFlag: "build/make/target/product/security/expiredkey.x509.pem build/make/target/product/security/expiredkey.pk8",
1945 certSigningFlags: "",
1946 overrides: []string{"qux", "foo_no_rename_resources"},
1947 packageFlag: "org.dandroid.bp",
1948 renameResources: true,
1949 logging_parent: "",
Jaewoong Jung525443a2019-02-28 15:35:54 -08001950 },
1951 }
1952 for _, expected := range expectedVariants {
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00001953 variant := result.ModuleForTests(expected.name, expected.variantName)
Jaewoong Jung525443a2019-02-28 15:35:54 -08001954
1955 // Check the final apk name
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00001956 variant.Output(expected.apkPath)
Jaewoong Jung525443a2019-02-28 15:35:54 -08001957
1958 // Check the certificate paths
Jaewoong Jung5a498812019-11-07 14:14:38 -08001959 signapk := variant.Output(expected.moduleName + ".apk")
Liz Kammere2b27f42020-05-07 13:24:05 -07001960 certFlag := signapk.Args["certificates"]
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00001961 android.AssertStringEquals(t, "certificates flags", expected.certFlag, certFlag)
Liz Kammere2b27f42020-05-07 13:24:05 -07001962
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -04001963 // Check the cert signing flags
1964 certSigningFlags := signapk.Args["flags"]
1965 android.AssertStringEquals(t, "cert signing flags", expected.certSigningFlags, certSigningFlags)
Jaewoong Jung525443a2019-02-28 15:35:54 -08001966
Jaewoong Jung6f373f62019-03-13 10:13:24 -07001967 // Check if the overrides field values are correctly aggregated.
Jaewoong Jung525443a2019-02-28 15:35:54 -08001968 mod := variant.Module().(*AndroidApp)
zhidoua2ce78f2022-02-17 02:33:12 +00001969 android.AssertDeepEquals(t, "overrides property", expected.overrides, mod.overridableAppProperties.Overrides)
Jaewoong Jung6f373f62019-03-13 10:13:24 -07001970
Baligh Uddin5b16dfb2020-02-11 17:27:19 -08001971 // Test Overridable property: Logging_parent
1972 logging_parent := mod.aapt.LoggingParent
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00001973 android.AssertStringEquals(t, "overrides property value for logging parent", expected.logging_parent, logging_parent)
Baligh Uddin5b16dfb2020-02-11 17:27:19 -08001974
Liz Kammer1d5983b2020-05-19 19:15:37 +00001975 // Check the package renaming flag, if exists.
Jaewoong Jung6f373f62019-03-13 10:13:24 -07001976 res := variant.Output("package-res.apk")
1977 aapt2Flags := res.Args["flags"]
Liz Kammer9f9fd022020-06-18 19:44:06 +00001978 checkAapt2LinkFlag(t, aapt2Flags, "rename-manifest-package", expected.packageFlag)
1979 expectedPackage := expected.packageFlag
1980 if !expected.renameResources {
1981 expectedPackage = ""
Liz Kammer1d5983b2020-05-19 19:15:37 +00001982 }
Liz Kammer9f9fd022020-06-18 19:44:06 +00001983 checkAapt2LinkFlag(t, aapt2Flags, "rename-resources-package", expectedPackage)
Jaewoong Jung525443a2019-02-28 15:35:54 -08001984 }
1985}
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001986
zhidoua2ce78f2022-02-17 02:33:12 +00001987func TestOverrideAndroidAppOverrides(t *testing.T) {
1988 ctx, _ := testJava(
1989 t, `
1990 android_app {
1991 name: "foo",
1992 srcs: ["a.java"],
1993 sdk_version: "current",
1994 overrides: ["qux"]
1995 }
1996
1997 android_app {
1998 name: "bar",
1999 srcs: ["b.java"],
2000 sdk_version: "current",
2001 overrides: ["foo"]
2002 }
2003
2004 override_android_app {
2005 name: "foo_override",
2006 base: "foo",
2007 overrides: ["bar"]
2008 }
2009 `)
2010
2011 expectedVariants := []struct {
2012 name string
2013 moduleName string
2014 variantName string
2015 overrides []string
2016 }{
2017 {
2018 name: "foo",
2019 moduleName: "foo",
2020 variantName: "android_common",
2021 overrides: []string{"qux"},
2022 },
2023 {
2024 name: "bar",
2025 moduleName: "bar",
2026 variantName: "android_common",
2027 overrides: []string{"foo"},
2028 },
2029 {
2030 name: "foo",
2031 moduleName: "foo_override",
2032 variantName: "android_common_foo_override",
2033 overrides: []string{"bar", "foo"},
2034 },
2035 }
2036 for _, expected := range expectedVariants {
2037 variant := ctx.ModuleForTests(expected.name, expected.variantName)
2038
2039 // Check if the overrides field values are correctly aggregated.
2040 mod := variant.Module().(*AndroidApp)
2041 android.AssertDeepEquals(t, "overrides property", expected.overrides, mod.overridableAppProperties.Overrides)
2042 }
2043}
2044
Jooyung Han01d80d82022-01-08 12:16:32 +09002045func TestOverrideAndroidAppStem(t *testing.T) {
2046 ctx, _ := testJava(t, `
2047 android_app {
2048 name: "foo",
2049 srcs: ["a.java"],
2050 sdk_version: "current",
2051 }
2052 override_android_app {
2053 name: "bar",
2054 base: "foo",
2055 }
2056 override_android_app {
2057 name: "baz",
2058 base: "foo",
2059 stem: "baz_stem",
2060 }
2061 android_app {
2062 name: "foo2",
2063 srcs: ["a.java"],
2064 sdk_version: "current",
2065 stem: "foo2_stem",
2066 }
2067 override_android_app {
2068 name: "bar2",
2069 base: "foo2",
2070 }
2071 override_android_app {
2072 name: "baz2",
2073 base: "foo2",
2074 stem: "baz2_stem",
2075 }
2076 `)
2077 for _, expected := range []struct {
2078 moduleName string
2079 variantName string
2080 apkPath string
2081 }{
2082 {
2083 moduleName: "foo",
2084 variantName: "android_common",
2085 apkPath: "out/soong/target/product/test_device/system/app/foo/foo.apk",
2086 },
2087 {
2088 moduleName: "foo",
2089 variantName: "android_common_bar",
2090 apkPath: "out/soong/target/product/test_device/system/app/bar/bar.apk",
2091 },
2092 {
2093 moduleName: "foo",
2094 variantName: "android_common_baz",
2095 apkPath: "out/soong/target/product/test_device/system/app/baz_stem/baz_stem.apk",
2096 },
2097 {
2098 moduleName: "foo2",
2099 variantName: "android_common",
2100 apkPath: "out/soong/target/product/test_device/system/app/foo2_stem/foo2_stem.apk",
2101 },
2102 {
2103 moduleName: "foo2",
2104 variantName: "android_common_bar2",
2105 // Note that this may cause the duplicate output error.
2106 apkPath: "out/soong/target/product/test_device/system/app/foo2_stem/foo2_stem.apk",
2107 },
2108 {
2109 moduleName: "foo2",
2110 variantName: "android_common_baz2",
2111 apkPath: "out/soong/target/product/test_device/system/app/baz2_stem/baz2_stem.apk",
2112 },
2113 } {
2114 variant := ctx.ModuleForTests(expected.moduleName, expected.variantName)
2115 variant.Output(expected.apkPath)
2116 }
2117}
2118
Jaewoong Jungb639a6a2019-05-10 15:16:29 -07002119func TestOverrideAndroidAppDependency(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07002120 ctx, _ := testJava(t, `
Jaewoong Jungb639a6a2019-05-10 15:16:29 -07002121 android_app {
2122 name: "foo",
2123 srcs: ["a.java"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09002124 sdk_version: "current",
Jaewoong Jungb639a6a2019-05-10 15:16:29 -07002125 }
2126
2127 override_android_app {
2128 name: "bar",
2129 base: "foo",
2130 package_name: "org.dandroid.bp",
2131 }
2132
2133 android_test {
2134 name: "baz",
2135 srcs: ["b.java"],
2136 instrumentation_for: "foo",
2137 }
2138
2139 android_test {
2140 name: "qux",
2141 srcs: ["b.java"],
2142 instrumentation_for: "bar",
2143 }
2144 `)
2145
2146 // Verify baz, which depends on the overridden module foo, has the correct classpath javac arg.
Paul Duffina71a67a2021-03-29 00:42:57 +01002147 javac := ctx.ModuleForTests("baz", "android_common").Rule("javac")
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00002148 fooTurbine := "out/soong/.intermediates/foo/android_common/turbine-combined/foo.jar"
Jaewoong Jungb639a6a2019-05-10 15:16:29 -07002149 if !strings.Contains(javac.Args["classpath"], fooTurbine) {
2150 t.Errorf("baz classpath %v does not contain %q", javac.Args["classpath"], fooTurbine)
2151 }
2152
2153 // Verify qux, which depends on the overriding module bar, has the correct classpath javac arg.
Paul Duffina71a67a2021-03-29 00:42:57 +01002154 javac = ctx.ModuleForTests("qux", "android_common").Rule("javac")
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00002155 barTurbine := "out/soong/.intermediates/foo/android_common_bar/turbine-combined/foo.jar"
Jaewoong Jungb639a6a2019-05-10 15:16:29 -07002156 if !strings.Contains(javac.Args["classpath"], barTurbine) {
2157 t.Errorf("qux classpath %v does not contain %q", javac.Args["classpath"], barTurbine)
2158 }
2159}
2160
Jaewoong Jung26dedd32019-06-06 08:45:58 -07002161func TestOverrideAndroidTest(t *testing.T) {
2162 ctx, _ := testJava(t, `
2163 android_app {
2164 name: "foo",
2165 srcs: ["a.java"],
2166 package_name: "com.android.foo",
2167 sdk_version: "current",
2168 }
2169
2170 override_android_app {
2171 name: "bar",
2172 base: "foo",
2173 package_name: "com.android.bar",
2174 }
2175
2176 android_test {
2177 name: "foo_test",
2178 srcs: ["b.java"],
2179 instrumentation_for: "foo",
2180 }
2181
2182 override_android_test {
2183 name: "bar_test",
2184 base: "foo_test",
2185 package_name: "com.android.bar.test",
2186 instrumentation_for: "bar",
2187 instrumentation_target_package: "com.android.bar",
2188 }
2189 `)
2190
2191 expectedVariants := []struct {
2192 moduleName string
2193 variantName string
2194 apkPath string
2195 overrides []string
2196 targetVariant string
2197 packageFlag string
2198 targetPackageFlag string
2199 }{
2200 {
2201 variantName: "android_common",
Jaewoong Jung326a9412019-11-21 10:41:00 -08002202 apkPath: "/target/product/test_device/testcases/foo_test/arm64/foo_test.apk",
Jaewoong Jung26dedd32019-06-06 08:45:58 -07002203 overrides: nil,
2204 targetVariant: "android_common",
2205 packageFlag: "",
2206 targetPackageFlag: "",
2207 },
2208 {
2209 variantName: "android_common_bar_test",
Jaewoong Jung326a9412019-11-21 10:41:00 -08002210 apkPath: "/target/product/test_device/testcases/bar_test/arm64/bar_test.apk",
Jaewoong Jung26dedd32019-06-06 08:45:58 -07002211 overrides: []string{"foo_test"},
2212 targetVariant: "android_common_bar",
2213 packageFlag: "com.android.bar.test",
2214 targetPackageFlag: "com.android.bar",
2215 },
2216 }
2217 for _, expected := range expectedVariants {
2218 variant := ctx.ModuleForTests("foo_test", expected.variantName)
2219
2220 // Check the final apk name
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00002221 variant.Output("out/soong" + expected.apkPath)
Jaewoong Jung26dedd32019-06-06 08:45:58 -07002222
2223 // Check if the overrides field values are correctly aggregated.
2224 mod := variant.Module().(*AndroidTest)
zhidoua2ce78f2022-02-17 02:33:12 +00002225 if !reflect.DeepEqual(expected.overrides, mod.overridableAppProperties.Overrides) {
Jaewoong Jung26dedd32019-06-06 08:45:58 -07002226 t.Errorf("Incorrect overrides property value, expected: %q, got: %q",
zhidoua2ce78f2022-02-17 02:33:12 +00002227 expected.overrides, mod.overridableAppProperties.Overrides)
Jaewoong Jung26dedd32019-06-06 08:45:58 -07002228 }
2229
2230 // Check if javac classpath has the correct jar file path. This checks instrumentation_for overrides.
Paul Duffina71a67a2021-03-29 00:42:57 +01002231 javac := variant.Rule("javac")
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00002232 turbine := filepath.Join("out", "soong", ".intermediates", "foo", expected.targetVariant, "turbine-combined", "foo.jar")
Jaewoong Jung26dedd32019-06-06 08:45:58 -07002233 if !strings.Contains(javac.Args["classpath"], turbine) {
2234 t.Errorf("classpath %q does not contain %q", javac.Args["classpath"], turbine)
2235 }
2236
2237 // Check aapt2 flags.
2238 res := variant.Output("package-res.apk")
2239 aapt2Flags := res.Args["flags"]
2240 checkAapt2LinkFlag(t, aapt2Flags, "rename-manifest-package", expected.packageFlag)
Liz Kammer9f9fd022020-06-18 19:44:06 +00002241 checkAapt2LinkFlag(t, aapt2Flags, "rename-resources-package", expected.packageFlag)
Jaewoong Jung26dedd32019-06-06 08:45:58 -07002242 checkAapt2LinkFlag(t, aapt2Flags, "rename-instrumentation-target-package", expected.targetPackageFlag)
2243 }
2244}
2245
Jaewoong Jung39982342020-01-14 10:27:18 -08002246func TestAndroidTest_FixTestConfig(t *testing.T) {
2247 ctx, _ := testJava(t, `
2248 android_app {
2249 name: "foo",
2250 srcs: ["a.java"],
2251 package_name: "com.android.foo",
2252 sdk_version: "current",
2253 }
2254
2255 android_test {
2256 name: "foo_test",
2257 srcs: ["b.java"],
2258 instrumentation_for: "foo",
2259 }
2260
2261 android_test {
2262 name: "bar_test",
2263 srcs: ["b.java"],
2264 package_name: "com.android.bar.test",
2265 instrumentation_for: "foo",
2266 }
2267
2268 override_android_test {
2269 name: "baz_test",
2270 base: "foo_test",
2271 package_name: "com.android.baz.test",
2272 }
2273 `)
2274
2275 testCases := []struct {
2276 moduleName string
2277 variantName string
2278 expectedFlags []string
2279 }{
2280 {
2281 moduleName: "foo_test",
2282 variantName: "android_common",
2283 },
2284 {
2285 moduleName: "bar_test",
2286 variantName: "android_common",
2287 expectedFlags: []string{
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00002288 "--manifest out/soong/.intermediates/bar_test/android_common/manifest_fixer/AndroidManifest.xml",
Jaewoong Jung39982342020-01-14 10:27:18 -08002289 "--package-name com.android.bar.test",
2290 },
2291 },
2292 {
2293 moduleName: "foo_test",
2294 variantName: "android_common_baz_test",
2295 expectedFlags: []string{
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00002296 "--manifest out/soong/.intermediates/foo_test/android_common_baz_test/manifest_fixer/AndroidManifest.xml",
Jaewoong Jung39982342020-01-14 10:27:18 -08002297 "--package-name com.android.baz.test",
2298 "--test-file-name baz_test.apk",
2299 },
2300 },
2301 }
2302
2303 for _, test := range testCases {
2304 variant := ctx.ModuleForTests(test.moduleName, test.variantName)
Paul Duffina71a67a2021-03-29 00:42:57 +01002305 params := variant.MaybeOutput("test_config_fixer/AndroidTest.xml")
Jaewoong Jung39982342020-01-14 10:27:18 -08002306
2307 if len(test.expectedFlags) > 0 {
2308 if params.Rule == nil {
2309 t.Errorf("test_config_fixer was expected to run, but didn't")
2310 } else {
2311 for _, flag := range test.expectedFlags {
2312 if !strings.Contains(params.RuleParams.Command, flag) {
2313 t.Errorf("Flag %q was not found in command: %q", flag, params.RuleParams.Command)
2314 }
2315 }
2316 }
2317 } else {
2318 if params.Rule != nil {
2319 t.Errorf("test_config_fixer was not expected to run, but did: %q", params.RuleParams.Command)
2320 }
2321 }
Jaewoong Jung39982342020-01-14 10:27:18 -08002322 }
2323}
2324
Paul Duffin53a70a42022-01-11 14:35:55 +00002325func TestInstrumentationTargetPrebuilt(t *testing.T) {
2326 bp := `
2327 android_app_import {
2328 name: "foo",
2329 apk: "foo.apk",
2330 presigned: true,
2331 }
2332
2333 android_test {
2334 name: "bar",
2335 srcs: ["a.java"],
2336 instrumentation_for: "foo",
2337 sdk_version: "current",
2338 }
2339 `
2340
2341 android.GroupFixturePreparers(
2342 PrepareForTestWithJavaDefaultModules,
2343 ).ExtendWithErrorHandler(
2344 android.FixtureExpectsAtLeastOneErrorMatchingPattern(
2345 "instrumentation_for: dependency \"foo\" of type \"android_app_import\" does not provide JavaInfo so is unsuitable for use with this property")).
2346 RunTestWithBp(t, bp)
2347}
2348
Jaewoong Jungbc625cd2019-05-06 15:48:44 -07002349func TestStl(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07002350 ctx, _ := testJava(t, cc.GatherRequiredDepsForTest(android.Android)+`
Jaewoong Jungbc625cd2019-05-06 15:48:44 -07002351 cc_library {
2352 name: "libjni",
Peter Collingbournead84f972019-12-17 16:46:18 -08002353 sdk_version: "current",
2354 stl: "c++_shared",
Jaewoong Jungbc625cd2019-05-06 15:48:44 -07002355 }
2356
2357 android_test {
2358 name: "stl",
2359 jni_libs: ["libjni"],
2360 compile_multilib: "both",
2361 sdk_version: "current",
2362 stl: "c++_shared",
2363 }
2364
2365 android_test {
2366 name: "system",
2367 jni_libs: ["libjni"],
2368 compile_multilib: "both",
2369 sdk_version: "current",
2370 }
2371 `)
2372
2373 testCases := []struct {
2374 name string
2375 jnis []string
2376 }{
2377 {"stl",
2378 []string{
2379 "libjni.so",
Jaewoong Jung710756a2019-06-04 11:53:47 -07002380 "libc++_shared.so",
Jaewoong Jungbc625cd2019-05-06 15:48:44 -07002381 },
2382 },
2383 {"system",
2384 []string{
2385 "libjni.so",
2386 },
2387 },
2388 }
2389
2390 for _, test := range testCases {
2391 t.Run(test.name, func(t *testing.T) {
2392 app := ctx.ModuleForTests(test.name, "android_common")
2393 jniLibZip := app.Output("jnilibs.zip")
2394 var jnis []string
2395 args := strings.Fields(jniLibZip.Args["jarArgs"])
2396 for i := 0; i < len(args); i++ {
2397 if args[i] == "-f" {
2398 jnis = append(jnis, args[i+1])
2399 i += 1
2400 }
2401 }
2402 jnisJoined := strings.Join(jnis, " ")
2403 for _, jni := range test.jnis {
2404 if !strings.Contains(jnisJoined, jni) {
2405 t.Errorf("missing jni %q in %q", jni, jnis)
2406 }
2407 }
2408 })
2409 }
2410}
Colin Cross50ddcc42019-05-16 12:28:22 -07002411
2412func TestUsesLibraries(t *testing.T) {
2413 bp := `
2414 java_sdk_library {
2415 name: "foo",
2416 srcs: ["a.java"],
2417 api_packages: ["foo"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09002418 sdk_version: "current",
Colin Cross50ddcc42019-05-16 12:28:22 -07002419 }
2420
2421 java_sdk_library {
Paul Duffin859fe962020-05-15 10:20:31 +01002422 name: "qux",
2423 srcs: ["a.java"],
2424 api_packages: ["qux"],
2425 sdk_version: "current",
2426 }
2427
2428 java_sdk_library {
2429 name: "quuz",
2430 srcs: ["a.java"],
2431 api_packages: ["quuz"],
2432 sdk_version: "current",
2433 }
2434
2435 java_sdk_library {
Ulya Trafimovich65b03192020-12-03 16:50:22 +00002436 name: "fred",
2437 srcs: ["a.java"],
2438 api_packages: ["fred"],
2439 sdk_version: "current",
2440 }
2441
2442 java_sdk_library {
Colin Cross50ddcc42019-05-16 12:28:22 -07002443 name: "bar",
2444 srcs: ["a.java"],
2445 api_packages: ["bar"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09002446 sdk_version: "current",
Colin Cross50ddcc42019-05-16 12:28:22 -07002447 }
2448
Ulya Trafimovich4b6d4c12020-08-19 14:58:01 +01002449 java_sdk_library {
2450 name: "runtime-library",
2451 srcs: ["a.java"],
2452 sdk_version: "current",
2453 }
2454
2455 java_library {
2456 name: "static-runtime-helper",
2457 srcs: ["a.java"],
2458 libs: ["runtime-library"],
2459 sdk_version: "current",
2460 }
2461
Ulya Trafimoviche4432872021-08-18 16:57:11 +01002462 java_library {
2463 name: "runtime-required-x",
2464 srcs: ["a.java"],
2465 installable: true,
2466 sdk_version: "current",
2467 }
2468
2469 java_library {
2470 name: "runtime-optional-x",
2471 srcs: ["a.java"],
2472 installable: true,
2473 sdk_version: "current",
2474 }
2475
2476 android_library {
2477 name: "static-x",
2478 uses_libs: ["runtime-required-x"],
2479 optional_uses_libs: ["runtime-optional-x"],
2480 sdk_version: "current",
2481 }
2482
2483 java_library {
2484 name: "runtime-required-y",
2485 srcs: ["a.java"],
2486 installable: true,
2487 sdk_version: "current",
2488 }
2489
2490 java_library {
2491 name: "runtime-optional-y",
2492 srcs: ["a.java"],
2493 installable: true,
2494 sdk_version: "current",
2495 }
2496
2497 java_library {
2498 name: "static-y",
2499 srcs: ["a.java"],
2500 uses_libs: ["runtime-required-y"],
2501 optional_uses_libs: ["runtime-optional-y"],
2502 sdk_version: "current",
2503 }
2504
Ulya Trafimovich861a8962021-02-26 14:49:07 +00002505 // A library that has to use "provides_uses_lib", because:
2506 // - it is not an SDK library
2507 // - its library name is different from its module name
2508 java_library {
2509 name: "non-sdk-lib",
2510 provides_uses_lib: "com.non.sdk.lib",
2511 installable: true,
2512 srcs: ["a.java"],
2513 }
2514
Colin Cross50ddcc42019-05-16 12:28:22 -07002515 android_app {
2516 name: "app",
2517 srcs: ["a.java"],
Ulya Trafimovich861a8962021-02-26 14:49:07 +00002518 libs: [
2519 "qux",
2520 "quuz.stubs"
2521 ],
Ulya Trafimovich65b03192020-12-03 16:50:22 +00002522 static_libs: [
2523 "static-runtime-helper",
2524 // statically linked component libraries should not pull their SDK libraries,
2525 // so "fred" should not be added to class loader context
2526 "fred.stubs",
Ulya Trafimoviche4432872021-08-18 16:57:11 +01002527 "static-x",
2528 "static-y",
Ulya Trafimovich65b03192020-12-03 16:50:22 +00002529 ],
Ulya Trafimovich861a8962021-02-26 14:49:07 +00002530 uses_libs: [
2531 "foo",
2532 "non-sdk-lib"
2533 ],
Jeongik Cha538c0d02019-07-11 15:54:27 +09002534 sdk_version: "current",
Colin Cross50ddcc42019-05-16 12:28:22 -07002535 optional_uses_libs: [
2536 "bar",
2537 "baz",
2538 ],
2539 }
2540
2541 android_app_import {
2542 name: "prebuilt",
2543 apk: "prebuilts/apk/app.apk",
2544 certificate: "platform",
Ulya Trafimovich861a8962021-02-26 14:49:07 +00002545 uses_libs: [
2546 "foo",
2547 "non-sdk-lib",
2548 "android.test.runner"
2549 ],
Colin Cross50ddcc42019-05-16 12:28:22 -07002550 optional_uses_libs: [
2551 "bar",
2552 "baz",
2553 ],
2554 }
2555 `
2556
Paul Duffin71ae5942021-03-22 15:36:52 +00002557 result := android.GroupFixturePreparers(
2558 prepareForJavaTest,
Paul Duffin2645a292021-03-13 02:36:00 +00002559 PrepareForTestWithJavaSdkLibraryFiles,
2560 FixtureWithLastReleaseApis("runtime-library", "foo", "quuz", "qux", "bar", "fred"),
Paul Duffind234b412021-03-12 23:04:46 +00002561 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
2562 variables.MissingUsesLibraries = []string{"baz"}
2563 }),
2564 ).RunTestWithBp(t, bp)
Colin Cross50ddcc42019-05-16 12:28:22 -07002565
Paul Duffind234b412021-03-12 23:04:46 +00002566 app := result.ModuleForTests("app", "android_common")
2567 prebuilt := result.ModuleForTests("prebuilt", "android_common")
Colin Cross50ddcc42019-05-16 12:28:22 -07002568
Paul Duffin859fe962020-05-15 10:20:31 +01002569 // Test that implicit dependencies on java_sdk_library instances are passed to the manifest.
Ulya Trafimovich2eaf5c52021-02-26 12:05:11 +00002570 // This should not include explicit `uses_libs`/`optional_uses_libs` entries.
2571 actualManifestFixerArgs := app.Output("manifest_fixer/AndroidManifest.xml").Args["args"]
2572 expectManifestFixerArgs := `--extract-native-libs=true ` +
2573 `--uses-library qux ` +
2574 `--uses-library quuz ` +
Ulya Trafimovich0b1c70e2021-08-20 15:39:12 +01002575 `--uses-library runtime-library`
Gurpreet Singh75d65f32022-01-24 17:44:05 +00002576 android.AssertStringDoesContain(t, "manifest_fixer args", actualManifestFixerArgs, expectManifestFixerArgs)
Paul Duffin859fe962020-05-15 10:20:31 +01002577
Ulya Trafimovich2eaf5c52021-02-26 12:05:11 +00002578 // Test that all libraries are verified (library order matters).
2579 verifyCmd := app.Rule("verify_uses_libraries").RuleParams.Command
2580 verifyArgs := `--uses-library foo ` +
Ulya Trafimovicheea486a2021-02-26 11:38:21 +00002581 `--uses-library com.non.sdk.lib ` +
Ulya Trafimovich2eaf5c52021-02-26 12:05:11 +00002582 `--uses-library qux ` +
2583 `--uses-library quuz ` +
2584 `--uses-library runtime-library ` +
Ulya Trafimoviche4432872021-08-18 16:57:11 +01002585 `--uses-library runtime-required-x ` +
2586 `--uses-library runtime-required-y ` +
Ulya Trafimovich2eaf5c52021-02-26 12:05:11 +00002587 `--optional-uses-library bar ` +
Ulya Trafimoviche4432872021-08-18 16:57:11 +01002588 `--optional-uses-library baz ` +
2589 `--optional-uses-library runtime-optional-x ` +
2590 `--optional-uses-library runtime-optional-y `
Paul Duffind234b412021-03-12 23:04:46 +00002591 android.AssertStringDoesContain(t, "verify cmd args", verifyCmd, verifyArgs)
Colin Cross50ddcc42019-05-16 12:28:22 -07002592
Ulya Trafimovich2eaf5c52021-02-26 12:05:11 +00002593 // Test that all libraries are verified for an APK (library order matters).
2594 verifyApkCmd := prebuilt.Rule("verify_uses_libraries").RuleParams.Command
Ulya Trafimovich0aba2522021-03-03 16:38:37 +00002595 verifyApkArgs := `--uses-library foo ` +
2596 `--uses-library com.non.sdk.lib ` +
2597 `--uses-library android.test.runner ` +
2598 `--optional-uses-library bar ` +
2599 `--optional-uses-library baz `
Paul Duffind234b412021-03-12 23:04:46 +00002600 android.AssertStringDoesContain(t, "verify apk cmd args", verifyApkCmd, verifyApkArgs)
Colin Cross50ddcc42019-05-16 12:28:22 -07002601
Ulya Trafimovich8130c482020-10-07 15:17:13 +01002602 // Test that all present libraries are preopted, including implicit SDK dependencies, possibly stubs
Ulya Trafimovich2eaf5c52021-02-26 12:05:11 +00002603 cmd := app.Rule("dexpreopt").RuleParams.Command
Ulya Trafimovich8130c482020-10-07 15:17:13 +01002604 w := `--target-context-for-sdk any ` +
Ulya Trafimovich8130c482020-10-07 15:17:13 +01002605 `PCL[/system/framework/qux.jar]#` +
Ulya Trafimovich8cbc5d22020-11-03 15:15:46 +00002606 `PCL[/system/framework/quuz.jar]#` +
2607 `PCL[/system/framework/foo.jar]#` +
Ulya Trafimovich861a8962021-02-26 14:49:07 +00002608 `PCL[/system/framework/non-sdk-lib.jar]#` +
Ulya Trafimovich8cbc5d22020-11-03 15:15:46 +00002609 `PCL[/system/framework/bar.jar]#` +
Ulya Trafimoviche4432872021-08-18 16:57:11 +01002610 `PCL[/system/framework/runtime-library.jar]#` +
2611 `PCL[/system/framework/runtime-required-x.jar]#` +
2612 `PCL[/system/framework/runtime-optional-x.jar]#` +
2613 `PCL[/system/framework/runtime-required-y.jar]#` +
2614 `PCL[/system/framework/runtime-optional-y.jar] `
Paul Duffind234b412021-03-12 23:04:46 +00002615 android.AssertStringDoesContain(t, "dexpreopt app cmd args", cmd, w)
Colin Cross50ddcc42019-05-16 12:28:22 -07002616
Ulya Trafimovich24813e12020-10-07 15:05:21 +01002617 // Test conditional context for target SDK version 28.
Paul Duffind234b412021-03-12 23:04:46 +00002618 android.AssertStringDoesContain(t, "dexpreopt app cmd 28", cmd,
2619 `--target-context-for-sdk 28`+
2620 ` PCL[/system/framework/org.apache.http.legacy.jar] `)
Ulya Trafimovich24813e12020-10-07 15:05:21 +01002621
2622 // Test conditional context for target SDK version 29.
Paul Duffind234b412021-03-12 23:04:46 +00002623 android.AssertStringDoesContain(t, "dexpreopt app cmd 29", cmd,
2624 `--target-context-for-sdk 29`+
2625 ` PCL[/system/framework/android.hidl.manager-V1.0-java.jar]`+
2626 `#PCL[/system/framework/android.hidl.base-V1.0-java.jar] `)
Ulya Trafimovich24813e12020-10-07 15:05:21 +01002627
2628 // Test conditional context for target SDK version 30.
Ulya Trafimovich46b3d5b2020-10-21 13:20:55 +01002629 // "android.test.mock" is absent because "android.test.runner" is not used.
Paul Duffind234b412021-03-12 23:04:46 +00002630 android.AssertStringDoesContain(t, "dexpreopt app cmd 30", cmd,
2631 `--target-context-for-sdk 30`+
2632 ` PCL[/system/framework/android.test.base.jar] `)
Ulya Trafimovich24813e12020-10-07 15:05:21 +01002633
Ulya Trafimovichfc24ad32020-08-19 16:32:54 +01002634 cmd = prebuilt.Rule("dexpreopt").RuleParams.Command
Paul Duffind234b412021-03-12 23:04:46 +00002635 android.AssertStringDoesContain(t, "dexpreopt prebuilt cmd", cmd,
2636 `--target-context-for-sdk any`+
2637 ` PCL[/system/framework/foo.jar]`+
2638 `#PCL[/system/framework/non-sdk-lib.jar]`+
2639 `#PCL[/system/framework/android.test.runner.jar]`+
2640 `#PCL[/system/framework/bar.jar] `)
Ulya Trafimovich24813e12020-10-07 15:05:21 +01002641
2642 // Test conditional context for target SDK version 30.
Ulya Trafimovich46b3d5b2020-10-21 13:20:55 +01002643 // "android.test.mock" is present because "android.test.runner" is used.
Paul Duffind234b412021-03-12 23:04:46 +00002644 android.AssertStringDoesContain(t, "dexpreopt prebuilt cmd 30", cmd,
2645 `--target-context-for-sdk 30`+
2646 ` PCL[/system/framework/android.test.base.jar]`+
2647 `#PCL[/system/framework/android.test.mock.jar] `)
Colin Cross50ddcc42019-05-16 12:28:22 -07002648}
Jaewoong Jungc27ab662019-05-30 15:51:14 -07002649
Ulya Trafimovich9023b022021-03-22 16:02:28 +00002650func TestDexpreoptBcp(t *testing.T) {
2651 bp := `
2652 java_sdk_library {
2653 name: "foo",
2654 srcs: ["a.java"],
2655 api_packages: ["foo"],
2656 sdk_version: "current",
2657 }
2658
2659 java_sdk_library {
2660 name: "bar",
2661 srcs: ["a.java"],
2662 api_packages: ["bar"],
2663 permitted_packages: ["bar"],
2664 sdk_version: "current",
2665 }
2666
2667 android_app {
2668 name: "app",
2669 srcs: ["a.java"],
2670 sdk_version: "current",
2671 }
2672 `
2673
2674 testCases := []struct {
2675 name string
2676 with bool
2677 expect string
2678 }{
2679 {
2680 name: "with updatable bcp",
2681 with: true,
2682 expect: "/system/framework/foo.jar:/system/framework/bar.jar",
2683 },
2684 {
2685 name: "without updatable bcp",
2686 with: false,
2687 expect: "/system/framework/foo.jar",
2688 },
2689 }
2690
2691 for _, test := range testCases {
2692 t.Run(test.name, func(t *testing.T) {
2693 result := android.GroupFixturePreparers(
2694 prepareForJavaTest,
2695 PrepareForTestWithJavaSdkLibraryFiles,
2696 FixtureWithLastReleaseApis("runtime-library", "foo", "bar"),
2697 dexpreopt.FixtureSetBootJars("platform:foo"),
satayevd604b212021-07-21 14:23:52 +01002698 dexpreopt.FixtureSetApexBootJars("platform:bar"),
Ulya Trafimovich9023b022021-03-22 16:02:28 +00002699 dexpreopt.FixtureSetPreoptWithUpdatableBcp(test.with),
2700 ).RunTestWithBp(t, bp)
2701
2702 app := result.ModuleForTests("app", "android_common")
2703 cmd := app.Rule("dexpreopt").RuleParams.Command
2704 bcp := " -Xbootclasspath-locations:" + test.expect + " " // space at the end matters
2705 android.AssertStringDoesContain(t, "dexpreopt app bcp", cmd, bcp)
2706 })
2707 }
2708}
2709
Jaewoong Jungc27ab662019-05-30 15:51:14 -07002710func TestCodelessApp(t *testing.T) {
2711 testCases := []struct {
2712 name string
2713 bp string
2714 noCode bool
2715 }{
2716 {
2717 name: "normal",
2718 bp: `
2719 android_app {
2720 name: "foo",
2721 srcs: ["a.java"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09002722 sdk_version: "current",
Jaewoong Jungc27ab662019-05-30 15:51:14 -07002723 }
2724 `,
2725 noCode: false,
2726 },
2727 {
2728 name: "app without sources",
2729 bp: `
2730 android_app {
2731 name: "foo",
Jeongik Cha538c0d02019-07-11 15:54:27 +09002732 sdk_version: "current",
Jaewoong Jungc27ab662019-05-30 15:51:14 -07002733 }
2734 `,
2735 noCode: true,
2736 },
2737 {
2738 name: "app with libraries",
2739 bp: `
2740 android_app {
2741 name: "foo",
2742 static_libs: ["lib"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09002743 sdk_version: "current",
Jaewoong Jungc27ab662019-05-30 15:51:14 -07002744 }
2745
2746 java_library {
2747 name: "lib",
2748 srcs: ["a.java"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09002749 sdk_version: "current",
Jaewoong Jungc27ab662019-05-30 15:51:14 -07002750 }
2751 `,
2752 noCode: false,
2753 },
2754 {
2755 name: "app with sourceless libraries",
2756 bp: `
2757 android_app {
2758 name: "foo",
2759 static_libs: ["lib"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09002760 sdk_version: "current",
Jaewoong Jungc27ab662019-05-30 15:51:14 -07002761 }
2762
2763 java_library {
2764 name: "lib",
Jeongik Cha538c0d02019-07-11 15:54:27 +09002765 sdk_version: "current",
Jaewoong Jungc27ab662019-05-30 15:51:14 -07002766 }
2767 `,
2768 // TODO(jungjw): this should probably be true
2769 noCode: false,
2770 },
2771 }
2772
2773 for _, test := range testCases {
2774 t.Run(test.name, func(t *testing.T) {
2775 ctx := testApp(t, test.bp)
2776
2777 foo := ctx.ModuleForTests("foo", "android_common")
2778 manifestFixerArgs := foo.Output("manifest_fixer/AndroidManifest.xml").Args["args"]
2779 if strings.Contains(manifestFixerArgs, "--has-no-code") != test.noCode {
2780 t.Errorf("unexpected manifest_fixer args: %q", manifestFixerArgs)
2781 }
2782 })
2783 }
2784}
Jaewoong Jung5b425e22019-06-17 17:40:56 -07002785
2786func TestEmbedNotice(t *testing.T) {
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00002787 result := android.GroupFixturePreparers(
2788 PrepareForTestWithJavaDefaultModules,
2789 cc.PrepareForTestWithCcDefaultModules,
2790 genrule.PrepareForTestWithGenRuleBuildComponents,
2791 android.MockFS{
2792 "APP_NOTICE": nil,
2793 "GENRULE_NOTICE": nil,
2794 "LIB_NOTICE": nil,
2795 "TOOL_NOTICE": nil,
2796 }.AddToFixture(),
2797 ).RunTestWithBp(t, `
Jaewoong Jung5b425e22019-06-17 17:40:56 -07002798 android_app {
2799 name: "foo",
2800 srcs: ["a.java"],
2801 static_libs: ["javalib"],
2802 jni_libs: ["libjni"],
2803 notice: "APP_NOTICE",
2804 embed_notices: true,
Jeongik Cha538c0d02019-07-11 15:54:27 +09002805 sdk_version: "current",
Jaewoong Jung5b425e22019-06-17 17:40:56 -07002806 }
2807
2808 // No embed_notice flag
2809 android_app {
2810 name: "bar",
2811 srcs: ["a.java"],
2812 jni_libs: ["libjni"],
2813 notice: "APP_NOTICE",
Jeongik Cha538c0d02019-07-11 15:54:27 +09002814 sdk_version: "current",
Jaewoong Jung5b425e22019-06-17 17:40:56 -07002815 }
2816
2817 // No NOTICE files
2818 android_app {
2819 name: "baz",
2820 srcs: ["a.java"],
2821 embed_notices: true,
Jeongik Cha538c0d02019-07-11 15:54:27 +09002822 sdk_version: "current",
Jaewoong Jung5b425e22019-06-17 17:40:56 -07002823 }
2824
2825 cc_library {
2826 name: "libjni",
2827 system_shared_libs: [],
2828 stl: "none",
2829 notice: "LIB_NOTICE",
Colin Cross094cde42020-02-15 10:38:00 -08002830 sdk_version: "current",
Jaewoong Jung5b425e22019-06-17 17:40:56 -07002831 }
2832
2833 java_library {
2834 name: "javalib",
2835 srcs: [
2836 ":gen",
2837 ],
Jeongik Cha538c0d02019-07-11 15:54:27 +09002838 sdk_version: "current",
Jaewoong Jung5b425e22019-06-17 17:40:56 -07002839 }
2840
2841 genrule {
2842 name: "gen",
2843 tools: ["gentool"],
2844 out: ["gen.java"],
2845 notice: "GENRULE_NOTICE",
2846 }
2847
2848 java_binary_host {
2849 name: "gentool",
2850 srcs: ["b.java"],
2851 notice: "TOOL_NOTICE",
2852 }
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00002853 `)
Jaewoong Jung5b425e22019-06-17 17:40:56 -07002854
2855 // foo has NOTICE files to process, and embed_notices is true.
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00002856 foo := result.ModuleForTests("foo", "android_common")
Jaewoong Jung5b425e22019-06-17 17:40:56 -07002857 // verify merge notices rule.
2858 mergeNotices := foo.Rule("mergeNoticesRule")
2859 noticeInputs := mergeNotices.Inputs.Strings()
2860 // TOOL_NOTICE should be excluded as it's a host module.
2861 if len(mergeNotices.Inputs) != 3 {
2862 t.Errorf("number of input notice files: expected = 3, actual = %q", noticeInputs)
2863 }
2864 if !inList("APP_NOTICE", noticeInputs) {
2865 t.Errorf("APP_NOTICE is missing from notice files, %q", noticeInputs)
2866 }
2867 if !inList("LIB_NOTICE", noticeInputs) {
2868 t.Errorf("LIB_NOTICE is missing from notice files, %q", noticeInputs)
2869 }
2870 if !inList("GENRULE_NOTICE", noticeInputs) {
2871 t.Errorf("GENRULE_NOTICE is missing from notice files, %q", noticeInputs)
2872 }
2873 // aapt2 flags should include -A <NOTICE dir> so that its contents are put in the APK's /assets.
Paul Duffina71a67a2021-03-29 00:42:57 +01002874 res := foo.Output("package-res.apk")
Jaewoong Jung5b425e22019-06-17 17:40:56 -07002875 aapt2Flags := res.Args["flags"]
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00002876 e := "-A out/soong/.intermediates/foo/android_common/NOTICE"
2877 android.AssertStringDoesContain(t, "expected.apkPath", aapt2Flags, e)
Jaewoong Jung5b425e22019-06-17 17:40:56 -07002878
2879 // bar has NOTICE files to process, but embed_notices is not set.
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00002880 bar := result.ModuleForTests("bar", "android_common")
Paul Duffina71a67a2021-03-29 00:42:57 +01002881 res = bar.Output("package-res.apk")
Jaewoong Jung98772792019-07-01 17:15:13 -07002882 aapt2Flags = res.Args["flags"]
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00002883 e = "-A out/soong/.intermediates/bar/android_common/NOTICE"
2884 android.AssertStringDoesNotContain(t, "bar shouldn't have the asset dir flag for NOTICE", aapt2Flags, e)
Jaewoong Jung5b425e22019-06-17 17:40:56 -07002885
2886 // baz's embed_notice is true, but it doesn't have any NOTICE files.
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00002887 baz := result.ModuleForTests("baz", "android_common")
Jaewoong Jung98772792019-07-01 17:15:13 -07002888 res = baz.Output("package-res.apk")
2889 aapt2Flags = res.Args["flags"]
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00002890 e = "-A out/soong/.intermediates/baz/android_common/NOTICE"
Jaewoong Jung98772792019-07-01 17:15:13 -07002891 if strings.Contains(aapt2Flags, e) {
2892 t.Errorf("baz shouldn't have the asset dir flag for NOTICE: %q", e)
Jaewoong Jung5b425e22019-06-17 17:40:56 -07002893 }
2894}
Colin Cross53a87f52019-06-25 13:35:30 -07002895
2896func TestUncompressDex(t *testing.T) {
2897 testCases := []struct {
2898 name string
2899 bp string
2900
2901 uncompressedPlatform bool
2902 uncompressedUnbundled bool
2903 }{
2904 {
2905 name: "normal",
2906 bp: `
2907 android_app {
2908 name: "foo",
2909 srcs: ["a.java"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09002910 sdk_version: "current",
Colin Cross53a87f52019-06-25 13:35:30 -07002911 }
2912 `,
2913 uncompressedPlatform: true,
2914 uncompressedUnbundled: false,
2915 },
2916 {
2917 name: "use_embedded_dex",
2918 bp: `
2919 android_app {
2920 name: "foo",
2921 use_embedded_dex: true,
2922 srcs: ["a.java"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09002923 sdk_version: "current",
Colin Cross53a87f52019-06-25 13:35:30 -07002924 }
2925 `,
2926 uncompressedPlatform: true,
2927 uncompressedUnbundled: true,
2928 },
2929 {
2930 name: "privileged",
2931 bp: `
2932 android_app {
2933 name: "foo",
2934 privileged: true,
2935 srcs: ["a.java"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09002936 sdk_version: "current",
Colin Cross53a87f52019-06-25 13:35:30 -07002937 }
2938 `,
2939 uncompressedPlatform: true,
2940 uncompressedUnbundled: true,
2941 },
David Srbeckye033cba2020-05-20 22:20:28 +01002942 {
2943 name: "normal_uncompress_dex_true",
2944 bp: `
2945 android_app {
2946 name: "foo",
2947 srcs: ["a.java"],
2948 sdk_version: "current",
2949 uncompress_dex: true,
2950 }
2951 `,
2952 uncompressedPlatform: true,
2953 uncompressedUnbundled: true,
2954 },
2955 {
2956 name: "normal_uncompress_dex_false",
2957 bp: `
2958 android_app {
2959 name: "foo",
2960 srcs: ["a.java"],
2961 sdk_version: "current",
2962 uncompress_dex: false,
2963 }
2964 `,
2965 uncompressedPlatform: false,
2966 uncompressedUnbundled: false,
2967 },
Colin Cross53a87f52019-06-25 13:35:30 -07002968 }
2969
2970 test := func(t *testing.T, bp string, want bool, unbundled bool) {
2971 t.Helper()
2972
Paul Duffin71ae5942021-03-22 15:36:52 +00002973 result := android.GroupFixturePreparers(
2974 prepareForJavaTest,
Paul Duffin2645a292021-03-13 02:36:00 +00002975 PrepareForTestWithPrebuiltsOfCurrentApi,
Paul Duffincdb88a92021-03-14 00:36:50 +00002976 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
2977 if unbundled {
2978 variables.Unbundled_build = proptools.BoolPtr(true)
2979 variables.Always_use_prebuilt_sdks = proptools.BoolPtr(true)
2980 }
2981 }),
2982 ).RunTestWithBp(t, bp)
Colin Cross53a87f52019-06-25 13:35:30 -07002983
Paul Duffincdb88a92021-03-14 00:36:50 +00002984 foo := result.ModuleForTests("foo", "android_common")
Colin Cross53a87f52019-06-25 13:35:30 -07002985 dex := foo.Rule("r8")
2986 uncompressedInDexJar := strings.Contains(dex.Args["zipFlags"], "-L 0")
2987 aligned := foo.MaybeRule("zipalign").Rule != nil
2988
Paul Duffincdb88a92021-03-14 00:36:50 +00002989 android.AssertBoolEquals(t, "uncompressed in dex", want, uncompressedInDexJar)
Colin Cross53a87f52019-06-25 13:35:30 -07002990
Paul Duffincdb88a92021-03-14 00:36:50 +00002991 android.AssertBoolEquals(t, "aligne", want, aligned)
Colin Cross53a87f52019-06-25 13:35:30 -07002992 }
2993
2994 for _, tt := range testCases {
2995 t.Run(tt.name, func(t *testing.T) {
2996 t.Run("platform", func(t *testing.T) {
2997 test(t, tt.bp, tt.uncompressedPlatform, false)
2998 })
2999 t.Run("unbundled", func(t *testing.T) {
3000 test(t, tt.bp, tt.uncompressedUnbundled, true)
3001 })
3002 })
3003 }
3004}
Jaewoong Jung26dedd32019-06-06 08:45:58 -07003005
3006func checkAapt2LinkFlag(t *testing.T, aapt2Flags, flagName, expectedValue string) {
3007 if expectedValue != "" {
3008 expectedFlag := "--" + flagName + " " + expectedValue
3009 if !strings.Contains(aapt2Flags, expectedFlag) {
3010 t.Errorf("%q is missing in aapt2 link flags, %q", expectedFlag, aapt2Flags)
3011 }
3012 } else {
3013 unexpectedFlag := "--" + flagName
3014 if strings.Contains(aapt2Flags, unexpectedFlag) {
3015 t.Errorf("unexpected flag, %q is found in aapt2 link flags, %q", unexpectedFlag, aapt2Flags)
3016 }
3017 }
3018}
Jaewoong Jung9befb0c2020-01-18 10:33:43 -08003019
Cole Faust9a631312020-10-22 21:05:24 +00003020func TestExportedProguardFlagFiles(t *testing.T) {
3021 ctx, _ := testJava(t, `
3022 android_app {
3023 name: "foo",
3024 sdk_version: "current",
3025 static_libs: ["lib1"],
3026 }
3027
3028 android_library {
3029 name: "lib1",
3030 sdk_version: "current",
3031 optimize: {
3032 proguard_flags_files: ["lib1proguard.cfg"],
3033 }
3034 }
3035 `)
3036
3037 m := ctx.ModuleForTests("foo", "android_common")
3038 hasLib1Proguard := false
3039 for _, s := range m.Rule("java.r8").Implicits.Strings() {
3040 if s == "lib1proguard.cfg" {
3041 hasLib1Proguard = true
3042 break
3043 }
3044 }
3045
3046 if !hasLib1Proguard {
3047 t.Errorf("App does not use library proguard config")
3048 }
3049}
Spandan Dasc8cb0c32021-12-08 17:00:38 +00003050
3051func TestTargetSdkVersionManifestFixer(t *testing.T) {
3052 platform_sdk_codename := "Tiramisu"
3053 testCases := []struct {
3054 name string
3055 targetSdkVersionInBp string
3056 targetSdkVersionExpected string
3057 unbundledBuild bool
3058 }{
3059 {
3060 name: "Non-Unbundled build: Android.bp has targetSdkVersion",
3061 targetSdkVersionInBp: "30",
3062 targetSdkVersionExpected: "30",
3063 unbundledBuild: false,
3064 },
3065 {
3066 name: "Unbundled build: Android.bp has targetSdkVersion",
3067 targetSdkVersionInBp: "30",
3068 targetSdkVersionExpected: "30",
3069 unbundledBuild: true,
3070 },
3071 {
3072 name: "Non-Unbundled build: Android.bp has targetSdkVersion equal to platform_sdk_codename",
3073 targetSdkVersionInBp: platform_sdk_codename,
3074 targetSdkVersionExpected: platform_sdk_codename,
3075 unbundledBuild: false,
3076 },
3077 {
3078 name: "Unbundled build: Android.bp has targetSdkVersion equal to platform_sdk_codename",
3079 targetSdkVersionInBp: platform_sdk_codename,
3080 targetSdkVersionExpected: "10000",
3081 unbundledBuild: true,
3082 },
3083
3084 {
3085 name: "Non-Unbundled build: Android.bp has no targetSdkVersion",
3086 targetSdkVersionExpected: platform_sdk_codename,
3087 unbundledBuild: false,
3088 },
3089 {
3090 name: "Unbundled build: Android.bp has no targetSdkVersion",
3091 targetSdkVersionExpected: "10000",
3092 unbundledBuild: true,
3093 },
3094 }
3095 for _, testCase := range testCases {
3096 bp := fmt.Sprintf(`
3097 android_app {
3098 name: "foo",
3099 sdk_version: "current",
3100 target_sdk_version: "%v",
3101 }
3102 `, testCase.targetSdkVersionInBp)
3103 fixture := android.GroupFixturePreparers(
3104 prepareForJavaTest,
3105 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
3106 // explicitly set platform_sdk_codename to make the test deterministic
3107 variables.Platform_sdk_codename = &platform_sdk_codename
3108 variables.Platform_version_active_codenames = []string{platform_sdk_codename}
3109 // create a non-empty list if unbundledBuild==true
3110 if testCase.unbundledBuild {
3111 variables.Unbundled_build_apps = []string{"apex_a", "apex_b"}
3112 }
3113 }),
3114 )
3115
3116 result := fixture.RunTestWithBp(t, bp)
3117 foo := result.ModuleForTests("foo", "android_common")
3118
Gurpreet Singh75d65f32022-01-24 17:44:05 +00003119 manifestFixerArgs := foo.Output("manifest_fixer/AndroidManifest.xml").Args["args"]
3120 android.AssertStringDoesContain(t, testCase.name, manifestFixerArgs, "--targetSdkVersion "+testCase.targetSdkVersionExpected)
Spandan Dasc8cb0c32021-12-08 17:00:38 +00003121 }
3122}