blob: 239bd52578dea0a44b3712e6c855ecfa777cf452 [file] [log] [blame]
Colin Cross3bc7ffa2017-11-22 16:19:37 -08001// Copyright 2017 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package java
16
17import (
Colin Crossd09b0b62018-04-18 11:06:47 -070018 "fmt"
Colin Crossa4f08812018-10-02 22:03:40 -070019 "path/filepath"
Colin Cross3bc7ffa2017-11-22 16:19:37 -080020 "reflect"
Colin Crossb69301e2017-12-01 10:48:26 -080021 "sort"
Colin Crossd09b0b62018-04-18 11:06:47 -070022 "strings"
Colin Cross3bc7ffa2017-11-22 16:19:37 -080023 "testing"
Jaewoong Junga5e5abc2019-04-26 14:31:50 -070024
25 "github.com/google/blueprint/proptools"
26
27 "android/soong/android"
28 "android/soong/cc"
Ulya Trafimovich9023b022021-03-22 16:02:28 +000029 "android/soong/dexpreopt"
Colin Cross3bc7ffa2017-11-22 16:19:37 -080030)
31
Paul Duffin71ae5942021-03-22 15:36:52 +000032// testApp runs tests using the prepareForJavaTest
Paul Duffin0ed42d32021-03-13 02:19:32 +000033//
34// See testJava for an explanation as to how to stop using this deprecated method.
35//
36// deprecated
37func testApp(t *testing.T, bp string) *android.TestContext {
38 t.Helper()
Paul Duffin71ae5942021-03-22 15:36:52 +000039 result := prepareForJavaTest.RunTestWithBp(t, bp)
Paul Duffin0ed42d32021-03-13 02:19:32 +000040 return result.TestContext
41}
42
43func TestApp(t *testing.T) {
44 resourceFiles := []string{
Colin Cross3bc7ffa2017-11-22 16:19:37 -080045 "res/layout/layout.xml",
46 "res/values/strings.xml",
47 "res/values-en-rUS/strings.xml",
48 }
49
Paul Duffin0ed42d32021-03-13 02:19:32 +000050 compiledResourceFiles := []string{
Colin Cross3bc7ffa2017-11-22 16:19:37 -080051 "aapt2/res/layout_layout.xml.flat",
52 "aapt2/res/values_strings.arsc.flat",
53 "aapt2/res/values-en-rUS_strings.arsc.flat",
54 }
Colin Cross3bc7ffa2017-11-22 16:19:37 -080055
Colin Crossa97c5d32018-03-28 14:58:31 -070056 for _, moduleType := range []string{"android_app", "android_library"} {
57 t.Run(moduleType, func(t *testing.T) {
Paul Duffin71ae5942021-03-22 15:36:52 +000058 result := android.GroupFixturePreparers(
59 prepareForJavaTest,
Paul Duffin0ed42d32021-03-13 02:19:32 +000060 android.FixtureModifyMockFS(func(fs android.MockFS) {
61 for _, file := range resourceFiles {
62 fs[file] = nil
63 }
64 }),
65 ).RunTestWithBp(t, moduleType+` {
Colin Crossa97c5d32018-03-28 14:58:31 -070066 name: "foo",
67 srcs: ["a.java"],
Jeongik Cha538c0d02019-07-11 15:54:27 +090068 sdk_version: "current"
Colin Crossa97c5d32018-03-28 14:58:31 -070069 }
70 `)
Colin Cross3bc7ffa2017-11-22 16:19:37 -080071
Paul Duffin0ed42d32021-03-13 02:19:32 +000072 foo := result.ModuleForTests("foo", "android_common")
Colin Cross3bc7ffa2017-11-22 16:19:37 -080073
Colin Cross31656952018-05-24 16:11:20 -070074 var expectedLinkImplicits []string
75
76 manifestFixer := foo.Output("manifest_fixer/AndroidManifest.xml")
77 expectedLinkImplicits = append(expectedLinkImplicits, manifestFixer.Output.String())
Colin Cross3bc7ffa2017-11-22 16:19:37 -080078
Paul Duffin0ed42d32021-03-13 02:19:32 +000079 frameworkRes := result.ModuleForTests("framework-res", "android_common")
Rashed Abdel-Tawab8782f152018-08-09 14:08:53 -070080 lineageRes := result.ModuleForTests("org.lineageos.platform-res", "android_common")
Colin Crossa97c5d32018-03-28 14:58:31 -070081 expectedLinkImplicits = append(expectedLinkImplicits,
82 frameworkRes.Output("package-res.apk").Output.String())
Rashed Abdel-Tawab8782f152018-08-09 14:08:53 -070083 expectedLinkImplicits = append(expectedLinkImplicits,
84 lineageRes.Output("package-res.apk").Output.String())
Colin Cross3bc7ffa2017-11-22 16:19:37 -080085
Colin Crossa97c5d32018-03-28 14:58:31 -070086 // Test the mapping from input files to compiled output file names
87 compile := foo.Output(compiledResourceFiles[0])
Paul Duffin0ed42d32021-03-13 02:19:32 +000088 android.AssertDeepEquals(t, "aapt2 compile inputs", resourceFiles, compile.Inputs.Strings())
Colin Crossb69301e2017-12-01 10:48:26 -080089
Colin Crossa97c5d32018-03-28 14:58:31 -070090 compiledResourceOutputs := compile.Outputs.Strings()
91 sort.Strings(compiledResourceOutputs)
Colin Crossb69301e2017-12-01 10:48:26 -080092
Colin Crossa97c5d32018-03-28 14:58:31 -070093 expectedLinkImplicits = append(expectedLinkImplicits, compiledResourceOutputs...)
Colin Cross3bc7ffa2017-11-22 16:19:37 -080094
Colin Crossa97c5d32018-03-28 14:58:31 -070095 list := foo.Output("aapt2/res.list")
96 expectedLinkImplicits = append(expectedLinkImplicits, list.Output.String())
Colin Cross3bc7ffa2017-11-22 16:19:37 -080097
Colin Crossa97c5d32018-03-28 14:58:31 -070098 // Check that the link rule uses
Paul Duffin0ed42d32021-03-13 02:19:32 +000099 res := result.ModuleForTests("foo", "android_common").Output("package-res.apk")
100 android.AssertDeepEquals(t, "aapt2 link implicits", expectedLinkImplicits, res.Implicits.Strings())
Colin Crossa97c5d32018-03-28 14:58:31 -0700101 })
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800102 }
103}
Colin Cross890ff552017-11-30 20:13:19 -0800104
Colin Crosse560c4a2019-03-19 16:03:11 -0700105func TestAppSplits(t *testing.T) {
106 ctx := testApp(t, `
107 android_app {
108 name: "foo",
109 srcs: ["a.java"],
110 package_splits: ["v4", "v7,hdpi"],
Jeongik Cha538c0d02019-07-11 15:54:27 +0900111 sdk_version: "current"
Colin Crosse560c4a2019-03-19 16:03:11 -0700112 }`)
113
114 foo := ctx.ModuleForTests("foo", "android_common")
115
116 expectedOutputs := []string{
Paul Duffinfb0fe9f2021-03-22 17:31:52 +0000117 "out/soong/.intermediates/foo/android_common/foo.apk",
118 "out/soong/.intermediates/foo/android_common/foo_v4.apk",
119 "out/soong/.intermediates/foo/android_common/foo_v7_hdpi.apk",
Colin Crosse560c4a2019-03-19 16:03:11 -0700120 }
121 for _, expectedOutput := range expectedOutputs {
122 foo.Output(expectedOutput)
123 }
124
Colin Cross41955e82019-05-29 14:40:35 -0700125 outputFiles, err := foo.Module().(*AndroidApp).OutputFiles("")
126 if err != nil {
127 t.Fatal(err)
128 }
Paul Duffinfb0fe9f2021-03-22 17:31:52 +0000129 android.AssertPathsRelativeToTopEquals(t, `OutputFiles("")`, expectedOutputs, outputFiles)
Colin Crosse560c4a2019-03-19 16:03:11 -0700130}
131
Jeongik Cha538c0d02019-07-11 15:54:27 +0900132func TestPlatformAPIs(t *testing.T) {
133 testJava(t, `
134 android_app {
135 name: "foo",
136 srcs: ["a.java"],
137 platform_apis: true,
138 }
139 `)
140
141 testJava(t, `
142 android_app {
143 name: "foo",
144 srcs: ["a.java"],
145 sdk_version: "current",
146 }
147 `)
148
Spandan Das60999342021-11-16 04:15:33 +0000149 testJavaError(t, "This module has conflicting settings. sdk_version is empty, which means that this module is build against platform APIs. However platform_apis is not set to true", `
Jeongik Cha538c0d02019-07-11 15:54:27 +0900150 android_app {
151 name: "bar",
152 srcs: ["b.java"],
153 }
154 `)
155
Spandan Das60999342021-11-16 04:15:33 +0000156 testJavaError(t, "This module has conflicting settings. sdk_version is not empty, which means this module cannot use platform APIs. However platform_apis is set to true.", `
Jeongik Cha538c0d02019-07-11 15:54:27 +0900157 android_app {
158 name: "bar",
159 srcs: ["b.java"],
160 sdk_version: "system_current",
161 platform_apis: true,
162 }
163 `)
164}
165
Jeongik Chae403e9e2019-12-07 00:16:24 +0900166func TestAndroidAppLinkType(t *testing.T) {
167 testJava(t, `
168 android_app {
169 name: "foo",
170 srcs: ["a.java"],
171 libs: ["bar"],
172 static_libs: ["baz"],
173 platform_apis: true,
174 }
175
176 java_library {
177 name: "bar",
178 sdk_version: "current",
179 srcs: ["b.java"],
180 }
181
182 android_library {
183 name: "baz",
184 sdk_version: "system_current",
185 srcs: ["c.java"],
186 }
187 `)
188
Steven Moreland00298982020-11-17 21:44:36 +0000189 testJavaError(t, "consider adjusting sdk_version: OR platform_apis:", `
Jeongik Chae403e9e2019-12-07 00:16:24 +0900190 android_app {
191 name: "foo",
192 srcs: ["a.java"],
193 libs: ["bar"],
194 sdk_version: "current",
195 static_libs: ["baz"],
196 }
197
198 java_library {
199 name: "bar",
200 sdk_version: "current",
201 srcs: ["b.java"],
202 }
203
204 android_library {
205 name: "baz",
206 sdk_version: "system_current",
207 srcs: ["c.java"],
208 }
209 `)
210
211 testJava(t, `
212 android_app {
213 name: "foo",
214 srcs: ["a.java"],
215 libs: ["bar"],
216 sdk_version: "system_current",
217 static_libs: ["baz"],
218 }
219
220 java_library {
221 name: "bar",
222 sdk_version: "current",
223 srcs: ["b.java"],
224 }
225
226 android_library {
227 name: "baz",
228 sdk_version: "system_current",
229 srcs: ["c.java"],
230 }
231 `)
232
Steven Moreland00298982020-11-17 21:44:36 +0000233 testJavaError(t, "consider adjusting sdk_version: OR platform_apis:", `
Jeongik Chae403e9e2019-12-07 00:16:24 +0900234 android_app {
235 name: "foo",
236 srcs: ["a.java"],
237 libs: ["bar"],
238 sdk_version: "system_current",
239 static_libs: ["baz"],
240 }
241
242 java_library {
243 name: "bar",
244 sdk_version: "current",
245 srcs: ["b.java"],
246 }
247
248 android_library {
249 name: "baz",
250 srcs: ["c.java"],
251 }
252 `)
253}
254
Artur Satayev2db1c3f2020-04-08 19:09:30 +0100255func TestUpdatableApps(t *testing.T) {
256 testCases := []struct {
257 name string
258 bp string
259 expectedError string
260 }{
261 {
262 name: "Stable public SDK",
263 bp: `android_app {
264 name: "foo",
265 srcs: ["a.java"],
266 sdk_version: "29",
Artur Satayevf40fc852020-04-16 13:43:02 +0100267 min_sdk_version: "29",
Artur Satayev2db1c3f2020-04-08 19:09:30 +0100268 updatable: true,
269 }`,
270 },
271 {
272 name: "Stable system SDK",
273 bp: `android_app {
274 name: "foo",
275 srcs: ["a.java"],
276 sdk_version: "system_29",
Artur Satayevf40fc852020-04-16 13:43:02 +0100277 min_sdk_version: "29",
Artur Satayev2db1c3f2020-04-08 19:09:30 +0100278 updatable: true,
279 }`,
280 },
281 {
282 name: "Current public SDK",
283 bp: `android_app {
284 name: "foo",
285 srcs: ["a.java"],
286 sdk_version: "current",
Artur Satayevf40fc852020-04-16 13:43:02 +0100287 min_sdk_version: "29",
Artur Satayev2db1c3f2020-04-08 19:09:30 +0100288 updatable: true,
289 }`,
290 },
291 {
292 name: "Current system SDK",
293 bp: `android_app {
294 name: "foo",
295 srcs: ["a.java"],
296 sdk_version: "system_current",
Artur Satayevf40fc852020-04-16 13:43:02 +0100297 min_sdk_version: "29",
Artur Satayev2db1c3f2020-04-08 19:09:30 +0100298 updatable: true,
299 }`,
300 },
301 {
302 name: "Current module SDK",
303 bp: `android_app {
304 name: "foo",
305 srcs: ["a.java"],
306 sdk_version: "module_current",
Artur Satayevf40fc852020-04-16 13:43:02 +0100307 min_sdk_version: "29",
Artur Satayev2db1c3f2020-04-08 19:09:30 +0100308 updatable: true,
309 }`,
310 },
311 {
312 name: "Current core SDK",
313 bp: `android_app {
314 name: "foo",
315 srcs: ["a.java"],
316 sdk_version: "core_current",
Artur Satayevf40fc852020-04-16 13:43:02 +0100317 min_sdk_version: "29",
Artur Satayev2db1c3f2020-04-08 19:09:30 +0100318 updatable: true,
319 }`,
320 },
321 {
322 name: "No Platform APIs",
323 bp: `android_app {
324 name: "foo",
325 srcs: ["a.java"],
326 platform_apis: true,
Artur Satayevf40fc852020-04-16 13:43:02 +0100327 min_sdk_version: "29",
Artur Satayev2db1c3f2020-04-08 19:09:30 +0100328 updatable: true,
329 }`,
330 expectedError: "Updatable apps must use stable SDKs",
331 },
332 {
333 name: "No Core Platform APIs",
334 bp: `android_app {
335 name: "foo",
336 srcs: ["a.java"],
337 sdk_version: "core_platform",
Artur Satayevf40fc852020-04-16 13:43:02 +0100338 min_sdk_version: "29",
Artur Satayev2db1c3f2020-04-08 19:09:30 +0100339 updatable: true,
340 }`,
341 expectedError: "Updatable apps must use stable SDKs",
342 },
343 {
344 name: "No unspecified APIs",
345 bp: `android_app {
346 name: "foo",
347 srcs: ["a.java"],
348 updatable: true,
Artur Satayevf40fc852020-04-16 13:43:02 +0100349 min_sdk_version: "29",
Artur Satayev2db1c3f2020-04-08 19:09:30 +0100350 }`,
351 expectedError: "Updatable apps must use stable SDK",
352 },
Artur Satayevf40fc852020-04-16 13:43:02 +0100353 {
354 name: "Must specify min_sdk_version",
355 bp: `android_app {
356 name: "app_without_min_sdk_version",
357 srcs: ["a.java"],
358 sdk_version: "29",
359 updatable: true,
360 }`,
361 expectedError: "updatable apps must set min_sdk_version.",
362 },
Artur Satayev2db1c3f2020-04-08 19:09:30 +0100363 }
364
365 for _, test := range testCases {
366 t.Run(test.name, func(t *testing.T) {
Paul Duffincdb88a92021-03-14 00:36:50 +0000367 errorHandler := android.FixtureExpectsNoErrors
368 if test.expectedError != "" {
369 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(test.expectedError)
Artur Satayev2db1c3f2020-04-08 19:09:30 +0100370 }
Paul Duffin71ae5942021-03-22 15:36:52 +0000371 android.GroupFixturePreparers(
372 prepareForJavaTest, FixtureWithPrebuiltApis(map[string][]string{
Paul Duffin2645a292021-03-13 02:36:00 +0000373 "29": {"foo"},
374 })).
Paul Duffincdb88a92021-03-14 00:36:50 +0000375 ExtendWithErrorHandler(errorHandler).RunTestWithBp(t, test.bp)
Artur Satayev2db1c3f2020-04-08 19:09:30 +0100376 })
377 }
378}
379
Jooyung Han749dc692020-04-15 11:03:39 +0900380func TestUpdatableApps_TransitiveDepsShouldSetMinSdkVersion(t *testing.T) {
381 testJavaError(t, `module "bar".*: should support min_sdk_version\(29\)`, cc.GatherRequiredDepsForTest(android.Android)+`
382 android_app {
383 name: "foo",
384 srcs: ["a.java"],
385 updatable: true,
386 sdk_version: "current",
387 min_sdk_version: "29",
388 static_libs: ["bar"],
389 }
390
391 java_library {
392 name: "bar",
393 sdk_version: "current",
394 }
395 `)
396}
397
Jooyung Hanbbc3fb72020-04-29 14:01:06 +0900398func TestUpdatableApps_JniLibsShouldShouldSupportMinSdkVersion(t *testing.T) {
399 testJava(t, cc.GatherRequiredDepsForTest(android.Android)+`
400 android_app {
401 name: "foo",
402 srcs: ["a.java"],
403 updatable: true,
404 sdk_version: "current",
405 min_sdk_version: "current",
406 jni_libs: ["libjni"],
407 }
408
409 cc_library {
410 name: "libjni",
411 stl: "none",
412 system_shared_libs: [],
413 sdk_version: "current",
414 }
415 `)
416}
417
418func TestUpdatableApps_JniLibShouldBeBuiltAgainstMinSdkVersion(t *testing.T) {
419 bp := cc.GatherRequiredDepsForTest(android.Android) + `
420 android_app {
421 name: "foo",
422 srcs: ["a.java"],
423 updatable: true,
424 sdk_version: "current",
425 min_sdk_version: "29",
426 jni_libs: ["libjni"],
427 }
428
429 cc_library {
430 name: "libjni",
431 stl: "none",
432 system_shared_libs: [],
Spandan Das456a77e2022-05-08 00:39:35 +0000433 sdk_version: "current",
434 min_sdk_version: "29",
Jooyung Hanbbc3fb72020-04-29 14:01:06 +0900435 }
Jooyung Hanbbc3fb72020-04-29 14:01:06 +0900436 `
437 fs := map[string][]byte{
438 "prebuilts/ndk/current/platforms/android-29/arch-arm64/usr/lib/crtbegin_so.o": nil,
439 "prebuilts/ndk/current/platforms/android-29/arch-arm64/usr/lib/crtend_so.o": nil,
440 "prebuilts/ndk/current/platforms/android-29/arch-arm/usr/lib/crtbegin_so.o": nil,
441 "prebuilts/ndk/current/platforms/android-29/arch-arm/usr/lib/crtend_so.o": nil,
442 }
443
Paul Duffinfb0fe9f2021-03-22 17:31:52 +0000444 ctx, _ := testJavaWithFS(t, bp, fs)
Jooyung Hanbbc3fb72020-04-29 14:01:06 +0900445
446 inputs := ctx.ModuleForTests("libjni", "android_arm64_armv8-a_sdk_shared").Description("link").Implicits
447 var crtbeginFound, crtendFound bool
Dan Albert92fe7402020-07-15 13:33:30 -0700448 expectedCrtBegin := ctx.ModuleForTests("crtbegin_so",
449 "android_arm64_armv8-a_sdk_29").Rule("partialLd").Output
450 expectedCrtEnd := ctx.ModuleForTests("crtend_so",
451 "android_arm64_armv8-a_sdk_29").Rule("partialLd").Output
452 implicits := []string{}
Jooyung Hanbbc3fb72020-04-29 14:01:06 +0900453 for _, input := range inputs {
Dan Albert92fe7402020-07-15 13:33:30 -0700454 implicits = append(implicits, input.String())
455 if strings.HasSuffix(input.String(), expectedCrtBegin.String()) {
Jooyung Hanbbc3fb72020-04-29 14:01:06 +0900456 crtbeginFound = true
Dan Albert92fe7402020-07-15 13:33:30 -0700457 } else if strings.HasSuffix(input.String(), expectedCrtEnd.String()) {
Jooyung Hanbbc3fb72020-04-29 14:01:06 +0900458 crtendFound = true
459 }
460 }
Dan Albert92fe7402020-07-15 13:33:30 -0700461 if !crtbeginFound {
462 t.Error(fmt.Sprintf(
463 "expected implicit with suffix %q, have the following implicits:\n%s",
464 expectedCrtBegin, strings.Join(implicits, "\n")))
465 }
466 if !crtendFound {
467 t.Error(fmt.Sprintf(
468 "expected implicit with suffix %q, have the following implicits:\n%s",
469 expectedCrtEnd, strings.Join(implicits, "\n")))
Jooyung Hanbbc3fb72020-04-29 14:01:06 +0900470 }
471}
472
473func TestUpdatableApps_ErrorIfJniLibDoesntSupportMinSdkVersion(t *testing.T) {
474 bp := cc.GatherRequiredDepsForTest(android.Android) + `
475 android_app {
476 name: "foo",
477 srcs: ["a.java"],
478 updatable: true,
479 sdk_version: "current",
480 min_sdk_version: "29", // this APK should support 29
481 jni_libs: ["libjni"],
482 }
483
484 cc_library {
485 name: "libjni",
486 stl: "none",
487 sdk_version: "current",
Spandan Das456a77e2022-05-08 00:39:35 +0000488 min_sdk_version: "current",
Jooyung Hanbbc3fb72020-04-29 14:01:06 +0900489 }
490 `
Spandan Das456a77e2022-05-08 00:39:35 +0000491 testJavaError(t, `"libjni" .*: min_sdk_version\(current\) is higher than min_sdk_version\(29\)`, bp)
Jooyung Hanbbc3fb72020-04-29 14:01:06 +0900492}
493
Spandan Das456a77e2022-05-08 00:39:35 +0000494func TestUpdatableApps_ErrorIfDepMinSdkVersionIsHigher(t *testing.T) {
Jooyung Hanbbc3fb72020-04-29 14:01:06 +0900495 bp := cc.GatherRequiredDepsForTest(android.Android) + `
496 android_app {
497 name: "foo",
498 srcs: ["a.java"],
499 updatable: true,
500 sdk_version: "current",
501 min_sdk_version: "29", // this APK should support 29
502 jni_libs: ["libjni"],
503 }
504
505 cc_library {
506 name: "libjni",
507 stl: "none",
508 shared_libs: ["libbar"],
509 system_shared_libs: [],
510 sdk_version: "27",
Spandan Das456a77e2022-05-08 00:39:35 +0000511 min_sdk_version: "27",
Jooyung Hanbbc3fb72020-04-29 14:01:06 +0900512 }
513
514 cc_library {
515 name: "libbar",
516 stl: "none",
517 system_shared_libs: [],
518 sdk_version: "current",
Spandan Das456a77e2022-05-08 00:39:35 +0000519 min_sdk_version: "current",
Jooyung Hanbbc3fb72020-04-29 14:01:06 +0900520 }
521 `
522 testJavaError(t, `"libjni" .*: links "libbar" built against newer API version "current"`, bp)
523}
524
Colin Cross0ddae7f2019-02-07 15:30:01 -0800525func TestResourceDirs(t *testing.T) {
526 testCases := []struct {
527 name string
528 prop string
529 resources []string
530 }{
531 {
532 name: "no resource_dirs",
533 prop: "",
534 resources: []string{"res/res/values/strings.xml"},
535 },
536 {
537 name: "resource_dirs",
538 prop: `resource_dirs: ["res"]`,
539 resources: []string{"res/res/values/strings.xml"},
540 },
541 {
542 name: "empty resource_dirs",
543 prop: `resource_dirs: []`,
544 resources: nil,
545 },
546 }
547
Paul Duffinfb0fe9f2021-03-22 17:31:52 +0000548 fs := android.MockFS{
Colin Cross0ddae7f2019-02-07 15:30:01 -0800549 "res/res/values/strings.xml": nil,
550 }
551
552 bp := `
553 android_app {
554 name: "foo",
Jeongik Cha538c0d02019-07-11 15:54:27 +0900555 sdk_version: "current",
Colin Cross0ddae7f2019-02-07 15:30:01 -0800556 %s
557 }
558 `
559
560 for _, testCase := range testCases {
561 t.Run(testCase.name, func(t *testing.T) {
Paul Duffinfb0fe9f2021-03-22 17:31:52 +0000562 result := android.GroupFixturePreparers(
563 PrepareForTestWithJavaDefaultModules,
564 PrepareForTestWithOverlayBuildComponents,
565 fs.AddToFixture(),
566 ).RunTestWithBp(t, fmt.Sprintf(bp, testCase.prop))
Colin Cross0ddae7f2019-02-07 15:30:01 -0800567
Paul Duffinfb0fe9f2021-03-22 17:31:52 +0000568 module := result.ModuleForTests("foo", "android_common")
Colin Cross0ddae7f2019-02-07 15:30:01 -0800569 resourceList := module.MaybeOutput("aapt2/res.list")
570
571 var resources []string
572 if resourceList.Rule != nil {
573 for _, compiledResource := range resourceList.Inputs.Strings() {
574 resources = append(resources, module.Output(compiledResource).Inputs.Strings()...)
575 }
576 }
577
Paul Duffinfb0fe9f2021-03-22 17:31:52 +0000578 android.AssertDeepEquals(t, "resource files", testCase.resources, resources)
Colin Cross0ddae7f2019-02-07 15:30:01 -0800579 })
580 }
581}
582
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800583func TestLibraryAssets(t *testing.T) {
584 bp := `
585 android_app {
586 name: "foo",
587 sdk_version: "current",
588 static_libs: ["lib1", "lib2", "lib3"],
589 }
590
591 android_library {
592 name: "lib1",
593 sdk_version: "current",
594 asset_dirs: ["assets_a"],
595 }
596
597 android_library {
598 name: "lib2",
599 sdk_version: "current",
600 }
601
602 android_library {
603 name: "lib3",
604 sdk_version: "current",
605 static_libs: ["lib4"],
606 }
607
608 android_library {
609 name: "lib4",
610 sdk_version: "current",
611 asset_dirs: ["assets_b"],
612 }
613 `
614
615 testCases := []struct {
616 name string
617 assetFlag string
618 assetPackages []string
619 }{
620 {
621 name: "foo",
622 // lib1 has its own asset. lib3 doesn't have any, but provides lib4's transitively.
623 assetPackages: []string{
Paul Duffinfb0fe9f2021-03-22 17:31:52 +0000624 "out/soong/.intermediates/foo/android_common/aapt2/package-res.apk",
625 "out/soong/.intermediates/lib1/android_common/assets.zip",
626 "out/soong/.intermediates/lib3/android_common/assets.zip",
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800627 },
628 },
629 {
630 name: "lib1",
631 assetFlag: "-A assets_a",
632 },
633 {
634 name: "lib2",
635 },
636 {
637 name: "lib3",
638 assetPackages: []string{
Paul Duffinfb0fe9f2021-03-22 17:31:52 +0000639 "out/soong/.intermediates/lib3/android_common/aapt2/package-res.apk",
640 "out/soong/.intermediates/lib4/android_common/assets.zip",
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800641 },
642 },
643 {
644 name: "lib4",
645 assetFlag: "-A assets_b",
646 },
647 }
648 ctx := testApp(t, bp)
649
650 for _, test := range testCases {
651 t.Run(test.name, func(t *testing.T) {
652 m := ctx.ModuleForTests(test.name, "android_common")
653
654 // Check asset flag in aapt2 link flags
655 var aapt2link android.TestingBuildParams
656 if len(test.assetPackages) > 0 {
657 aapt2link = m.Output("aapt2/package-res.apk")
658 } else {
659 aapt2link = m.Output("package-res.apk")
660 }
Paul Duffina71a67a2021-03-29 00:42:57 +0100661 aapt2link = aapt2link
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800662 aapt2Flags := aapt2link.Args["flags"]
663 if test.assetFlag != "" {
Paul Duffinfb0fe9f2021-03-22 17:31:52 +0000664 android.AssertStringDoesContain(t, "asset flag", aapt2Flags, test.assetFlag)
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800665 } else {
Paul Duffinfb0fe9f2021-03-22 17:31:52 +0000666 android.AssertStringDoesNotContain(t, "aapt2 link flags", aapt2Flags, " -A ")
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800667 }
668
669 // Check asset merge rule.
670 if len(test.assetPackages) > 0 {
671 mergeAssets := m.Output("package-res.apk")
Paul Duffinfb0fe9f2021-03-22 17:31:52 +0000672 android.AssertPathsRelativeToTopEquals(t, "mergeAssets inputs", test.assetPackages, mergeAssets.Inputs)
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800673 }
674 })
675 }
676}
677
Colin Crossb014f072021-02-26 14:54:36 -0800678func TestAppJavaResources(t *testing.T) {
679 bp := `
680 android_app {
681 name: "foo",
682 sdk_version: "current",
683 java_resources: ["resources/a"],
684 srcs: ["a.java"],
685 }
686
687 android_app {
688 name: "bar",
689 sdk_version: "current",
690 java_resources: ["resources/a"],
691 }
692 `
693
694 ctx := testApp(t, bp)
695
696 foo := ctx.ModuleForTests("foo", "android_common")
697 fooResources := foo.Output("res/foo.jar")
698 fooDexJar := foo.Output("dex-withres/foo.jar")
699 fooDexJarAligned := foo.Output("dex-withres-aligned/foo.jar")
700 fooApk := foo.Rule("combineApk")
701
702 if g, w := fooDexJar.Inputs.Strings(), fooResources.Output.String(); !android.InList(w, g) {
703 t.Errorf("expected resource jar %q in foo dex jar inputs %q", w, g)
704 }
705
706 if g, w := fooDexJarAligned.Input.String(), fooDexJar.Output.String(); g != w {
707 t.Errorf("expected dex jar %q in foo aligned dex jar inputs %q", w, g)
708 }
709
710 if g, w := fooApk.Inputs.Strings(), fooDexJarAligned.Output.String(); !android.InList(w, g) {
711 t.Errorf("expected aligned dex jar %q in foo apk inputs %q", w, g)
712 }
713
714 bar := ctx.ModuleForTests("bar", "android_common")
715 barResources := bar.Output("res/bar.jar")
716 barApk := bar.Rule("combineApk")
717
718 if g, w := barApk.Inputs.Strings(), barResources.Output.String(); !android.InList(w, g) {
719 t.Errorf("expected resources jar %q in bar apk inputs %q", w, g)
720 }
721}
722
Colin Crossbec85302019-02-13 13:15:46 -0800723func TestAndroidResources(t *testing.T) {
Colin Cross5c4791c2019-02-01 11:44:44 -0800724 testCases := []struct {
725 name string
726 enforceRROTargets []string
727 enforceRROExcludedOverlays []string
Colin Crossbec85302019-02-13 13:15:46 -0800728 resourceFiles map[string][]string
Colin Cross5c4791c2019-02-01 11:44:44 -0800729 overlayFiles map[string][]string
730 rroDirs map[string][]string
731 }{
732 {
733 name: "no RRO",
734 enforceRROTargets: nil,
735 enforceRROExcludedOverlays: nil,
Colin Crossbec85302019-02-13 13:15:46 -0800736 resourceFiles: map[string][]string{
737 "foo": nil,
738 "bar": {"bar/res/res/values/strings.xml"},
739 "lib": nil,
740 "lib2": {"lib2/res/res/values/strings.xml"},
741 },
Colin Cross5c4791c2019-02-01 11:44:44 -0800742 overlayFiles: map[string][]string{
Colin Crossbec85302019-02-13 13:15:46 -0800743 "foo": {
Paul Duffinfb0fe9f2021-03-22 17:31:52 +0000744 "out/soong/.intermediates/lib2/android_common/package-res.apk",
745 "out/soong/.intermediates/lib/android_common/package-res.apk",
746 "out/soong/.intermediates/lib3/android_common/package-res.apk",
Colin Cross6ed7dea2019-01-31 14:44:30 -0800747 "foo/res/res/values/strings.xml",
Colin Cross5c4791c2019-02-01 11:44:44 -0800748 "device/vendor/blah/static_overlay/foo/res/values/strings.xml",
749 "device/vendor/blah/overlay/foo/res/values/strings.xml",
Anton Hansson53c88442019-03-18 15:53:16 +0000750 "product/vendor/blah/overlay/foo/res/values/strings.xml",
Colin Cross5c4791c2019-02-01 11:44:44 -0800751 },
Colin Crossbec85302019-02-13 13:15:46 -0800752 "bar": {
Colin Cross5c4791c2019-02-01 11:44:44 -0800753 "device/vendor/blah/static_overlay/bar/res/values/strings.xml",
754 "device/vendor/blah/overlay/bar/res/values/strings.xml",
755 },
Colin Crossbec85302019-02-13 13:15:46 -0800756 "lib": {
Paul Duffinfb0fe9f2021-03-22 17:31:52 +0000757 "out/soong/.intermediates/lib2/android_common/package-res.apk",
Colin Crossbec85302019-02-13 13:15:46 -0800758 "lib/res/res/values/strings.xml",
759 "device/vendor/blah/overlay/lib/res/values/strings.xml",
760 },
Colin Cross5c4791c2019-02-01 11:44:44 -0800761 },
762 rroDirs: map[string][]string{
763 "foo": nil,
764 "bar": nil,
765 },
766 },
767 {
768 name: "enforce RRO on foo",
769 enforceRROTargets: []string{"foo"},
770 enforceRROExcludedOverlays: []string{"device/vendor/blah/static_overlay"},
Colin Crossbec85302019-02-13 13:15:46 -0800771 resourceFiles: map[string][]string{
772 "foo": nil,
773 "bar": {"bar/res/res/values/strings.xml"},
774 "lib": nil,
775 "lib2": {"lib2/res/res/values/strings.xml"},
776 },
Colin Cross5c4791c2019-02-01 11:44:44 -0800777 overlayFiles: map[string][]string{
Colin Crossbec85302019-02-13 13:15:46 -0800778 "foo": {
Paul Duffinfb0fe9f2021-03-22 17:31:52 +0000779 "out/soong/.intermediates/lib2/android_common/package-res.apk",
780 "out/soong/.intermediates/lib/android_common/package-res.apk",
781 "out/soong/.intermediates/lib3/android_common/package-res.apk",
Colin Cross6ed7dea2019-01-31 14:44:30 -0800782 "foo/res/res/values/strings.xml",
783 "device/vendor/blah/static_overlay/foo/res/values/strings.xml",
784 },
Colin Crossbec85302019-02-13 13:15:46 -0800785 "bar": {
Colin Cross5c4791c2019-02-01 11:44:44 -0800786 "device/vendor/blah/static_overlay/bar/res/values/strings.xml",
787 "device/vendor/blah/overlay/bar/res/values/strings.xml",
788 },
Colin Crossbec85302019-02-13 13:15:46 -0800789 "lib": {
Paul Duffinfb0fe9f2021-03-22 17:31:52 +0000790 "out/soong/.intermediates/lib2/android_common/package-res.apk",
Colin Crossbec85302019-02-13 13:15:46 -0800791 "lib/res/res/values/strings.xml",
Colin Crossbec85302019-02-13 13:15:46 -0800792 },
Colin Cross5c4791c2019-02-01 11:44:44 -0800793 },
Colin Crossc1c37552019-01-31 11:42:41 -0800794
Colin Cross5c4791c2019-02-01 11:44:44 -0800795 rroDirs: map[string][]string{
Colin Crossbec85302019-02-13 13:15:46 -0800796 "foo": {
Anton Hansson53c88442019-03-18 15:53:16 +0000797 "device:device/vendor/blah/overlay/foo/res",
Anton Hansson53c88442019-03-18 15:53:16 +0000798 "product:product/vendor/blah/overlay/foo/res",
Jaewoong Jungc779cd42020-10-06 18:56:10 -0700799 "device:device/vendor/blah/overlay/lib/res",
Colin Crossc1c37552019-01-31 11:42:41 -0800800 },
Colin Cross5c4791c2019-02-01 11:44:44 -0800801 "bar": nil,
Jaewoong Jungc779cd42020-10-06 18:56:10 -0700802 "lib": {"device:device/vendor/blah/overlay/lib/res"},
Colin Cross5c4791c2019-02-01 11:44:44 -0800803 },
804 },
805 {
806 name: "enforce RRO on all",
807 enforceRROTargets: []string{"*"},
808 enforceRROExcludedOverlays: []string{
809 // Excluding specific apps/res directories also allowed.
810 "device/vendor/blah/static_overlay/foo",
811 "device/vendor/blah/static_overlay/bar/res",
812 },
Colin Crossbec85302019-02-13 13:15:46 -0800813 resourceFiles: map[string][]string{
814 "foo": nil,
815 "bar": {"bar/res/res/values/strings.xml"},
816 "lib": nil,
817 "lib2": {"lib2/res/res/values/strings.xml"},
818 },
Colin Cross5c4791c2019-02-01 11:44:44 -0800819 overlayFiles: map[string][]string{
Colin Crossbec85302019-02-13 13:15:46 -0800820 "foo": {
Paul Duffinfb0fe9f2021-03-22 17:31:52 +0000821 "out/soong/.intermediates/lib2/android_common/package-res.apk",
822 "out/soong/.intermediates/lib/android_common/package-res.apk",
823 "out/soong/.intermediates/lib3/android_common/package-res.apk",
Colin Cross6ed7dea2019-01-31 14:44:30 -0800824 "foo/res/res/values/strings.xml",
825 "device/vendor/blah/static_overlay/foo/res/values/strings.xml",
826 },
Colin Crossbec85302019-02-13 13:15:46 -0800827 "bar": {"device/vendor/blah/static_overlay/bar/res/values/strings.xml"},
828 "lib": {
Paul Duffinfb0fe9f2021-03-22 17:31:52 +0000829 "out/soong/.intermediates/lib2/android_common/package-res.apk",
Colin Crossbec85302019-02-13 13:15:46 -0800830 "lib/res/res/values/strings.xml",
831 },
Colin Cross5c4791c2019-02-01 11:44:44 -0800832 },
833 rroDirs: map[string][]string{
Colin Crossbec85302019-02-13 13:15:46 -0800834 "foo": {
Anton Hansson53c88442019-03-18 15:53:16 +0000835 "device:device/vendor/blah/overlay/foo/res",
836 "product:product/vendor/blah/overlay/foo/res",
837 // Lib dep comes after the direct deps
838 "device:device/vendor/blah/overlay/lib/res",
Colin Crossc1c37552019-01-31 11:42:41 -0800839 },
Anton Hansson53c88442019-03-18 15:53:16 +0000840 "bar": {"device:device/vendor/blah/overlay/bar/res"},
841 "lib": {"device:device/vendor/blah/overlay/lib/res"},
Colin Cross5c4791c2019-02-01 11:44:44 -0800842 },
843 },
844 }
845
Anton Hansson53c88442019-03-18 15:53:16 +0000846 deviceResourceOverlays := []string{
Colin Cross890ff552017-11-30 20:13:19 -0800847 "device/vendor/blah/overlay",
848 "device/vendor/blah/overlay2",
849 "device/vendor/blah/static_overlay",
850 }
851
Anton Hansson53c88442019-03-18 15:53:16 +0000852 productResourceOverlays := []string{
853 "product/vendor/blah/overlay",
854 }
855
Paul Duffinfb0fe9f2021-03-22 17:31:52 +0000856 fs := android.MockFS{
Colin Cross890ff552017-11-30 20:13:19 -0800857 "foo/res/res/values/strings.xml": nil,
858 "bar/res/res/values/strings.xml": nil,
Colin Cross6ed7dea2019-01-31 14:44:30 -0800859 "lib/res/res/values/strings.xml": nil,
Colin Crossbec85302019-02-13 13:15:46 -0800860 "lib2/res/res/values/strings.xml": nil,
Colin Cross890ff552017-11-30 20:13:19 -0800861 "device/vendor/blah/overlay/foo/res/values/strings.xml": nil,
862 "device/vendor/blah/overlay/bar/res/values/strings.xml": nil,
Colin Cross6ed7dea2019-01-31 14:44:30 -0800863 "device/vendor/blah/overlay/lib/res/values/strings.xml": nil,
Colin Cross890ff552017-11-30 20:13:19 -0800864 "device/vendor/blah/static_overlay/foo/res/values/strings.xml": nil,
865 "device/vendor/blah/static_overlay/bar/res/values/strings.xml": nil,
866 "device/vendor/blah/overlay2/res/values/strings.xml": nil,
Anton Hansson53c88442019-03-18 15:53:16 +0000867 "product/vendor/blah/overlay/foo/res/values/strings.xml": nil,
Colin Cross890ff552017-11-30 20:13:19 -0800868 }
869
870 bp := `
871 android_app {
872 name: "foo",
Jeongik Cha538c0d02019-07-11 15:54:27 +0900873 sdk_version: "current",
Colin Cross890ff552017-11-30 20:13:19 -0800874 resource_dirs: ["foo/res"],
Anton Hansson53c88442019-03-18 15:53:16 +0000875 static_libs: ["lib", "lib3"],
Colin Cross890ff552017-11-30 20:13:19 -0800876 }
877
878 android_app {
879 name: "bar",
Jeongik Cha538c0d02019-07-11 15:54:27 +0900880 sdk_version: "current",
Colin Cross890ff552017-11-30 20:13:19 -0800881 resource_dirs: ["bar/res"],
882 }
Colin Cross6ed7dea2019-01-31 14:44:30 -0800883
884 android_library {
885 name: "lib",
Jeongik Cha75b83b02019-11-01 15:28:00 +0900886 sdk_version: "current",
Colin Cross6ed7dea2019-01-31 14:44:30 -0800887 resource_dirs: ["lib/res"],
Colin Crossbec85302019-02-13 13:15:46 -0800888 static_libs: ["lib2"],
889 }
890
891 android_library {
892 name: "lib2",
Jeongik Cha75b83b02019-11-01 15:28:00 +0900893 sdk_version: "current",
Colin Crossbec85302019-02-13 13:15:46 -0800894 resource_dirs: ["lib2/res"],
Colin Cross6ed7dea2019-01-31 14:44:30 -0800895 }
Anton Hansson53c88442019-03-18 15:53:16 +0000896
897 // This library has the same resources as lib (should not lead to dupe RROs)
898 android_library {
899 name: "lib3",
Jeongik Cha75b83b02019-11-01 15:28:00 +0900900 sdk_version: "current",
Anton Hansson53c88442019-03-18 15:53:16 +0000901 resource_dirs: ["lib/res"]
902 }
Colin Cross890ff552017-11-30 20:13:19 -0800903 `
904
Colin Cross5c4791c2019-02-01 11:44:44 -0800905 for _, testCase := range testCases {
Colin Cross890ff552017-11-30 20:13:19 -0800906 t.Run(testCase.name, func(t *testing.T) {
Paul Duffinfb0fe9f2021-03-22 17:31:52 +0000907 result := android.GroupFixturePreparers(
908 PrepareForTestWithJavaDefaultModules,
909 PrepareForTestWithOverlayBuildComponents,
910 fs.AddToFixture(),
911 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
912 variables.DeviceResourceOverlays = deviceResourceOverlays
913 variables.ProductResourceOverlays = productResourceOverlays
914 if testCase.enforceRROTargets != nil {
915 variables.EnforceRROTargets = testCase.enforceRROTargets
916 }
917 if testCase.enforceRROExcludedOverlays != nil {
918 variables.EnforceRROExcludedOverlays = testCase.enforceRROExcludedOverlays
919 }
920 }),
921 ).RunTestWithBp(t, bp)
Colin Cross890ff552017-11-30 20:13:19 -0800922
Colin Crossbec85302019-02-13 13:15:46 -0800923 resourceListToFiles := func(module android.TestingModule, list []string) (files []string) {
924 for _, o := range list {
925 res := module.MaybeOutput(o)
926 if res.Rule != nil {
927 // If the overlay is compiled as part of this module (i.e. a .arsc.flat file),
928 // verify the inputs to the .arsc.flat rule.
929 files = append(files, res.Inputs.Strings()...)
930 } else {
931 // Otherwise, verify the full path to the output of the other module
932 files = append(files, o)
Anton Hansson94c93f32019-01-30 16:03:37 +0000933 }
Colin Cross890ff552017-11-30 20:13:19 -0800934 }
Colin Crossbec85302019-02-13 13:15:46 -0800935 return files
Colin Cross890ff552017-11-30 20:13:19 -0800936 }
937
Colin Crossbec85302019-02-13 13:15:46 -0800938 getResources := func(moduleName string) (resourceFiles, overlayFiles, rroDirs []string) {
Paul Duffinfb0fe9f2021-03-22 17:31:52 +0000939 module := result.ModuleForTests(moduleName, "android_common")
Colin Crossbec85302019-02-13 13:15:46 -0800940 resourceList := module.MaybeOutput("aapt2/res.list")
941 if resourceList.Rule != nil {
Paul Duffinfb0fe9f2021-03-22 17:31:52 +0000942 resourceFiles = resourceListToFiles(module, android.PathsRelativeToTop(resourceList.Inputs))
Anton Hansson0375a4f2019-01-24 14:39:19 +0000943 }
Colin Crossbec85302019-02-13 13:15:46 -0800944 overlayList := module.MaybeOutput("aapt2/overlay.list")
945 if overlayList.Rule != nil {
Paul Duffinfb0fe9f2021-03-22 17:31:52 +0000946 overlayFiles = resourceListToFiles(module, android.PathsRelativeToTop(overlayList.Inputs))
Colin Crossbec85302019-02-13 13:15:46 -0800947 }
948
Anton Hansson53c88442019-03-18 15:53:16 +0000949 for _, d := range module.Module().(AndroidLibraryDependency).ExportedRRODirs() {
950 var prefix string
951 if d.overlayType == device {
952 prefix = "device:"
953 } else if d.overlayType == product {
954 prefix = "product:"
955 } else {
956 t.Fatalf("Unexpected overlayType %d", d.overlayType)
957 }
Paul Duffinfb0fe9f2021-03-22 17:31:52 +0000958 rroDirs = append(rroDirs, prefix+android.PathRelativeToTop(d.path))
Anton Hansson53c88442019-03-18 15:53:16 +0000959 }
Colin Crossbec85302019-02-13 13:15:46 -0800960
961 return resourceFiles, overlayFiles, rroDirs
962 }
963
964 modules := []string{"foo", "bar", "lib", "lib2"}
965 for _, module := range modules {
966 resourceFiles, overlayFiles, rroDirs := getResources(module)
967
968 if !reflect.DeepEqual(resourceFiles, testCase.resourceFiles[module]) {
969 t.Errorf("expected %s resource files:\n %#v\n got:\n %#v",
970 module, testCase.resourceFiles[module], resourceFiles)
971 }
972 if !reflect.DeepEqual(overlayFiles, testCase.overlayFiles[module]) {
973 t.Errorf("expected %s overlay files:\n %#v\n got:\n %#v",
974 module, testCase.overlayFiles[module], overlayFiles)
975 }
976 if !reflect.DeepEqual(rroDirs, testCase.rroDirs[module]) {
Anton Hansson0375a4f2019-01-24 14:39:19 +0000977 t.Errorf("expected %s rroDirs: %#v\n got:\n %#v",
Colin Crossbec85302019-02-13 13:15:46 -0800978 module, testCase.rroDirs[module], rroDirs)
Anton Hansson0375a4f2019-01-24 14:39:19 +0000979 }
Colin Cross890ff552017-11-30 20:13:19 -0800980 }
Colin Cross890ff552017-11-30 20:13:19 -0800981 })
982 }
983}
Colin Crossd09b0b62018-04-18 11:06:47 -0700984
Paul Duffincdb88a92021-03-14 00:36:50 +0000985func checkSdkVersion(t *testing.T, result *android.TestResult, expectedSdkVersion string) {
986 foo := result.ModuleForTests("foo", "android_common")
Jeongik Cha219141c2020-08-06 23:00:37 +0900987 link := foo.Output("package-res.apk")
988 linkFlags := strings.Split(link.Args["flags"], " ")
989 min := android.IndexList("--min-sdk-version", linkFlags)
990 target := android.IndexList("--target-sdk-version", linkFlags)
991
992 if min == -1 || target == -1 || min == len(linkFlags)-1 || target == len(linkFlags)-1 {
993 t.Fatalf("missing --min-sdk-version or --target-sdk-version in link flags: %q", linkFlags)
994 }
995
996 gotMinSdkVersion := linkFlags[min+1]
997 gotTargetSdkVersion := linkFlags[target+1]
998
Paul Duffincdb88a92021-03-14 00:36:50 +0000999 android.AssertStringEquals(t, "incorrect --min-sdk-version", expectedSdkVersion, gotMinSdkVersion)
Jeongik Cha219141c2020-08-06 23:00:37 +09001000
Paul Duffincdb88a92021-03-14 00:36:50 +00001001 android.AssertStringEquals(t, "incorrect --target-sdk-version", expectedSdkVersion, gotTargetSdkVersion)
Jeongik Cha219141c2020-08-06 23:00:37 +09001002}
1003
Colin Crossd09b0b62018-04-18 11:06:47 -07001004func TestAppSdkVersion(t *testing.T) {
1005 testCases := []struct {
1006 name string
1007 sdkVersion string
1008 platformSdkInt int
1009 platformSdkCodename string
1010 platformSdkFinal bool
1011 expectedMinSdkVersion string
Jeongik Cha538c0d02019-07-11 15:54:27 +09001012 platformApis bool
Dan Albert4f378d72020-07-23 17:32:15 -07001013 activeCodenames []string
Colin Crossd09b0b62018-04-18 11:06:47 -07001014 }{
1015 {
1016 name: "current final SDK",
1017 sdkVersion: "current",
1018 platformSdkInt: 27,
1019 platformSdkCodename: "REL",
1020 platformSdkFinal: true,
1021 expectedMinSdkVersion: "27",
1022 },
1023 {
1024 name: "current non-final SDK",
1025 sdkVersion: "current",
1026 platformSdkInt: 27,
1027 platformSdkCodename: "OMR1",
1028 platformSdkFinal: false,
1029 expectedMinSdkVersion: "OMR1",
Dan Albert4f378d72020-07-23 17:32:15 -07001030 activeCodenames: []string{"OMR1"},
Colin Crossd09b0b62018-04-18 11:06:47 -07001031 },
1032 {
1033 name: "default final SDK",
1034 sdkVersion: "",
Jeongik Cha538c0d02019-07-11 15:54:27 +09001035 platformApis: true,
Colin Crossd09b0b62018-04-18 11:06:47 -07001036 platformSdkInt: 27,
1037 platformSdkCodename: "REL",
1038 platformSdkFinal: true,
1039 expectedMinSdkVersion: "27",
1040 },
1041 {
1042 name: "default non-final SDK",
1043 sdkVersion: "",
Jeongik Cha538c0d02019-07-11 15:54:27 +09001044 platformApis: true,
Colin Crossd09b0b62018-04-18 11:06:47 -07001045 platformSdkInt: 27,
1046 platformSdkCodename: "OMR1",
1047 platformSdkFinal: false,
1048 expectedMinSdkVersion: "OMR1",
Dan Albert4f378d72020-07-23 17:32:15 -07001049 activeCodenames: []string{"OMR1"},
Colin Crossd09b0b62018-04-18 11:06:47 -07001050 },
1051 {
1052 name: "14",
1053 sdkVersion: "14",
1054 expectedMinSdkVersion: "14",
Dan Albert4f378d72020-07-23 17:32:15 -07001055 platformSdkCodename: "S",
1056 activeCodenames: []string{"S"},
Colin Crossd09b0b62018-04-18 11:06:47 -07001057 },
1058 }
1059
1060 for _, moduleType := range []string{"android_app", "android_library"} {
1061 for _, test := range testCases {
1062 t.Run(moduleType+" "+test.name, func(t *testing.T) {
Jeongik Cha538c0d02019-07-11 15:54:27 +09001063 platformApiProp := ""
1064 if test.platformApis {
1065 platformApiProp = "platform_apis: true,"
1066 }
Colin Crossd09b0b62018-04-18 11:06:47 -07001067 bp := fmt.Sprintf(`%s {
1068 name: "foo",
1069 srcs: ["a.java"],
1070 sdk_version: "%s",
Jeongik Cha538c0d02019-07-11 15:54:27 +09001071 %s
1072 }`, moduleType, test.sdkVersion, platformApiProp)
Colin Crossd09b0b62018-04-18 11:06:47 -07001073
Paul Duffin71ae5942021-03-22 15:36:52 +00001074 result := android.GroupFixturePreparers(
1075 prepareForJavaTest,
Paul Duffincdb88a92021-03-14 00:36:50 +00001076 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1077 variables.Platform_sdk_version = &test.platformSdkInt
1078 variables.Platform_sdk_codename = &test.platformSdkCodename
1079 variables.Platform_version_active_codenames = test.activeCodenames
1080 variables.Platform_sdk_final = &test.platformSdkFinal
1081 }),
Paul Duffin2645a292021-03-13 02:36:00 +00001082 FixtureWithPrebuiltApis(map[string][]string{
1083 "14": {"foo"},
1084 }),
Paul Duffincdb88a92021-03-14 00:36:50 +00001085 ).RunTestWithBp(t, bp)
Colin Crossd09b0b62018-04-18 11:06:47 -07001086
Paul Duffincdb88a92021-03-14 00:36:50 +00001087 checkSdkVersion(t, result, test.expectedMinSdkVersion)
Colin Crossd09b0b62018-04-18 11:06:47 -07001088 })
1089 }
1090 }
1091}
Colin Crossa4f08812018-10-02 22:03:40 -07001092
Jeongik Cha219141c2020-08-06 23:00:37 +09001093func TestVendorAppSdkVersion(t *testing.T) {
1094 testCases := []struct {
1095 name string
1096 sdkVersion string
1097 platformSdkInt int
1098 platformSdkCodename string
1099 platformSdkFinal bool
1100 deviceCurrentApiLevelForVendorModules string
1101 expectedMinSdkVersion string
1102 }{
1103 {
1104 name: "current final SDK",
1105 sdkVersion: "current",
1106 platformSdkInt: 29,
1107 platformSdkCodename: "REL",
1108 platformSdkFinal: true,
1109 deviceCurrentApiLevelForVendorModules: "29",
1110 expectedMinSdkVersion: "29",
1111 },
1112 {
1113 name: "current final SDK",
1114 sdkVersion: "current",
1115 platformSdkInt: 29,
1116 platformSdkCodename: "REL",
1117 platformSdkFinal: true,
1118 deviceCurrentApiLevelForVendorModules: "28",
1119 expectedMinSdkVersion: "28",
1120 },
1121 {
1122 name: "current final SDK",
1123 sdkVersion: "current",
1124 platformSdkInt: 29,
1125 platformSdkCodename: "Q",
1126 platformSdkFinal: false,
Jeongik Cha219141c2020-08-06 23:00:37 +09001127 deviceCurrentApiLevelForVendorModules: "28",
1128 expectedMinSdkVersion: "28",
1129 },
1130 }
1131
1132 for _, moduleType := range []string{"android_app", "android_library"} {
1133 for _, sdkKind := range []string{"", "system_"} {
1134 for _, test := range testCases {
1135 t.Run(moduleType+" "+test.name, func(t *testing.T) {
1136 bp := fmt.Sprintf(`%s {
1137 name: "foo",
1138 srcs: ["a.java"],
1139 sdk_version: "%s%s",
1140 vendor: true,
1141 }`, moduleType, sdkKind, test.sdkVersion)
1142
Paul Duffin71ae5942021-03-22 15:36:52 +00001143 result := android.GroupFixturePreparers(
1144 prepareForJavaTest,
Paul Duffincdb88a92021-03-14 00:36:50 +00001145 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1146 variables.Platform_sdk_version = &test.platformSdkInt
1147 variables.Platform_sdk_codename = &test.platformSdkCodename
1148 variables.Platform_sdk_final = &test.platformSdkFinal
1149 variables.DeviceCurrentApiLevelForVendorModules = &test.deviceCurrentApiLevelForVendorModules
1150 variables.DeviceSystemSdkVersions = []string{"28", "29"}
1151 }),
Paul Duffin2645a292021-03-13 02:36:00 +00001152 FixtureWithPrebuiltApis(map[string][]string{
1153 "28": {"foo"},
1154 "29": {"foo"},
1155 "current": {"foo"},
1156 }),
Paul Duffincdb88a92021-03-14 00:36:50 +00001157 ).RunTestWithBp(t, bp)
1158
1159 checkSdkVersion(t, result, test.expectedMinSdkVersion)
Jeongik Cha219141c2020-08-06 23:00:37 +09001160 })
1161 }
1162 }
1163 }
1164}
1165
Paul Duffin50c217c2019-06-12 13:25:22 +01001166func TestJNIABI(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001167 ctx, _ := testJava(t, cc.GatherRequiredDepsForTest(android.Android)+`
Paul Duffin50c217c2019-06-12 13:25:22 +01001168 cc_library {
1169 name: "libjni",
1170 system_shared_libs: [],
Colin Crossc511bc52020-04-07 16:50:32 +00001171 sdk_version: "current",
Paul Duffin50c217c2019-06-12 13:25:22 +01001172 stl: "none",
1173 }
1174
1175 android_test {
1176 name: "test",
1177 sdk_version: "core_platform",
1178 jni_libs: ["libjni"],
1179 }
1180
1181 android_test {
1182 name: "test_first",
1183 sdk_version: "core_platform",
1184 compile_multilib: "first",
1185 jni_libs: ["libjni"],
1186 }
1187
1188 android_test {
1189 name: "test_both",
1190 sdk_version: "core_platform",
1191 compile_multilib: "both",
1192 jni_libs: ["libjni"],
1193 }
1194
1195 android_test {
1196 name: "test_32",
1197 sdk_version: "core_platform",
1198 compile_multilib: "32",
1199 jni_libs: ["libjni"],
1200 }
1201
1202 android_test {
1203 name: "test_64",
1204 sdk_version: "core_platform",
1205 compile_multilib: "64",
1206 jni_libs: ["libjni"],
1207 }
1208 `)
1209
1210 testCases := []struct {
1211 name string
1212 abis []string
1213 }{
1214 {"test", []string{"arm64-v8a"}},
1215 {"test_first", []string{"arm64-v8a"}},
1216 {"test_both", []string{"arm64-v8a", "armeabi-v7a"}},
1217 {"test_32", []string{"armeabi-v7a"}},
1218 {"test_64", []string{"arm64-v8a"}},
1219 }
1220
1221 for _, test := range testCases {
1222 t.Run(test.name, func(t *testing.T) {
1223 app := ctx.ModuleForTests(test.name, "android_common")
1224 jniLibZip := app.Output("jnilibs.zip")
1225 var abis []string
1226 args := strings.Fields(jniLibZip.Args["jarArgs"])
1227 for i := 0; i < len(args); i++ {
1228 if args[i] == "-P" {
1229 abis = append(abis, filepath.Base(args[i+1]))
1230 i++
1231 }
1232 }
1233 if !reflect.DeepEqual(abis, test.abis) {
1234 t.Errorf("want abis %v, got %v", test.abis, abis)
1235 }
1236 })
1237 }
1238}
1239
Jeongik Cha2cc570d2019-10-29 15:44:45 +09001240func TestAppSdkVersionByPartition(t *testing.T) {
1241 testJavaError(t, "sdk_version must have a value when the module is located at vendor or product", `
1242 android_app {
1243 name: "foo",
1244 srcs: ["a.java"],
1245 vendor: true,
1246 platform_apis: true,
1247 }
1248 `)
1249
1250 testJava(t, `
1251 android_app {
1252 name: "bar",
1253 srcs: ["b.java"],
1254 platform_apis: true,
1255 }
1256 `)
1257
1258 for _, enforce := range []bool{true, false} {
Jeongik Cha2cc570d2019-10-29 15:44:45 +09001259 bp := `
1260 android_app {
1261 name: "foo",
1262 srcs: ["a.java"],
1263 product_specific: true,
1264 platform_apis: true,
1265 }
1266 `
Colin Cross98be1bb2019-12-13 20:41:13 -08001267
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00001268 errorHandler := android.FixtureExpectsNoErrors
Jeongik Cha2cc570d2019-10-29 15:44:45 +09001269 if enforce {
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00001270 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 +09001271 }
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00001272
1273 android.GroupFixturePreparers(
1274 PrepareForTestWithJavaDefaultModules,
1275 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1276 variables.EnforceProductPartitionInterface = proptools.BoolPtr(enforce)
1277 }),
1278 ).
1279 ExtendWithErrorHandler(errorHandler).
1280 RunTestWithBp(t, bp)
Jeongik Cha2cc570d2019-10-29 15:44:45 +09001281 }
1282}
1283
Paul Duffin50c217c2019-06-12 13:25:22 +01001284func TestJNIPackaging(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001285 ctx, _ := testJava(t, cc.GatherRequiredDepsForTest(android.Android)+`
Paul Duffin50c217c2019-06-12 13:25:22 +01001286 cc_library {
1287 name: "libjni",
1288 system_shared_libs: [],
1289 stl: "none",
Colin Cross094cde42020-02-15 10:38:00 -08001290 sdk_version: "current",
Paul Duffin50c217c2019-06-12 13:25:22 +01001291 }
1292
1293 android_app {
1294 name: "app",
1295 jni_libs: ["libjni"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09001296 sdk_version: "current",
Paul Duffin50c217c2019-06-12 13:25:22 +01001297 }
1298
1299 android_app {
1300 name: "app_noembed",
1301 jni_libs: ["libjni"],
1302 use_embedded_native_libs: false,
Jeongik Cha538c0d02019-07-11 15:54:27 +09001303 sdk_version: "current",
Paul Duffin50c217c2019-06-12 13:25:22 +01001304 }
1305
1306 android_app {
1307 name: "app_embed",
1308 jni_libs: ["libjni"],
1309 use_embedded_native_libs: true,
Jeongik Cha538c0d02019-07-11 15:54:27 +09001310 sdk_version: "current",
Paul Duffin50c217c2019-06-12 13:25:22 +01001311 }
1312
1313 android_test {
1314 name: "test",
Colin Crossc511bc52020-04-07 16:50:32 +00001315 sdk_version: "current",
Paul Duffin50c217c2019-06-12 13:25:22 +01001316 jni_libs: ["libjni"],
1317 }
1318
1319 android_test {
1320 name: "test_noembed",
Colin Crossc511bc52020-04-07 16:50:32 +00001321 sdk_version: "current",
Paul Duffin50c217c2019-06-12 13:25:22 +01001322 jni_libs: ["libjni"],
1323 use_embedded_native_libs: false,
1324 }
1325
1326 android_test_helper_app {
1327 name: "test_helper",
Colin Crossc511bc52020-04-07 16:50:32 +00001328 sdk_version: "current",
Paul Duffin50c217c2019-06-12 13:25:22 +01001329 jni_libs: ["libjni"],
1330 }
1331
1332 android_test_helper_app {
1333 name: "test_helper_noembed",
Colin Crossc511bc52020-04-07 16:50:32 +00001334 sdk_version: "current",
Paul Duffin50c217c2019-06-12 13:25:22 +01001335 jni_libs: ["libjni"],
1336 use_embedded_native_libs: false,
1337 }
1338 `)
1339
1340 testCases := []struct {
1341 name string
1342 packaged bool
1343 compressed bool
1344 }{
1345 {"app", false, false},
1346 {"app_noembed", false, false},
1347 {"app_embed", true, false},
1348 {"test", true, false},
1349 {"test_noembed", true, true},
1350 {"test_helper", true, false},
1351 {"test_helper_noembed", true, true},
1352 }
1353
1354 for _, test := range testCases {
1355 t.Run(test.name, func(t *testing.T) {
1356 app := ctx.ModuleForTests(test.name, "android_common")
1357 jniLibZip := app.MaybeOutput("jnilibs.zip")
1358 if g, w := (jniLibZip.Rule != nil), test.packaged; g != w {
1359 t.Errorf("expected jni packaged %v, got %v", w, g)
1360 }
1361
1362 if jniLibZip.Rule != nil {
1363 if g, w := !strings.Contains(jniLibZip.Args["jarArgs"], "-L 0"), test.compressed; g != w {
1364 t.Errorf("expected jni compressed %v, got %v", w, g)
1365 }
Colin Crossc511bc52020-04-07 16:50:32 +00001366
1367 if !strings.Contains(jniLibZip.Implicits[0].String(), "_sdk_") {
1368 t.Errorf("expected input %q to use sdk variant", jniLibZip.Implicits[0].String())
1369 }
Paul Duffin50c217c2019-06-12 13:25:22 +01001370 }
1371 })
1372 }
Colin Cross47fa9d32019-03-26 10:51:39 -07001373}
1374
Colin Cross3c007702020-05-08 11:20:24 -07001375func TestJNISDK(t *testing.T) {
1376 ctx, _ := testJava(t, cc.GatherRequiredDepsForTest(android.Android)+`
1377 cc_library {
1378 name: "libjni",
1379 system_shared_libs: [],
1380 stl: "none",
1381 sdk_version: "current",
1382 }
1383
1384 android_test {
1385 name: "app_platform",
1386 jni_libs: ["libjni"],
1387 platform_apis: true,
1388 }
1389
1390 android_test {
1391 name: "app_sdk",
1392 jni_libs: ["libjni"],
1393 sdk_version: "current",
1394 }
1395
1396 android_test {
1397 name: "app_force_platform",
1398 jni_libs: ["libjni"],
1399 sdk_version: "current",
1400 jni_uses_platform_apis: true,
1401 }
1402
1403 android_test {
1404 name: "app_force_sdk",
1405 jni_libs: ["libjni"],
1406 platform_apis: true,
1407 jni_uses_sdk_apis: true,
1408 }
Colin Crossc2d24052020-05-13 11:05:02 -07001409
1410 cc_library {
1411 name: "libvendorjni",
1412 system_shared_libs: [],
1413 stl: "none",
1414 vendor: true,
1415 }
1416
1417 android_test {
1418 name: "app_vendor",
1419 jni_libs: ["libvendorjni"],
1420 sdk_version: "current",
1421 vendor: true,
1422 }
Colin Cross3c007702020-05-08 11:20:24 -07001423 `)
1424
1425 testCases := []struct {
Colin Crossc2d24052020-05-13 11:05:02 -07001426 name string
1427 sdkJNI bool
1428 vendorJNI bool
Colin Cross3c007702020-05-08 11:20:24 -07001429 }{
Colin Crossc2d24052020-05-13 11:05:02 -07001430 {name: "app_platform"},
1431 {name: "app_sdk", sdkJNI: true},
1432 {name: "app_force_platform"},
1433 {name: "app_force_sdk", sdkJNI: true},
1434 {name: "app_vendor", vendorJNI: true},
Colin Cross3c007702020-05-08 11:20:24 -07001435 }
1436
Colin Crossc2d24052020-05-13 11:05:02 -07001437 platformJNI := ctx.ModuleForTests("libjni", "android_arm64_armv8-a_shared").
1438 Output("libjni.so").Output.String()
1439 sdkJNI := ctx.ModuleForTests("libjni", "android_arm64_armv8-a_sdk_shared").
1440 Output("libjni.so").Output.String()
1441 vendorJNI := ctx.ModuleForTests("libvendorjni", "android_arm64_armv8-a_shared").
1442 Output("libvendorjni.so").Output.String()
1443
Colin Cross3c007702020-05-08 11:20:24 -07001444 for _, test := range testCases {
1445 t.Run(test.name, func(t *testing.T) {
1446 app := ctx.ModuleForTests(test.name, "android_common")
Colin Cross3c007702020-05-08 11:20:24 -07001447
1448 jniLibZip := app.MaybeOutput("jnilibs.zip")
1449 if len(jniLibZip.Implicits) != 1 {
1450 t.Fatalf("expected exactly one jni library, got %q", jniLibZip.Implicits.Strings())
1451 }
1452 gotJNI := jniLibZip.Implicits[0].String()
1453
1454 if test.sdkJNI {
1455 if gotJNI != sdkJNI {
1456 t.Errorf("expected SDK JNI library %q, got %q", sdkJNI, gotJNI)
1457 }
Colin Crossc2d24052020-05-13 11:05:02 -07001458 } else if test.vendorJNI {
1459 if gotJNI != vendorJNI {
1460 t.Errorf("expected platform JNI library %q, got %q", vendorJNI, gotJNI)
1461 }
Colin Cross3c007702020-05-08 11:20:24 -07001462 } else {
1463 if gotJNI != platformJNI {
1464 t.Errorf("expected platform JNI library %q, got %q", platformJNI, gotJNI)
1465 }
1466 }
1467 })
1468 }
1469
1470 t.Run("jni_uses_platform_apis_error", func(t *testing.T) {
1471 testJavaError(t, `jni_uses_platform_apis: can only be set for modules that set sdk_version`, `
1472 android_test {
1473 name: "app_platform",
1474 platform_apis: true,
1475 jni_uses_platform_apis: true,
1476 }
1477 `)
1478 })
1479
1480 t.Run("jni_uses_sdk_apis_error", func(t *testing.T) {
1481 testJavaError(t, `jni_uses_sdk_apis: can only be set for modules that do not set sdk_version`, `
1482 android_test {
1483 name: "app_sdk",
1484 sdk_version: "current",
1485 jni_uses_sdk_apis: true,
1486 }
1487 `)
1488 })
1489
1490}
1491
Jaewoong Jung2ad817c2019-01-18 14:27:16 -08001492func TestCertificates(t *testing.T) {
1493 testCases := []struct {
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -04001494 name string
1495 bp string
1496 certificateOverride string
1497 expectedCertSigningFlags string
1498 expectedCertificate string
Jaewoong Jung2ad817c2019-01-18 14:27:16 -08001499 }{
1500 {
1501 name: "default",
1502 bp: `
1503 android_app {
1504 name: "foo",
1505 srcs: ["a.java"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09001506 sdk_version: "current",
Jaewoong Jung2ad817c2019-01-18 14:27:16 -08001507 }
1508 `,
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -04001509 certificateOverride: "",
1510 expectedCertSigningFlags: "",
1511 expectedCertificate: "build/make/target/product/security/testkey.x509.pem build/make/target/product/security/testkey.pk8",
Jaewoong Jung2ad817c2019-01-18 14:27:16 -08001512 },
1513 {
1514 name: "module certificate property",
1515 bp: `
1516 android_app {
1517 name: "foo",
1518 srcs: ["a.java"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09001519 certificate: ":new_certificate",
1520 sdk_version: "current",
Jaewoong Jung2ad817c2019-01-18 14:27:16 -08001521 }
1522
1523 android_app_certificate {
1524 name: "new_certificate",
Colin Cross3c007702020-05-08 11:20:24 -07001525 certificate: "cert/new_cert",
Jaewoong Jung2ad817c2019-01-18 14:27:16 -08001526 }
1527 `,
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -04001528 certificateOverride: "",
1529 expectedCertSigningFlags: "",
1530 expectedCertificate: "cert/new_cert.x509.pem cert/new_cert.pk8",
Jaewoong Jung2ad817c2019-01-18 14:27:16 -08001531 },
1532 {
1533 name: "path certificate property",
1534 bp: `
1535 android_app {
1536 name: "foo",
1537 srcs: ["a.java"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09001538 certificate: "expiredkey",
1539 sdk_version: "current",
Jaewoong Jung2ad817c2019-01-18 14:27:16 -08001540 }
1541 `,
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -04001542 certificateOverride: "",
1543 expectedCertSigningFlags: "",
1544 expectedCertificate: "build/make/target/product/security/expiredkey.x509.pem build/make/target/product/security/expiredkey.pk8",
Jaewoong Jung2ad817c2019-01-18 14:27:16 -08001545 },
1546 {
1547 name: "certificate overrides",
1548 bp: `
1549 android_app {
1550 name: "foo",
1551 srcs: ["a.java"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09001552 certificate: "expiredkey",
1553 sdk_version: "current",
Jaewoong Jung2ad817c2019-01-18 14:27:16 -08001554 }
1555
1556 android_app_certificate {
1557 name: "new_certificate",
Colin Cross3c007702020-05-08 11:20:24 -07001558 certificate: "cert/new_cert",
Jaewoong Jung2ad817c2019-01-18 14:27:16 -08001559 }
1560 `,
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -04001561 certificateOverride: "foo:new_certificate",
1562 expectedCertSigningFlags: "",
1563 expectedCertificate: "cert/new_cert.x509.pem cert/new_cert.pk8",
Liz Kammere2b27f42020-05-07 13:24:05 -07001564 },
1565 {
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -04001566 name: "certificate signing flags",
Liz Kammere2b27f42020-05-07 13:24:05 -07001567 bp: `
1568 android_app {
1569 name: "foo",
1570 srcs: ["a.java"],
1571 certificate: ":new_certificate",
1572 lineage: "lineage.bin",
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -04001573 rotationMinSdkVersion: "32",
Liz Kammere2b27f42020-05-07 13:24:05 -07001574 sdk_version: "current",
1575 }
1576
1577 android_app_certificate {
1578 name: "new_certificate",
1579 certificate: "cert/new_cert",
1580 }
1581 `,
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -04001582 certificateOverride: "",
1583 expectedCertSigningFlags: "--lineage lineage.bin --rotation-min-sdk-version 32",
1584 expectedCertificate: "cert/new_cert.x509.pem cert/new_cert.pk8",
Jaewoong Jung2ad817c2019-01-18 14:27:16 -08001585 },
Jaewoong Jung1c1b6e62021-03-09 15:02:31 -08001586 {
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -04001587 name: "cert signing flags from filegroup",
Jaewoong Jung1c1b6e62021-03-09 15:02:31 -08001588 bp: `
1589 android_app {
1590 name: "foo",
1591 srcs: ["a.java"],
1592 certificate: ":new_certificate",
1593 lineage: ":lineage_bin",
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -04001594 rotationMinSdkVersion: "32",
Jaewoong Jung1c1b6e62021-03-09 15:02:31 -08001595 sdk_version: "current",
1596 }
1597
1598 android_app_certificate {
1599 name: "new_certificate",
1600 certificate: "cert/new_cert",
1601 }
1602
1603 filegroup {
1604 name: "lineage_bin",
1605 srcs: ["lineage.bin"],
1606 }
1607 `,
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -04001608 certificateOverride: "",
1609 expectedCertSigningFlags: "--lineage lineage.bin --rotation-min-sdk-version 32",
1610 expectedCertificate: "cert/new_cert.x509.pem cert/new_cert.pk8",
Jaewoong Jung1c1b6e62021-03-09 15:02:31 -08001611 },
Jaewoong Jung2ad817c2019-01-18 14:27:16 -08001612 }
1613
1614 for _, test := range testCases {
1615 t.Run(test.name, func(t *testing.T) {
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00001616 result := android.GroupFixturePreparers(
1617 PrepareForTestWithJavaDefaultModules,
1618 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1619 if test.certificateOverride != "" {
1620 variables.CertificateOverrides = []string{test.certificateOverride}
1621 }
1622 }),
1623 ).RunTestWithBp(t, test.bp)
Jaewoong Jung2ad817c2019-01-18 14:27:16 -08001624
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00001625 foo := result.ModuleForTests("foo", "android_common")
Jaewoong Jung2ad817c2019-01-18 14:27:16 -08001626
1627 signapk := foo.Output("foo.apk")
Liz Kammere2b27f42020-05-07 13:24:05 -07001628 signCertificateFlags := signapk.Args["certificates"]
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00001629 android.AssertStringEquals(t, "certificates flags", test.expectedCertificate, signCertificateFlags)
Liz Kammere2b27f42020-05-07 13:24:05 -07001630
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -04001631 certSigningFlags := signapk.Args["flags"]
1632 android.AssertStringEquals(t, "cert signing flags", test.expectedCertSigningFlags, certSigningFlags)
Jaewoong Jung2ad817c2019-01-18 14:27:16 -08001633 })
1634 }
1635}
Jaewoong Jung9d22a912019-01-23 16:27:47 -08001636
Songchun Fan688de9a2020-03-24 20:32:24 -07001637func TestRequestV4SigningFlag(t *testing.T) {
1638 testCases := []struct {
1639 name string
1640 bp string
1641 expected string
1642 }{
1643 {
1644 name: "default",
1645 bp: `
1646 android_app {
1647 name: "foo",
1648 srcs: ["a.java"],
1649 sdk_version: "current",
1650 }
1651 `,
1652 expected: "",
1653 },
1654 {
1655 name: "default",
1656 bp: `
1657 android_app {
1658 name: "foo",
1659 srcs: ["a.java"],
1660 sdk_version: "current",
1661 v4_signature: false,
1662 }
1663 `,
1664 expected: "",
1665 },
1666 {
1667 name: "module certificate property",
1668 bp: `
1669 android_app {
1670 name: "foo",
1671 srcs: ["a.java"],
1672 sdk_version: "current",
1673 v4_signature: true,
1674 }
1675 `,
1676 expected: "--enable-v4",
1677 },
1678 }
1679
1680 for _, test := range testCases {
1681 t.Run(test.name, func(t *testing.T) {
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00001682 result := android.GroupFixturePreparers(
1683 PrepareForTestWithJavaDefaultModules,
1684 ).RunTestWithBp(t, test.bp)
Songchun Fan688de9a2020-03-24 20:32:24 -07001685
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00001686 foo := result.ModuleForTests("foo", "android_common")
Songchun Fan688de9a2020-03-24 20:32:24 -07001687
1688 signapk := foo.Output("foo.apk")
1689 signFlags := signapk.Args["flags"]
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00001690 android.AssertStringEquals(t, "signing flags", test.expected, signFlags)
Songchun Fan688de9a2020-03-24 20:32:24 -07001691 })
1692 }
1693}
1694
Jaewoong Jung9d22a912019-01-23 16:27:47 -08001695func TestPackageNameOverride(t *testing.T) {
1696 testCases := []struct {
1697 name string
1698 bp string
1699 packageNameOverride string
1700 expected []string
1701 }{
1702 {
1703 name: "default",
1704 bp: `
1705 android_app {
1706 name: "foo",
1707 srcs: ["a.java"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09001708 sdk_version: "current",
Jaewoong Jung9d22a912019-01-23 16:27:47 -08001709 }
1710 `,
1711 packageNameOverride: "",
1712 expected: []string{
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00001713 "out/soong/.intermediates/foo/android_common/foo.apk",
1714 "out/soong/target/product/test_device/system/app/foo/foo.apk",
Jaewoong Jung9d22a912019-01-23 16:27:47 -08001715 },
1716 },
1717 {
Jooyung Han29e2f6d2022-01-08 12:13:59 +09001718 name: "overridden via PRODUCT_PACKAGE_NAME_OVERRIDES",
Jaewoong Jung9d22a912019-01-23 16:27:47 -08001719 bp: `
1720 android_app {
1721 name: "foo",
1722 srcs: ["a.java"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09001723 sdk_version: "current",
Jaewoong Jung9d22a912019-01-23 16:27:47 -08001724 }
1725 `,
1726 packageNameOverride: "foo:bar",
1727 expected: []string{
1728 // The package apk should be still be the original name for test dependencies.
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00001729 "out/soong/.intermediates/foo/android_common/bar.apk",
1730 "out/soong/target/product/test_device/system/app/bar/bar.apk",
Jaewoong Jung9d22a912019-01-23 16:27:47 -08001731 },
1732 },
Jooyung Han29e2f6d2022-01-08 12:13:59 +09001733 {
1734 name: "overridden via stem",
1735 bp: `
1736 android_app {
1737 name: "foo",
1738 srcs: ["a.java"],
1739 sdk_version: "current",
1740 stem: "bar",
1741 }
1742 `,
1743 packageNameOverride: "",
1744 expected: []string{
1745 "out/soong/.intermediates/foo/android_common/bar.apk",
1746 "out/soong/target/product/test_device/system/app/bar/bar.apk",
1747 },
1748 },
Jaewoong Jung9d22a912019-01-23 16:27:47 -08001749 }
1750
1751 for _, test := range testCases {
1752 t.Run(test.name, func(t *testing.T) {
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00001753 result := android.GroupFixturePreparers(
1754 PrepareForTestWithJavaDefaultModules,
1755 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1756 if test.packageNameOverride != "" {
1757 variables.PackageNameOverrides = []string{test.packageNameOverride}
1758 }
1759 }),
1760 ).RunTestWithBp(t, test.bp)
Jaewoong Jung9d22a912019-01-23 16:27:47 -08001761
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00001762 foo := result.ModuleForTests("foo", "android_common")
1763
Lukacs T. Berkifbaf7252021-08-19 09:36:42 +02001764 outSoongDir := result.Config.SoongOutDir()
Jaewoong Jung9d22a912019-01-23 16:27:47 -08001765
1766 outputs := foo.AllOutputs()
1767 outputMap := make(map[string]bool)
1768 for _, o := range outputs {
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00001769 outputMap[android.StringPathRelativeToTop(outSoongDir, o)] = true
Jaewoong Jung9d22a912019-01-23 16:27:47 -08001770 }
1771 for _, e := range test.expected {
1772 if _, exist := outputMap[e]; !exist {
1773 t.Errorf("Can't find %q in output files.\nAll outputs:%v", e, outputs)
1774 }
1775 }
1776 })
1777 }
1778}
Jaewoong Jung4102e5d2019-02-27 16:26:28 -08001779
1780func TestInstrumentationTargetOverridden(t *testing.T) {
1781 bp := `
1782 android_app {
1783 name: "foo",
1784 srcs: ["a.java"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09001785 sdk_version: "current",
Jaewoong Jung4102e5d2019-02-27 16:26:28 -08001786 }
1787
1788 android_test {
1789 name: "bar",
1790 instrumentation_for: "foo",
Jeongik Cha538c0d02019-07-11 15:54:27 +09001791 sdk_version: "current",
Jaewoong Jung4102e5d2019-02-27 16:26:28 -08001792 }
1793 `
Jaewoong Jung4102e5d2019-02-27 16:26:28 -08001794
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00001795 result := android.GroupFixturePreparers(
1796 PrepareForTestWithJavaDefaultModules,
1797 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1798 variables.ManifestPackageNameOverrides = []string{"foo:org.dandroid.bp"}
1799 }),
1800 ).RunTestWithBp(t, bp)
Jaewoong Jung4102e5d2019-02-27 16:26:28 -08001801
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00001802 bar := result.ModuleForTests("bar", "android_common")
Jaewoong Jung4102e5d2019-02-27 16:26:28 -08001803 res := bar.Output("package-res.apk")
1804 aapt2Flags := res.Args["flags"]
1805 e := "--rename-instrumentation-target-package org.dandroid.bp"
1806 if !strings.Contains(aapt2Flags, e) {
1807 t.Errorf("target package renaming flag, %q is missing in aapt2 link flags, %q", e, aapt2Flags)
1808 }
1809}
Jaewoong Jung525443a2019-02-28 15:35:54 -08001810
1811func TestOverrideAndroidApp(t *testing.T) {
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00001812 result := PrepareForTestWithJavaDefaultModules.RunTestWithBp(
1813 t, `
Jaewoong Jung525443a2019-02-28 15:35:54 -08001814 android_app {
1815 name: "foo",
1816 srcs: ["a.java"],
Jaewoong Junga641ee92019-03-27 11:17:14 -07001817 certificate: "expiredkey",
Jaewoong Jungb639a6a2019-05-10 15:16:29 -07001818 overrides: ["qux"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09001819 sdk_version: "current",
Jaewoong Jung525443a2019-02-28 15:35:54 -08001820 }
1821
1822 override_android_app {
1823 name: "bar",
1824 base: "foo",
1825 certificate: ":new_certificate",
Liz Kammere2b27f42020-05-07 13:24:05 -07001826 lineage: "lineage.bin",
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -04001827 rotationMinSdkVersion: "32",
Baligh Uddin5b16dfb2020-02-11 17:27:19 -08001828 logging_parent: "bah",
Jaewoong Jung525443a2019-02-28 15:35:54 -08001829 }
1830
1831 android_app_certificate {
1832 name: "new_certificate",
1833 certificate: "cert/new_cert",
1834 }
Jaewoong Jung6f373f62019-03-13 10:13:24 -07001835
1836 override_android_app {
1837 name: "baz",
1838 base: "foo",
1839 package_name: "org.dandroid.bp",
1840 }
Liz Kammer9f9fd022020-06-18 19:44:06 +00001841
1842 override_android_app {
1843 name: "baz_no_rename_resources",
1844 base: "foo",
1845 package_name: "org.dandroid.bp",
1846 rename_resources_package: false,
1847 }
1848
1849 android_app {
1850 name: "foo_no_rename_resources",
1851 srcs: ["a.java"],
1852 certificate: "expiredkey",
1853 overrides: ["qux"],
1854 rename_resources_package: false,
1855 sdk_version: "current",
1856 }
1857
1858 override_android_app {
1859 name: "baz_base_no_rename_resources",
1860 base: "foo_no_rename_resources",
1861 package_name: "org.dandroid.bp",
1862 }
1863
1864 override_android_app {
1865 name: "baz_override_base_rename_resources",
1866 base: "foo_no_rename_resources",
1867 package_name: "org.dandroid.bp",
1868 rename_resources_package: true,
1869 }
Jaewoong Jung525443a2019-02-28 15:35:54 -08001870 `)
1871
1872 expectedVariants := []struct {
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -04001873 name string
1874 moduleName string
1875 variantName string
1876 apkName string
1877 apkPath string
1878 certFlag string
1879 certSigningFlags string
1880 overrides []string
1881 packageFlag string
1882 renameResources bool
1883 logging_parent string
Jaewoong Jung525443a2019-02-28 15:35:54 -08001884 }{
1885 {
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -04001886 name: "foo",
1887 moduleName: "foo",
1888 variantName: "android_common",
1889 apkPath: "out/soong/target/product/test_device/system/app/foo/foo.apk",
1890 certFlag: "build/make/target/product/security/expiredkey.x509.pem build/make/target/product/security/expiredkey.pk8",
1891 certSigningFlags: "",
1892 overrides: []string{"qux"},
1893 packageFlag: "",
1894 renameResources: false,
1895 logging_parent: "",
Jaewoong Jung525443a2019-02-28 15:35:54 -08001896 },
1897 {
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -04001898 name: "foo",
1899 moduleName: "bar",
1900 variantName: "android_common_bar",
1901 apkPath: "out/soong/target/product/test_device/system/app/bar/bar.apk",
1902 certFlag: "cert/new_cert.x509.pem cert/new_cert.pk8",
1903 certSigningFlags: "--lineage lineage.bin --rotation-min-sdk-version 32",
1904 overrides: []string{"qux", "foo"},
1905 packageFlag: "",
1906 renameResources: false,
1907 logging_parent: "bah",
Jaewoong Jung6f373f62019-03-13 10:13:24 -07001908 },
1909 {
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -04001910 name: "foo",
1911 moduleName: "baz",
1912 variantName: "android_common_baz",
1913 apkPath: "out/soong/target/product/test_device/system/app/baz/baz.apk",
1914 certFlag: "build/make/target/product/security/expiredkey.x509.pem build/make/target/product/security/expiredkey.pk8",
1915 certSigningFlags: "",
1916 overrides: []string{"qux", "foo"},
1917 packageFlag: "org.dandroid.bp",
1918 renameResources: true,
1919 logging_parent: "",
Liz Kammer9f9fd022020-06-18 19:44:06 +00001920 },
1921 {
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -04001922 name: "foo",
1923 moduleName: "baz_no_rename_resources",
1924 variantName: "android_common_baz_no_rename_resources",
1925 apkPath: "out/soong/target/product/test_device/system/app/baz_no_rename_resources/baz_no_rename_resources.apk",
1926 certFlag: "build/make/target/product/security/expiredkey.x509.pem build/make/target/product/security/expiredkey.pk8",
1927 certSigningFlags: "",
1928 overrides: []string{"qux", "foo"},
1929 packageFlag: "org.dandroid.bp",
1930 renameResources: false,
1931 logging_parent: "",
Liz Kammer9f9fd022020-06-18 19:44:06 +00001932 },
1933 {
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -04001934 name: "foo_no_rename_resources",
1935 moduleName: "baz_base_no_rename_resources",
1936 variantName: "android_common_baz_base_no_rename_resources",
1937 apkPath: "out/soong/target/product/test_device/system/app/baz_base_no_rename_resources/baz_base_no_rename_resources.apk",
1938 certFlag: "build/make/target/product/security/expiredkey.x509.pem build/make/target/product/security/expiredkey.pk8",
1939 certSigningFlags: "",
1940 overrides: []string{"qux", "foo_no_rename_resources"},
1941 packageFlag: "org.dandroid.bp",
1942 renameResources: false,
1943 logging_parent: "",
Liz Kammer9f9fd022020-06-18 19:44:06 +00001944 },
1945 {
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -04001946 name: "foo_no_rename_resources",
1947 moduleName: "baz_override_base_rename_resources",
1948 variantName: "android_common_baz_override_base_rename_resources",
1949 apkPath: "out/soong/target/product/test_device/system/app/baz_override_base_rename_resources/baz_override_base_rename_resources.apk",
1950 certFlag: "build/make/target/product/security/expiredkey.x509.pem build/make/target/product/security/expiredkey.pk8",
1951 certSigningFlags: "",
1952 overrides: []string{"qux", "foo_no_rename_resources"},
1953 packageFlag: "org.dandroid.bp",
1954 renameResources: true,
1955 logging_parent: "",
Jaewoong Jung525443a2019-02-28 15:35:54 -08001956 },
1957 }
1958 for _, expected := range expectedVariants {
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00001959 variant := result.ModuleForTests(expected.name, expected.variantName)
Jaewoong Jung525443a2019-02-28 15:35:54 -08001960
1961 // Check the final apk name
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00001962 variant.Output(expected.apkPath)
Jaewoong Jung525443a2019-02-28 15:35:54 -08001963
1964 // Check the certificate paths
Jaewoong Jung5a498812019-11-07 14:14:38 -08001965 signapk := variant.Output(expected.moduleName + ".apk")
Liz Kammere2b27f42020-05-07 13:24:05 -07001966 certFlag := signapk.Args["certificates"]
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00001967 android.AssertStringEquals(t, "certificates flags", expected.certFlag, certFlag)
Liz Kammere2b27f42020-05-07 13:24:05 -07001968
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -04001969 // Check the cert signing flags
1970 certSigningFlags := signapk.Args["flags"]
1971 android.AssertStringEquals(t, "cert signing flags", expected.certSigningFlags, certSigningFlags)
Jaewoong Jung525443a2019-02-28 15:35:54 -08001972
Jaewoong Jung6f373f62019-03-13 10:13:24 -07001973 // Check if the overrides field values are correctly aggregated.
Jaewoong Jung525443a2019-02-28 15:35:54 -08001974 mod := variant.Module().(*AndroidApp)
zhidoua2ce78f2022-02-17 02:33:12 +00001975 android.AssertDeepEquals(t, "overrides property", expected.overrides, mod.overridableAppProperties.Overrides)
Jaewoong Jung6f373f62019-03-13 10:13:24 -07001976
Baligh Uddin5b16dfb2020-02-11 17:27:19 -08001977 // Test Overridable property: Logging_parent
1978 logging_parent := mod.aapt.LoggingParent
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00001979 android.AssertStringEquals(t, "overrides property value for logging parent", expected.logging_parent, logging_parent)
Baligh Uddin5b16dfb2020-02-11 17:27:19 -08001980
Liz Kammer1d5983b2020-05-19 19:15:37 +00001981 // Check the package renaming flag, if exists.
Jaewoong Jung6f373f62019-03-13 10:13:24 -07001982 res := variant.Output("package-res.apk")
1983 aapt2Flags := res.Args["flags"]
Liz Kammer9f9fd022020-06-18 19:44:06 +00001984 checkAapt2LinkFlag(t, aapt2Flags, "rename-manifest-package", expected.packageFlag)
1985 expectedPackage := expected.packageFlag
1986 if !expected.renameResources {
1987 expectedPackage = ""
Liz Kammer1d5983b2020-05-19 19:15:37 +00001988 }
Liz Kammer9f9fd022020-06-18 19:44:06 +00001989 checkAapt2LinkFlag(t, aapt2Flags, "rename-resources-package", expectedPackage)
Jaewoong Jung525443a2019-02-28 15:35:54 -08001990 }
1991}
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001992
zhidoua2ce78f2022-02-17 02:33:12 +00001993func TestOverrideAndroidAppOverrides(t *testing.T) {
1994 ctx, _ := testJava(
1995 t, `
1996 android_app {
1997 name: "foo",
1998 srcs: ["a.java"],
1999 sdk_version: "current",
2000 overrides: ["qux"]
2001 }
2002
2003 android_app {
2004 name: "bar",
2005 srcs: ["b.java"],
2006 sdk_version: "current",
2007 overrides: ["foo"]
2008 }
2009
2010 override_android_app {
2011 name: "foo_override",
2012 base: "foo",
2013 overrides: ["bar"]
2014 }
2015 `)
2016
2017 expectedVariants := []struct {
2018 name string
2019 moduleName string
2020 variantName string
2021 overrides []string
2022 }{
2023 {
2024 name: "foo",
2025 moduleName: "foo",
2026 variantName: "android_common",
2027 overrides: []string{"qux"},
2028 },
2029 {
2030 name: "bar",
2031 moduleName: "bar",
2032 variantName: "android_common",
2033 overrides: []string{"foo"},
2034 },
2035 {
2036 name: "foo",
2037 moduleName: "foo_override",
2038 variantName: "android_common_foo_override",
2039 overrides: []string{"bar", "foo"},
2040 },
2041 }
2042 for _, expected := range expectedVariants {
2043 variant := ctx.ModuleForTests(expected.name, expected.variantName)
2044
2045 // Check if the overrides field values are correctly aggregated.
2046 mod := variant.Module().(*AndroidApp)
2047 android.AssertDeepEquals(t, "overrides property", expected.overrides, mod.overridableAppProperties.Overrides)
2048 }
2049}
2050
Colin Cross50885442022-05-16 16:19:54 -07002051func TestOverrideAndroidAppWithPrebuilt(t *testing.T) {
2052 result := PrepareForTestWithJavaDefaultModules.RunTestWithBp(
2053 t, `
2054 android_app {
2055 name: "foo",
2056 srcs: ["a.java"],
2057 sdk_version: "current",
2058 }
2059
2060 override_android_app {
2061 name: "bar",
2062 base: "foo",
2063 }
2064
2065 android_app_import {
2066 name: "bar",
2067 prefer: true,
2068 apk: "bar.apk",
2069 presigned: true,
2070 }
2071 `)
2072
2073 // An app that has an override that also has a prebuilt should not be hidden.
2074 foo := result.ModuleForTests("foo", "android_common")
2075 if foo.Module().IsHideFromMake() {
2076 t.Errorf("expected foo to have HideFromMake false")
2077 }
2078
2079 // An override that also has a prebuilt should be hidden.
2080 barOverride := result.ModuleForTests("foo", "android_common_bar")
2081 if !barOverride.Module().IsHideFromMake() {
2082 t.Errorf("expected bar override variant of foo to have HideFromMake true")
2083 }
2084}
2085
Jooyung Han01d80d82022-01-08 12:16:32 +09002086func TestOverrideAndroidAppStem(t *testing.T) {
2087 ctx, _ := testJava(t, `
2088 android_app {
2089 name: "foo",
2090 srcs: ["a.java"],
2091 sdk_version: "current",
2092 }
2093 override_android_app {
2094 name: "bar",
2095 base: "foo",
2096 }
2097 override_android_app {
2098 name: "baz",
2099 base: "foo",
2100 stem: "baz_stem",
2101 }
2102 android_app {
2103 name: "foo2",
2104 srcs: ["a.java"],
2105 sdk_version: "current",
2106 stem: "foo2_stem",
2107 }
2108 override_android_app {
2109 name: "bar2",
2110 base: "foo2",
2111 }
2112 override_android_app {
2113 name: "baz2",
2114 base: "foo2",
2115 stem: "baz2_stem",
2116 }
2117 `)
2118 for _, expected := range []struct {
2119 moduleName string
2120 variantName string
2121 apkPath string
2122 }{
2123 {
2124 moduleName: "foo",
2125 variantName: "android_common",
2126 apkPath: "out/soong/target/product/test_device/system/app/foo/foo.apk",
2127 },
2128 {
2129 moduleName: "foo",
2130 variantName: "android_common_bar",
2131 apkPath: "out/soong/target/product/test_device/system/app/bar/bar.apk",
2132 },
2133 {
2134 moduleName: "foo",
2135 variantName: "android_common_baz",
2136 apkPath: "out/soong/target/product/test_device/system/app/baz_stem/baz_stem.apk",
2137 },
2138 {
2139 moduleName: "foo2",
2140 variantName: "android_common",
2141 apkPath: "out/soong/target/product/test_device/system/app/foo2_stem/foo2_stem.apk",
2142 },
2143 {
2144 moduleName: "foo2",
2145 variantName: "android_common_bar2",
2146 // Note that this may cause the duplicate output error.
2147 apkPath: "out/soong/target/product/test_device/system/app/foo2_stem/foo2_stem.apk",
2148 },
2149 {
2150 moduleName: "foo2",
2151 variantName: "android_common_baz2",
2152 apkPath: "out/soong/target/product/test_device/system/app/baz2_stem/baz2_stem.apk",
2153 },
2154 } {
2155 variant := ctx.ModuleForTests(expected.moduleName, expected.variantName)
2156 variant.Output(expected.apkPath)
2157 }
2158}
2159
Jaewoong Jungb639a6a2019-05-10 15:16:29 -07002160func TestOverrideAndroidAppDependency(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07002161 ctx, _ := testJava(t, `
Jaewoong Jungb639a6a2019-05-10 15:16:29 -07002162 android_app {
2163 name: "foo",
2164 srcs: ["a.java"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09002165 sdk_version: "current",
Jaewoong Jungb639a6a2019-05-10 15:16:29 -07002166 }
2167
2168 override_android_app {
2169 name: "bar",
2170 base: "foo",
2171 package_name: "org.dandroid.bp",
2172 }
2173
2174 android_test {
2175 name: "baz",
2176 srcs: ["b.java"],
2177 instrumentation_for: "foo",
2178 }
2179
2180 android_test {
2181 name: "qux",
2182 srcs: ["b.java"],
2183 instrumentation_for: "bar",
2184 }
2185 `)
2186
2187 // Verify baz, which depends on the overridden module foo, has the correct classpath javac arg.
Paul Duffina71a67a2021-03-29 00:42:57 +01002188 javac := ctx.ModuleForTests("baz", "android_common").Rule("javac")
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00002189 fooTurbine := "out/soong/.intermediates/foo/android_common/turbine-combined/foo.jar"
Jaewoong Jungb639a6a2019-05-10 15:16:29 -07002190 if !strings.Contains(javac.Args["classpath"], fooTurbine) {
2191 t.Errorf("baz classpath %v does not contain %q", javac.Args["classpath"], fooTurbine)
2192 }
2193
2194 // Verify qux, which depends on the overriding module bar, has the correct classpath javac arg.
Paul Duffina71a67a2021-03-29 00:42:57 +01002195 javac = ctx.ModuleForTests("qux", "android_common").Rule("javac")
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00002196 barTurbine := "out/soong/.intermediates/foo/android_common_bar/turbine-combined/foo.jar"
Jaewoong Jungb639a6a2019-05-10 15:16:29 -07002197 if !strings.Contains(javac.Args["classpath"], barTurbine) {
2198 t.Errorf("qux classpath %v does not contain %q", javac.Args["classpath"], barTurbine)
2199 }
2200}
2201
Jaewoong Jung26dedd32019-06-06 08:45:58 -07002202func TestOverrideAndroidTest(t *testing.T) {
2203 ctx, _ := testJava(t, `
2204 android_app {
2205 name: "foo",
2206 srcs: ["a.java"],
2207 package_name: "com.android.foo",
2208 sdk_version: "current",
2209 }
2210
2211 override_android_app {
2212 name: "bar",
2213 base: "foo",
2214 package_name: "com.android.bar",
2215 }
2216
2217 android_test {
2218 name: "foo_test",
2219 srcs: ["b.java"],
2220 instrumentation_for: "foo",
2221 }
2222
2223 override_android_test {
2224 name: "bar_test",
2225 base: "foo_test",
2226 package_name: "com.android.bar.test",
2227 instrumentation_for: "bar",
2228 instrumentation_target_package: "com.android.bar",
2229 }
2230 `)
2231
2232 expectedVariants := []struct {
2233 moduleName string
2234 variantName string
2235 apkPath string
2236 overrides []string
2237 targetVariant string
2238 packageFlag string
2239 targetPackageFlag string
2240 }{
2241 {
2242 variantName: "android_common",
Jaewoong Jung326a9412019-11-21 10:41:00 -08002243 apkPath: "/target/product/test_device/testcases/foo_test/arm64/foo_test.apk",
Jaewoong Jung26dedd32019-06-06 08:45:58 -07002244 overrides: nil,
2245 targetVariant: "android_common",
2246 packageFlag: "",
2247 targetPackageFlag: "",
2248 },
2249 {
2250 variantName: "android_common_bar_test",
Jaewoong Jung326a9412019-11-21 10:41:00 -08002251 apkPath: "/target/product/test_device/testcases/bar_test/arm64/bar_test.apk",
Jaewoong Jung26dedd32019-06-06 08:45:58 -07002252 overrides: []string{"foo_test"},
2253 targetVariant: "android_common_bar",
2254 packageFlag: "com.android.bar.test",
2255 targetPackageFlag: "com.android.bar",
2256 },
2257 }
2258 for _, expected := range expectedVariants {
2259 variant := ctx.ModuleForTests("foo_test", expected.variantName)
2260
2261 // Check the final apk name
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00002262 variant.Output("out/soong" + expected.apkPath)
Jaewoong Jung26dedd32019-06-06 08:45:58 -07002263
2264 // Check if the overrides field values are correctly aggregated.
2265 mod := variant.Module().(*AndroidTest)
zhidoua2ce78f2022-02-17 02:33:12 +00002266 if !reflect.DeepEqual(expected.overrides, mod.overridableAppProperties.Overrides) {
Jaewoong Jung26dedd32019-06-06 08:45:58 -07002267 t.Errorf("Incorrect overrides property value, expected: %q, got: %q",
zhidoua2ce78f2022-02-17 02:33:12 +00002268 expected.overrides, mod.overridableAppProperties.Overrides)
Jaewoong Jung26dedd32019-06-06 08:45:58 -07002269 }
2270
2271 // Check if javac classpath has the correct jar file path. This checks instrumentation_for overrides.
Paul Duffina71a67a2021-03-29 00:42:57 +01002272 javac := variant.Rule("javac")
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00002273 turbine := filepath.Join("out", "soong", ".intermediates", "foo", expected.targetVariant, "turbine-combined", "foo.jar")
Jaewoong Jung26dedd32019-06-06 08:45:58 -07002274 if !strings.Contains(javac.Args["classpath"], turbine) {
2275 t.Errorf("classpath %q does not contain %q", javac.Args["classpath"], turbine)
2276 }
2277
2278 // Check aapt2 flags.
2279 res := variant.Output("package-res.apk")
2280 aapt2Flags := res.Args["flags"]
2281 checkAapt2LinkFlag(t, aapt2Flags, "rename-manifest-package", expected.packageFlag)
Liz Kammer9f9fd022020-06-18 19:44:06 +00002282 checkAapt2LinkFlag(t, aapt2Flags, "rename-resources-package", expected.packageFlag)
Jaewoong Jung26dedd32019-06-06 08:45:58 -07002283 checkAapt2LinkFlag(t, aapt2Flags, "rename-instrumentation-target-package", expected.targetPackageFlag)
2284 }
2285}
2286
Jaewoong Jung39982342020-01-14 10:27:18 -08002287func TestAndroidTest_FixTestConfig(t *testing.T) {
2288 ctx, _ := testJava(t, `
2289 android_app {
2290 name: "foo",
2291 srcs: ["a.java"],
2292 package_name: "com.android.foo",
2293 sdk_version: "current",
2294 }
2295
2296 android_test {
2297 name: "foo_test",
2298 srcs: ["b.java"],
2299 instrumentation_for: "foo",
2300 }
2301
2302 android_test {
2303 name: "bar_test",
2304 srcs: ["b.java"],
2305 package_name: "com.android.bar.test",
2306 instrumentation_for: "foo",
2307 }
2308
2309 override_android_test {
2310 name: "baz_test",
2311 base: "foo_test",
2312 package_name: "com.android.baz.test",
2313 }
2314 `)
2315
2316 testCases := []struct {
2317 moduleName string
2318 variantName string
2319 expectedFlags []string
2320 }{
2321 {
2322 moduleName: "foo_test",
2323 variantName: "android_common",
2324 },
2325 {
2326 moduleName: "bar_test",
2327 variantName: "android_common",
2328 expectedFlags: []string{
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00002329 "--manifest out/soong/.intermediates/bar_test/android_common/manifest_fixer/AndroidManifest.xml",
Jaewoong Jung39982342020-01-14 10:27:18 -08002330 "--package-name com.android.bar.test",
2331 },
2332 },
2333 {
2334 moduleName: "foo_test",
2335 variantName: "android_common_baz_test",
2336 expectedFlags: []string{
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00002337 "--manifest out/soong/.intermediates/foo_test/android_common_baz_test/manifest_fixer/AndroidManifest.xml",
Jaewoong Jung39982342020-01-14 10:27:18 -08002338 "--package-name com.android.baz.test",
2339 "--test-file-name baz_test.apk",
2340 },
2341 },
2342 }
2343
2344 for _, test := range testCases {
2345 variant := ctx.ModuleForTests(test.moduleName, test.variantName)
Paul Duffina71a67a2021-03-29 00:42:57 +01002346 params := variant.MaybeOutput("test_config_fixer/AndroidTest.xml")
Jaewoong Jung39982342020-01-14 10:27:18 -08002347
2348 if len(test.expectedFlags) > 0 {
2349 if params.Rule == nil {
2350 t.Errorf("test_config_fixer was expected to run, but didn't")
2351 } else {
2352 for _, flag := range test.expectedFlags {
2353 if !strings.Contains(params.RuleParams.Command, flag) {
2354 t.Errorf("Flag %q was not found in command: %q", flag, params.RuleParams.Command)
2355 }
2356 }
2357 }
2358 } else {
2359 if params.Rule != nil {
2360 t.Errorf("test_config_fixer was not expected to run, but did: %q", params.RuleParams.Command)
2361 }
2362 }
Jaewoong Jung39982342020-01-14 10:27:18 -08002363 }
2364}
2365
Paul Duffin53a70a42022-01-11 14:35:55 +00002366func TestInstrumentationTargetPrebuilt(t *testing.T) {
2367 bp := `
2368 android_app_import {
2369 name: "foo",
2370 apk: "foo.apk",
2371 presigned: true,
2372 }
2373
2374 android_test {
2375 name: "bar",
2376 srcs: ["a.java"],
2377 instrumentation_for: "foo",
2378 sdk_version: "current",
2379 }
2380 `
2381
2382 android.GroupFixturePreparers(
2383 PrepareForTestWithJavaDefaultModules,
2384 ).ExtendWithErrorHandler(
2385 android.FixtureExpectsAtLeastOneErrorMatchingPattern(
2386 "instrumentation_for: dependency \"foo\" of type \"android_app_import\" does not provide JavaInfo so is unsuitable for use with this property")).
2387 RunTestWithBp(t, bp)
2388}
2389
Jaewoong Jungbc625cd2019-05-06 15:48:44 -07002390func TestStl(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07002391 ctx, _ := testJava(t, cc.GatherRequiredDepsForTest(android.Android)+`
Jaewoong Jungbc625cd2019-05-06 15:48:44 -07002392 cc_library {
2393 name: "libjni",
Peter Collingbournead84f972019-12-17 16:46:18 -08002394 sdk_version: "current",
2395 stl: "c++_shared",
Jaewoong Jungbc625cd2019-05-06 15:48:44 -07002396 }
2397
2398 android_test {
2399 name: "stl",
2400 jni_libs: ["libjni"],
2401 compile_multilib: "both",
2402 sdk_version: "current",
2403 stl: "c++_shared",
2404 }
2405
2406 android_test {
2407 name: "system",
2408 jni_libs: ["libjni"],
2409 compile_multilib: "both",
2410 sdk_version: "current",
2411 }
2412 `)
2413
2414 testCases := []struct {
2415 name string
2416 jnis []string
2417 }{
2418 {"stl",
2419 []string{
2420 "libjni.so",
Jaewoong Jung710756a2019-06-04 11:53:47 -07002421 "libc++_shared.so",
Jaewoong Jungbc625cd2019-05-06 15:48:44 -07002422 },
2423 },
2424 {"system",
2425 []string{
2426 "libjni.so",
2427 },
2428 },
2429 }
2430
2431 for _, test := range testCases {
2432 t.Run(test.name, func(t *testing.T) {
2433 app := ctx.ModuleForTests(test.name, "android_common")
2434 jniLibZip := app.Output("jnilibs.zip")
2435 var jnis []string
2436 args := strings.Fields(jniLibZip.Args["jarArgs"])
2437 for i := 0; i < len(args); i++ {
2438 if args[i] == "-f" {
2439 jnis = append(jnis, args[i+1])
2440 i += 1
2441 }
2442 }
2443 jnisJoined := strings.Join(jnis, " ")
2444 for _, jni := range test.jnis {
2445 if !strings.Contains(jnisJoined, jni) {
2446 t.Errorf("missing jni %q in %q", jni, jnis)
2447 }
2448 }
2449 })
2450 }
2451}
Colin Cross50ddcc42019-05-16 12:28:22 -07002452
2453func TestUsesLibraries(t *testing.T) {
2454 bp := `
2455 java_sdk_library {
2456 name: "foo",
2457 srcs: ["a.java"],
2458 api_packages: ["foo"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09002459 sdk_version: "current",
Colin Cross50ddcc42019-05-16 12:28:22 -07002460 }
2461
2462 java_sdk_library {
Paul Duffin859fe962020-05-15 10:20:31 +01002463 name: "qux",
2464 srcs: ["a.java"],
2465 api_packages: ["qux"],
2466 sdk_version: "current",
2467 }
2468
2469 java_sdk_library {
2470 name: "quuz",
2471 srcs: ["a.java"],
2472 api_packages: ["quuz"],
2473 sdk_version: "current",
2474 }
2475
2476 java_sdk_library {
Ulya Trafimovich65b03192020-12-03 16:50:22 +00002477 name: "fred",
2478 srcs: ["a.java"],
2479 api_packages: ["fred"],
2480 sdk_version: "current",
2481 }
2482
2483 java_sdk_library {
Colin Cross50ddcc42019-05-16 12:28:22 -07002484 name: "bar",
2485 srcs: ["a.java"],
2486 api_packages: ["bar"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09002487 sdk_version: "current",
Colin Cross50ddcc42019-05-16 12:28:22 -07002488 }
2489
Ulya Trafimovich4b6d4c12020-08-19 14:58:01 +01002490 java_sdk_library {
2491 name: "runtime-library",
2492 srcs: ["a.java"],
2493 sdk_version: "current",
2494 }
2495
2496 java_library {
2497 name: "static-runtime-helper",
2498 srcs: ["a.java"],
2499 libs: ["runtime-library"],
2500 sdk_version: "current",
2501 }
2502
Ulya Trafimoviche4432872021-08-18 16:57:11 +01002503 java_library {
2504 name: "runtime-required-x",
2505 srcs: ["a.java"],
2506 installable: true,
2507 sdk_version: "current",
2508 }
2509
2510 java_library {
2511 name: "runtime-optional-x",
2512 srcs: ["a.java"],
2513 installable: true,
2514 sdk_version: "current",
2515 }
2516
2517 android_library {
2518 name: "static-x",
2519 uses_libs: ["runtime-required-x"],
2520 optional_uses_libs: ["runtime-optional-x"],
2521 sdk_version: "current",
2522 }
2523
2524 java_library {
2525 name: "runtime-required-y",
2526 srcs: ["a.java"],
2527 installable: true,
2528 sdk_version: "current",
2529 }
2530
2531 java_library {
2532 name: "runtime-optional-y",
2533 srcs: ["a.java"],
2534 installable: true,
2535 sdk_version: "current",
2536 }
2537
2538 java_library {
2539 name: "static-y",
2540 srcs: ["a.java"],
2541 uses_libs: ["runtime-required-y"],
2542 optional_uses_libs: ["runtime-optional-y"],
2543 sdk_version: "current",
2544 }
2545
Ulya Trafimovich861a8962021-02-26 14:49:07 +00002546 // A library that has to use "provides_uses_lib", because:
2547 // - it is not an SDK library
2548 // - its library name is different from its module name
2549 java_library {
2550 name: "non-sdk-lib",
2551 provides_uses_lib: "com.non.sdk.lib",
2552 installable: true,
2553 srcs: ["a.java"],
2554 }
2555
Colin Cross50ddcc42019-05-16 12:28:22 -07002556 android_app {
2557 name: "app",
2558 srcs: ["a.java"],
Ulya Trafimovich861a8962021-02-26 14:49:07 +00002559 libs: [
2560 "qux",
2561 "quuz.stubs"
2562 ],
Ulya Trafimovich65b03192020-12-03 16:50:22 +00002563 static_libs: [
2564 "static-runtime-helper",
2565 // statically linked component libraries should not pull their SDK libraries,
2566 // so "fred" should not be added to class loader context
2567 "fred.stubs",
Ulya Trafimoviche4432872021-08-18 16:57:11 +01002568 "static-x",
2569 "static-y",
Ulya Trafimovich65b03192020-12-03 16:50:22 +00002570 ],
Ulya Trafimovich861a8962021-02-26 14:49:07 +00002571 uses_libs: [
2572 "foo",
2573 "non-sdk-lib"
2574 ],
Jeongik Cha538c0d02019-07-11 15:54:27 +09002575 sdk_version: "current",
Colin Cross50ddcc42019-05-16 12:28:22 -07002576 optional_uses_libs: [
2577 "bar",
2578 "baz",
2579 ],
2580 }
2581
2582 android_app_import {
2583 name: "prebuilt",
2584 apk: "prebuilts/apk/app.apk",
2585 certificate: "platform",
Ulya Trafimovich861a8962021-02-26 14:49:07 +00002586 uses_libs: [
2587 "foo",
2588 "non-sdk-lib",
2589 "android.test.runner"
2590 ],
Colin Cross50ddcc42019-05-16 12:28:22 -07002591 optional_uses_libs: [
2592 "bar",
2593 "baz",
2594 ],
2595 }
2596 `
2597
Paul Duffin71ae5942021-03-22 15:36:52 +00002598 result := android.GroupFixturePreparers(
2599 prepareForJavaTest,
Paul Duffin2645a292021-03-13 02:36:00 +00002600 PrepareForTestWithJavaSdkLibraryFiles,
2601 FixtureWithLastReleaseApis("runtime-library", "foo", "quuz", "qux", "bar", "fred"),
Paul Duffind234b412021-03-12 23:04:46 +00002602 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
2603 variables.MissingUsesLibraries = []string{"baz"}
2604 }),
2605 ).RunTestWithBp(t, bp)
Colin Cross50ddcc42019-05-16 12:28:22 -07002606
Paul Duffind234b412021-03-12 23:04:46 +00002607 app := result.ModuleForTests("app", "android_common")
2608 prebuilt := result.ModuleForTests("prebuilt", "android_common")
Colin Cross50ddcc42019-05-16 12:28:22 -07002609
Paul Duffin859fe962020-05-15 10:20:31 +01002610 // Test that implicit dependencies on java_sdk_library instances are passed to the manifest.
Ulya Trafimovich2eaf5c52021-02-26 12:05:11 +00002611 // This should not include explicit `uses_libs`/`optional_uses_libs` entries.
2612 actualManifestFixerArgs := app.Output("manifest_fixer/AndroidManifest.xml").Args["args"]
2613 expectManifestFixerArgs := `--extract-native-libs=true ` +
2614 `--uses-library qux ` +
2615 `--uses-library quuz ` +
Ulya Trafimovich0b1c70e2021-08-20 15:39:12 +01002616 `--uses-library runtime-library`
Gurpreet Singh75d65f32022-01-24 17:44:05 +00002617 android.AssertStringDoesContain(t, "manifest_fixer args", actualManifestFixerArgs, expectManifestFixerArgs)
Paul Duffin859fe962020-05-15 10:20:31 +01002618
Ulya Trafimovich2eaf5c52021-02-26 12:05:11 +00002619 // Test that all libraries are verified (library order matters).
2620 verifyCmd := app.Rule("verify_uses_libraries").RuleParams.Command
2621 verifyArgs := `--uses-library foo ` +
Ulya Trafimovicheea486a2021-02-26 11:38:21 +00002622 `--uses-library com.non.sdk.lib ` +
Ulya Trafimovich2eaf5c52021-02-26 12:05:11 +00002623 `--uses-library qux ` +
2624 `--uses-library quuz ` +
2625 `--uses-library runtime-library ` +
Ulya Trafimoviche4432872021-08-18 16:57:11 +01002626 `--uses-library runtime-required-x ` +
2627 `--uses-library runtime-required-y ` +
Ulya Trafimovich2eaf5c52021-02-26 12:05:11 +00002628 `--optional-uses-library bar ` +
Ulya Trafimoviche4432872021-08-18 16:57:11 +01002629 `--optional-uses-library baz ` +
2630 `--optional-uses-library runtime-optional-x ` +
2631 `--optional-uses-library runtime-optional-y `
Paul Duffind234b412021-03-12 23:04:46 +00002632 android.AssertStringDoesContain(t, "verify cmd args", verifyCmd, verifyArgs)
Colin Cross50ddcc42019-05-16 12:28:22 -07002633
Ulya Trafimovich2eaf5c52021-02-26 12:05:11 +00002634 // Test that all libraries are verified for an APK (library order matters).
2635 verifyApkCmd := prebuilt.Rule("verify_uses_libraries").RuleParams.Command
Ulya Trafimovich0aba2522021-03-03 16:38:37 +00002636 verifyApkArgs := `--uses-library foo ` +
2637 `--uses-library com.non.sdk.lib ` +
2638 `--uses-library android.test.runner ` +
2639 `--optional-uses-library bar ` +
2640 `--optional-uses-library baz `
Paul Duffind234b412021-03-12 23:04:46 +00002641 android.AssertStringDoesContain(t, "verify apk cmd args", verifyApkCmd, verifyApkArgs)
Colin Cross50ddcc42019-05-16 12:28:22 -07002642
Ulya Trafimovich8130c482020-10-07 15:17:13 +01002643 // Test that all present libraries are preopted, including implicit SDK dependencies, possibly stubs
Ulya Trafimovich2eaf5c52021-02-26 12:05:11 +00002644 cmd := app.Rule("dexpreopt").RuleParams.Command
Ulya Trafimovich8130c482020-10-07 15:17:13 +01002645 w := `--target-context-for-sdk any ` +
Ulya Trafimovich8130c482020-10-07 15:17:13 +01002646 `PCL[/system/framework/qux.jar]#` +
Ulya Trafimovich8cbc5d22020-11-03 15:15:46 +00002647 `PCL[/system/framework/quuz.jar]#` +
2648 `PCL[/system/framework/foo.jar]#` +
Ulya Trafimovich861a8962021-02-26 14:49:07 +00002649 `PCL[/system/framework/non-sdk-lib.jar]#` +
Ulya Trafimovich8cbc5d22020-11-03 15:15:46 +00002650 `PCL[/system/framework/bar.jar]#` +
Ulya Trafimoviche4432872021-08-18 16:57:11 +01002651 `PCL[/system/framework/runtime-library.jar]#` +
2652 `PCL[/system/framework/runtime-required-x.jar]#` +
2653 `PCL[/system/framework/runtime-optional-x.jar]#` +
2654 `PCL[/system/framework/runtime-required-y.jar]#` +
2655 `PCL[/system/framework/runtime-optional-y.jar] `
Paul Duffind234b412021-03-12 23:04:46 +00002656 android.AssertStringDoesContain(t, "dexpreopt app cmd args", cmd, w)
Colin Cross50ddcc42019-05-16 12:28:22 -07002657
Ulya Trafimovich24813e12020-10-07 15:05:21 +01002658 // Test conditional context for target SDK version 28.
Paul Duffind234b412021-03-12 23:04:46 +00002659 android.AssertStringDoesContain(t, "dexpreopt app cmd 28", cmd,
2660 `--target-context-for-sdk 28`+
2661 ` PCL[/system/framework/org.apache.http.legacy.jar] `)
Ulya Trafimovich24813e12020-10-07 15:05:21 +01002662
2663 // Test conditional context for target SDK version 29.
Paul Duffind234b412021-03-12 23:04:46 +00002664 android.AssertStringDoesContain(t, "dexpreopt app cmd 29", cmd,
2665 `--target-context-for-sdk 29`+
2666 ` PCL[/system/framework/android.hidl.manager-V1.0-java.jar]`+
2667 `#PCL[/system/framework/android.hidl.base-V1.0-java.jar] `)
Ulya Trafimovich24813e12020-10-07 15:05:21 +01002668
2669 // Test conditional context for target SDK version 30.
Ulya Trafimovich46b3d5b2020-10-21 13:20:55 +01002670 // "android.test.mock" is absent because "android.test.runner" is not used.
Paul Duffind234b412021-03-12 23:04:46 +00002671 android.AssertStringDoesContain(t, "dexpreopt app cmd 30", cmd,
2672 `--target-context-for-sdk 30`+
2673 ` PCL[/system/framework/android.test.base.jar] `)
Ulya Trafimovich24813e12020-10-07 15:05:21 +01002674
Ulya Trafimovichfc24ad32020-08-19 16:32:54 +01002675 cmd = prebuilt.Rule("dexpreopt").RuleParams.Command
Paul Duffind234b412021-03-12 23:04:46 +00002676 android.AssertStringDoesContain(t, "dexpreopt prebuilt cmd", cmd,
2677 `--target-context-for-sdk any`+
2678 ` PCL[/system/framework/foo.jar]`+
2679 `#PCL[/system/framework/non-sdk-lib.jar]`+
2680 `#PCL[/system/framework/android.test.runner.jar]`+
2681 `#PCL[/system/framework/bar.jar] `)
Ulya Trafimovich24813e12020-10-07 15:05:21 +01002682
2683 // Test conditional context for target SDK version 30.
Ulya Trafimovich46b3d5b2020-10-21 13:20:55 +01002684 // "android.test.mock" is present because "android.test.runner" is used.
Paul Duffind234b412021-03-12 23:04:46 +00002685 android.AssertStringDoesContain(t, "dexpreopt prebuilt cmd 30", cmd,
2686 `--target-context-for-sdk 30`+
2687 ` PCL[/system/framework/android.test.base.jar]`+
2688 `#PCL[/system/framework/android.test.mock.jar] `)
Colin Cross50ddcc42019-05-16 12:28:22 -07002689}
Jaewoong Jungc27ab662019-05-30 15:51:14 -07002690
Ulya Trafimovich9023b022021-03-22 16:02:28 +00002691func TestDexpreoptBcp(t *testing.T) {
2692 bp := `
2693 java_sdk_library {
2694 name: "foo",
2695 srcs: ["a.java"],
2696 api_packages: ["foo"],
2697 sdk_version: "current",
2698 }
2699
2700 java_sdk_library {
2701 name: "bar",
2702 srcs: ["a.java"],
2703 api_packages: ["bar"],
2704 permitted_packages: ["bar"],
2705 sdk_version: "current",
2706 }
2707
2708 android_app {
2709 name: "app",
2710 srcs: ["a.java"],
2711 sdk_version: "current",
2712 }
2713 `
2714
2715 testCases := []struct {
2716 name string
2717 with bool
2718 expect string
2719 }{
2720 {
2721 name: "with updatable bcp",
2722 with: true,
2723 expect: "/system/framework/foo.jar:/system/framework/bar.jar",
2724 },
2725 {
2726 name: "without updatable bcp",
2727 with: false,
2728 expect: "/system/framework/foo.jar",
2729 },
2730 }
2731
2732 for _, test := range testCases {
2733 t.Run(test.name, func(t *testing.T) {
2734 result := android.GroupFixturePreparers(
2735 prepareForJavaTest,
2736 PrepareForTestWithJavaSdkLibraryFiles,
2737 FixtureWithLastReleaseApis("runtime-library", "foo", "bar"),
2738 dexpreopt.FixtureSetBootJars("platform:foo"),
satayevd604b212021-07-21 14:23:52 +01002739 dexpreopt.FixtureSetApexBootJars("platform:bar"),
Ulya Trafimovich9023b022021-03-22 16:02:28 +00002740 dexpreopt.FixtureSetPreoptWithUpdatableBcp(test.with),
2741 ).RunTestWithBp(t, bp)
2742
2743 app := result.ModuleForTests("app", "android_common")
2744 cmd := app.Rule("dexpreopt").RuleParams.Command
2745 bcp := " -Xbootclasspath-locations:" + test.expect + " " // space at the end matters
2746 android.AssertStringDoesContain(t, "dexpreopt app bcp", cmd, bcp)
2747 })
2748 }
2749}
2750
Jaewoong Jungc27ab662019-05-30 15:51:14 -07002751func TestCodelessApp(t *testing.T) {
2752 testCases := []struct {
2753 name string
2754 bp string
2755 noCode bool
2756 }{
2757 {
2758 name: "normal",
2759 bp: `
2760 android_app {
2761 name: "foo",
2762 srcs: ["a.java"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09002763 sdk_version: "current",
Jaewoong Jungc27ab662019-05-30 15:51:14 -07002764 }
2765 `,
2766 noCode: false,
2767 },
2768 {
2769 name: "app without sources",
2770 bp: `
2771 android_app {
2772 name: "foo",
Jeongik Cha538c0d02019-07-11 15:54:27 +09002773 sdk_version: "current",
Jaewoong Jungc27ab662019-05-30 15:51:14 -07002774 }
2775 `,
2776 noCode: true,
2777 },
2778 {
2779 name: "app with libraries",
2780 bp: `
2781 android_app {
2782 name: "foo",
2783 static_libs: ["lib"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09002784 sdk_version: "current",
Jaewoong Jungc27ab662019-05-30 15:51:14 -07002785 }
2786
2787 java_library {
2788 name: "lib",
2789 srcs: ["a.java"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09002790 sdk_version: "current",
Jaewoong Jungc27ab662019-05-30 15:51:14 -07002791 }
2792 `,
2793 noCode: false,
2794 },
2795 {
2796 name: "app with sourceless libraries",
2797 bp: `
2798 android_app {
2799 name: "foo",
2800 static_libs: ["lib"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09002801 sdk_version: "current",
Jaewoong Jungc27ab662019-05-30 15:51:14 -07002802 }
2803
2804 java_library {
2805 name: "lib",
Jeongik Cha538c0d02019-07-11 15:54:27 +09002806 sdk_version: "current",
Jaewoong Jungc27ab662019-05-30 15:51:14 -07002807 }
2808 `,
2809 // TODO(jungjw): this should probably be true
2810 noCode: false,
2811 },
2812 }
2813
2814 for _, test := range testCases {
2815 t.Run(test.name, func(t *testing.T) {
2816 ctx := testApp(t, test.bp)
2817
2818 foo := ctx.ModuleForTests("foo", "android_common")
2819 manifestFixerArgs := foo.Output("manifest_fixer/AndroidManifest.xml").Args["args"]
2820 if strings.Contains(manifestFixerArgs, "--has-no-code") != test.noCode {
2821 t.Errorf("unexpected manifest_fixer args: %q", manifestFixerArgs)
2822 }
2823 })
2824 }
2825}
Jaewoong Jung5b425e22019-06-17 17:40:56 -07002826
Colin Cross53a87f52019-06-25 13:35:30 -07002827func TestUncompressDex(t *testing.T) {
2828 testCases := []struct {
2829 name string
2830 bp string
2831
2832 uncompressedPlatform bool
2833 uncompressedUnbundled bool
2834 }{
2835 {
2836 name: "normal",
2837 bp: `
2838 android_app {
2839 name: "foo",
2840 srcs: ["a.java"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09002841 sdk_version: "current",
Colin Cross53a87f52019-06-25 13:35:30 -07002842 }
2843 `,
2844 uncompressedPlatform: true,
2845 uncompressedUnbundled: false,
2846 },
2847 {
2848 name: "use_embedded_dex",
2849 bp: `
2850 android_app {
2851 name: "foo",
2852 use_embedded_dex: true,
2853 srcs: ["a.java"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09002854 sdk_version: "current",
Colin Cross53a87f52019-06-25 13:35:30 -07002855 }
2856 `,
2857 uncompressedPlatform: true,
2858 uncompressedUnbundled: true,
2859 },
2860 {
2861 name: "privileged",
2862 bp: `
2863 android_app {
2864 name: "foo",
2865 privileged: true,
2866 srcs: ["a.java"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09002867 sdk_version: "current",
Colin Cross53a87f52019-06-25 13:35:30 -07002868 }
2869 `,
2870 uncompressedPlatform: true,
2871 uncompressedUnbundled: true,
2872 },
David Srbeckye033cba2020-05-20 22:20:28 +01002873 {
2874 name: "normal_uncompress_dex_true",
2875 bp: `
2876 android_app {
2877 name: "foo",
2878 srcs: ["a.java"],
2879 sdk_version: "current",
2880 uncompress_dex: true,
2881 }
2882 `,
2883 uncompressedPlatform: true,
2884 uncompressedUnbundled: true,
2885 },
2886 {
2887 name: "normal_uncompress_dex_false",
2888 bp: `
2889 android_app {
2890 name: "foo",
2891 srcs: ["a.java"],
2892 sdk_version: "current",
2893 uncompress_dex: false,
2894 }
2895 `,
2896 uncompressedPlatform: false,
2897 uncompressedUnbundled: false,
2898 },
Colin Cross53a87f52019-06-25 13:35:30 -07002899 }
2900
2901 test := func(t *testing.T, bp string, want bool, unbundled bool) {
2902 t.Helper()
2903
Paul Duffin71ae5942021-03-22 15:36:52 +00002904 result := android.GroupFixturePreparers(
2905 prepareForJavaTest,
Paul Duffin2645a292021-03-13 02:36:00 +00002906 PrepareForTestWithPrebuiltsOfCurrentApi,
Paul Duffincdb88a92021-03-14 00:36:50 +00002907 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
2908 if unbundled {
2909 variables.Unbundled_build = proptools.BoolPtr(true)
2910 variables.Always_use_prebuilt_sdks = proptools.BoolPtr(true)
2911 }
2912 }),
2913 ).RunTestWithBp(t, bp)
Colin Cross53a87f52019-06-25 13:35:30 -07002914
Paul Duffincdb88a92021-03-14 00:36:50 +00002915 foo := result.ModuleForTests("foo", "android_common")
Colin Cross53a87f52019-06-25 13:35:30 -07002916 dex := foo.Rule("r8")
2917 uncompressedInDexJar := strings.Contains(dex.Args["zipFlags"], "-L 0")
2918 aligned := foo.MaybeRule("zipalign").Rule != nil
2919
Paul Duffincdb88a92021-03-14 00:36:50 +00002920 android.AssertBoolEquals(t, "uncompressed in dex", want, uncompressedInDexJar)
Colin Cross53a87f52019-06-25 13:35:30 -07002921
Paul Duffincdb88a92021-03-14 00:36:50 +00002922 android.AssertBoolEquals(t, "aligne", want, aligned)
Colin Cross53a87f52019-06-25 13:35:30 -07002923 }
2924
2925 for _, tt := range testCases {
2926 t.Run(tt.name, func(t *testing.T) {
2927 t.Run("platform", func(t *testing.T) {
2928 test(t, tt.bp, tt.uncompressedPlatform, false)
2929 })
2930 t.Run("unbundled", func(t *testing.T) {
2931 test(t, tt.bp, tt.uncompressedUnbundled, true)
2932 })
2933 })
2934 }
2935}
Jaewoong Jung26dedd32019-06-06 08:45:58 -07002936
2937func checkAapt2LinkFlag(t *testing.T, aapt2Flags, flagName, expectedValue string) {
2938 if expectedValue != "" {
2939 expectedFlag := "--" + flagName + " " + expectedValue
2940 if !strings.Contains(aapt2Flags, expectedFlag) {
2941 t.Errorf("%q is missing in aapt2 link flags, %q", expectedFlag, aapt2Flags)
2942 }
2943 } else {
2944 unexpectedFlag := "--" + flagName
2945 if strings.Contains(aapt2Flags, unexpectedFlag) {
2946 t.Errorf("unexpected flag, %q is found in aapt2 link flags, %q", unexpectedFlag, aapt2Flags)
2947 }
2948 }
2949}
Jaewoong Jung9befb0c2020-01-18 10:33:43 -08002950
Cole Faust9a631312020-10-22 21:05:24 +00002951func TestExportedProguardFlagFiles(t *testing.T) {
2952 ctx, _ := testJava(t, `
2953 android_app {
2954 name: "foo",
2955 sdk_version: "current",
2956 static_libs: ["lib1"],
2957 }
2958
2959 android_library {
2960 name: "lib1",
2961 sdk_version: "current",
2962 optimize: {
2963 proguard_flags_files: ["lib1proguard.cfg"],
2964 }
2965 }
2966 `)
2967
2968 m := ctx.ModuleForTests("foo", "android_common")
2969 hasLib1Proguard := false
2970 for _, s := range m.Rule("java.r8").Implicits.Strings() {
2971 if s == "lib1proguard.cfg" {
2972 hasLib1Proguard = true
2973 break
2974 }
2975 }
2976
2977 if !hasLib1Proguard {
2978 t.Errorf("App does not use library proguard config")
2979 }
2980}
Spandan Dasc8cb0c32021-12-08 17:00:38 +00002981
2982func TestTargetSdkVersionManifestFixer(t *testing.T) {
2983 platform_sdk_codename := "Tiramisu"
2984 testCases := []struct {
2985 name string
2986 targetSdkVersionInBp string
2987 targetSdkVersionExpected string
2988 unbundledBuild bool
2989 }{
2990 {
2991 name: "Non-Unbundled build: Android.bp has targetSdkVersion",
2992 targetSdkVersionInBp: "30",
2993 targetSdkVersionExpected: "30",
2994 unbundledBuild: false,
2995 },
2996 {
2997 name: "Unbundled build: Android.bp has targetSdkVersion",
2998 targetSdkVersionInBp: "30",
2999 targetSdkVersionExpected: "30",
3000 unbundledBuild: true,
3001 },
3002 {
3003 name: "Non-Unbundled build: Android.bp has targetSdkVersion equal to platform_sdk_codename",
3004 targetSdkVersionInBp: platform_sdk_codename,
3005 targetSdkVersionExpected: platform_sdk_codename,
3006 unbundledBuild: false,
3007 },
3008 {
3009 name: "Unbundled build: Android.bp has targetSdkVersion equal to platform_sdk_codename",
3010 targetSdkVersionInBp: platform_sdk_codename,
3011 targetSdkVersionExpected: "10000",
3012 unbundledBuild: true,
3013 },
3014
3015 {
3016 name: "Non-Unbundled build: Android.bp has no targetSdkVersion",
3017 targetSdkVersionExpected: platform_sdk_codename,
3018 unbundledBuild: false,
3019 },
3020 {
3021 name: "Unbundled build: Android.bp has no targetSdkVersion",
3022 targetSdkVersionExpected: "10000",
3023 unbundledBuild: true,
3024 },
3025 }
3026 for _, testCase := range testCases {
3027 bp := fmt.Sprintf(`
3028 android_app {
3029 name: "foo",
3030 sdk_version: "current",
3031 target_sdk_version: "%v",
3032 }
3033 `, testCase.targetSdkVersionInBp)
3034 fixture := android.GroupFixturePreparers(
3035 prepareForJavaTest,
3036 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
3037 // explicitly set platform_sdk_codename to make the test deterministic
3038 variables.Platform_sdk_codename = &platform_sdk_codename
3039 variables.Platform_version_active_codenames = []string{platform_sdk_codename}
3040 // create a non-empty list if unbundledBuild==true
3041 if testCase.unbundledBuild {
3042 variables.Unbundled_build_apps = []string{"apex_a", "apex_b"}
3043 }
3044 }),
3045 )
3046
3047 result := fixture.RunTestWithBp(t, bp)
3048 foo := result.ModuleForTests("foo", "android_common")
3049
Gurpreet Singh75d65f32022-01-24 17:44:05 +00003050 manifestFixerArgs := foo.Output("manifest_fixer/AndroidManifest.xml").Args["args"]
3051 android.AssertStringDoesContain(t, testCase.name, manifestFixerArgs, "--targetSdkVersion "+testCase.targetSdkVersionExpected)
Spandan Dasc8cb0c32021-12-08 17:00:38 +00003052 }
3053}
Colin Cross412436f2022-04-07 17:40:07 -07003054
3055func TestAppMissingCertificateAllowMissingDependencies(t *testing.T) {
3056 result := android.GroupFixturePreparers(
3057 PrepareForTestWithJavaDefaultModules,
3058 android.PrepareForTestWithAllowMissingDependencies,
3059 android.PrepareForTestWithAndroidMk,
3060 ).RunTestWithBp(t, `
3061 android_app {
3062 name: "foo",
3063 srcs: ["a.java"],
3064 certificate: ":missing_certificate",
3065 sdk_version: "current",
3066 }`)
3067
3068 foo := result.ModuleForTests("foo", "android_common")
3069 fooApk := foo.Output("foo.apk")
3070 if fooApk.Rule != android.ErrorRule {
3071 t.Fatalf("expected ErrorRule for foo.apk, got %s", fooApk.Rule.String())
3072 }
3073 android.AssertStringDoesContain(t, "expected error rule message", fooApk.Args["error"], "missing dependencies: missing_certificate\n")
3074}