blob: f4fdfa756f3f4df9d59a14876349ae14447f9267 [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
147 testJavaError(t, "platform_apis must be true when sdk_version is empty.", `
148 android_app {
149 name: "bar",
150 srcs: ["b.java"],
151 }
152 `)
153
154 testJavaError(t, "platform_apis must be false when sdk_version is not empty.", `
155 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 {
1488 name string
1489 bp string
1490 certificateOverride string
Liz Kammere2b27f42020-05-07 13:24:05 -07001491 expectedLineage 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 `,
1503 certificateOverride: "",
Liz Kammere2b27f42020-05-07 13:24:05 -07001504 expectedLineage: "",
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 `,
1522 certificateOverride: "",
Liz Kammere2b27f42020-05-07 13:24:05 -07001523 expectedLineage: "",
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 `,
1536 certificateOverride: "",
Liz Kammere2b27f42020-05-07 13:24:05 -07001537 expectedLineage: "",
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 `,
1555 certificateOverride: "foo:new_certificate",
Liz Kammere2b27f42020-05-07 13:24:05 -07001556 expectedLineage: "",
1557 expectedCertificate: "cert/new_cert.x509.pem cert/new_cert.pk8",
1558 },
1559 {
1560 name: "certificate lineage",
1561 bp: `
1562 android_app {
1563 name: "foo",
1564 srcs: ["a.java"],
1565 certificate: ":new_certificate",
1566 lineage: "lineage.bin",
1567 sdk_version: "current",
1568 }
1569
1570 android_app_certificate {
1571 name: "new_certificate",
1572 certificate: "cert/new_cert",
1573 }
1574 `,
1575 certificateOverride: "",
1576 expectedLineage: "--lineage lineage.bin",
1577 expectedCertificate: "cert/new_cert.x509.pem cert/new_cert.pk8",
Jaewoong Jung2ad817c2019-01-18 14:27:16 -08001578 },
Jaewoong Jung1c1b6e62021-03-09 15:02:31 -08001579 {
1580 name: "lineage from filegroup",
1581 bp: `
1582 android_app {
1583 name: "foo",
1584 srcs: ["a.java"],
1585 certificate: ":new_certificate",
1586 lineage: ":lineage_bin",
1587 sdk_version: "current",
1588 }
1589
1590 android_app_certificate {
1591 name: "new_certificate",
1592 certificate: "cert/new_cert",
1593 }
1594
1595 filegroup {
1596 name: "lineage_bin",
1597 srcs: ["lineage.bin"],
1598 }
1599 `,
1600 certificateOverride: "",
1601 expectedLineage: "--lineage lineage.bin",
1602 expectedCertificate: "cert/new_cert.x509.pem cert/new_cert.pk8",
1603 },
Jaewoong Jung2ad817c2019-01-18 14:27:16 -08001604 }
1605
1606 for _, test := range testCases {
1607 t.Run(test.name, func(t *testing.T) {
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00001608 result := android.GroupFixturePreparers(
1609 PrepareForTestWithJavaDefaultModules,
1610 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1611 if test.certificateOverride != "" {
1612 variables.CertificateOverrides = []string{test.certificateOverride}
1613 }
1614 }),
1615 ).RunTestWithBp(t, test.bp)
Jaewoong Jung2ad817c2019-01-18 14:27:16 -08001616
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00001617 foo := result.ModuleForTests("foo", "android_common")
Jaewoong Jung2ad817c2019-01-18 14:27:16 -08001618
1619 signapk := foo.Output("foo.apk")
Liz Kammere2b27f42020-05-07 13:24:05 -07001620 signCertificateFlags := signapk.Args["certificates"]
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00001621 android.AssertStringEquals(t, "certificates flags", test.expectedCertificate, signCertificateFlags)
Liz Kammere2b27f42020-05-07 13:24:05 -07001622
1623 signFlags := signapk.Args["flags"]
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00001624 android.AssertStringEquals(t, "signing flags", test.expectedLineage, signFlags)
Jaewoong Jung2ad817c2019-01-18 14:27:16 -08001625 })
1626 }
1627}
Jaewoong Jung9d22a912019-01-23 16:27:47 -08001628
Songchun Fan688de9a2020-03-24 20:32:24 -07001629func TestRequestV4SigningFlag(t *testing.T) {
1630 testCases := []struct {
1631 name string
1632 bp string
1633 expected string
1634 }{
1635 {
1636 name: "default",
1637 bp: `
1638 android_app {
1639 name: "foo",
1640 srcs: ["a.java"],
1641 sdk_version: "current",
1642 }
1643 `,
1644 expected: "",
1645 },
1646 {
1647 name: "default",
1648 bp: `
1649 android_app {
1650 name: "foo",
1651 srcs: ["a.java"],
1652 sdk_version: "current",
1653 v4_signature: false,
1654 }
1655 `,
1656 expected: "",
1657 },
1658 {
1659 name: "module certificate property",
1660 bp: `
1661 android_app {
1662 name: "foo",
1663 srcs: ["a.java"],
1664 sdk_version: "current",
1665 v4_signature: true,
1666 }
1667 `,
1668 expected: "--enable-v4",
1669 },
1670 }
1671
1672 for _, test := range testCases {
1673 t.Run(test.name, func(t *testing.T) {
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00001674 result := android.GroupFixturePreparers(
1675 PrepareForTestWithJavaDefaultModules,
1676 ).RunTestWithBp(t, test.bp)
Songchun Fan688de9a2020-03-24 20:32:24 -07001677
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00001678 foo := result.ModuleForTests("foo", "android_common")
Songchun Fan688de9a2020-03-24 20:32:24 -07001679
1680 signapk := foo.Output("foo.apk")
1681 signFlags := signapk.Args["flags"]
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00001682 android.AssertStringEquals(t, "signing flags", test.expected, signFlags)
Songchun Fan688de9a2020-03-24 20:32:24 -07001683 })
1684 }
1685}
1686
Jaewoong Jung9d22a912019-01-23 16:27:47 -08001687func TestPackageNameOverride(t *testing.T) {
1688 testCases := []struct {
1689 name string
1690 bp string
1691 packageNameOverride string
1692 expected []string
1693 }{
1694 {
1695 name: "default",
1696 bp: `
1697 android_app {
1698 name: "foo",
1699 srcs: ["a.java"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09001700 sdk_version: "current",
Jaewoong Jung9d22a912019-01-23 16:27:47 -08001701 }
1702 `,
1703 packageNameOverride: "",
1704 expected: []string{
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00001705 "out/soong/.intermediates/foo/android_common/foo.apk",
1706 "out/soong/target/product/test_device/system/app/foo/foo.apk",
Jaewoong Jung9d22a912019-01-23 16:27:47 -08001707 },
1708 },
1709 {
1710 name: "overridden",
1711 bp: `
1712 android_app {
1713 name: "foo",
1714 srcs: ["a.java"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09001715 sdk_version: "current",
Jaewoong Jung9d22a912019-01-23 16:27:47 -08001716 }
1717 `,
1718 packageNameOverride: "foo:bar",
1719 expected: []string{
1720 // The package apk should be still be the original name for test dependencies.
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00001721 "out/soong/.intermediates/foo/android_common/bar.apk",
1722 "out/soong/target/product/test_device/system/app/bar/bar.apk",
Jaewoong Jung9d22a912019-01-23 16:27:47 -08001723 },
1724 },
1725 }
1726
1727 for _, test := range testCases {
1728 t.Run(test.name, func(t *testing.T) {
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00001729 result := android.GroupFixturePreparers(
1730 PrepareForTestWithJavaDefaultModules,
1731 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1732 if test.packageNameOverride != "" {
1733 variables.PackageNameOverrides = []string{test.packageNameOverride}
1734 }
1735 }),
1736 ).RunTestWithBp(t, test.bp)
Jaewoong Jung9d22a912019-01-23 16:27:47 -08001737
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00001738 foo := result.ModuleForTests("foo", "android_common")
1739
Lukacs T. Berkifbaf7252021-08-19 09:36:42 +02001740 outSoongDir := result.Config.SoongOutDir()
Jaewoong Jung9d22a912019-01-23 16:27:47 -08001741
1742 outputs := foo.AllOutputs()
1743 outputMap := make(map[string]bool)
1744 for _, o := range outputs {
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00001745 outputMap[android.StringPathRelativeToTop(outSoongDir, o)] = true
Jaewoong Jung9d22a912019-01-23 16:27:47 -08001746 }
1747 for _, e := range test.expected {
1748 if _, exist := outputMap[e]; !exist {
1749 t.Errorf("Can't find %q in output files.\nAll outputs:%v", e, outputs)
1750 }
1751 }
1752 })
1753 }
1754}
Jaewoong Jung4102e5d2019-02-27 16:26:28 -08001755
1756func TestInstrumentationTargetOverridden(t *testing.T) {
1757 bp := `
1758 android_app {
1759 name: "foo",
1760 srcs: ["a.java"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09001761 sdk_version: "current",
Jaewoong Jung4102e5d2019-02-27 16:26:28 -08001762 }
1763
1764 android_test {
1765 name: "bar",
1766 instrumentation_for: "foo",
Jeongik Cha538c0d02019-07-11 15:54:27 +09001767 sdk_version: "current",
Jaewoong Jung4102e5d2019-02-27 16:26:28 -08001768 }
1769 `
Jaewoong Jung4102e5d2019-02-27 16:26:28 -08001770
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00001771 result := android.GroupFixturePreparers(
1772 PrepareForTestWithJavaDefaultModules,
1773 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1774 variables.ManifestPackageNameOverrides = []string{"foo:org.dandroid.bp"}
1775 }),
1776 ).RunTestWithBp(t, bp)
Jaewoong Jung4102e5d2019-02-27 16:26:28 -08001777
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00001778 bar := result.ModuleForTests("bar", "android_common")
Jaewoong Jung4102e5d2019-02-27 16:26:28 -08001779 res := bar.Output("package-res.apk")
1780 aapt2Flags := res.Args["flags"]
1781 e := "--rename-instrumentation-target-package org.dandroid.bp"
1782 if !strings.Contains(aapt2Flags, e) {
1783 t.Errorf("target package renaming flag, %q is missing in aapt2 link flags, %q", e, aapt2Flags)
1784 }
1785}
Jaewoong Jung525443a2019-02-28 15:35:54 -08001786
1787func TestOverrideAndroidApp(t *testing.T) {
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00001788 result := PrepareForTestWithJavaDefaultModules.RunTestWithBp(
1789 t, `
Jaewoong Jung525443a2019-02-28 15:35:54 -08001790 android_app {
1791 name: "foo",
1792 srcs: ["a.java"],
Jaewoong Junga641ee92019-03-27 11:17:14 -07001793 certificate: "expiredkey",
Jaewoong Jungb639a6a2019-05-10 15:16:29 -07001794 overrides: ["qux"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09001795 sdk_version: "current",
Jaewoong Jung525443a2019-02-28 15:35:54 -08001796 }
1797
1798 override_android_app {
1799 name: "bar",
1800 base: "foo",
1801 certificate: ":new_certificate",
Liz Kammere2b27f42020-05-07 13:24:05 -07001802 lineage: "lineage.bin",
Baligh Uddin5b16dfb2020-02-11 17:27:19 -08001803 logging_parent: "bah",
Jaewoong Jung525443a2019-02-28 15:35:54 -08001804 }
1805
1806 android_app_certificate {
1807 name: "new_certificate",
1808 certificate: "cert/new_cert",
1809 }
Jaewoong Jung6f373f62019-03-13 10:13:24 -07001810
1811 override_android_app {
1812 name: "baz",
1813 base: "foo",
1814 package_name: "org.dandroid.bp",
1815 }
Liz Kammer9f9fd022020-06-18 19:44:06 +00001816
1817 override_android_app {
1818 name: "baz_no_rename_resources",
1819 base: "foo",
1820 package_name: "org.dandroid.bp",
1821 rename_resources_package: false,
1822 }
1823
1824 android_app {
1825 name: "foo_no_rename_resources",
1826 srcs: ["a.java"],
1827 certificate: "expiredkey",
1828 overrides: ["qux"],
1829 rename_resources_package: false,
1830 sdk_version: "current",
1831 }
1832
1833 override_android_app {
1834 name: "baz_base_no_rename_resources",
1835 base: "foo_no_rename_resources",
1836 package_name: "org.dandroid.bp",
1837 }
1838
1839 override_android_app {
1840 name: "baz_override_base_rename_resources",
1841 base: "foo_no_rename_resources",
1842 package_name: "org.dandroid.bp",
1843 rename_resources_package: true,
1844 }
Jaewoong Jung525443a2019-02-28 15:35:54 -08001845 `)
1846
1847 expectedVariants := []struct {
Liz Kammer9f9fd022020-06-18 19:44:06 +00001848 name string
1849 moduleName string
1850 variantName string
1851 apkName string
1852 apkPath string
1853 certFlag string
1854 lineageFlag string
1855 overrides []string
1856 packageFlag string
1857 renameResources bool
1858 logging_parent string
Jaewoong Jung525443a2019-02-28 15:35:54 -08001859 }{
1860 {
Liz Kammer9f9fd022020-06-18 19:44:06 +00001861 name: "foo",
1862 moduleName: "foo",
1863 variantName: "android_common",
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00001864 apkPath: "out/soong/target/product/test_device/system/app/foo/foo.apk",
Liz Kammer9f9fd022020-06-18 19:44:06 +00001865 certFlag: "build/make/target/product/security/expiredkey.x509.pem build/make/target/product/security/expiredkey.pk8",
1866 lineageFlag: "",
1867 overrides: []string{"qux"},
1868 packageFlag: "",
1869 renameResources: false,
1870 logging_parent: "",
Jaewoong Jung525443a2019-02-28 15:35:54 -08001871 },
1872 {
Liz Kammer9f9fd022020-06-18 19:44:06 +00001873 name: "foo",
1874 moduleName: "bar",
1875 variantName: "android_common_bar",
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00001876 apkPath: "out/soong/target/product/test_device/system/app/bar/bar.apk",
Liz Kammer9f9fd022020-06-18 19:44:06 +00001877 certFlag: "cert/new_cert.x509.pem cert/new_cert.pk8",
1878 lineageFlag: "--lineage lineage.bin",
1879 overrides: []string{"qux", "foo"},
1880 packageFlag: "",
1881 renameResources: false,
1882 logging_parent: "bah",
Jaewoong Jung6f373f62019-03-13 10:13:24 -07001883 },
1884 {
Liz Kammer9f9fd022020-06-18 19:44:06 +00001885 name: "foo",
1886 moduleName: "baz",
1887 variantName: "android_common_baz",
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00001888 apkPath: "out/soong/target/product/test_device/system/app/baz/baz.apk",
Liz Kammer9f9fd022020-06-18 19:44:06 +00001889 certFlag: "build/make/target/product/security/expiredkey.x509.pem build/make/target/product/security/expiredkey.pk8",
1890 lineageFlag: "",
1891 overrides: []string{"qux", "foo"},
1892 packageFlag: "org.dandroid.bp",
1893 renameResources: true,
1894 logging_parent: "",
1895 },
1896 {
1897 name: "foo",
1898 moduleName: "baz_no_rename_resources",
1899 variantName: "android_common_baz_no_rename_resources",
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00001900 apkPath: "out/soong/target/product/test_device/system/app/baz_no_rename_resources/baz_no_rename_resources.apk",
Liz Kammer9f9fd022020-06-18 19:44:06 +00001901 certFlag: "build/make/target/product/security/expiredkey.x509.pem build/make/target/product/security/expiredkey.pk8",
1902 lineageFlag: "",
1903 overrides: []string{"qux", "foo"},
1904 packageFlag: "org.dandroid.bp",
1905 renameResources: false,
1906 logging_parent: "",
1907 },
1908 {
1909 name: "foo_no_rename_resources",
1910 moduleName: "baz_base_no_rename_resources",
1911 variantName: "android_common_baz_base_no_rename_resources",
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00001912 apkPath: "out/soong/target/product/test_device/system/app/baz_base_no_rename_resources/baz_base_no_rename_resources.apk",
Liz Kammer9f9fd022020-06-18 19:44:06 +00001913 certFlag: "build/make/target/product/security/expiredkey.x509.pem build/make/target/product/security/expiredkey.pk8",
1914 lineageFlag: "",
1915 overrides: []string{"qux", "foo_no_rename_resources"},
1916 packageFlag: "org.dandroid.bp",
1917 renameResources: false,
1918 logging_parent: "",
1919 },
1920 {
1921 name: "foo_no_rename_resources",
1922 moduleName: "baz_override_base_rename_resources",
1923 variantName: "android_common_baz_override_base_rename_resources",
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00001924 apkPath: "out/soong/target/product/test_device/system/app/baz_override_base_rename_resources/baz_override_base_rename_resources.apk",
Liz Kammer9f9fd022020-06-18 19:44:06 +00001925 certFlag: "build/make/target/product/security/expiredkey.x509.pem build/make/target/product/security/expiredkey.pk8",
1926 lineageFlag: "",
1927 overrides: []string{"qux", "foo_no_rename_resources"},
1928 packageFlag: "org.dandroid.bp",
1929 renameResources: true,
1930 logging_parent: "",
Jaewoong Jung525443a2019-02-28 15:35:54 -08001931 },
1932 }
1933 for _, expected := range expectedVariants {
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00001934 variant := result.ModuleForTests(expected.name, expected.variantName)
Jaewoong Jung525443a2019-02-28 15:35:54 -08001935
1936 // Check the final apk name
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00001937 variant.Output(expected.apkPath)
Jaewoong Jung525443a2019-02-28 15:35:54 -08001938
1939 // Check the certificate paths
Jaewoong Jung5a498812019-11-07 14:14:38 -08001940 signapk := variant.Output(expected.moduleName + ".apk")
Liz Kammere2b27f42020-05-07 13:24:05 -07001941 certFlag := signapk.Args["certificates"]
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00001942 android.AssertStringEquals(t, "certificates flags", expected.certFlag, certFlag)
Liz Kammere2b27f42020-05-07 13:24:05 -07001943
1944 // Check the lineage flags
1945 lineageFlag := signapk.Args["flags"]
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00001946 android.AssertStringEquals(t, "signing flags", expected.lineageFlag, lineageFlag)
Jaewoong Jung525443a2019-02-28 15:35:54 -08001947
Jaewoong Jung6f373f62019-03-13 10:13:24 -07001948 // Check if the overrides field values are correctly aggregated.
Jaewoong Jung525443a2019-02-28 15:35:54 -08001949 mod := variant.Module().(*AndroidApp)
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00001950 android.AssertDeepEquals(t, "overrides property", expected.overrides, mod.appProperties.Overrides)
Jaewoong Jung6f373f62019-03-13 10:13:24 -07001951
Baligh Uddin5b16dfb2020-02-11 17:27:19 -08001952 // Test Overridable property: Logging_parent
1953 logging_parent := mod.aapt.LoggingParent
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00001954 android.AssertStringEquals(t, "overrides property value for logging parent", expected.logging_parent, logging_parent)
Baligh Uddin5b16dfb2020-02-11 17:27:19 -08001955
Liz Kammer1d5983b2020-05-19 19:15:37 +00001956 // Check the package renaming flag, if exists.
Jaewoong Jung6f373f62019-03-13 10:13:24 -07001957 res := variant.Output("package-res.apk")
1958 aapt2Flags := res.Args["flags"]
Liz Kammer9f9fd022020-06-18 19:44:06 +00001959 checkAapt2LinkFlag(t, aapt2Flags, "rename-manifest-package", expected.packageFlag)
1960 expectedPackage := expected.packageFlag
1961 if !expected.renameResources {
1962 expectedPackage = ""
Liz Kammer1d5983b2020-05-19 19:15:37 +00001963 }
Liz Kammer9f9fd022020-06-18 19:44:06 +00001964 checkAapt2LinkFlag(t, aapt2Flags, "rename-resources-package", expectedPackage)
Jaewoong Jung525443a2019-02-28 15:35:54 -08001965 }
1966}
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001967
Jaewoong Jungb639a6a2019-05-10 15:16:29 -07001968func TestOverrideAndroidAppDependency(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001969 ctx, _ := testJava(t, `
Jaewoong Jungb639a6a2019-05-10 15:16:29 -07001970 android_app {
1971 name: "foo",
1972 srcs: ["a.java"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09001973 sdk_version: "current",
Jaewoong Jungb639a6a2019-05-10 15:16:29 -07001974 }
1975
1976 override_android_app {
1977 name: "bar",
1978 base: "foo",
1979 package_name: "org.dandroid.bp",
1980 }
1981
1982 android_test {
1983 name: "baz",
1984 srcs: ["b.java"],
1985 instrumentation_for: "foo",
1986 }
1987
1988 android_test {
1989 name: "qux",
1990 srcs: ["b.java"],
1991 instrumentation_for: "bar",
1992 }
1993 `)
1994
1995 // Verify baz, which depends on the overridden module foo, has the correct classpath javac arg.
Paul Duffina71a67a2021-03-29 00:42:57 +01001996 javac := ctx.ModuleForTests("baz", "android_common").Rule("javac")
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00001997 fooTurbine := "out/soong/.intermediates/foo/android_common/turbine-combined/foo.jar"
Jaewoong Jungb639a6a2019-05-10 15:16:29 -07001998 if !strings.Contains(javac.Args["classpath"], fooTurbine) {
1999 t.Errorf("baz classpath %v does not contain %q", javac.Args["classpath"], fooTurbine)
2000 }
2001
2002 // Verify qux, which depends on the overriding module bar, has the correct classpath javac arg.
Paul Duffina71a67a2021-03-29 00:42:57 +01002003 javac = ctx.ModuleForTests("qux", "android_common").Rule("javac")
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00002004 barTurbine := "out/soong/.intermediates/foo/android_common_bar/turbine-combined/foo.jar"
Jaewoong Jungb639a6a2019-05-10 15:16:29 -07002005 if !strings.Contains(javac.Args["classpath"], barTurbine) {
2006 t.Errorf("qux classpath %v does not contain %q", javac.Args["classpath"], barTurbine)
2007 }
2008}
2009
Jaewoong Jung26dedd32019-06-06 08:45:58 -07002010func TestOverrideAndroidTest(t *testing.T) {
2011 ctx, _ := testJava(t, `
2012 android_app {
2013 name: "foo",
2014 srcs: ["a.java"],
2015 package_name: "com.android.foo",
2016 sdk_version: "current",
2017 }
2018
2019 override_android_app {
2020 name: "bar",
2021 base: "foo",
2022 package_name: "com.android.bar",
2023 }
2024
2025 android_test {
2026 name: "foo_test",
2027 srcs: ["b.java"],
2028 instrumentation_for: "foo",
2029 }
2030
2031 override_android_test {
2032 name: "bar_test",
2033 base: "foo_test",
2034 package_name: "com.android.bar.test",
2035 instrumentation_for: "bar",
2036 instrumentation_target_package: "com.android.bar",
2037 }
2038 `)
2039
2040 expectedVariants := []struct {
2041 moduleName string
2042 variantName string
2043 apkPath string
2044 overrides []string
2045 targetVariant string
2046 packageFlag string
2047 targetPackageFlag string
2048 }{
2049 {
2050 variantName: "android_common",
Jaewoong Jung326a9412019-11-21 10:41:00 -08002051 apkPath: "/target/product/test_device/testcases/foo_test/arm64/foo_test.apk",
Jaewoong Jung26dedd32019-06-06 08:45:58 -07002052 overrides: nil,
2053 targetVariant: "android_common",
2054 packageFlag: "",
2055 targetPackageFlag: "",
2056 },
2057 {
2058 variantName: "android_common_bar_test",
Jaewoong Jung326a9412019-11-21 10:41:00 -08002059 apkPath: "/target/product/test_device/testcases/bar_test/arm64/bar_test.apk",
Jaewoong Jung26dedd32019-06-06 08:45:58 -07002060 overrides: []string{"foo_test"},
2061 targetVariant: "android_common_bar",
2062 packageFlag: "com.android.bar.test",
2063 targetPackageFlag: "com.android.bar",
2064 },
2065 }
2066 for _, expected := range expectedVariants {
2067 variant := ctx.ModuleForTests("foo_test", expected.variantName)
2068
2069 // Check the final apk name
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00002070 variant.Output("out/soong" + expected.apkPath)
Jaewoong Jung26dedd32019-06-06 08:45:58 -07002071
2072 // Check if the overrides field values are correctly aggregated.
2073 mod := variant.Module().(*AndroidTest)
2074 if !reflect.DeepEqual(expected.overrides, mod.appProperties.Overrides) {
2075 t.Errorf("Incorrect overrides property value, expected: %q, got: %q",
2076 expected.overrides, mod.appProperties.Overrides)
2077 }
2078
2079 // Check if javac classpath has the correct jar file path. This checks instrumentation_for overrides.
Paul Duffina71a67a2021-03-29 00:42:57 +01002080 javac := variant.Rule("javac")
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00002081 turbine := filepath.Join("out", "soong", ".intermediates", "foo", expected.targetVariant, "turbine-combined", "foo.jar")
Jaewoong Jung26dedd32019-06-06 08:45:58 -07002082 if !strings.Contains(javac.Args["classpath"], turbine) {
2083 t.Errorf("classpath %q does not contain %q", javac.Args["classpath"], turbine)
2084 }
2085
2086 // Check aapt2 flags.
2087 res := variant.Output("package-res.apk")
2088 aapt2Flags := res.Args["flags"]
2089 checkAapt2LinkFlag(t, aapt2Flags, "rename-manifest-package", expected.packageFlag)
Liz Kammer9f9fd022020-06-18 19:44:06 +00002090 checkAapt2LinkFlag(t, aapt2Flags, "rename-resources-package", expected.packageFlag)
Jaewoong Jung26dedd32019-06-06 08:45:58 -07002091 checkAapt2LinkFlag(t, aapt2Flags, "rename-instrumentation-target-package", expected.targetPackageFlag)
2092 }
2093}
2094
Jaewoong Jung39982342020-01-14 10:27:18 -08002095func TestAndroidTest_FixTestConfig(t *testing.T) {
2096 ctx, _ := testJava(t, `
2097 android_app {
2098 name: "foo",
2099 srcs: ["a.java"],
2100 package_name: "com.android.foo",
2101 sdk_version: "current",
2102 }
2103
2104 android_test {
2105 name: "foo_test",
2106 srcs: ["b.java"],
2107 instrumentation_for: "foo",
2108 }
2109
2110 android_test {
2111 name: "bar_test",
2112 srcs: ["b.java"],
2113 package_name: "com.android.bar.test",
2114 instrumentation_for: "foo",
2115 }
2116
2117 override_android_test {
2118 name: "baz_test",
2119 base: "foo_test",
2120 package_name: "com.android.baz.test",
2121 }
2122 `)
2123
2124 testCases := []struct {
2125 moduleName string
2126 variantName string
2127 expectedFlags []string
2128 }{
2129 {
2130 moduleName: "foo_test",
2131 variantName: "android_common",
2132 },
2133 {
2134 moduleName: "bar_test",
2135 variantName: "android_common",
2136 expectedFlags: []string{
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00002137 "--manifest out/soong/.intermediates/bar_test/android_common/manifest_fixer/AndroidManifest.xml",
Jaewoong Jung39982342020-01-14 10:27:18 -08002138 "--package-name com.android.bar.test",
2139 },
2140 },
2141 {
2142 moduleName: "foo_test",
2143 variantName: "android_common_baz_test",
2144 expectedFlags: []string{
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00002145 "--manifest out/soong/.intermediates/foo_test/android_common_baz_test/manifest_fixer/AndroidManifest.xml",
Jaewoong Jung39982342020-01-14 10:27:18 -08002146 "--package-name com.android.baz.test",
2147 "--test-file-name baz_test.apk",
2148 },
2149 },
2150 }
2151
2152 for _, test := range testCases {
2153 variant := ctx.ModuleForTests(test.moduleName, test.variantName)
Paul Duffina71a67a2021-03-29 00:42:57 +01002154 params := variant.MaybeOutput("test_config_fixer/AndroidTest.xml")
Jaewoong Jung39982342020-01-14 10:27:18 -08002155
2156 if len(test.expectedFlags) > 0 {
2157 if params.Rule == nil {
2158 t.Errorf("test_config_fixer was expected to run, but didn't")
2159 } else {
2160 for _, flag := range test.expectedFlags {
2161 if !strings.Contains(params.RuleParams.Command, flag) {
2162 t.Errorf("Flag %q was not found in command: %q", flag, params.RuleParams.Command)
2163 }
2164 }
2165 }
2166 } else {
2167 if params.Rule != nil {
2168 t.Errorf("test_config_fixer was not expected to run, but did: %q", params.RuleParams.Command)
2169 }
2170 }
2171
2172 }
2173}
2174
Jaewoong Jungbc625cd2019-05-06 15:48:44 -07002175func TestStl(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07002176 ctx, _ := testJava(t, cc.GatherRequiredDepsForTest(android.Android)+`
Jaewoong Jungbc625cd2019-05-06 15:48:44 -07002177 cc_library {
2178 name: "libjni",
Peter Collingbournead84f972019-12-17 16:46:18 -08002179 sdk_version: "current",
2180 stl: "c++_shared",
Jaewoong Jungbc625cd2019-05-06 15:48:44 -07002181 }
2182
2183 android_test {
2184 name: "stl",
2185 jni_libs: ["libjni"],
2186 compile_multilib: "both",
2187 sdk_version: "current",
2188 stl: "c++_shared",
2189 }
2190
2191 android_test {
2192 name: "system",
2193 jni_libs: ["libjni"],
2194 compile_multilib: "both",
2195 sdk_version: "current",
2196 }
2197 `)
2198
2199 testCases := []struct {
2200 name string
2201 jnis []string
2202 }{
2203 {"stl",
2204 []string{
2205 "libjni.so",
Jaewoong Jung710756a2019-06-04 11:53:47 -07002206 "libc++_shared.so",
Jaewoong Jungbc625cd2019-05-06 15:48:44 -07002207 },
2208 },
2209 {"system",
2210 []string{
2211 "libjni.so",
2212 },
2213 },
2214 }
2215
2216 for _, test := range testCases {
2217 t.Run(test.name, func(t *testing.T) {
2218 app := ctx.ModuleForTests(test.name, "android_common")
2219 jniLibZip := app.Output("jnilibs.zip")
2220 var jnis []string
2221 args := strings.Fields(jniLibZip.Args["jarArgs"])
2222 for i := 0; i < len(args); i++ {
2223 if args[i] == "-f" {
2224 jnis = append(jnis, args[i+1])
2225 i += 1
2226 }
2227 }
2228 jnisJoined := strings.Join(jnis, " ")
2229 for _, jni := range test.jnis {
2230 if !strings.Contains(jnisJoined, jni) {
2231 t.Errorf("missing jni %q in %q", jni, jnis)
2232 }
2233 }
2234 })
2235 }
2236}
Colin Cross50ddcc42019-05-16 12:28:22 -07002237
2238func TestUsesLibraries(t *testing.T) {
2239 bp := `
2240 java_sdk_library {
2241 name: "foo",
2242 srcs: ["a.java"],
2243 api_packages: ["foo"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09002244 sdk_version: "current",
Colin Cross50ddcc42019-05-16 12:28:22 -07002245 }
2246
2247 java_sdk_library {
Paul Duffin859fe962020-05-15 10:20:31 +01002248 name: "qux",
2249 srcs: ["a.java"],
2250 api_packages: ["qux"],
2251 sdk_version: "current",
2252 }
2253
2254 java_sdk_library {
2255 name: "quuz",
2256 srcs: ["a.java"],
2257 api_packages: ["quuz"],
2258 sdk_version: "current",
2259 }
2260
2261 java_sdk_library {
Ulya Trafimovich65b03192020-12-03 16:50:22 +00002262 name: "fred",
2263 srcs: ["a.java"],
2264 api_packages: ["fred"],
2265 sdk_version: "current",
2266 }
2267
2268 java_sdk_library {
Colin Cross50ddcc42019-05-16 12:28:22 -07002269 name: "bar",
2270 srcs: ["a.java"],
2271 api_packages: ["bar"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09002272 sdk_version: "current",
Colin Cross50ddcc42019-05-16 12:28:22 -07002273 }
2274
Ulya Trafimovich4b6d4c12020-08-19 14:58:01 +01002275 java_sdk_library {
2276 name: "runtime-library",
2277 srcs: ["a.java"],
2278 sdk_version: "current",
2279 }
2280
2281 java_library {
2282 name: "static-runtime-helper",
2283 srcs: ["a.java"],
2284 libs: ["runtime-library"],
2285 sdk_version: "current",
2286 }
2287
Ulya Trafimoviche4432872021-08-18 16:57:11 +01002288 java_library {
2289 name: "runtime-required-x",
2290 srcs: ["a.java"],
2291 installable: true,
2292 sdk_version: "current",
2293 }
2294
2295 java_library {
2296 name: "runtime-optional-x",
2297 srcs: ["a.java"],
2298 installable: true,
2299 sdk_version: "current",
2300 }
2301
2302 android_library {
2303 name: "static-x",
2304 uses_libs: ["runtime-required-x"],
2305 optional_uses_libs: ["runtime-optional-x"],
2306 sdk_version: "current",
2307 }
2308
2309 java_library {
2310 name: "runtime-required-y",
2311 srcs: ["a.java"],
2312 installable: true,
2313 sdk_version: "current",
2314 }
2315
2316 java_library {
2317 name: "runtime-optional-y",
2318 srcs: ["a.java"],
2319 installable: true,
2320 sdk_version: "current",
2321 }
2322
2323 java_library {
2324 name: "static-y",
2325 srcs: ["a.java"],
2326 uses_libs: ["runtime-required-y"],
2327 optional_uses_libs: ["runtime-optional-y"],
2328 sdk_version: "current",
2329 }
2330
Ulya Trafimovich861a8962021-02-26 14:49:07 +00002331 // A library that has to use "provides_uses_lib", because:
2332 // - it is not an SDK library
2333 // - its library name is different from its module name
2334 java_library {
2335 name: "non-sdk-lib",
2336 provides_uses_lib: "com.non.sdk.lib",
2337 installable: true,
2338 srcs: ["a.java"],
2339 }
2340
Colin Cross50ddcc42019-05-16 12:28:22 -07002341 android_app {
2342 name: "app",
2343 srcs: ["a.java"],
Ulya Trafimovich861a8962021-02-26 14:49:07 +00002344 libs: [
2345 "qux",
2346 "quuz.stubs"
2347 ],
Ulya Trafimovich65b03192020-12-03 16:50:22 +00002348 static_libs: [
2349 "static-runtime-helper",
2350 // statically linked component libraries should not pull their SDK libraries,
2351 // so "fred" should not be added to class loader context
2352 "fred.stubs",
Ulya Trafimoviche4432872021-08-18 16:57:11 +01002353 "static-x",
2354 "static-y",
Ulya Trafimovich65b03192020-12-03 16:50:22 +00002355 ],
Ulya Trafimovich861a8962021-02-26 14:49:07 +00002356 uses_libs: [
2357 "foo",
2358 "non-sdk-lib"
2359 ],
Jeongik Cha538c0d02019-07-11 15:54:27 +09002360 sdk_version: "current",
Colin Cross50ddcc42019-05-16 12:28:22 -07002361 optional_uses_libs: [
2362 "bar",
2363 "baz",
2364 ],
2365 }
2366
2367 android_app_import {
2368 name: "prebuilt",
2369 apk: "prebuilts/apk/app.apk",
2370 certificate: "platform",
Ulya Trafimovich861a8962021-02-26 14:49:07 +00002371 uses_libs: [
2372 "foo",
2373 "non-sdk-lib",
2374 "android.test.runner"
2375 ],
Colin Cross50ddcc42019-05-16 12:28:22 -07002376 optional_uses_libs: [
2377 "bar",
2378 "baz",
2379 ],
2380 }
2381 `
2382
Paul Duffin71ae5942021-03-22 15:36:52 +00002383 result := android.GroupFixturePreparers(
2384 prepareForJavaTest,
Paul Duffin2645a292021-03-13 02:36:00 +00002385 PrepareForTestWithJavaSdkLibraryFiles,
2386 FixtureWithLastReleaseApis("runtime-library", "foo", "quuz", "qux", "bar", "fred"),
Paul Duffind234b412021-03-12 23:04:46 +00002387 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
2388 variables.MissingUsesLibraries = []string{"baz"}
2389 }),
2390 ).RunTestWithBp(t, bp)
Colin Cross50ddcc42019-05-16 12:28:22 -07002391
Paul Duffind234b412021-03-12 23:04:46 +00002392 app := result.ModuleForTests("app", "android_common")
2393 prebuilt := result.ModuleForTests("prebuilt", "android_common")
Colin Cross50ddcc42019-05-16 12:28:22 -07002394
Paul Duffin859fe962020-05-15 10:20:31 +01002395 // Test that implicit dependencies on java_sdk_library instances are passed to the manifest.
Ulya Trafimovich2eaf5c52021-02-26 12:05:11 +00002396 // This should not include explicit `uses_libs`/`optional_uses_libs` entries.
2397 actualManifestFixerArgs := app.Output("manifest_fixer/AndroidManifest.xml").Args["args"]
2398 expectManifestFixerArgs := `--extract-native-libs=true ` +
2399 `--uses-library qux ` +
2400 `--uses-library quuz ` +
2401 `--uses-library foo ` + // TODO(b/132357300): "foo" should not be passed to manifest_fixer
Ulya Trafimovicheea486a2021-02-26 11:38:21 +00002402 `--uses-library com.non.sdk.lib ` + // TODO(b/132357300): "com.non.sdk.lib" should not be passed to manifest_fixer
Ulya Trafimovichfc0f6e32021-08-12 16:16:11 +01002403 `--uses-library runtime-library ` +
Ulya Trafimoviche4432872021-08-18 16:57:11 +01002404 `--uses-library runtime-required-x ` + // TODO(b/132357300): "runtime-required-x" should not be passed to manifest_fixer
2405 `--uses-library runtime-required-y ` + // TODO(b/132357300): "runtime-required-y" should not be passed to manifest_fixer
2406 `--optional-uses-library bar ` + // TODO(b/132357300): "bar" should not be passed to manifest_fixer
2407 `--optional-uses-library runtime-optional-x ` + // TODO(b/132357300): "runtime-optional-x" should not be passed to manifest_fixer
2408 `--optional-uses-library runtime-optional-y` // TODO(b/132357300): "runtime-optional-y" should not be passed to manifest_fixer
Paul Duffind234b412021-03-12 23:04:46 +00002409 android.AssertStringEquals(t, "manifest_fixer args", expectManifestFixerArgs, actualManifestFixerArgs)
Paul Duffin859fe962020-05-15 10:20:31 +01002410
Ulya Trafimovich2eaf5c52021-02-26 12:05:11 +00002411 // Test that all libraries are verified (library order matters).
2412 verifyCmd := app.Rule("verify_uses_libraries").RuleParams.Command
2413 verifyArgs := `--uses-library foo ` +
Ulya Trafimovicheea486a2021-02-26 11:38:21 +00002414 `--uses-library com.non.sdk.lib ` +
Ulya Trafimovich2eaf5c52021-02-26 12:05:11 +00002415 `--uses-library qux ` +
2416 `--uses-library quuz ` +
2417 `--uses-library runtime-library ` +
Ulya Trafimoviche4432872021-08-18 16:57:11 +01002418 `--uses-library runtime-required-x ` +
2419 `--uses-library runtime-required-y ` +
Ulya Trafimovich2eaf5c52021-02-26 12:05:11 +00002420 `--optional-uses-library bar ` +
Ulya Trafimoviche4432872021-08-18 16:57:11 +01002421 `--optional-uses-library baz ` +
2422 `--optional-uses-library runtime-optional-x ` +
2423 `--optional-uses-library runtime-optional-y `
Paul Duffind234b412021-03-12 23:04:46 +00002424 android.AssertStringDoesContain(t, "verify cmd args", verifyCmd, verifyArgs)
Colin Cross50ddcc42019-05-16 12:28:22 -07002425
Ulya Trafimovich2eaf5c52021-02-26 12:05:11 +00002426 // Test that all libraries are verified for an APK (library order matters).
2427 verifyApkCmd := prebuilt.Rule("verify_uses_libraries").RuleParams.Command
Ulya Trafimovich0aba2522021-03-03 16:38:37 +00002428 verifyApkArgs := `--uses-library foo ` +
2429 `--uses-library com.non.sdk.lib ` +
2430 `--uses-library android.test.runner ` +
2431 `--optional-uses-library bar ` +
2432 `--optional-uses-library baz `
Paul Duffind234b412021-03-12 23:04:46 +00002433 android.AssertStringDoesContain(t, "verify apk cmd args", verifyApkCmd, verifyApkArgs)
Colin Cross50ddcc42019-05-16 12:28:22 -07002434
Ulya Trafimovich8130c482020-10-07 15:17:13 +01002435 // Test that all present libraries are preopted, including implicit SDK dependencies, possibly stubs
Ulya Trafimovich2eaf5c52021-02-26 12:05:11 +00002436 cmd := app.Rule("dexpreopt").RuleParams.Command
Ulya Trafimovich8130c482020-10-07 15:17:13 +01002437 w := `--target-context-for-sdk any ` +
Ulya Trafimovich8130c482020-10-07 15:17:13 +01002438 `PCL[/system/framework/qux.jar]#` +
Ulya Trafimovich8cbc5d22020-11-03 15:15:46 +00002439 `PCL[/system/framework/quuz.jar]#` +
2440 `PCL[/system/framework/foo.jar]#` +
Ulya Trafimovich861a8962021-02-26 14:49:07 +00002441 `PCL[/system/framework/non-sdk-lib.jar]#` +
Ulya Trafimovich8cbc5d22020-11-03 15:15:46 +00002442 `PCL[/system/framework/bar.jar]#` +
Ulya Trafimoviche4432872021-08-18 16:57:11 +01002443 `PCL[/system/framework/runtime-library.jar]#` +
2444 `PCL[/system/framework/runtime-required-x.jar]#` +
2445 `PCL[/system/framework/runtime-optional-x.jar]#` +
2446 `PCL[/system/framework/runtime-required-y.jar]#` +
2447 `PCL[/system/framework/runtime-optional-y.jar] `
Paul Duffind234b412021-03-12 23:04:46 +00002448 android.AssertStringDoesContain(t, "dexpreopt app cmd args", cmd, w)
Colin Cross50ddcc42019-05-16 12:28:22 -07002449
Ulya Trafimovich24813e12020-10-07 15:05:21 +01002450 // Test conditional context for target SDK version 28.
Paul Duffind234b412021-03-12 23:04:46 +00002451 android.AssertStringDoesContain(t, "dexpreopt app cmd 28", cmd,
2452 `--target-context-for-sdk 28`+
2453 ` PCL[/system/framework/org.apache.http.legacy.jar] `)
Ulya Trafimovich24813e12020-10-07 15:05:21 +01002454
2455 // Test conditional context for target SDK version 29.
Paul Duffind234b412021-03-12 23:04:46 +00002456 android.AssertStringDoesContain(t, "dexpreopt app cmd 29", cmd,
2457 `--target-context-for-sdk 29`+
2458 ` PCL[/system/framework/android.hidl.manager-V1.0-java.jar]`+
2459 `#PCL[/system/framework/android.hidl.base-V1.0-java.jar] `)
Ulya Trafimovich24813e12020-10-07 15:05:21 +01002460
2461 // Test conditional context for target SDK version 30.
Ulya Trafimovich46b3d5b2020-10-21 13:20:55 +01002462 // "android.test.mock" is absent because "android.test.runner" is not used.
Paul Duffind234b412021-03-12 23:04:46 +00002463 android.AssertStringDoesContain(t, "dexpreopt app cmd 30", cmd,
2464 `--target-context-for-sdk 30`+
2465 ` PCL[/system/framework/android.test.base.jar] `)
Ulya Trafimovich24813e12020-10-07 15:05:21 +01002466
Ulya Trafimovichfc24ad32020-08-19 16:32:54 +01002467 cmd = prebuilt.Rule("dexpreopt").RuleParams.Command
Paul Duffind234b412021-03-12 23:04:46 +00002468 android.AssertStringDoesContain(t, "dexpreopt prebuilt cmd", cmd,
2469 `--target-context-for-sdk any`+
2470 ` PCL[/system/framework/foo.jar]`+
2471 `#PCL[/system/framework/non-sdk-lib.jar]`+
2472 `#PCL[/system/framework/android.test.runner.jar]`+
2473 `#PCL[/system/framework/bar.jar] `)
Ulya Trafimovich24813e12020-10-07 15:05:21 +01002474
2475 // Test conditional context for target SDK version 30.
Ulya Trafimovich46b3d5b2020-10-21 13:20:55 +01002476 // "android.test.mock" is present because "android.test.runner" is used.
Paul Duffind234b412021-03-12 23:04:46 +00002477 android.AssertStringDoesContain(t, "dexpreopt prebuilt cmd 30", cmd,
2478 `--target-context-for-sdk 30`+
2479 ` PCL[/system/framework/android.test.base.jar]`+
2480 `#PCL[/system/framework/android.test.mock.jar] `)
Colin Cross50ddcc42019-05-16 12:28:22 -07002481}
Jaewoong Jungc27ab662019-05-30 15:51:14 -07002482
Ulya Trafimovich9023b022021-03-22 16:02:28 +00002483func TestDexpreoptBcp(t *testing.T) {
2484 bp := `
2485 java_sdk_library {
2486 name: "foo",
2487 srcs: ["a.java"],
2488 api_packages: ["foo"],
2489 sdk_version: "current",
2490 }
2491
2492 java_sdk_library {
2493 name: "bar",
2494 srcs: ["a.java"],
2495 api_packages: ["bar"],
2496 permitted_packages: ["bar"],
2497 sdk_version: "current",
2498 }
2499
2500 android_app {
2501 name: "app",
2502 srcs: ["a.java"],
2503 sdk_version: "current",
2504 }
2505 `
2506
2507 testCases := []struct {
2508 name string
2509 with bool
2510 expect string
2511 }{
2512 {
2513 name: "with updatable bcp",
2514 with: true,
2515 expect: "/system/framework/foo.jar:/system/framework/bar.jar",
2516 },
2517 {
2518 name: "without updatable bcp",
2519 with: false,
2520 expect: "/system/framework/foo.jar",
2521 },
2522 }
2523
2524 for _, test := range testCases {
2525 t.Run(test.name, func(t *testing.T) {
2526 result := android.GroupFixturePreparers(
2527 prepareForJavaTest,
2528 PrepareForTestWithJavaSdkLibraryFiles,
2529 FixtureWithLastReleaseApis("runtime-library", "foo", "bar"),
2530 dexpreopt.FixtureSetBootJars("platform:foo"),
satayevd604b212021-07-21 14:23:52 +01002531 dexpreopt.FixtureSetApexBootJars("platform:bar"),
Ulya Trafimovich9023b022021-03-22 16:02:28 +00002532 dexpreopt.FixtureSetPreoptWithUpdatableBcp(test.with),
2533 ).RunTestWithBp(t, bp)
2534
2535 app := result.ModuleForTests("app", "android_common")
2536 cmd := app.Rule("dexpreopt").RuleParams.Command
2537 bcp := " -Xbootclasspath-locations:" + test.expect + " " // space at the end matters
2538 android.AssertStringDoesContain(t, "dexpreopt app bcp", cmd, bcp)
2539 })
2540 }
2541}
2542
Jaewoong Jungc27ab662019-05-30 15:51:14 -07002543func TestCodelessApp(t *testing.T) {
2544 testCases := []struct {
2545 name string
2546 bp string
2547 noCode bool
2548 }{
2549 {
2550 name: "normal",
2551 bp: `
2552 android_app {
2553 name: "foo",
2554 srcs: ["a.java"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09002555 sdk_version: "current",
Jaewoong Jungc27ab662019-05-30 15:51:14 -07002556 }
2557 `,
2558 noCode: false,
2559 },
2560 {
2561 name: "app without sources",
2562 bp: `
2563 android_app {
2564 name: "foo",
Jeongik Cha538c0d02019-07-11 15:54:27 +09002565 sdk_version: "current",
Jaewoong Jungc27ab662019-05-30 15:51:14 -07002566 }
2567 `,
2568 noCode: true,
2569 },
2570 {
2571 name: "app with libraries",
2572 bp: `
2573 android_app {
2574 name: "foo",
2575 static_libs: ["lib"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09002576 sdk_version: "current",
Jaewoong Jungc27ab662019-05-30 15:51:14 -07002577 }
2578
2579 java_library {
2580 name: "lib",
2581 srcs: ["a.java"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09002582 sdk_version: "current",
Jaewoong Jungc27ab662019-05-30 15:51:14 -07002583 }
2584 `,
2585 noCode: false,
2586 },
2587 {
2588 name: "app with sourceless libraries",
2589 bp: `
2590 android_app {
2591 name: "foo",
2592 static_libs: ["lib"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09002593 sdk_version: "current",
Jaewoong Jungc27ab662019-05-30 15:51:14 -07002594 }
2595
2596 java_library {
2597 name: "lib",
Jeongik Cha538c0d02019-07-11 15:54:27 +09002598 sdk_version: "current",
Jaewoong Jungc27ab662019-05-30 15:51:14 -07002599 }
2600 `,
2601 // TODO(jungjw): this should probably be true
2602 noCode: false,
2603 },
2604 }
2605
2606 for _, test := range testCases {
2607 t.Run(test.name, func(t *testing.T) {
2608 ctx := testApp(t, test.bp)
2609
2610 foo := ctx.ModuleForTests("foo", "android_common")
2611 manifestFixerArgs := foo.Output("manifest_fixer/AndroidManifest.xml").Args["args"]
2612 if strings.Contains(manifestFixerArgs, "--has-no-code") != test.noCode {
2613 t.Errorf("unexpected manifest_fixer args: %q", manifestFixerArgs)
2614 }
2615 })
2616 }
2617}
Jaewoong Jung5b425e22019-06-17 17:40:56 -07002618
2619func TestEmbedNotice(t *testing.T) {
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00002620 result := android.GroupFixturePreparers(
2621 PrepareForTestWithJavaDefaultModules,
2622 cc.PrepareForTestWithCcDefaultModules,
2623 genrule.PrepareForTestWithGenRuleBuildComponents,
2624 android.MockFS{
2625 "APP_NOTICE": nil,
2626 "GENRULE_NOTICE": nil,
2627 "LIB_NOTICE": nil,
2628 "TOOL_NOTICE": nil,
2629 }.AddToFixture(),
2630 ).RunTestWithBp(t, `
Jaewoong Jung5b425e22019-06-17 17:40:56 -07002631 android_app {
2632 name: "foo",
2633 srcs: ["a.java"],
2634 static_libs: ["javalib"],
2635 jni_libs: ["libjni"],
2636 notice: "APP_NOTICE",
2637 embed_notices: true,
Jeongik Cha538c0d02019-07-11 15:54:27 +09002638 sdk_version: "current",
Jaewoong Jung5b425e22019-06-17 17:40:56 -07002639 }
2640
2641 // No embed_notice flag
2642 android_app {
2643 name: "bar",
2644 srcs: ["a.java"],
2645 jni_libs: ["libjni"],
2646 notice: "APP_NOTICE",
Jeongik Cha538c0d02019-07-11 15:54:27 +09002647 sdk_version: "current",
Jaewoong Jung5b425e22019-06-17 17:40:56 -07002648 }
2649
2650 // No NOTICE files
2651 android_app {
2652 name: "baz",
2653 srcs: ["a.java"],
2654 embed_notices: true,
Jeongik Cha538c0d02019-07-11 15:54:27 +09002655 sdk_version: "current",
Jaewoong Jung5b425e22019-06-17 17:40:56 -07002656 }
2657
2658 cc_library {
2659 name: "libjni",
2660 system_shared_libs: [],
2661 stl: "none",
2662 notice: "LIB_NOTICE",
Colin Cross094cde42020-02-15 10:38:00 -08002663 sdk_version: "current",
Jaewoong Jung5b425e22019-06-17 17:40:56 -07002664 }
2665
2666 java_library {
2667 name: "javalib",
2668 srcs: [
2669 ":gen",
2670 ],
Jeongik Cha538c0d02019-07-11 15:54:27 +09002671 sdk_version: "current",
Jaewoong Jung5b425e22019-06-17 17:40:56 -07002672 }
2673
2674 genrule {
2675 name: "gen",
2676 tools: ["gentool"],
2677 out: ["gen.java"],
2678 notice: "GENRULE_NOTICE",
2679 }
2680
2681 java_binary_host {
2682 name: "gentool",
2683 srcs: ["b.java"],
2684 notice: "TOOL_NOTICE",
2685 }
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00002686 `)
Jaewoong Jung5b425e22019-06-17 17:40:56 -07002687
2688 // foo has NOTICE files to process, and embed_notices is true.
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00002689 foo := result.ModuleForTests("foo", "android_common")
Jaewoong Jung5b425e22019-06-17 17:40:56 -07002690 // verify merge notices rule.
2691 mergeNotices := foo.Rule("mergeNoticesRule")
2692 noticeInputs := mergeNotices.Inputs.Strings()
2693 // TOOL_NOTICE should be excluded as it's a host module.
2694 if len(mergeNotices.Inputs) != 3 {
2695 t.Errorf("number of input notice files: expected = 3, actual = %q", noticeInputs)
2696 }
2697 if !inList("APP_NOTICE", noticeInputs) {
2698 t.Errorf("APP_NOTICE is missing from notice files, %q", noticeInputs)
2699 }
2700 if !inList("LIB_NOTICE", noticeInputs) {
2701 t.Errorf("LIB_NOTICE is missing from notice files, %q", noticeInputs)
2702 }
2703 if !inList("GENRULE_NOTICE", noticeInputs) {
2704 t.Errorf("GENRULE_NOTICE is missing from notice files, %q", noticeInputs)
2705 }
2706 // 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 +01002707 res := foo.Output("package-res.apk")
Jaewoong Jung5b425e22019-06-17 17:40:56 -07002708 aapt2Flags := res.Args["flags"]
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00002709 e := "-A out/soong/.intermediates/foo/android_common/NOTICE"
2710 android.AssertStringDoesContain(t, "expected.apkPath", aapt2Flags, e)
Jaewoong Jung5b425e22019-06-17 17:40:56 -07002711
2712 // bar has NOTICE files to process, but embed_notices is not set.
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00002713 bar := result.ModuleForTests("bar", "android_common")
Paul Duffina71a67a2021-03-29 00:42:57 +01002714 res = bar.Output("package-res.apk")
Jaewoong Jung98772792019-07-01 17:15:13 -07002715 aapt2Flags = res.Args["flags"]
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00002716 e = "-A out/soong/.intermediates/bar/android_common/NOTICE"
2717 android.AssertStringDoesNotContain(t, "bar shouldn't have the asset dir flag for NOTICE", aapt2Flags, e)
Jaewoong Jung5b425e22019-06-17 17:40:56 -07002718
2719 // baz's embed_notice is true, but it doesn't have any NOTICE files.
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00002720 baz := result.ModuleForTests("baz", "android_common")
Jaewoong Jung98772792019-07-01 17:15:13 -07002721 res = baz.Output("package-res.apk")
2722 aapt2Flags = res.Args["flags"]
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00002723 e = "-A out/soong/.intermediates/baz/android_common/NOTICE"
Jaewoong Jung98772792019-07-01 17:15:13 -07002724 if strings.Contains(aapt2Flags, e) {
2725 t.Errorf("baz shouldn't have the asset dir flag for NOTICE: %q", e)
Jaewoong Jung5b425e22019-06-17 17:40:56 -07002726 }
2727}
Colin Cross53a87f52019-06-25 13:35:30 -07002728
2729func TestUncompressDex(t *testing.T) {
2730 testCases := []struct {
2731 name string
2732 bp string
2733
2734 uncompressedPlatform bool
2735 uncompressedUnbundled bool
2736 }{
2737 {
2738 name: "normal",
2739 bp: `
2740 android_app {
2741 name: "foo",
2742 srcs: ["a.java"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09002743 sdk_version: "current",
Colin Cross53a87f52019-06-25 13:35:30 -07002744 }
2745 `,
2746 uncompressedPlatform: true,
2747 uncompressedUnbundled: false,
2748 },
2749 {
2750 name: "use_embedded_dex",
2751 bp: `
2752 android_app {
2753 name: "foo",
2754 use_embedded_dex: true,
2755 srcs: ["a.java"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09002756 sdk_version: "current",
Colin Cross53a87f52019-06-25 13:35:30 -07002757 }
2758 `,
2759 uncompressedPlatform: true,
2760 uncompressedUnbundled: true,
2761 },
2762 {
2763 name: "privileged",
2764 bp: `
2765 android_app {
2766 name: "foo",
2767 privileged: true,
2768 srcs: ["a.java"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09002769 sdk_version: "current",
Colin Cross53a87f52019-06-25 13:35:30 -07002770 }
2771 `,
2772 uncompressedPlatform: true,
2773 uncompressedUnbundled: true,
2774 },
David Srbeckye033cba2020-05-20 22:20:28 +01002775 {
2776 name: "normal_uncompress_dex_true",
2777 bp: `
2778 android_app {
2779 name: "foo",
2780 srcs: ["a.java"],
2781 sdk_version: "current",
2782 uncompress_dex: true,
2783 }
2784 `,
2785 uncompressedPlatform: true,
2786 uncompressedUnbundled: true,
2787 },
2788 {
2789 name: "normal_uncompress_dex_false",
2790 bp: `
2791 android_app {
2792 name: "foo",
2793 srcs: ["a.java"],
2794 sdk_version: "current",
2795 uncompress_dex: false,
2796 }
2797 `,
2798 uncompressedPlatform: false,
2799 uncompressedUnbundled: false,
2800 },
Colin Cross53a87f52019-06-25 13:35:30 -07002801 }
2802
2803 test := func(t *testing.T, bp string, want bool, unbundled bool) {
2804 t.Helper()
2805
Paul Duffin71ae5942021-03-22 15:36:52 +00002806 result := android.GroupFixturePreparers(
2807 prepareForJavaTest,
Paul Duffin2645a292021-03-13 02:36:00 +00002808 PrepareForTestWithPrebuiltsOfCurrentApi,
Paul Duffincdb88a92021-03-14 00:36:50 +00002809 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
2810 if unbundled {
2811 variables.Unbundled_build = proptools.BoolPtr(true)
2812 variables.Always_use_prebuilt_sdks = proptools.BoolPtr(true)
2813 }
2814 }),
2815 ).RunTestWithBp(t, bp)
Colin Cross53a87f52019-06-25 13:35:30 -07002816
Paul Duffincdb88a92021-03-14 00:36:50 +00002817 foo := result.ModuleForTests("foo", "android_common")
Colin Cross53a87f52019-06-25 13:35:30 -07002818 dex := foo.Rule("r8")
2819 uncompressedInDexJar := strings.Contains(dex.Args["zipFlags"], "-L 0")
2820 aligned := foo.MaybeRule("zipalign").Rule != nil
2821
Paul Duffincdb88a92021-03-14 00:36:50 +00002822 android.AssertBoolEquals(t, "uncompressed in dex", want, uncompressedInDexJar)
Colin Cross53a87f52019-06-25 13:35:30 -07002823
Paul Duffincdb88a92021-03-14 00:36:50 +00002824 android.AssertBoolEquals(t, "aligne", want, aligned)
Colin Cross53a87f52019-06-25 13:35:30 -07002825 }
2826
2827 for _, tt := range testCases {
2828 t.Run(tt.name, func(t *testing.T) {
2829 t.Run("platform", func(t *testing.T) {
2830 test(t, tt.bp, tt.uncompressedPlatform, false)
2831 })
2832 t.Run("unbundled", func(t *testing.T) {
2833 test(t, tt.bp, tt.uncompressedUnbundled, true)
2834 })
2835 })
2836 }
2837}
Jaewoong Jung26dedd32019-06-06 08:45:58 -07002838
2839func checkAapt2LinkFlag(t *testing.T, aapt2Flags, flagName, expectedValue string) {
2840 if expectedValue != "" {
2841 expectedFlag := "--" + flagName + " " + expectedValue
2842 if !strings.Contains(aapt2Flags, expectedFlag) {
2843 t.Errorf("%q is missing in aapt2 link flags, %q", expectedFlag, aapt2Flags)
2844 }
2845 } else {
2846 unexpectedFlag := "--" + flagName
2847 if strings.Contains(aapt2Flags, unexpectedFlag) {
2848 t.Errorf("unexpected flag, %q is found in aapt2 link flags, %q", unexpectedFlag, aapt2Flags)
2849 }
2850 }
2851}
Jaewoong Jung9befb0c2020-01-18 10:33:43 -08002852
Cole Faust9a631312020-10-22 21:05:24 +00002853func TestExportedProguardFlagFiles(t *testing.T) {
2854 ctx, _ := testJava(t, `
2855 android_app {
2856 name: "foo",
2857 sdk_version: "current",
2858 static_libs: ["lib1"],
2859 }
2860
2861 android_library {
2862 name: "lib1",
2863 sdk_version: "current",
2864 optimize: {
2865 proguard_flags_files: ["lib1proguard.cfg"],
2866 }
2867 }
2868 `)
2869
2870 m := ctx.ModuleForTests("foo", "android_common")
2871 hasLib1Proguard := false
2872 for _, s := range m.Rule("java.r8").Implicits.Strings() {
2873 if s == "lib1proguard.cfg" {
2874 hasLib1Proguard = true
2875 break
2876 }
2877 }
2878
2879 if !hasLib1Proguard {
2880 t.Errorf("App does not use library proguard config")
2881 }
2882}