blob: 145f03949352eafe4287e1d20335c4db16110bd0 [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-Tawabec0843e2018-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-Tawabec0843e2018-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 Das2e8c0442022-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",
Colin Crossdea1d032022-12-06 14:50:08 -0800449 "android_arm64_armv8-a_sdk_29").Rule("noAddrSig").Output
Dan Albert92fe7402020-07-15 13:33:30 -0700450 expectedCrtEnd := ctx.ModuleForTests("crtend_so",
Colin Crossdea1d032022-12-06 14:50:08 -0800451 "android_arm64_armv8-a_sdk_29").Rule("noAddrSig").Output
Dan Albert92fe7402020-07-15 13:33:30 -0700452 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 Das2e8c0442022-05-08 00:39:35 +0000488 min_sdk_version: "current",
Jooyung Hanbbc3fb72020-04-29 14:01:06 +0900489 }
490 `
Spandan Das2e8c0442022-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 Das2e8c0442022-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 Das2e8c0442022-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 Das2e8c0442022-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
Alyssa Ketpreechasawat02774e82024-07-04 10:45:04 +0000525func TestUpdatableApps_ApplyDefaultUpdatableModuleVersion(t *testing.T) {
526 result := android.GroupFixturePreparers(
527 PrepareForTestWithJavaDefaultModules,
528 ).RunTestWithBp(t, `
529 android_app {
530 name: "com.android.foo",
531 srcs: ["a.java"],
532 sdk_version: "current",
533 min_sdk_version: "31",
534 updatable: true,
535 }
536 `)
537 foo := result.ModuleForTests("com.android.foo", "android_common").Rule("manifestFixer")
538 android.AssertStringDoesContain(t,
539 "com.android.foo: expected manifest fixer to set override-placeholder-version to android.DefaultUpdatableModuleVersion",
540 foo.BuildParams.Args["args"],
541 fmt.Sprintf("--override-placeholder-version %s", android.DefaultUpdatableModuleVersion),
542 )
543}
544
545func TestUpdatableApps_ApplyOverrideApexManifestDefaultVersion(t *testing.T) {
546 result := android.GroupFixturePreparers(
547 PrepareForTestWithJavaDefaultModules,
548 android.FixtureMergeEnv(map[string]string{
549 "OVERRIDE_APEX_MANIFEST_DEFAULT_VERSION": "1234",
550 }),
551 ).RunTestWithBp(t, `
552 android_app {
553 name: "com.android.foo",
554 srcs: ["a.java"],
555 sdk_version: "current",
556 min_sdk_version: "31",
557 updatable: true,
558 }
559 `)
560 foo := result.ModuleForTests("com.android.foo", "android_common").Rule("manifestFixer")
561 android.AssertStringDoesContain(t,
562 "com.android.foo: expected manifest fixer to set override-placeholder-version to 1234",
563 foo.BuildParams.Args["args"],
564 "--override-placeholder-version 1234",
565 )
566}
567
Colin Cross0ddae7f2019-02-07 15:30:01 -0800568func TestResourceDirs(t *testing.T) {
569 testCases := []struct {
570 name string
571 prop string
572 resources []string
573 }{
574 {
575 name: "no resource_dirs",
576 prop: "",
577 resources: []string{"res/res/values/strings.xml"},
578 },
579 {
580 name: "resource_dirs",
581 prop: `resource_dirs: ["res"]`,
582 resources: []string{"res/res/values/strings.xml"},
583 },
584 {
585 name: "empty resource_dirs",
586 prop: `resource_dirs: []`,
587 resources: nil,
588 },
589 }
590
Paul Duffinfb0fe9f2021-03-22 17:31:52 +0000591 fs := android.MockFS{
Colin Cross0ddae7f2019-02-07 15:30:01 -0800592 "res/res/values/strings.xml": nil,
593 }
594
595 bp := `
596 android_app {
597 name: "foo",
Jeongik Cha538c0d02019-07-11 15:54:27 +0900598 sdk_version: "current",
Colin Cross0ddae7f2019-02-07 15:30:01 -0800599 %s
600 }
601 `
602
603 for _, testCase := range testCases {
604 t.Run(testCase.name, func(t *testing.T) {
Paul Duffinfb0fe9f2021-03-22 17:31:52 +0000605 result := android.GroupFixturePreparers(
606 PrepareForTestWithJavaDefaultModules,
Paul Duffinfb0fe9f2021-03-22 17:31:52 +0000607 fs.AddToFixture(),
608 ).RunTestWithBp(t, fmt.Sprintf(bp, testCase.prop))
Colin Cross0ddae7f2019-02-07 15:30:01 -0800609
Paul Duffinfb0fe9f2021-03-22 17:31:52 +0000610 module := result.ModuleForTests("foo", "android_common")
Colin Cross0ddae7f2019-02-07 15:30:01 -0800611 resourceList := module.MaybeOutput("aapt2/res.list")
612
613 var resources []string
614 if resourceList.Rule != nil {
615 for _, compiledResource := range resourceList.Inputs.Strings() {
616 resources = append(resources, module.Output(compiledResource).Inputs.Strings()...)
617 }
618 }
619
Paul Duffinfb0fe9f2021-03-22 17:31:52 +0000620 android.AssertDeepEquals(t, "resource files", testCase.resources, resources)
Colin Cross0ddae7f2019-02-07 15:30:01 -0800621 })
622 }
623}
624
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800625func TestLibraryAssets(t *testing.T) {
626 bp := `
627 android_app {
628 name: "foo",
629 sdk_version: "current",
630 static_libs: ["lib1", "lib2", "lib3"],
631 }
632
633 android_library {
634 name: "lib1",
635 sdk_version: "current",
636 asset_dirs: ["assets_a"],
637 }
638
639 android_library {
640 name: "lib2",
641 sdk_version: "current",
642 }
643
644 android_library {
645 name: "lib3",
646 sdk_version: "current",
Colin Crossab8d1382023-07-14 17:23:41 +0000647 static_libs: ["lib4", "import"],
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800648 }
649
650 android_library {
651 name: "lib4",
652 sdk_version: "current",
653 asset_dirs: ["assets_b"],
654 }
Colin Crossab8d1382023-07-14 17:23:41 +0000655
Jiakai Zhangba82e282023-10-13 18:08:59 +0100656 android_library {
657 name: "lib5",
658 sdk_version: "current",
659 assets: [
660 "path/to/asset_file_1",
661 "path/to/asset_file_2",
662 ],
663 }
664
Colin Crossab8d1382023-07-14 17:23:41 +0000665 android_library_import {
666 name: "import",
667 sdk_version: "current",
668 aars: ["import.aar"],
669 }
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800670 `
671
672 testCases := []struct {
Jiakai Zhangba82e282023-10-13 18:08:59 +0100673 name string
674 assetFlag string
675 assetPackages []string
676 tmpAssetDirInputs []string
677 tmpAssetDirOutputs []string
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800678 }{
679 {
680 name: "foo",
Colin Crossab8d1382023-07-14 17:23:41 +0000681 // lib1 has its own assets. lib3 doesn't have any, but lib4 and import have assets.
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800682 assetPackages: []string{
Paul Duffinfb0fe9f2021-03-22 17:31:52 +0000683 "out/soong/.intermediates/foo/android_common/aapt2/package-res.apk",
684 "out/soong/.intermediates/lib1/android_common/assets.zip",
Colin Crossab8d1382023-07-14 17:23:41 +0000685 "out/soong/.intermediates/lib4/android_common/assets.zip",
686 "out/soong/.intermediates/import/android_common/assets.zip",
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800687 },
688 },
689 {
690 name: "lib1",
691 assetFlag: "-A assets_a",
692 },
693 {
694 name: "lib2",
695 },
696 {
697 name: "lib3",
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800698 },
699 {
700 name: "lib4",
701 assetFlag: "-A assets_b",
702 },
Jiakai Zhangba82e282023-10-13 18:08:59 +0100703 {
704 name: "lib5",
705 assetFlag: "-A out/soong/.intermediates/lib5/android_common/tmp_asset_dir",
706 tmpAssetDirInputs: []string{
707 "path/to/asset_file_1",
708 "path/to/asset_file_2",
709 },
710 tmpAssetDirOutputs: []string{
711 "out/soong/.intermediates/lib5/android_common/tmp_asset_dir/path/to/asset_file_1",
712 "out/soong/.intermediates/lib5/android_common/tmp_asset_dir/path/to/asset_file_2",
713 },
714 },
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800715 }
716 ctx := testApp(t, bp)
717
718 for _, test := range testCases {
719 t.Run(test.name, func(t *testing.T) {
720 m := ctx.ModuleForTests(test.name, "android_common")
721
722 // Check asset flag in aapt2 link flags
723 var aapt2link android.TestingBuildParams
724 if len(test.assetPackages) > 0 {
725 aapt2link = m.Output("aapt2/package-res.apk")
726 } else {
727 aapt2link = m.Output("package-res.apk")
728 }
Paul Duffina71a67a2021-03-29 00:42:57 +0100729 aapt2link = aapt2link
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800730 aapt2Flags := aapt2link.Args["flags"]
731 if test.assetFlag != "" {
Paul Duffinfb0fe9f2021-03-22 17:31:52 +0000732 android.AssertStringDoesContain(t, "asset flag", aapt2Flags, test.assetFlag)
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800733 } else {
Paul Duffinfb0fe9f2021-03-22 17:31:52 +0000734 android.AssertStringDoesNotContain(t, "aapt2 link flags", aapt2Flags, " -A ")
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800735 }
736
737 // Check asset merge rule.
738 if len(test.assetPackages) > 0 {
739 mergeAssets := m.Output("package-res.apk")
Paul Duffinfb0fe9f2021-03-22 17:31:52 +0000740 android.AssertPathsRelativeToTopEquals(t, "mergeAssets inputs", test.assetPackages, mergeAssets.Inputs)
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800741 }
Jiakai Zhangba82e282023-10-13 18:08:59 +0100742
743 if len(test.tmpAssetDirInputs) > 0 {
744 rule := m.Rule("tmp_asset_dir")
745 inputs := rule.Implicits
746 outputs := append(android.WritablePaths{rule.Output}, rule.ImplicitOutputs...).Paths()
747 android.AssertPathsRelativeToTopEquals(t, "tmp_asset_dir inputs", test.tmpAssetDirInputs, inputs)
748 android.AssertPathsRelativeToTopEquals(t, "tmp_asset_dir outputs", test.tmpAssetDirOutputs, outputs)
749 }
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800750 })
751 }
752}
753
Colin Crossb014f072021-02-26 14:54:36 -0800754func TestAppJavaResources(t *testing.T) {
755 bp := `
756 android_app {
757 name: "foo",
758 sdk_version: "current",
759 java_resources: ["resources/a"],
760 srcs: ["a.java"],
761 }
762
763 android_app {
764 name: "bar",
765 sdk_version: "current",
766 java_resources: ["resources/a"],
767 }
768 `
769
770 ctx := testApp(t, bp)
771
772 foo := ctx.ModuleForTests("foo", "android_common")
773 fooResources := foo.Output("res/foo.jar")
774 fooDexJar := foo.Output("dex-withres/foo.jar")
775 fooDexJarAligned := foo.Output("dex-withres-aligned/foo.jar")
776 fooApk := foo.Rule("combineApk")
777
778 if g, w := fooDexJar.Inputs.Strings(), fooResources.Output.String(); !android.InList(w, g) {
779 t.Errorf("expected resource jar %q in foo dex jar inputs %q", w, g)
780 }
781
782 if g, w := fooDexJarAligned.Input.String(), fooDexJar.Output.String(); g != w {
783 t.Errorf("expected dex jar %q in foo aligned dex jar inputs %q", w, g)
784 }
785
786 if g, w := fooApk.Inputs.Strings(), fooDexJarAligned.Output.String(); !android.InList(w, g) {
787 t.Errorf("expected aligned dex jar %q in foo apk inputs %q", w, g)
788 }
789
790 bar := ctx.ModuleForTests("bar", "android_common")
791 barResources := bar.Output("res/bar.jar")
792 barApk := bar.Rule("combineApk")
793
794 if g, w := barApk.Inputs.Strings(), barResources.Output.String(); !android.InList(w, g) {
795 t.Errorf("expected resources jar %q in bar apk inputs %q", w, g)
796 }
797}
798
Colin Crossff3ff7f2023-07-05 14:04:12 -0700799func TestAndroidResourceProcessor(t *testing.T) {
800 testCases := []struct {
Colin Cross8676c8c2023-10-12 15:58:57 -0700801 name string
802 appUsesRP bool
803 directLibUsesRP bool
804 transitiveLibUsesRP bool
805 sharedLibUsesRP bool
806 sharedTransitiveStaticLibUsesRP bool
807 sharedTransitiveSharedLibUsesRP bool
Colin Crossff3ff7f2023-07-05 14:04:12 -0700808
809 dontVerifyApp bool
810 appResources []string
811 appOverlays []string
812 appImports []string
813 appSrcJars []string
814 appClasspath []string
815 appCombined []string
816
817 dontVerifyDirect bool
818 directResources []string
819 directOverlays []string
820 directImports []string
821 directSrcJars []string
822 directClasspath []string
823 directCombined []string
824
825 dontVerifyTransitive bool
826 transitiveResources []string
827 transitiveOverlays []string
828 transitiveImports []string
829 transitiveSrcJars []string
830 transitiveClasspath []string
831 transitiveCombined []string
832
833 dontVerifyDirectImport bool
834 directImportResources []string
835 directImportOverlays []string
836 directImportImports []string
837
838 dontVerifyTransitiveImport bool
839 transitiveImportResources []string
840 transitiveImportOverlays []string
841 transitiveImportImports []string
Colin Cross8676c8c2023-10-12 15:58:57 -0700842
843 dontVerifyShared bool
844 sharedResources []string
845 sharedOverlays []string
846 sharedImports []string
847 sharedSrcJars []string
848 sharedClasspath []string
849 sharedCombined []string
Colin Crossff3ff7f2023-07-05 14:04:12 -0700850 }{
851 {
Colin Cross4eae06d2023-06-20 22:40:02 -0700852 // Test with all modules set to use_resource_processor: false (except android_library_import modules,
853 // which always use resource processor).
854 name: "legacy",
855 appUsesRP: false,
856 directLibUsesRP: false,
857 transitiveLibUsesRP: false,
Colin Crossff3ff7f2023-07-05 14:04:12 -0700858
859 appResources: nil,
860 appOverlays: []string{
861 "out/soong/.intermediates/transitive/android_common/package-res.apk",
Colin Crossab8d1382023-07-14 17:23:41 +0000862 "out/soong/.intermediates/transitive_import_dep/android_common/package-res.apk",
Colin Crossff3ff7f2023-07-05 14:04:12 -0700863 "out/soong/.intermediates/transitive_import/android_common/package-res.apk",
864 "out/soong/.intermediates/direct/android_common/package-res.apk",
Colin Crossab8d1382023-07-14 17:23:41 +0000865 "out/soong/.intermediates/direct_import_dep/android_common/package-res.apk",
Colin Crossff3ff7f2023-07-05 14:04:12 -0700866 "out/soong/.intermediates/direct_import/android_common/package-res.apk",
867 "out/soong/.intermediates/app/android_common/aapt2/app/res/values_strings.arsc.flat",
868 },
Colin Cross8676c8c2023-10-12 15:58:57 -0700869 appImports: []string{
870 "out/soong/.intermediates/shared/android_common/package-res.apk",
871 "out/soong/.intermediates/default/java/framework-res/android_common/package-res.apk",
872 },
Colin Crossff3ff7f2023-07-05 14:04:12 -0700873 appSrcJars: []string{"out/soong/.intermediates/app/android_common/gen/android/R.srcjar"},
874 appClasspath: []string{
875 "out/soong/.intermediates/default/java/android_stubs_current/android_common/turbine-combined/android_stubs_current.jar",
Colin Cross8676c8c2023-10-12 15:58:57 -0700876 "out/soong/.intermediates/shared/android_common/turbine-combined/shared.jar",
Colin Crossff3ff7f2023-07-05 14:04:12 -0700877 "out/soong/.intermediates/direct/android_common/turbine-combined/direct.jar",
Colin Cross9055e212024-03-23 04:43:41 +0000878 "out/soong/.intermediates/direct_import/android_common/turbine-combined/direct_import.jar",
Colin Crossff3ff7f2023-07-05 14:04:12 -0700879 },
880 appCombined: []string{
881 "out/soong/.intermediates/app/android_common/javac/app.jar",
882 "out/soong/.intermediates/direct/android_common/combined/direct.jar",
Colin Cross9055e212024-03-23 04:43:41 +0000883 "out/soong/.intermediates/direct_import/android_common/combined/direct_import.jar",
Colin Crossff3ff7f2023-07-05 14:04:12 -0700884 },
885
886 directResources: nil,
887 directOverlays: []string{
888 "out/soong/.intermediates/transitive/android_common/package-res.apk",
Colin Crossab8d1382023-07-14 17:23:41 +0000889 "out/soong/.intermediates/transitive_import_dep/android_common/package-res.apk",
Colin Crossff3ff7f2023-07-05 14:04:12 -0700890 "out/soong/.intermediates/transitive_import/android_common/package-res.apk",
891 "out/soong/.intermediates/direct/android_common/aapt2/direct/res/values_strings.arsc.flat",
892 },
893 directImports: []string{"out/soong/.intermediates/default/java/framework-res/android_common/package-res.apk"},
894 directSrcJars: []string{"out/soong/.intermediates/direct/android_common/gen/android/R.srcjar"},
895 directClasspath: []string{
896 "out/soong/.intermediates/default/java/android_stubs_current/android_common/turbine-combined/android_stubs_current.jar",
897 "out/soong/.intermediates/transitive/android_common/turbine-combined/transitive.jar",
Colin Cross9055e212024-03-23 04:43:41 +0000898 "out/soong/.intermediates/transitive_import/android_common/turbine-combined/transitive_import.jar",
Colin Crossff3ff7f2023-07-05 14:04:12 -0700899 },
900 directCombined: []string{
901 "out/soong/.intermediates/direct/android_common/javac/direct.jar",
902 "out/soong/.intermediates/transitive/android_common/javac/transitive.jar",
Colin Cross9055e212024-03-23 04:43:41 +0000903 "out/soong/.intermediates/transitive_import/android_common/combined/transitive_import.jar",
Colin Crossff3ff7f2023-07-05 14:04:12 -0700904 },
905
906 transitiveResources: []string{"out/soong/.intermediates/transitive/android_common/aapt2/transitive/res/values_strings.arsc.flat"},
907 transitiveOverlays: nil,
908 transitiveImports: []string{"out/soong/.intermediates/default/java/framework-res/android_common/package-res.apk"},
909 transitiveSrcJars: []string{"out/soong/.intermediates/transitive/android_common/gen/android/R.srcjar"},
910 transitiveClasspath: []string{"out/soong/.intermediates/default/java/android_stubs_current/android_common/turbine-combined/android_stubs_current.jar"},
911 transitiveCombined: nil,
912
Colin Cross8676c8c2023-10-12 15:58:57 -0700913 sharedResources: nil,
914 sharedOverlays: []string{
915 "out/soong/.intermediates/shared_transitive_static/android_common/package-res.apk",
916 "out/soong/.intermediates/shared/android_common/aapt2/shared/res/values_strings.arsc.flat",
917 },
918 sharedImports: []string{
919 "out/soong/.intermediates/shared_transitive_shared/android_common/package-res.apk",
920 "out/soong/.intermediates/default/java/framework-res/android_common/package-res.apk",
921 },
922 sharedSrcJars: []string{"out/soong/.intermediates/shared/android_common/gen/android/R.srcjar"},
923 sharedClasspath: []string{
924 "out/soong/.intermediates/default/java/android_stubs_current/android_common/turbine-combined/android_stubs_current.jar",
925 "out/soong/.intermediates/shared_transitive_shared/android_common/turbine-combined/shared_transitive_shared.jar",
926 "out/soong/.intermediates/shared_transitive_static/android_common/turbine-combined/shared_transitive_static.jar",
927 },
928 sharedCombined: []string{
929 "out/soong/.intermediates/shared/android_common/javac/shared.jar",
930 "out/soong/.intermediates/shared_transitive_static/android_common/javac/shared_transitive_static.jar",
931 },
932
Colin Crossff3ff7f2023-07-05 14:04:12 -0700933 directImportResources: nil,
Colin Cross4eae06d2023-06-20 22:40:02 -0700934 directImportOverlays: []string{"out/soong/.intermediates/direct_import/android_common/flat-res/gen_res.flata"},
935 directImportImports: []string{
936 "out/soong/.intermediates/default/java/framework-res/android_common/package-res.apk",
Colin Crossff3ff7f2023-07-05 14:04:12 -0700937 "out/soong/.intermediates/direct_import_dep/android_common/package-res.apk",
938 },
Colin Crossff3ff7f2023-07-05 14:04:12 -0700939
940 transitiveImportResources: nil,
Colin Cross4eae06d2023-06-20 22:40:02 -0700941 transitiveImportOverlays: []string{"out/soong/.intermediates/transitive_import/android_common/flat-res/gen_res.flata"},
942 transitiveImportImports: []string{
943 "out/soong/.intermediates/default/java/framework-res/android_common/package-res.apk",
Colin Crossff3ff7f2023-07-05 14:04:12 -0700944 "out/soong/.intermediates/transitive_import_dep/android_common/package-res.apk",
945 },
Colin Cross4eae06d2023-06-20 22:40:02 -0700946 },
947 {
948 // Test with all modules set to use_resource_processor: true.
Colin Cross8676c8c2023-10-12 15:58:57 -0700949 name: "resource_processor",
950 appUsesRP: true,
951 directLibUsesRP: true,
952 transitiveLibUsesRP: true,
953 sharedLibUsesRP: true,
954 sharedTransitiveSharedLibUsesRP: true,
955 sharedTransitiveStaticLibUsesRP: true,
Colin Cross4eae06d2023-06-20 22:40:02 -0700956
957 appResources: nil,
958 appOverlays: []string{
959 "out/soong/.intermediates/transitive/android_common/package-res.apk",
960 "out/soong/.intermediates/transitive_import_dep/android_common/package-res.apk",
961 "out/soong/.intermediates/transitive_import/android_common/package-res.apk",
962 "out/soong/.intermediates/direct/android_common/package-res.apk",
963 "out/soong/.intermediates/direct_import_dep/android_common/package-res.apk",
964 "out/soong/.intermediates/direct_import/android_common/package-res.apk",
965 "out/soong/.intermediates/app/android_common/aapt2/app/res/values_strings.arsc.flat",
966 },
Colin Cross8676c8c2023-10-12 15:58:57 -0700967 appImports: []string{
968 "out/soong/.intermediates/shared/android_common/package-res.apk",
969 "out/soong/.intermediates/default/java/framework-res/android_common/package-res.apk",
970 },
Colin Cross4eae06d2023-06-20 22:40:02 -0700971 appSrcJars: nil,
972 appClasspath: []string{
973 "out/soong/.intermediates/default/java/android_stubs_current/android_common/turbine-combined/android_stubs_current.jar",
974 "out/soong/.intermediates/app/android_common/busybox/R.jar",
Colin Cross8676c8c2023-10-12 15:58:57 -0700975 "out/soong/.intermediates/shared/android_common/turbine-combined/shared.jar",
Colin Cross4eae06d2023-06-20 22:40:02 -0700976 "out/soong/.intermediates/direct/android_common/turbine-combined/direct.jar",
Colin Cross9055e212024-03-23 04:43:41 +0000977 "out/soong/.intermediates/direct_import/android_common/turbine-combined/direct_import.jar",
Colin Cross4eae06d2023-06-20 22:40:02 -0700978 },
979 appCombined: []string{
Colin Cross4eae06d2023-06-20 22:40:02 -0700980 "out/soong/.intermediates/app/android_common/javac/app.jar",
Colin Crossfd620b22024-02-23 10:05:21 -0800981 "out/soong/.intermediates/app/android_common/busybox/R.jar",
Colin Cross4eae06d2023-06-20 22:40:02 -0700982 "out/soong/.intermediates/direct/android_common/combined/direct.jar",
Colin Cross9055e212024-03-23 04:43:41 +0000983 "out/soong/.intermediates/direct_import/android_common/combined/direct_import.jar",
Colin Cross4eae06d2023-06-20 22:40:02 -0700984 },
985
986 directResources: nil,
987 directOverlays: []string{"out/soong/.intermediates/direct/android_common/aapt2/direct/res/values_strings.arsc.flat"},
988 directImports: []string{
989 "out/soong/.intermediates/default/java/framework-res/android_common/package-res.apk",
990 "out/soong/.intermediates/transitive_import/android_common/package-res.apk",
991 "out/soong/.intermediates/transitive_import_dep/android_common/package-res.apk",
992 "out/soong/.intermediates/transitive/android_common/package-res.apk",
993 },
994 directSrcJars: nil,
995 directClasspath: []string{
996 "out/soong/.intermediates/default/java/android_stubs_current/android_common/turbine-combined/android_stubs_current.jar",
Colin Cross4eae06d2023-06-20 22:40:02 -0700997 "out/soong/.intermediates/direct/android_common/busybox/R.jar",
Colin Cross1d3f5902024-03-05 11:51:54 -0800998 "out/soong/.intermediates/transitive/android_common/busybox/R.jar",
999 "out/soong/.intermediates/transitive_import_dep/android_common/busybox/R.jar",
1000 "out/soong/.intermediates/transitive_import/android_common/busybox/R.jar",
Colin Cross4eae06d2023-06-20 22:40:02 -07001001 "out/soong/.intermediates/transitive/android_common/turbine-combined/transitive.jar",
Colin Cross9055e212024-03-23 04:43:41 +00001002 "out/soong/.intermediates/transitive_import/android_common/turbine-combined/transitive_import.jar",
Colin Cross4eae06d2023-06-20 22:40:02 -07001003 },
1004 directCombined: []string{
1005 "out/soong/.intermediates/direct/android_common/javac/direct.jar",
1006 "out/soong/.intermediates/transitive/android_common/javac/transitive.jar",
Colin Cross9055e212024-03-23 04:43:41 +00001007 "out/soong/.intermediates/transitive_import/android_common/combined/transitive_import.jar",
Colin Cross4eae06d2023-06-20 22:40:02 -07001008 },
1009
1010 transitiveResources: []string{"out/soong/.intermediates/transitive/android_common/aapt2/transitive/res/values_strings.arsc.flat"},
1011 transitiveOverlays: nil,
1012 transitiveImports: []string{"out/soong/.intermediates/default/java/framework-res/android_common/package-res.apk"},
1013 transitiveSrcJars: nil,
1014 transitiveClasspath: []string{
1015 "out/soong/.intermediates/default/java/android_stubs_current/android_common/turbine-combined/android_stubs_current.jar",
1016 "out/soong/.intermediates/transitive/android_common/busybox/R.jar",
1017 },
1018 transitiveCombined: nil,
1019
Colin Cross8676c8c2023-10-12 15:58:57 -07001020 sharedResources: nil,
1021 sharedOverlays: []string{"out/soong/.intermediates/shared/android_common/aapt2/shared/res/values_strings.arsc.flat"},
1022 sharedImports: []string{
1023 "out/soong/.intermediates/shared_transitive_shared/android_common/package-res.apk",
1024 "out/soong/.intermediates/default/java/framework-res/android_common/package-res.apk",
1025 "out/soong/.intermediates/shared_transitive_static/android_common/package-res.apk",
1026 },
1027 sharedSrcJars: nil,
1028 sharedClasspath: []string{
1029 "out/soong/.intermediates/default/java/android_stubs_current/android_common/turbine-combined/android_stubs_current.jar",
Colin Cross1d3f5902024-03-05 11:51:54 -08001030 "out/soong/.intermediates/shared/android_common/busybox/R.jar",
Colin Cross8676c8c2023-10-12 15:58:57 -07001031 "out/soong/.intermediates/shared_transitive_static/android_common/busybox/R.jar",
1032 "out/soong/.intermediates/shared_transitive_shared/android_common/busybox/R.jar",
Colin Cross8676c8c2023-10-12 15:58:57 -07001033 "out/soong/.intermediates/shared_transitive_shared/android_common/turbine-combined/shared_transitive_shared.jar",
1034 "out/soong/.intermediates/shared_transitive_static/android_common/turbine-combined/shared_transitive_static.jar",
1035 },
1036 sharedCombined: []string{
1037 "out/soong/.intermediates/shared/android_common/javac/shared.jar",
1038 "out/soong/.intermediates/shared_transitive_static/android_common/javac/shared_transitive_static.jar",
1039 },
1040
Colin Cross4eae06d2023-06-20 22:40:02 -07001041 directImportResources: nil,
1042 directImportOverlays: []string{"out/soong/.intermediates/direct_import/android_common/flat-res/gen_res.flata"},
1043 directImportImports: []string{
1044 "out/soong/.intermediates/default/java/framework-res/android_common/package-res.apk",
1045 "out/soong/.intermediates/direct_import_dep/android_common/package-res.apk",
1046 },
1047
1048 transitiveImportResources: nil,
1049 transitiveImportOverlays: []string{"out/soong/.intermediates/transitive_import/android_common/flat-res/gen_res.flata"},
1050 transitiveImportImports: []string{
1051 "out/soong/.intermediates/default/java/framework-res/android_common/package-res.apk",
1052 "out/soong/.intermediates/transitive_import_dep/android_common/package-res.apk",
1053 },
1054 }, {
1055 // Test an app building with resource processor enabled but with dependencies built without
1056 // resource processor.
1057 name: "app_resource_processor",
1058 appUsesRP: true,
1059 directLibUsesRP: false,
1060 transitiveLibUsesRP: false,
1061
1062 appResources: nil,
1063 appOverlays: []string{
1064 "out/soong/.intermediates/transitive/android_common/package-res.apk",
1065 "out/soong/.intermediates/transitive_import_dep/android_common/package-res.apk",
1066 "out/soong/.intermediates/transitive_import/android_common/package-res.apk",
1067 "out/soong/.intermediates/direct/android_common/package-res.apk",
1068 "out/soong/.intermediates/direct_import_dep/android_common/package-res.apk",
1069 "out/soong/.intermediates/direct_import/android_common/package-res.apk",
1070 "out/soong/.intermediates/app/android_common/aapt2/app/res/values_strings.arsc.flat",
1071 },
Colin Cross8676c8c2023-10-12 15:58:57 -07001072 appImports: []string{
1073 "out/soong/.intermediates/shared/android_common/package-res.apk",
1074 "out/soong/.intermediates/default/java/framework-res/android_common/package-res.apk",
1075 },
Colin Cross4eae06d2023-06-20 22:40:02 -07001076 appSrcJars: nil,
1077 appClasspath: []string{
1078 "out/soong/.intermediates/default/java/android_stubs_current/android_common/turbine-combined/android_stubs_current.jar",
1079 // R.jar has to come before direct.jar
1080 "out/soong/.intermediates/app/android_common/busybox/R.jar",
Colin Cross8676c8c2023-10-12 15:58:57 -07001081 "out/soong/.intermediates/shared/android_common/turbine-combined/shared.jar",
Colin Cross4eae06d2023-06-20 22:40:02 -07001082 "out/soong/.intermediates/direct/android_common/turbine-combined/direct.jar",
Colin Cross9055e212024-03-23 04:43:41 +00001083 "out/soong/.intermediates/direct_import/android_common/turbine-combined/direct_import.jar",
Colin Cross4eae06d2023-06-20 22:40:02 -07001084 },
1085 appCombined: []string{
Colin Cross4eae06d2023-06-20 22:40:02 -07001086 "out/soong/.intermediates/app/android_common/javac/app.jar",
Colin Crossfd620b22024-02-23 10:05:21 -08001087 "out/soong/.intermediates/app/android_common/busybox/R.jar",
Colin Cross4eae06d2023-06-20 22:40:02 -07001088 "out/soong/.intermediates/direct/android_common/combined/direct.jar",
Colin Cross9055e212024-03-23 04:43:41 +00001089 "out/soong/.intermediates/direct_import/android_common/combined/direct_import.jar",
Colin Cross4eae06d2023-06-20 22:40:02 -07001090 },
1091
1092 dontVerifyDirect: true,
1093 dontVerifyTransitive: true,
Colin Cross8676c8c2023-10-12 15:58:57 -07001094 dontVerifyShared: true,
Colin Cross4eae06d2023-06-20 22:40:02 -07001095 dontVerifyDirectImport: true,
1096 dontVerifyTransitiveImport: true,
1097 },
1098 {
1099 // Test an app building without resource processor enabled but with a dependency built with
1100 // resource processor.
1101 name: "app_dependency_lib_resource_processor",
1102 appUsesRP: false,
1103 directLibUsesRP: true,
1104 transitiveLibUsesRP: false,
1105
1106 appOverlays: []string{
1107 "out/soong/.intermediates/transitive/android_common/package-res.apk",
1108 "out/soong/.intermediates/transitive_import_dep/android_common/package-res.apk",
1109 "out/soong/.intermediates/transitive_import/android_common/package-res.apk",
1110 "out/soong/.intermediates/direct/android_common/package-res.apk",
1111 "out/soong/.intermediates/direct_import_dep/android_common/package-res.apk",
1112 "out/soong/.intermediates/direct_import/android_common/package-res.apk",
1113 "out/soong/.intermediates/app/android_common/aapt2/app/res/values_strings.arsc.flat",
1114 },
Colin Cross8676c8c2023-10-12 15:58:57 -07001115 appImports: []string{
1116 "out/soong/.intermediates/shared/android_common/package-res.apk",
1117 "out/soong/.intermediates/default/java/framework-res/android_common/package-res.apk",
1118 },
Colin Cross4eae06d2023-06-20 22:40:02 -07001119 appSrcJars: []string{"out/soong/.intermediates/app/android_common/gen/android/R.srcjar"},
1120 appClasspath: []string{
1121 "out/soong/.intermediates/default/java/android_stubs_current/android_common/turbine-combined/android_stubs_current.jar",
Colin Cross8676c8c2023-10-12 15:58:57 -07001122 "out/soong/.intermediates/shared/android_common/turbine-combined/shared.jar",
Colin Cross4eae06d2023-06-20 22:40:02 -07001123 "out/soong/.intermediates/direct/android_common/turbine-combined/direct.jar",
Colin Cross9055e212024-03-23 04:43:41 +00001124 "out/soong/.intermediates/direct_import/android_common/turbine-combined/direct_import.jar",
Colin Cross4eae06d2023-06-20 22:40:02 -07001125 },
1126 appCombined: []string{
1127 "out/soong/.intermediates/app/android_common/javac/app.jar",
1128 "out/soong/.intermediates/direct/android_common/combined/direct.jar",
Colin Cross9055e212024-03-23 04:43:41 +00001129 "out/soong/.intermediates/direct_import/android_common/combined/direct_import.jar",
Colin Cross4eae06d2023-06-20 22:40:02 -07001130 },
1131
1132 directResources: nil,
1133 directOverlays: []string{"out/soong/.intermediates/direct/android_common/aapt2/direct/res/values_strings.arsc.flat"},
1134 directImports: []string{
1135 "out/soong/.intermediates/default/java/framework-res/android_common/package-res.apk",
1136 "out/soong/.intermediates/transitive_import/android_common/package-res.apk",
1137 "out/soong/.intermediates/transitive_import_dep/android_common/package-res.apk",
1138 "out/soong/.intermediates/transitive/android_common/package-res.apk",
1139 },
1140 directSrcJars: nil,
1141 directClasspath: []string{
1142 "out/soong/.intermediates/default/java/android_stubs_current/android_common/turbine-combined/android_stubs_current.jar",
Colin Cross4eae06d2023-06-20 22:40:02 -07001143 "out/soong/.intermediates/direct/android_common/busybox/R.jar",
Colin Cross1d3f5902024-03-05 11:51:54 -08001144 "out/soong/.intermediates/transitive_import_dep/android_common/busybox/R.jar",
1145 "out/soong/.intermediates/transitive_import/android_common/busybox/R.jar",
Colin Cross4eae06d2023-06-20 22:40:02 -07001146 "out/soong/.intermediates/transitive/android_common/turbine-combined/transitive.jar",
Colin Cross9055e212024-03-23 04:43:41 +00001147 "out/soong/.intermediates/transitive_import/android_common/turbine-combined/transitive_import.jar",
Colin Cross4eae06d2023-06-20 22:40:02 -07001148 },
1149 directCombined: []string{
1150 "out/soong/.intermediates/direct/android_common/javac/direct.jar",
1151 "out/soong/.intermediates/transitive/android_common/javac/transitive.jar",
Colin Cross9055e212024-03-23 04:43:41 +00001152 "out/soong/.intermediates/transitive_import/android_common/combined/transitive_import.jar",
Colin Cross4eae06d2023-06-20 22:40:02 -07001153 },
1154
1155 dontVerifyTransitive: true,
Colin Cross8676c8c2023-10-12 15:58:57 -07001156 dontVerifyShared: true,
Colin Cross4eae06d2023-06-20 22:40:02 -07001157 dontVerifyDirectImport: true,
1158 dontVerifyTransitiveImport: true,
1159 },
1160 {
1161 // Test a library building without resource processor enabled but with a dependency built with
1162 // resource processor.
1163 name: "lib_dependency_lib_resource_processor",
1164 appUsesRP: false,
1165 directLibUsesRP: false,
1166 transitiveLibUsesRP: true,
1167
1168 appOverlays: []string{
1169 "out/soong/.intermediates/transitive/android_common/package-res.apk",
1170 "out/soong/.intermediates/transitive_import_dep/android_common/package-res.apk",
1171 "out/soong/.intermediates/transitive_import/android_common/package-res.apk",
1172 "out/soong/.intermediates/direct/android_common/package-res.apk",
1173 "out/soong/.intermediates/direct_import_dep/android_common/package-res.apk",
1174 "out/soong/.intermediates/direct_import/android_common/package-res.apk",
1175 "out/soong/.intermediates/app/android_common/aapt2/app/res/values_strings.arsc.flat",
1176 },
Colin Cross8676c8c2023-10-12 15:58:57 -07001177 appImports: []string{
1178 "out/soong/.intermediates/shared/android_common/package-res.apk",
1179 "out/soong/.intermediates/default/java/framework-res/android_common/package-res.apk",
1180 },
Colin Cross4eae06d2023-06-20 22:40:02 -07001181 appSrcJars: []string{"out/soong/.intermediates/app/android_common/gen/android/R.srcjar"},
1182 appClasspath: []string{
1183 "out/soong/.intermediates/default/java/android_stubs_current/android_common/turbine-combined/android_stubs_current.jar",
Colin Cross8676c8c2023-10-12 15:58:57 -07001184 "out/soong/.intermediates/shared/android_common/turbine-combined/shared.jar",
Colin Cross4eae06d2023-06-20 22:40:02 -07001185 "out/soong/.intermediates/direct/android_common/turbine-combined/direct.jar",
Colin Cross9055e212024-03-23 04:43:41 +00001186 "out/soong/.intermediates/direct_import/android_common/turbine-combined/direct_import.jar",
Colin Cross4eae06d2023-06-20 22:40:02 -07001187 },
1188 appCombined: []string{
1189 "out/soong/.intermediates/app/android_common/javac/app.jar",
1190 "out/soong/.intermediates/direct/android_common/combined/direct.jar",
Colin Cross9055e212024-03-23 04:43:41 +00001191 "out/soong/.intermediates/direct_import/android_common/combined/direct_import.jar",
Colin Cross4eae06d2023-06-20 22:40:02 -07001192 },
1193
1194 directResources: nil,
1195 directOverlays: []string{
1196 "out/soong/.intermediates/transitive/android_common/package-res.apk",
1197 "out/soong/.intermediates/transitive_import_dep/android_common/package-res.apk",
1198 "out/soong/.intermediates/transitive_import/android_common/package-res.apk",
1199 "out/soong/.intermediates/direct/android_common/aapt2/direct/res/values_strings.arsc.flat",
1200 },
1201 directImports: []string{"out/soong/.intermediates/default/java/framework-res/android_common/package-res.apk"},
1202 directSrcJars: []string{"out/soong/.intermediates/direct/android_common/gen/android/R.srcjar"},
1203 directClasspath: []string{
1204 "out/soong/.intermediates/default/java/android_stubs_current/android_common/turbine-combined/android_stubs_current.jar",
1205 "out/soong/.intermediates/transitive/android_common/turbine-combined/transitive.jar",
Colin Cross9055e212024-03-23 04:43:41 +00001206 "out/soong/.intermediates/transitive_import/android_common/turbine-combined/transitive_import.jar",
Colin Cross4eae06d2023-06-20 22:40:02 -07001207 },
1208 directCombined: []string{
1209 "out/soong/.intermediates/direct/android_common/javac/direct.jar",
1210 "out/soong/.intermediates/transitive/android_common/javac/transitive.jar",
Colin Cross9055e212024-03-23 04:43:41 +00001211 "out/soong/.intermediates/transitive_import/android_common/combined/transitive_import.jar",
Colin Cross4eae06d2023-06-20 22:40:02 -07001212 },
1213
1214 transitiveResources: []string{"out/soong/.intermediates/transitive/android_common/aapt2/transitive/res/values_strings.arsc.flat"},
1215 transitiveOverlays: nil,
1216 transitiveImports: []string{"out/soong/.intermediates/default/java/framework-res/android_common/package-res.apk"},
1217 transitiveSrcJars: nil,
1218 transitiveClasspath: []string{
1219 "out/soong/.intermediates/default/java/android_stubs_current/android_common/turbine-combined/android_stubs_current.jar",
1220 "out/soong/.intermediates/transitive/android_common/busybox/R.jar",
1221 },
1222 transitiveCombined: nil,
1223
Colin Cross8676c8c2023-10-12 15:58:57 -07001224 dontVerifyShared: true,
Colin Cross4eae06d2023-06-20 22:40:02 -07001225 dontVerifyDirectImport: true,
1226 dontVerifyTransitiveImport: true,
Colin Crossff3ff7f2023-07-05 14:04:12 -07001227 },
1228 }
1229
1230 for _, testCase := range testCases {
1231 t.Run(testCase.name, func(t *testing.T) {
1232 bp := fmt.Sprintf(`
1233 android_app {
1234 name: "app",
1235 sdk_version: "current",
1236 srcs: ["app/app.java"],
1237 resource_dirs: ["app/res"],
1238 manifest: "app/AndroidManifest.xml",
Colin Cross8676c8c2023-10-12 15:58:57 -07001239 libs: ["shared"],
Colin Crossff3ff7f2023-07-05 14:04:12 -07001240 static_libs: ["direct", "direct_import"],
Colin Cross4eae06d2023-06-20 22:40:02 -07001241 use_resource_processor: %v,
Colin Crossff3ff7f2023-07-05 14:04:12 -07001242 }
1243
1244 android_library {
1245 name: "direct",
1246 sdk_version: "current",
1247 srcs: ["direct/direct.java"],
1248 resource_dirs: ["direct/res"],
1249 manifest: "direct/AndroidManifest.xml",
1250 static_libs: ["transitive", "transitive_import"],
Colin Cross4eae06d2023-06-20 22:40:02 -07001251 use_resource_processor: %v,
Colin Crossff3ff7f2023-07-05 14:04:12 -07001252 }
1253
1254 android_library {
1255 name: "transitive",
1256 sdk_version: "current",
1257 srcs: ["transitive/transitive.java"],
1258 resource_dirs: ["transitive/res"],
1259 manifest: "transitive/AndroidManifest.xml",
Colin Cross4eae06d2023-06-20 22:40:02 -07001260 use_resource_processor: %v,
Colin Crossff3ff7f2023-07-05 14:04:12 -07001261 }
1262
Colin Cross8676c8c2023-10-12 15:58:57 -07001263 android_library {
1264 name: "shared",
1265 sdk_version: "current",
1266 srcs: ["shared/shared.java"],
1267 resource_dirs: ["shared/res"],
1268 manifest: "shared/AndroidManifest.xml",
1269 use_resource_processor: %v,
1270 libs: ["shared_transitive_shared"],
1271 static_libs: ["shared_transitive_static"],
1272 }
1273
1274 android_library {
1275 name: "shared_transitive_shared",
1276 sdk_version: "current",
1277 srcs: ["shared_transitive_shared/shared_transitive_shared.java"],
1278 resource_dirs: ["shared_transitive_shared/res"],
1279 manifest: "shared_transitive_shared/AndroidManifest.xml",
1280 use_resource_processor: %v,
1281 }
1282
1283 android_library {
1284 name: "shared_transitive_static",
1285 sdk_version: "current",
1286 srcs: ["shared_transitive_static/shared.java"],
1287 resource_dirs: ["shared_transitive_static/res"],
1288 manifest: "shared_transitive_static/AndroidManifest.xml",
1289 use_resource_processor: %v,
1290 }
1291
Colin Crossff3ff7f2023-07-05 14:04:12 -07001292 android_library_import {
1293 name: "direct_import",
1294 sdk_version: "current",
1295 aars: ["direct_import.aar"],
1296 static_libs: ["direct_import_dep"],
1297 }
1298
1299 android_library_import {
1300 name: "direct_import_dep",
1301 sdk_version: "current",
1302 aars: ["direct_import_dep.aar"],
1303 }
1304
1305 android_library_import {
1306 name: "transitive_import",
1307 sdk_version: "current",
1308 aars: ["transitive_import.aar"],
1309 static_libs: ["transitive_import_dep"],
1310 }
1311
1312 android_library_import {
1313 name: "transitive_import_dep",
1314 sdk_version: "current",
1315 aars: ["transitive_import_dep.aar"],
1316 }
Colin Cross8676c8c2023-10-12 15:58:57 -07001317 `, testCase.appUsesRP, testCase.directLibUsesRP, testCase.transitiveLibUsesRP,
1318 testCase.sharedLibUsesRP, testCase.sharedTransitiveSharedLibUsesRP, testCase.sharedTransitiveStaticLibUsesRP)
Colin Crossff3ff7f2023-07-05 14:04:12 -07001319
1320 fs := android.MockFS{
Colin Cross8676c8c2023-10-12 15:58:57 -07001321 "app/res/values/strings.xml": nil,
1322 "direct/res/values/strings.xml": nil,
1323 "transitive/res/values/strings.xml": nil,
1324 "shared/res/values/strings.xml": nil,
1325 "shared_transitive_static/res/values/strings.xml": nil,
1326 "shared_transitive_shared/res/values/strings.xml": nil,
Colin Crossff3ff7f2023-07-05 14:04:12 -07001327 }
1328
1329 result := android.GroupFixturePreparers(
1330 PrepareForTestWithJavaDefaultModules,
Colin Crossff3ff7f2023-07-05 14:04:12 -07001331 fs.AddToFixture(),
1332 ).RunTestWithBp(t, bp)
1333
1334 type aaptInfo struct {
1335 resources, overlays, imports, srcJars, classpath, combined android.Paths
1336 }
1337
1338 getAaptInfo := func(moduleName string) (aaptInfo aaptInfo) {
1339 mod := result.ModuleForTests(moduleName, "android_common")
1340 resourceListRule := mod.MaybeOutput("aapt2/res.list")
1341 overlayListRule := mod.MaybeOutput("aapt2/overlay.list")
1342 aaptRule := mod.Rule("aapt2Link")
1343 javacRule := mod.MaybeRule("javac")
1344 combinedRule := mod.MaybeOutput("combined/" + moduleName + ".jar")
1345
1346 aaptInfo.resources = resourceListRule.Inputs
1347 aaptInfo.overlays = overlayListRule.Inputs
1348
1349 aaptFlags := strings.Split(aaptRule.Args["flags"], " ")
1350 for i, flag := range aaptFlags {
1351 if flag == "-I" && i+1 < len(aaptFlags) {
1352 aaptInfo.imports = append(aaptInfo.imports, android.PathForTesting(aaptFlags[i+1]))
1353 }
1354 }
1355
1356 if len(javacRule.Args["srcJars"]) > 0 {
1357 aaptInfo.srcJars = android.PathsForTesting(strings.Split(javacRule.Args["srcJars"], " ")...)
1358 }
1359
1360 if len(javacRule.Args["classpath"]) > 0 {
1361 classpathArg := strings.TrimPrefix(javacRule.Args["classpath"], "-classpath ")
1362 aaptInfo.classpath = android.PathsForTesting(strings.Split(classpathArg, ":")...)
1363 }
1364
1365 aaptInfo.combined = combinedRule.Inputs
1366 return
1367 }
1368
1369 app := getAaptInfo("app")
1370 direct := getAaptInfo("direct")
1371 transitive := getAaptInfo("transitive")
Colin Cross8676c8c2023-10-12 15:58:57 -07001372 shared := getAaptInfo("shared")
Colin Crossff3ff7f2023-07-05 14:04:12 -07001373 directImport := getAaptInfo("direct_import")
1374 transitiveImport := getAaptInfo("transitive_import")
1375
1376 if !testCase.dontVerifyApp {
1377 android.AssertPathsRelativeToTopEquals(t, "app resources", testCase.appResources, app.resources)
1378 android.AssertPathsRelativeToTopEquals(t, "app overlays", testCase.appOverlays, app.overlays)
1379 android.AssertPathsRelativeToTopEquals(t, "app imports", testCase.appImports, app.imports)
1380 android.AssertPathsRelativeToTopEquals(t, "app srcjars", testCase.appSrcJars, app.srcJars)
1381 android.AssertPathsRelativeToTopEquals(t, "app classpath", testCase.appClasspath, app.classpath)
1382 android.AssertPathsRelativeToTopEquals(t, "app combined", testCase.appCombined, app.combined)
1383 }
1384
1385 if !testCase.dontVerifyDirect {
1386 android.AssertPathsRelativeToTopEquals(t, "direct resources", testCase.directResources, direct.resources)
1387 android.AssertPathsRelativeToTopEquals(t, "direct overlays", testCase.directOverlays, direct.overlays)
1388 android.AssertPathsRelativeToTopEquals(t, "direct imports", testCase.directImports, direct.imports)
1389 android.AssertPathsRelativeToTopEquals(t, "direct srcjars", testCase.directSrcJars, direct.srcJars)
1390 android.AssertPathsRelativeToTopEquals(t, "direct classpath", testCase.directClasspath, direct.classpath)
1391 android.AssertPathsRelativeToTopEquals(t, "direct combined", testCase.directCombined, direct.combined)
1392 }
1393
1394 if !testCase.dontVerifyTransitive {
1395 android.AssertPathsRelativeToTopEquals(t, "transitive resources", testCase.transitiveResources, transitive.resources)
1396 android.AssertPathsRelativeToTopEquals(t, "transitive overlays", testCase.transitiveOverlays, transitive.overlays)
1397 android.AssertPathsRelativeToTopEquals(t, "transitive imports", testCase.transitiveImports, transitive.imports)
1398 android.AssertPathsRelativeToTopEquals(t, "transitive srcjars", testCase.transitiveSrcJars, transitive.srcJars)
1399 android.AssertPathsRelativeToTopEquals(t, "transitive classpath", testCase.transitiveClasspath, transitive.classpath)
1400 android.AssertPathsRelativeToTopEquals(t, "transitive combined", testCase.transitiveCombined, transitive.combined)
1401 }
1402
Colin Cross8676c8c2023-10-12 15:58:57 -07001403 if !testCase.dontVerifyShared {
1404 android.AssertPathsRelativeToTopEquals(t, "shared resources", testCase.sharedResources, shared.resources)
1405 android.AssertPathsRelativeToTopEquals(t, "shared overlays", testCase.sharedOverlays, shared.overlays)
1406 android.AssertPathsRelativeToTopEquals(t, "shared imports", testCase.sharedImports, shared.imports)
1407 android.AssertPathsRelativeToTopEquals(t, "shared srcjars", testCase.sharedSrcJars, shared.srcJars)
1408 android.AssertPathsRelativeToTopEquals(t, "shared classpath", testCase.sharedClasspath, shared.classpath)
1409 android.AssertPathsRelativeToTopEquals(t, "shared combined", testCase.sharedCombined, shared.combined)
1410 }
1411
Colin Crossff3ff7f2023-07-05 14:04:12 -07001412 if !testCase.dontVerifyDirectImport {
1413 android.AssertPathsRelativeToTopEquals(t, "direct_import resources", testCase.directImportResources, directImport.resources)
1414 android.AssertPathsRelativeToTopEquals(t, "direct_import overlays", testCase.directImportOverlays, directImport.overlays)
1415 android.AssertPathsRelativeToTopEquals(t, "direct_import imports", testCase.directImportImports, directImport.imports)
1416 }
1417
1418 if !testCase.dontVerifyTransitiveImport {
1419 android.AssertPathsRelativeToTopEquals(t, "transitive_import resources", testCase.transitiveImportResources, transitiveImport.resources)
1420 android.AssertPathsRelativeToTopEquals(t, "transitive_import overlays", testCase.transitiveImportOverlays, transitiveImport.overlays)
1421 android.AssertPathsRelativeToTopEquals(t, "transitive_import imports", testCase.transitiveImportImports, transitiveImport.imports)
1422 }
1423 })
1424 }
1425}
1426
1427func TestAndroidResourceOverlays(t *testing.T) {
Colin Cross5c4791c2019-02-01 11:44:44 -08001428 testCases := []struct {
1429 name string
1430 enforceRROTargets []string
1431 enforceRROExcludedOverlays []string
Colin Crossbec85302019-02-13 13:15:46 -08001432 resourceFiles map[string][]string
Colin Cross5c4791c2019-02-01 11:44:44 -08001433 overlayFiles map[string][]string
1434 rroDirs map[string][]string
1435 }{
1436 {
1437 name: "no RRO",
1438 enforceRROTargets: nil,
1439 enforceRROExcludedOverlays: nil,
Colin Crossbec85302019-02-13 13:15:46 -08001440 resourceFiles: map[string][]string{
1441 "foo": nil,
1442 "bar": {"bar/res/res/values/strings.xml"},
1443 "lib": nil,
1444 "lib2": {"lib2/res/res/values/strings.xml"},
1445 },
Colin Cross5c4791c2019-02-01 11:44:44 -08001446 overlayFiles: map[string][]string{
Colin Crossbec85302019-02-13 13:15:46 -08001447 "foo": {
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00001448 "out/soong/.intermediates/lib2/android_common/package-res.apk",
1449 "out/soong/.intermediates/lib/android_common/package-res.apk",
1450 "out/soong/.intermediates/lib3/android_common/package-res.apk",
Colin Cross6ed7dea2019-01-31 14:44:30 -08001451 "foo/res/res/values/strings.xml",
Colin Cross5c4791c2019-02-01 11:44:44 -08001452 "device/vendor/blah/static_overlay/foo/res/values/strings.xml",
1453 "device/vendor/blah/overlay/foo/res/values/strings.xml",
Anton Hansson53c88442019-03-18 15:53:16 +00001454 "product/vendor/blah/overlay/foo/res/values/strings.xml",
Colin Cross5c4791c2019-02-01 11:44:44 -08001455 },
Colin Crossbec85302019-02-13 13:15:46 -08001456 "bar": {
Colin Cross5c4791c2019-02-01 11:44:44 -08001457 "device/vendor/blah/static_overlay/bar/res/values/strings.xml",
1458 "device/vendor/blah/overlay/bar/res/values/strings.xml",
1459 },
Colin Crossbec85302019-02-13 13:15:46 -08001460 "lib": {
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00001461 "out/soong/.intermediates/lib2/android_common/package-res.apk",
Colin Crossbec85302019-02-13 13:15:46 -08001462 "lib/res/res/values/strings.xml",
1463 "device/vendor/blah/overlay/lib/res/values/strings.xml",
1464 },
Colin Cross5c4791c2019-02-01 11:44:44 -08001465 },
1466 rroDirs: map[string][]string{
1467 "foo": nil,
1468 "bar": nil,
1469 },
1470 },
1471 {
1472 name: "enforce RRO on foo",
1473 enforceRROTargets: []string{"foo"},
1474 enforceRROExcludedOverlays: []string{"device/vendor/blah/static_overlay"},
Colin Crossbec85302019-02-13 13:15:46 -08001475 resourceFiles: map[string][]string{
1476 "foo": nil,
1477 "bar": {"bar/res/res/values/strings.xml"},
1478 "lib": nil,
1479 "lib2": {"lib2/res/res/values/strings.xml"},
1480 },
Colin Cross5c4791c2019-02-01 11:44:44 -08001481 overlayFiles: map[string][]string{
Colin Crossbec85302019-02-13 13:15:46 -08001482 "foo": {
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00001483 "out/soong/.intermediates/lib2/android_common/package-res.apk",
1484 "out/soong/.intermediates/lib/android_common/package-res.apk",
1485 "out/soong/.intermediates/lib3/android_common/package-res.apk",
Colin Cross6ed7dea2019-01-31 14:44:30 -08001486 "foo/res/res/values/strings.xml",
1487 "device/vendor/blah/static_overlay/foo/res/values/strings.xml",
1488 },
Colin Crossbec85302019-02-13 13:15:46 -08001489 "bar": {
Colin Cross5c4791c2019-02-01 11:44:44 -08001490 "device/vendor/blah/static_overlay/bar/res/values/strings.xml",
1491 "device/vendor/blah/overlay/bar/res/values/strings.xml",
1492 },
Colin Crossbec85302019-02-13 13:15:46 -08001493 "lib": {
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00001494 "out/soong/.intermediates/lib2/android_common/package-res.apk",
Colin Crossbec85302019-02-13 13:15:46 -08001495 "lib/res/res/values/strings.xml",
Colin Crossbec85302019-02-13 13:15:46 -08001496 },
Colin Cross5c4791c2019-02-01 11:44:44 -08001497 },
Colin Crossc1c37552019-01-31 11:42:41 -08001498
Colin Cross5c4791c2019-02-01 11:44:44 -08001499 rroDirs: map[string][]string{
Colin Crossbec85302019-02-13 13:15:46 -08001500 "foo": {
Anton Hansson53c88442019-03-18 15:53:16 +00001501 "device:device/vendor/blah/overlay/foo/res",
Anton Hansson53c88442019-03-18 15:53:16 +00001502 "product:product/vendor/blah/overlay/foo/res",
Jaewoong Jungc779cd42020-10-06 18:56:10 -07001503 "device:device/vendor/blah/overlay/lib/res",
Colin Crossc1c37552019-01-31 11:42:41 -08001504 },
Colin Cross5c4791c2019-02-01 11:44:44 -08001505 "bar": nil,
Jaewoong Jungc779cd42020-10-06 18:56:10 -07001506 "lib": {"device:device/vendor/blah/overlay/lib/res"},
Colin Cross5c4791c2019-02-01 11:44:44 -08001507 },
1508 },
1509 {
1510 name: "enforce RRO on all",
1511 enforceRROTargets: []string{"*"},
1512 enforceRROExcludedOverlays: []string{
1513 // Excluding specific apps/res directories also allowed.
1514 "device/vendor/blah/static_overlay/foo",
1515 "device/vendor/blah/static_overlay/bar/res",
1516 },
Colin Crossbec85302019-02-13 13:15:46 -08001517 resourceFiles: map[string][]string{
1518 "foo": nil,
1519 "bar": {"bar/res/res/values/strings.xml"},
1520 "lib": nil,
1521 "lib2": {"lib2/res/res/values/strings.xml"},
1522 },
Colin Cross5c4791c2019-02-01 11:44:44 -08001523 overlayFiles: map[string][]string{
Colin Crossbec85302019-02-13 13:15:46 -08001524 "foo": {
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00001525 "out/soong/.intermediates/lib2/android_common/package-res.apk",
1526 "out/soong/.intermediates/lib/android_common/package-res.apk",
1527 "out/soong/.intermediates/lib3/android_common/package-res.apk",
Colin Cross6ed7dea2019-01-31 14:44:30 -08001528 "foo/res/res/values/strings.xml",
1529 "device/vendor/blah/static_overlay/foo/res/values/strings.xml",
1530 },
Colin Crossbec85302019-02-13 13:15:46 -08001531 "bar": {"device/vendor/blah/static_overlay/bar/res/values/strings.xml"},
1532 "lib": {
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00001533 "out/soong/.intermediates/lib2/android_common/package-res.apk",
Colin Crossbec85302019-02-13 13:15:46 -08001534 "lib/res/res/values/strings.xml",
1535 },
Colin Cross5c4791c2019-02-01 11:44:44 -08001536 },
1537 rroDirs: map[string][]string{
Colin Crossbec85302019-02-13 13:15:46 -08001538 "foo": {
Anton Hansson53c88442019-03-18 15:53:16 +00001539 "device:device/vendor/blah/overlay/foo/res",
1540 "product:product/vendor/blah/overlay/foo/res",
1541 // Lib dep comes after the direct deps
1542 "device:device/vendor/blah/overlay/lib/res",
Colin Crossc1c37552019-01-31 11:42:41 -08001543 },
Anton Hansson53c88442019-03-18 15:53:16 +00001544 "bar": {"device:device/vendor/blah/overlay/bar/res"},
1545 "lib": {"device:device/vendor/blah/overlay/lib/res"},
Colin Cross5c4791c2019-02-01 11:44:44 -08001546 },
1547 },
1548 }
1549
Anton Hansson53c88442019-03-18 15:53:16 +00001550 deviceResourceOverlays := []string{
Colin Cross890ff552017-11-30 20:13:19 -08001551 "device/vendor/blah/overlay",
1552 "device/vendor/blah/overlay2",
1553 "device/vendor/blah/static_overlay",
1554 }
1555
Anton Hansson53c88442019-03-18 15:53:16 +00001556 productResourceOverlays := []string{
1557 "product/vendor/blah/overlay",
1558 }
1559
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00001560 fs := android.MockFS{
Colin Cross890ff552017-11-30 20:13:19 -08001561 "foo/res/res/values/strings.xml": nil,
1562 "bar/res/res/values/strings.xml": nil,
Colin Cross6ed7dea2019-01-31 14:44:30 -08001563 "lib/res/res/values/strings.xml": nil,
Colin Crossbec85302019-02-13 13:15:46 -08001564 "lib2/res/res/values/strings.xml": nil,
Colin Cross890ff552017-11-30 20:13:19 -08001565 "device/vendor/blah/overlay/foo/res/values/strings.xml": nil,
1566 "device/vendor/blah/overlay/bar/res/values/strings.xml": nil,
Colin Cross6ed7dea2019-01-31 14:44:30 -08001567 "device/vendor/blah/overlay/lib/res/values/strings.xml": nil,
Colin Cross890ff552017-11-30 20:13:19 -08001568 "device/vendor/blah/static_overlay/foo/res/values/strings.xml": nil,
1569 "device/vendor/blah/static_overlay/bar/res/values/strings.xml": nil,
1570 "device/vendor/blah/overlay2/res/values/strings.xml": nil,
Anton Hansson53c88442019-03-18 15:53:16 +00001571 "product/vendor/blah/overlay/foo/res/values/strings.xml": nil,
Colin Cross890ff552017-11-30 20:13:19 -08001572 }
1573
1574 bp := `
1575 android_app {
1576 name: "foo",
Jeongik Cha538c0d02019-07-11 15:54:27 +09001577 sdk_version: "current",
Colin Cross890ff552017-11-30 20:13:19 -08001578 resource_dirs: ["foo/res"],
Anton Hansson53c88442019-03-18 15:53:16 +00001579 static_libs: ["lib", "lib3"],
Colin Cross890ff552017-11-30 20:13:19 -08001580 }
1581
1582 android_app {
1583 name: "bar",
Jeongik Cha538c0d02019-07-11 15:54:27 +09001584 sdk_version: "current",
Colin Cross890ff552017-11-30 20:13:19 -08001585 resource_dirs: ["bar/res"],
1586 }
Colin Cross6ed7dea2019-01-31 14:44:30 -08001587
1588 android_library {
1589 name: "lib",
Jeongik Cha75b83b02019-11-01 15:28:00 +09001590 sdk_version: "current",
Colin Cross6ed7dea2019-01-31 14:44:30 -08001591 resource_dirs: ["lib/res"],
Colin Crossbec85302019-02-13 13:15:46 -08001592 static_libs: ["lib2"],
1593 }
1594
1595 android_library {
1596 name: "lib2",
Jeongik Cha75b83b02019-11-01 15:28:00 +09001597 sdk_version: "current",
Colin Crossbec85302019-02-13 13:15:46 -08001598 resource_dirs: ["lib2/res"],
Colin Cross6ed7dea2019-01-31 14:44:30 -08001599 }
Anton Hansson53c88442019-03-18 15:53:16 +00001600
1601 // This library has the same resources as lib (should not lead to dupe RROs)
1602 android_library {
1603 name: "lib3",
Jeongik Cha75b83b02019-11-01 15:28:00 +09001604 sdk_version: "current",
Anton Hansson53c88442019-03-18 15:53:16 +00001605 resource_dirs: ["lib/res"]
1606 }
Colin Cross890ff552017-11-30 20:13:19 -08001607 `
1608
Colin Cross5c4791c2019-02-01 11:44:44 -08001609 for _, testCase := range testCases {
Colin Cross890ff552017-11-30 20:13:19 -08001610 t.Run(testCase.name, func(t *testing.T) {
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00001611 result := android.GroupFixturePreparers(
1612 PrepareForTestWithJavaDefaultModules,
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00001613 fs.AddToFixture(),
1614 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1615 variables.DeviceResourceOverlays = deviceResourceOverlays
1616 variables.ProductResourceOverlays = productResourceOverlays
1617 if testCase.enforceRROTargets != nil {
1618 variables.EnforceRROTargets = testCase.enforceRROTargets
1619 }
1620 if testCase.enforceRROExcludedOverlays != nil {
1621 variables.EnforceRROExcludedOverlays = testCase.enforceRROExcludedOverlays
1622 }
1623 }),
1624 ).RunTestWithBp(t, bp)
Colin Cross890ff552017-11-30 20:13:19 -08001625
Colin Crossbec85302019-02-13 13:15:46 -08001626 resourceListToFiles := func(module android.TestingModule, list []string) (files []string) {
1627 for _, o := range list {
1628 res := module.MaybeOutput(o)
1629 if res.Rule != nil {
1630 // If the overlay is compiled as part of this module (i.e. a .arsc.flat file),
1631 // verify the inputs to the .arsc.flat rule.
1632 files = append(files, res.Inputs.Strings()...)
1633 } else {
1634 // Otherwise, verify the full path to the output of the other module
1635 files = append(files, o)
Anton Hansson94c93f32019-01-30 16:03:37 +00001636 }
Colin Cross890ff552017-11-30 20:13:19 -08001637 }
Colin Crossbec85302019-02-13 13:15:46 -08001638 return files
Colin Cross890ff552017-11-30 20:13:19 -08001639 }
1640
Colin Crossbec85302019-02-13 13:15:46 -08001641 getResources := func(moduleName string) (resourceFiles, overlayFiles, rroDirs []string) {
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00001642 module := result.ModuleForTests(moduleName, "android_common")
Colin Crossbec85302019-02-13 13:15:46 -08001643 resourceList := module.MaybeOutput("aapt2/res.list")
1644 if resourceList.Rule != nil {
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00001645 resourceFiles = resourceListToFiles(module, android.PathsRelativeToTop(resourceList.Inputs))
Anton Hansson0375a4f2019-01-24 14:39:19 +00001646 }
Colin Crossbec85302019-02-13 13:15:46 -08001647 overlayList := module.MaybeOutput("aapt2/overlay.list")
1648 if overlayList.Rule != nil {
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00001649 overlayFiles = resourceListToFiles(module, android.PathsRelativeToTop(overlayList.Inputs))
Colin Crossbec85302019-02-13 13:15:46 -08001650 }
1651
Colin Crossab8d1382023-07-14 17:23:41 +00001652 for _, d := range module.Module().(AndroidLibraryDependency).RRODirsDepSet().ToList() {
Anton Hansson53c88442019-03-18 15:53:16 +00001653 var prefix string
1654 if d.overlayType == device {
1655 prefix = "device:"
1656 } else if d.overlayType == product {
1657 prefix = "product:"
1658 } else {
1659 t.Fatalf("Unexpected overlayType %d", d.overlayType)
1660 }
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00001661 rroDirs = append(rroDirs, prefix+android.PathRelativeToTop(d.path))
Anton Hansson53c88442019-03-18 15:53:16 +00001662 }
Colin Crossbec85302019-02-13 13:15:46 -08001663
1664 return resourceFiles, overlayFiles, rroDirs
1665 }
1666
1667 modules := []string{"foo", "bar", "lib", "lib2"}
1668 for _, module := range modules {
1669 resourceFiles, overlayFiles, rroDirs := getResources(module)
1670
1671 if !reflect.DeepEqual(resourceFiles, testCase.resourceFiles[module]) {
1672 t.Errorf("expected %s resource files:\n %#v\n got:\n %#v",
1673 module, testCase.resourceFiles[module], resourceFiles)
1674 }
1675 if !reflect.DeepEqual(overlayFiles, testCase.overlayFiles[module]) {
1676 t.Errorf("expected %s overlay files:\n %#v\n got:\n %#v",
1677 module, testCase.overlayFiles[module], overlayFiles)
1678 }
1679 if !reflect.DeepEqual(rroDirs, testCase.rroDirs[module]) {
Anton Hansson0375a4f2019-01-24 14:39:19 +00001680 t.Errorf("expected %s rroDirs: %#v\n got:\n %#v",
Colin Crossbec85302019-02-13 13:15:46 -08001681 module, testCase.rroDirs[module], rroDirs)
Anton Hansson0375a4f2019-01-24 14:39:19 +00001682 }
Colin Cross890ff552017-11-30 20:13:19 -08001683 }
Colin Cross890ff552017-11-30 20:13:19 -08001684 })
1685 }
1686}
Colin Crossd09b0b62018-04-18 11:06:47 -07001687
Paul Duffincdb88a92021-03-14 00:36:50 +00001688func checkSdkVersion(t *testing.T, result *android.TestResult, expectedSdkVersion string) {
1689 foo := result.ModuleForTests("foo", "android_common")
Jeongik Cha219141c2020-08-06 23:00:37 +09001690 link := foo.Output("package-res.apk")
1691 linkFlags := strings.Split(link.Args["flags"], " ")
1692 min := android.IndexList("--min-sdk-version", linkFlags)
1693 target := android.IndexList("--target-sdk-version", linkFlags)
1694
1695 if min == -1 || target == -1 || min == len(linkFlags)-1 || target == len(linkFlags)-1 {
1696 t.Fatalf("missing --min-sdk-version or --target-sdk-version in link flags: %q", linkFlags)
1697 }
1698
1699 gotMinSdkVersion := linkFlags[min+1]
1700 gotTargetSdkVersion := linkFlags[target+1]
1701
Paul Duffincdb88a92021-03-14 00:36:50 +00001702 android.AssertStringEquals(t, "incorrect --min-sdk-version", expectedSdkVersion, gotMinSdkVersion)
Jeongik Cha219141c2020-08-06 23:00:37 +09001703
Paul Duffincdb88a92021-03-14 00:36:50 +00001704 android.AssertStringEquals(t, "incorrect --target-sdk-version", expectedSdkVersion, gotTargetSdkVersion)
Jeongik Cha219141c2020-08-06 23:00:37 +09001705}
1706
Colin Crossd09b0b62018-04-18 11:06:47 -07001707func TestAppSdkVersion(t *testing.T) {
1708 testCases := []struct {
1709 name string
1710 sdkVersion string
1711 platformSdkInt int
1712 platformSdkCodename string
1713 platformSdkFinal bool
Spandan Dasffb31af2023-03-01 19:46:18 +00001714 minSdkVersionBp string
Colin Crossd09b0b62018-04-18 11:06:47 -07001715 expectedMinSdkVersion string
Jeongik Cha538c0d02019-07-11 15:54:27 +09001716 platformApis bool
Dan Albert4f378d72020-07-23 17:32:15 -07001717 activeCodenames []string
Colin Crossd09b0b62018-04-18 11:06:47 -07001718 }{
1719 {
1720 name: "current final SDK",
1721 sdkVersion: "current",
1722 platformSdkInt: 27,
1723 platformSdkCodename: "REL",
1724 platformSdkFinal: true,
1725 expectedMinSdkVersion: "27",
1726 },
1727 {
1728 name: "current non-final SDK",
1729 sdkVersion: "current",
1730 platformSdkInt: 27,
1731 platformSdkCodename: "OMR1",
1732 platformSdkFinal: false,
1733 expectedMinSdkVersion: "OMR1",
Dan Albert4f378d72020-07-23 17:32:15 -07001734 activeCodenames: []string{"OMR1"},
Colin Crossd09b0b62018-04-18 11:06:47 -07001735 },
1736 {
1737 name: "default final SDK",
1738 sdkVersion: "",
Jeongik Cha538c0d02019-07-11 15:54:27 +09001739 platformApis: true,
Colin Crossd09b0b62018-04-18 11:06:47 -07001740 platformSdkInt: 27,
1741 platformSdkCodename: "REL",
1742 platformSdkFinal: true,
1743 expectedMinSdkVersion: "27",
1744 },
1745 {
1746 name: "default non-final SDK",
1747 sdkVersion: "",
Jeongik Cha538c0d02019-07-11 15:54:27 +09001748 platformApis: true,
Colin Crossd09b0b62018-04-18 11:06:47 -07001749 platformSdkInt: 27,
1750 platformSdkCodename: "OMR1",
1751 platformSdkFinal: false,
1752 expectedMinSdkVersion: "OMR1",
Dan Albert4f378d72020-07-23 17:32:15 -07001753 activeCodenames: []string{"OMR1"},
Colin Crossd09b0b62018-04-18 11:06:47 -07001754 },
1755 {
1756 name: "14",
1757 sdkVersion: "14",
1758 expectedMinSdkVersion: "14",
Dan Albert4f378d72020-07-23 17:32:15 -07001759 platformSdkCodename: "S",
1760 activeCodenames: []string{"S"},
Colin Crossd09b0b62018-04-18 11:06:47 -07001761 },
Spandan Dasffb31af2023-03-01 19:46:18 +00001762 {
1763 name: "two active SDKs",
1764 sdkVersion: "module_current",
1765 minSdkVersionBp: "UpsideDownCake",
1766 expectedMinSdkVersion: "UpsideDownCake", // And not VanillaIceCream
1767 platformSdkCodename: "VanillaIceCream",
1768 activeCodenames: []string{"UpsideDownCake", "VanillaIceCream"},
1769 },
Colin Crossd09b0b62018-04-18 11:06:47 -07001770 }
1771
1772 for _, moduleType := range []string{"android_app", "android_library"} {
1773 for _, test := range testCases {
1774 t.Run(moduleType+" "+test.name, func(t *testing.T) {
Jeongik Cha538c0d02019-07-11 15:54:27 +09001775 platformApiProp := ""
1776 if test.platformApis {
1777 platformApiProp = "platform_apis: true,"
1778 }
Spandan Dasffb31af2023-03-01 19:46:18 +00001779 minSdkVersionProp := ""
1780 if test.minSdkVersionBp != "" {
1781 minSdkVersionProp = fmt.Sprintf(` min_sdk_version: "%s",`, test.minSdkVersionBp)
1782 }
Colin Crossd09b0b62018-04-18 11:06:47 -07001783 bp := fmt.Sprintf(`%s {
1784 name: "foo",
1785 srcs: ["a.java"],
1786 sdk_version: "%s",
Jeongik Cha538c0d02019-07-11 15:54:27 +09001787 %s
Spandan Dasffb31af2023-03-01 19:46:18 +00001788 %s
1789 }`, moduleType, test.sdkVersion, platformApiProp, minSdkVersionProp)
Colin Crossd09b0b62018-04-18 11:06:47 -07001790
Paul Duffin71ae5942021-03-22 15:36:52 +00001791 result := android.GroupFixturePreparers(
1792 prepareForJavaTest,
Paul Duffincdb88a92021-03-14 00:36:50 +00001793 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1794 variables.Platform_sdk_version = &test.platformSdkInt
1795 variables.Platform_sdk_codename = &test.platformSdkCodename
1796 variables.Platform_version_active_codenames = test.activeCodenames
1797 variables.Platform_sdk_final = &test.platformSdkFinal
1798 }),
Paul Duffin2645a292021-03-13 02:36:00 +00001799 FixtureWithPrebuiltApis(map[string][]string{
1800 "14": {"foo"},
1801 }),
Paul Duffincdb88a92021-03-14 00:36:50 +00001802 ).RunTestWithBp(t, bp)
Colin Crossd09b0b62018-04-18 11:06:47 -07001803
Paul Duffincdb88a92021-03-14 00:36:50 +00001804 checkSdkVersion(t, result, test.expectedMinSdkVersion)
Colin Crossd09b0b62018-04-18 11:06:47 -07001805 })
1806 }
1807 }
1808}
Colin Crossa4f08812018-10-02 22:03:40 -07001809
Jeongik Cha219141c2020-08-06 23:00:37 +09001810func TestVendorAppSdkVersion(t *testing.T) {
1811 testCases := []struct {
1812 name string
1813 sdkVersion string
1814 platformSdkInt int
1815 platformSdkCodename string
1816 platformSdkFinal bool
1817 deviceCurrentApiLevelForVendorModules string
1818 expectedMinSdkVersion string
1819 }{
1820 {
1821 name: "current final SDK",
1822 sdkVersion: "current",
1823 platformSdkInt: 29,
1824 platformSdkCodename: "REL",
1825 platformSdkFinal: true,
1826 deviceCurrentApiLevelForVendorModules: "29",
1827 expectedMinSdkVersion: "29",
1828 },
1829 {
1830 name: "current final SDK",
1831 sdkVersion: "current",
1832 platformSdkInt: 29,
1833 platformSdkCodename: "REL",
1834 platformSdkFinal: true,
1835 deviceCurrentApiLevelForVendorModules: "28",
1836 expectedMinSdkVersion: "28",
1837 },
1838 {
1839 name: "current final SDK",
1840 sdkVersion: "current",
1841 platformSdkInt: 29,
1842 platformSdkCodename: "Q",
1843 platformSdkFinal: false,
Jeongik Cha219141c2020-08-06 23:00:37 +09001844 deviceCurrentApiLevelForVendorModules: "28",
1845 expectedMinSdkVersion: "28",
1846 },
1847 }
1848
1849 for _, moduleType := range []string{"android_app", "android_library"} {
1850 for _, sdkKind := range []string{"", "system_"} {
1851 for _, test := range testCases {
1852 t.Run(moduleType+" "+test.name, func(t *testing.T) {
1853 bp := fmt.Sprintf(`%s {
1854 name: "foo",
1855 srcs: ["a.java"],
1856 sdk_version: "%s%s",
1857 vendor: true,
1858 }`, moduleType, sdkKind, test.sdkVersion)
1859
Paul Duffin71ae5942021-03-22 15:36:52 +00001860 result := android.GroupFixturePreparers(
1861 prepareForJavaTest,
Paul Duffincdb88a92021-03-14 00:36:50 +00001862 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1863 variables.Platform_sdk_version = &test.platformSdkInt
1864 variables.Platform_sdk_codename = &test.platformSdkCodename
1865 variables.Platform_sdk_final = &test.platformSdkFinal
1866 variables.DeviceCurrentApiLevelForVendorModules = &test.deviceCurrentApiLevelForVendorModules
1867 variables.DeviceSystemSdkVersions = []string{"28", "29"}
1868 }),
Paul Duffin2645a292021-03-13 02:36:00 +00001869 FixtureWithPrebuiltApis(map[string][]string{
1870 "28": {"foo"},
1871 "29": {"foo"},
1872 "current": {"foo"},
1873 }),
Paul Duffincdb88a92021-03-14 00:36:50 +00001874 ).RunTestWithBp(t, bp)
1875
1876 checkSdkVersion(t, result, test.expectedMinSdkVersion)
Jeongik Cha219141c2020-08-06 23:00:37 +09001877 })
1878 }
1879 }
1880 }
1881}
1882
Paul Duffin50c217c2019-06-12 13:25:22 +01001883func TestJNIABI(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001884 ctx, _ := testJava(t, cc.GatherRequiredDepsForTest(android.Android)+`
Paul Duffin50c217c2019-06-12 13:25:22 +01001885 cc_library {
1886 name: "libjni",
1887 system_shared_libs: [],
Colin Crossc511bc52020-04-07 16:50:32 +00001888 sdk_version: "current",
Paul Duffin50c217c2019-06-12 13:25:22 +01001889 stl: "none",
1890 }
1891
1892 android_test {
1893 name: "test",
1894 sdk_version: "core_platform",
1895 jni_libs: ["libjni"],
1896 }
1897
1898 android_test {
1899 name: "test_first",
1900 sdk_version: "core_platform",
1901 compile_multilib: "first",
1902 jni_libs: ["libjni"],
1903 }
1904
1905 android_test {
1906 name: "test_both",
1907 sdk_version: "core_platform",
1908 compile_multilib: "both",
1909 jni_libs: ["libjni"],
1910 }
1911
1912 android_test {
1913 name: "test_32",
1914 sdk_version: "core_platform",
1915 compile_multilib: "32",
1916 jni_libs: ["libjni"],
1917 }
1918
1919 android_test {
1920 name: "test_64",
1921 sdk_version: "core_platform",
1922 compile_multilib: "64",
1923 jni_libs: ["libjni"],
1924 }
1925 `)
1926
1927 testCases := []struct {
1928 name string
1929 abis []string
1930 }{
1931 {"test", []string{"arm64-v8a"}},
1932 {"test_first", []string{"arm64-v8a"}},
1933 {"test_both", []string{"arm64-v8a", "armeabi-v7a"}},
1934 {"test_32", []string{"armeabi-v7a"}},
1935 {"test_64", []string{"arm64-v8a"}},
1936 }
1937
1938 for _, test := range testCases {
1939 t.Run(test.name, func(t *testing.T) {
1940 app := ctx.ModuleForTests(test.name, "android_common")
Colin Crossb3168ba2023-07-26 16:14:56 -07001941 jniLibZip := app.Output("jnilibs.zip")
Paul Duffin50c217c2019-06-12 13:25:22 +01001942 var abis []string
1943 args := strings.Fields(jniLibZip.Args["jarArgs"])
1944 for i := 0; i < len(args); i++ {
1945 if args[i] == "-P" {
1946 abis = append(abis, filepath.Base(args[i+1]))
1947 i++
1948 }
1949 }
1950 if !reflect.DeepEqual(abis, test.abis) {
1951 t.Errorf("want abis %v, got %v", test.abis, abis)
1952 }
1953 })
1954 }
1955}
1956
Jeongik Cha2cc570d2019-10-29 15:44:45 +09001957func TestAppSdkVersionByPartition(t *testing.T) {
1958 testJavaError(t, "sdk_version must have a value when the module is located at vendor or product", `
1959 android_app {
1960 name: "foo",
1961 srcs: ["a.java"],
1962 vendor: true,
1963 platform_apis: true,
1964 }
1965 `)
1966
1967 testJava(t, `
1968 android_app {
1969 name: "bar",
1970 srcs: ["b.java"],
1971 platform_apis: true,
1972 }
1973 `)
1974
1975 for _, enforce := range []bool{true, false} {
Jeongik Cha2cc570d2019-10-29 15:44:45 +09001976 bp := `
1977 android_app {
1978 name: "foo",
1979 srcs: ["a.java"],
1980 product_specific: true,
1981 platform_apis: true,
1982 }
1983 `
Colin Cross98be1bb2019-12-13 20:41:13 -08001984
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00001985 errorHandler := android.FixtureExpectsNoErrors
Jeongik Cha2cc570d2019-10-29 15:44:45 +09001986 if enforce {
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00001987 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 +09001988 }
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00001989
1990 android.GroupFixturePreparers(
1991 PrepareForTestWithJavaDefaultModules,
1992 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1993 variables.EnforceProductPartitionInterface = proptools.BoolPtr(enforce)
1994 }),
1995 ).
1996 ExtendWithErrorHandler(errorHandler).
1997 RunTestWithBp(t, bp)
Jeongik Cha2cc570d2019-10-29 15:44:45 +09001998 }
1999}
2000
Paul Duffin50c217c2019-06-12 13:25:22 +01002001func TestJNIPackaging(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07002002 ctx, _ := testJava(t, cc.GatherRequiredDepsForTest(android.Android)+`
Paul Duffin50c217c2019-06-12 13:25:22 +01002003 cc_library {
2004 name: "libjni",
2005 system_shared_libs: [],
2006 stl: "none",
Colin Cross094cde42020-02-15 10:38:00 -08002007 sdk_version: "current",
Paul Duffin50c217c2019-06-12 13:25:22 +01002008 }
2009
2010 android_app {
2011 name: "app",
2012 jni_libs: ["libjni"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09002013 sdk_version: "current",
Paul Duffin50c217c2019-06-12 13:25:22 +01002014 }
2015
2016 android_app {
2017 name: "app_noembed",
2018 jni_libs: ["libjni"],
2019 use_embedded_native_libs: false,
Jeongik Cha538c0d02019-07-11 15:54:27 +09002020 sdk_version: "current",
Paul Duffin50c217c2019-06-12 13:25:22 +01002021 }
2022
2023 android_app {
2024 name: "app_embed",
2025 jni_libs: ["libjni"],
2026 use_embedded_native_libs: true,
Jeongik Cha538c0d02019-07-11 15:54:27 +09002027 sdk_version: "current",
Paul Duffin50c217c2019-06-12 13:25:22 +01002028 }
2029
2030 android_test {
2031 name: "test",
Colin Crossc511bc52020-04-07 16:50:32 +00002032 sdk_version: "current",
Paul Duffin50c217c2019-06-12 13:25:22 +01002033 jni_libs: ["libjni"],
2034 }
2035
2036 android_test {
2037 name: "test_noembed",
Colin Crossc511bc52020-04-07 16:50:32 +00002038 sdk_version: "current",
Paul Duffin50c217c2019-06-12 13:25:22 +01002039 jni_libs: ["libjni"],
2040 use_embedded_native_libs: false,
2041 }
2042
2043 android_test_helper_app {
2044 name: "test_helper",
Colin Crossc511bc52020-04-07 16:50:32 +00002045 sdk_version: "current",
Paul Duffin50c217c2019-06-12 13:25:22 +01002046 jni_libs: ["libjni"],
2047 }
2048
2049 android_test_helper_app {
2050 name: "test_helper_noembed",
Colin Crossc511bc52020-04-07 16:50:32 +00002051 sdk_version: "current",
Paul Duffin50c217c2019-06-12 13:25:22 +01002052 jni_libs: ["libjni"],
2053 use_embedded_native_libs: false,
2054 }
2055 `)
2056
2057 testCases := []struct {
2058 name string
2059 packaged bool
2060 compressed bool
2061 }{
Jiyong Parkd044bb42024-05-15 02:09:54 +09002062 {"app", false, false},
2063 {"app_noembed", false, false},
Paul Duffin50c217c2019-06-12 13:25:22 +01002064 {"app_embed", true, false},
2065 {"test", true, false},
2066 {"test_noembed", true, true},
2067 {"test_helper", true, false},
2068 {"test_helper_noembed", true, true},
2069 }
2070
2071 for _, test := range testCases {
2072 t.Run(test.name, func(t *testing.T) {
2073 app := ctx.ModuleForTests(test.name, "android_common")
Colin Crossb3168ba2023-07-26 16:14:56 -07002074 jniLibZip := app.MaybeOutput("jnilibs.zip")
Paul Duffin50c217c2019-06-12 13:25:22 +01002075 if g, w := (jniLibZip.Rule != nil), test.packaged; g != w {
2076 t.Errorf("expected jni packaged %v, got %v", w, g)
2077 }
2078
2079 if jniLibZip.Rule != nil {
2080 if g, w := !strings.Contains(jniLibZip.Args["jarArgs"], "-L 0"), test.compressed; g != w {
2081 t.Errorf("expected jni compressed %v, got %v", w, g)
2082 }
Colin Crossc511bc52020-04-07 16:50:32 +00002083
2084 if !strings.Contains(jniLibZip.Implicits[0].String(), "_sdk_") {
2085 t.Errorf("expected input %q to use sdk variant", jniLibZip.Implicits[0].String())
2086 }
Paul Duffin50c217c2019-06-12 13:25:22 +01002087 }
2088 })
2089 }
Colin Cross47fa9d32019-03-26 10:51:39 -07002090}
2091
Colin Cross3c007702020-05-08 11:20:24 -07002092func TestJNISDK(t *testing.T) {
2093 ctx, _ := testJava(t, cc.GatherRequiredDepsForTest(android.Android)+`
2094 cc_library {
2095 name: "libjni",
2096 system_shared_libs: [],
2097 stl: "none",
2098 sdk_version: "current",
2099 }
2100
2101 android_test {
2102 name: "app_platform",
2103 jni_libs: ["libjni"],
2104 platform_apis: true,
2105 }
2106
2107 android_test {
2108 name: "app_sdk",
2109 jni_libs: ["libjni"],
2110 sdk_version: "current",
2111 }
2112
2113 android_test {
2114 name: "app_force_platform",
2115 jni_libs: ["libjni"],
2116 sdk_version: "current",
2117 jni_uses_platform_apis: true,
2118 }
2119
2120 android_test {
2121 name: "app_force_sdk",
2122 jni_libs: ["libjni"],
2123 platform_apis: true,
2124 jni_uses_sdk_apis: true,
2125 }
Colin Crossc2d24052020-05-13 11:05:02 -07002126
2127 cc_library {
2128 name: "libvendorjni",
2129 system_shared_libs: [],
2130 stl: "none",
2131 vendor: true,
2132 }
2133
2134 android_test {
2135 name: "app_vendor",
2136 jni_libs: ["libvendorjni"],
2137 sdk_version: "current",
2138 vendor: true,
2139 }
Colin Cross3c007702020-05-08 11:20:24 -07002140 `)
2141
2142 testCases := []struct {
Colin Crossc2d24052020-05-13 11:05:02 -07002143 name string
2144 sdkJNI bool
2145 vendorJNI bool
Colin Cross3c007702020-05-08 11:20:24 -07002146 }{
Colin Crossc2d24052020-05-13 11:05:02 -07002147 {name: "app_platform"},
2148 {name: "app_sdk", sdkJNI: true},
2149 {name: "app_force_platform"},
2150 {name: "app_force_sdk", sdkJNI: true},
2151 {name: "app_vendor", vendorJNI: true},
Colin Cross3c007702020-05-08 11:20:24 -07002152 }
2153
Colin Crossc2d24052020-05-13 11:05:02 -07002154 platformJNI := ctx.ModuleForTests("libjni", "android_arm64_armv8-a_shared").
2155 Output("libjni.so").Output.String()
2156 sdkJNI := ctx.ModuleForTests("libjni", "android_arm64_armv8-a_sdk_shared").
2157 Output("libjni.so").Output.String()
Kiyoung Kimb5fdb2e2024-01-03 14:24:34 +09002158 vendorJNI := ctx.ModuleForTests("libvendorjni", "android_vendor_arm64_armv8-a_shared").
Colin Crossc2d24052020-05-13 11:05:02 -07002159 Output("libvendorjni.so").Output.String()
2160
Colin Cross3c007702020-05-08 11:20:24 -07002161 for _, test := range testCases {
2162 t.Run(test.name, func(t *testing.T) {
2163 app := ctx.ModuleForTests(test.name, "android_common")
Colin Cross3c007702020-05-08 11:20:24 -07002164
Colin Crossb3168ba2023-07-26 16:14:56 -07002165 jniLibZip := app.MaybeOutput("jnilibs.zip")
Colin Cross3c007702020-05-08 11:20:24 -07002166 if len(jniLibZip.Implicits) != 1 {
2167 t.Fatalf("expected exactly one jni library, got %q", jniLibZip.Implicits.Strings())
2168 }
2169 gotJNI := jniLibZip.Implicits[0].String()
2170
2171 if test.sdkJNI {
2172 if gotJNI != sdkJNI {
2173 t.Errorf("expected SDK JNI library %q, got %q", sdkJNI, gotJNI)
2174 }
Colin Crossc2d24052020-05-13 11:05:02 -07002175 } else if test.vendorJNI {
2176 if gotJNI != vendorJNI {
2177 t.Errorf("expected platform JNI library %q, got %q", vendorJNI, gotJNI)
2178 }
Colin Cross3c007702020-05-08 11:20:24 -07002179 } else {
2180 if gotJNI != platformJNI {
2181 t.Errorf("expected platform JNI library %q, got %q", platformJNI, gotJNI)
2182 }
2183 }
2184 })
2185 }
2186
2187 t.Run("jni_uses_platform_apis_error", func(t *testing.T) {
2188 testJavaError(t, `jni_uses_platform_apis: can only be set for modules that set sdk_version`, `
2189 android_test {
2190 name: "app_platform",
2191 platform_apis: true,
2192 jni_uses_platform_apis: true,
2193 }
2194 `)
2195 })
2196
2197 t.Run("jni_uses_sdk_apis_error", func(t *testing.T) {
2198 testJavaError(t, `jni_uses_sdk_apis: can only be set for modules that do not set sdk_version`, `
2199 android_test {
2200 name: "app_sdk",
2201 sdk_version: "current",
2202 jni_uses_sdk_apis: true,
2203 }
2204 `)
2205 })
2206
2207}
2208
Jaewoong Jung2ad817c2019-01-18 14:27:16 -08002209func TestCertificates(t *testing.T) {
2210 testCases := []struct {
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -04002211 name string
2212 bp string
Colin Cross5caad2b2022-12-12 15:11:46 -08002213 allowMissingDependencies bool
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -04002214 certificateOverride string
2215 expectedCertSigningFlags string
2216 expectedCertificate string
Jaewoong Jung2ad817c2019-01-18 14:27:16 -08002217 }{
2218 {
2219 name: "default",
2220 bp: `
2221 android_app {
2222 name: "foo",
2223 srcs: ["a.java"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09002224 sdk_version: "current",
Jaewoong Jung2ad817c2019-01-18 14:27:16 -08002225 }
2226 `,
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -04002227 certificateOverride: "",
2228 expectedCertSigningFlags: "",
Colin Cross5caad2b2022-12-12 15:11:46 -08002229 expectedCertificate: "build/make/target/product/security/testkey",
Jaewoong Jung2ad817c2019-01-18 14:27:16 -08002230 },
2231 {
2232 name: "module certificate property",
2233 bp: `
2234 android_app {
2235 name: "foo",
2236 srcs: ["a.java"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09002237 certificate: ":new_certificate",
2238 sdk_version: "current",
Jaewoong Jung2ad817c2019-01-18 14:27:16 -08002239 }
2240
2241 android_app_certificate {
2242 name: "new_certificate",
Colin Cross3c007702020-05-08 11:20:24 -07002243 certificate: "cert/new_cert",
Jaewoong Jung2ad817c2019-01-18 14:27:16 -08002244 }
2245 `,
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -04002246 certificateOverride: "",
2247 expectedCertSigningFlags: "",
Colin Cross5caad2b2022-12-12 15:11:46 -08002248 expectedCertificate: "cert/new_cert",
Jaewoong Jung2ad817c2019-01-18 14:27:16 -08002249 },
2250 {
2251 name: "path certificate property",
2252 bp: `
2253 android_app {
2254 name: "foo",
2255 srcs: ["a.java"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09002256 certificate: "expiredkey",
2257 sdk_version: "current",
Jaewoong Jung2ad817c2019-01-18 14:27:16 -08002258 }
2259 `,
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -04002260 certificateOverride: "",
2261 expectedCertSigningFlags: "",
Colin Cross5caad2b2022-12-12 15:11:46 -08002262 expectedCertificate: "build/make/target/product/security/expiredkey",
Jaewoong Jung2ad817c2019-01-18 14:27:16 -08002263 },
2264 {
2265 name: "certificate overrides",
2266 bp: `
2267 android_app {
2268 name: "foo",
2269 srcs: ["a.java"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09002270 certificate: "expiredkey",
2271 sdk_version: "current",
Jaewoong Jung2ad817c2019-01-18 14:27:16 -08002272 }
2273
2274 android_app_certificate {
2275 name: "new_certificate",
Colin Cross3c007702020-05-08 11:20:24 -07002276 certificate: "cert/new_cert",
Jaewoong Jung2ad817c2019-01-18 14:27:16 -08002277 }
2278 `,
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -04002279 certificateOverride: "foo:new_certificate",
2280 expectedCertSigningFlags: "",
Colin Cross5caad2b2022-12-12 15:11:46 -08002281 expectedCertificate: "cert/new_cert",
Liz Kammere2b27f42020-05-07 13:24:05 -07002282 },
2283 {
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -04002284 name: "certificate signing flags",
Liz Kammere2b27f42020-05-07 13:24:05 -07002285 bp: `
2286 android_app {
2287 name: "foo",
2288 srcs: ["a.java"],
2289 certificate: ":new_certificate",
2290 lineage: "lineage.bin",
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -04002291 rotationMinSdkVersion: "32",
Liz Kammere2b27f42020-05-07 13:24:05 -07002292 sdk_version: "current",
2293 }
2294
2295 android_app_certificate {
2296 name: "new_certificate",
2297 certificate: "cert/new_cert",
2298 }
2299 `,
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -04002300 certificateOverride: "",
2301 expectedCertSigningFlags: "--lineage lineage.bin --rotation-min-sdk-version 32",
Colin Cross5caad2b2022-12-12 15:11:46 -08002302 expectedCertificate: "cert/new_cert",
Jaewoong Jung2ad817c2019-01-18 14:27:16 -08002303 },
Jaewoong Jung1c1b6e62021-03-09 15:02:31 -08002304 {
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -04002305 name: "cert signing flags from filegroup",
Jaewoong Jung1c1b6e62021-03-09 15:02:31 -08002306 bp: `
2307 android_app {
2308 name: "foo",
2309 srcs: ["a.java"],
2310 certificate: ":new_certificate",
2311 lineage: ":lineage_bin",
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -04002312 rotationMinSdkVersion: "32",
Jaewoong Jung1c1b6e62021-03-09 15:02:31 -08002313 sdk_version: "current",
2314 }
2315
2316 android_app_certificate {
2317 name: "new_certificate",
2318 certificate: "cert/new_cert",
2319 }
2320
2321 filegroup {
2322 name: "lineage_bin",
2323 srcs: ["lineage.bin"],
2324 }
2325 `,
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -04002326 certificateOverride: "",
2327 expectedCertSigningFlags: "--lineage lineage.bin --rotation-min-sdk-version 32",
Colin Cross5caad2b2022-12-12 15:11:46 -08002328 expectedCertificate: "cert/new_cert",
2329 },
2330 {
2331 name: "missing with AllowMissingDependencies",
2332 bp: `
2333 android_app {
2334 name: "foo",
2335 srcs: ["a.java"],
2336 certificate: ":new_certificate",
2337 sdk_version: "current",
2338 }
2339 `,
2340 expectedCertificate: "out/soong/.intermediates/foo/android_common/missing",
2341 allowMissingDependencies: true,
Jaewoong Jung1c1b6e62021-03-09 15:02:31 -08002342 },
Jaewoong Jung2ad817c2019-01-18 14:27:16 -08002343 }
2344
2345 for _, test := range testCases {
2346 t.Run(test.name, func(t *testing.T) {
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00002347 result := android.GroupFixturePreparers(
2348 PrepareForTestWithJavaDefaultModules,
2349 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
2350 if test.certificateOverride != "" {
2351 variables.CertificateOverrides = []string{test.certificateOverride}
2352 }
Colin Cross5caad2b2022-12-12 15:11:46 -08002353 if test.allowMissingDependencies {
2354 variables.Allow_missing_dependencies = proptools.BoolPtr(true)
2355 }
2356 }),
2357 android.FixtureModifyContext(func(ctx *android.TestContext) {
2358 ctx.SetAllowMissingDependencies(test.allowMissingDependencies)
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00002359 }),
2360 ).RunTestWithBp(t, test.bp)
Jaewoong Jung2ad817c2019-01-18 14:27:16 -08002361
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00002362 foo := result.ModuleForTests("foo", "android_common")
Jaewoong Jung2ad817c2019-01-18 14:27:16 -08002363
Colin Cross5caad2b2022-12-12 15:11:46 -08002364 certificate := foo.Module().(*AndroidApp).certificate
2365 android.AssertPathRelativeToTopEquals(t, "certificates key", test.expectedCertificate+".pk8", certificate.Key)
2366 // The sign_target_files_apks and check_target_files_signatures
2367 // tools require that certificates have a .x509.pem extension.
2368 android.AssertPathRelativeToTopEquals(t, "certificates pem", test.expectedCertificate+".x509.pem", certificate.Pem)
Liz Kammere2b27f42020-05-07 13:24:05 -07002369
Colin Cross5caad2b2022-12-12 15:11:46 -08002370 signapk := foo.Output("foo.apk")
2371 if signapk.Rule != android.ErrorRule {
2372 signCertificateFlags := signapk.Args["certificates"]
2373 expectedFlags := certificate.Pem.String() + " " + certificate.Key.String()
2374 android.AssertStringEquals(t, "certificates flags", expectedFlags, signCertificateFlags)
2375
2376 certSigningFlags := signapk.Args["flags"]
2377 android.AssertStringEquals(t, "cert signing flags", test.expectedCertSigningFlags, certSigningFlags)
2378 }
Jaewoong Jung2ad817c2019-01-18 14:27:16 -08002379 })
2380 }
2381}
Jaewoong Jung9d22a912019-01-23 16:27:47 -08002382
Songchun Fan688de9a2020-03-24 20:32:24 -07002383func TestRequestV4SigningFlag(t *testing.T) {
2384 testCases := []struct {
2385 name string
2386 bp string
2387 expected string
2388 }{
2389 {
2390 name: "default",
2391 bp: `
2392 android_app {
2393 name: "foo",
2394 srcs: ["a.java"],
2395 sdk_version: "current",
2396 }
2397 `,
2398 expected: "",
2399 },
2400 {
2401 name: "default",
2402 bp: `
2403 android_app {
2404 name: "foo",
2405 srcs: ["a.java"],
2406 sdk_version: "current",
2407 v4_signature: false,
2408 }
2409 `,
2410 expected: "",
2411 },
2412 {
2413 name: "module certificate property",
2414 bp: `
2415 android_app {
2416 name: "foo",
2417 srcs: ["a.java"],
2418 sdk_version: "current",
2419 v4_signature: true,
2420 }
2421 `,
2422 expected: "--enable-v4",
2423 },
2424 }
2425
2426 for _, test := range testCases {
2427 t.Run(test.name, func(t *testing.T) {
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00002428 result := android.GroupFixturePreparers(
2429 PrepareForTestWithJavaDefaultModules,
2430 ).RunTestWithBp(t, test.bp)
Songchun Fan688de9a2020-03-24 20:32:24 -07002431
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00002432 foo := result.ModuleForTests("foo", "android_common")
Songchun Fan688de9a2020-03-24 20:32:24 -07002433
2434 signapk := foo.Output("foo.apk")
2435 signFlags := signapk.Args["flags"]
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00002436 android.AssertStringEquals(t, "signing flags", test.expected, signFlags)
Songchun Fan688de9a2020-03-24 20:32:24 -07002437 })
2438 }
2439}
2440
Jaewoong Jung9d22a912019-01-23 16:27:47 -08002441func TestPackageNameOverride(t *testing.T) {
2442 testCases := []struct {
2443 name string
2444 bp string
2445 packageNameOverride string
2446 expected []string
2447 }{
2448 {
2449 name: "default",
2450 bp: `
2451 android_app {
2452 name: "foo",
2453 srcs: ["a.java"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09002454 sdk_version: "current",
Jaewoong Jung9d22a912019-01-23 16:27:47 -08002455 }
2456 `,
2457 packageNameOverride: "",
2458 expected: []string{
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00002459 "out/soong/.intermediates/foo/android_common/foo.apk",
2460 "out/soong/target/product/test_device/system/app/foo/foo.apk",
Jaewoong Jung9d22a912019-01-23 16:27:47 -08002461 },
2462 },
2463 {
Jooyung Han29e2f6d2022-01-08 12:13:59 +09002464 name: "overridden via PRODUCT_PACKAGE_NAME_OVERRIDES",
Jaewoong Jung9d22a912019-01-23 16:27:47 -08002465 bp: `
2466 android_app {
2467 name: "foo",
2468 srcs: ["a.java"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09002469 sdk_version: "current",
Jaewoong Jung9d22a912019-01-23 16:27:47 -08002470 }
2471 `,
2472 packageNameOverride: "foo:bar",
2473 expected: []string{
2474 // The package apk should be still be the original name for test dependencies.
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00002475 "out/soong/.intermediates/foo/android_common/bar.apk",
2476 "out/soong/target/product/test_device/system/app/bar/bar.apk",
Jaewoong Jung9d22a912019-01-23 16:27:47 -08002477 },
2478 },
Jooyung Han29e2f6d2022-01-08 12:13:59 +09002479 {
2480 name: "overridden via stem",
2481 bp: `
2482 android_app {
2483 name: "foo",
2484 srcs: ["a.java"],
2485 sdk_version: "current",
2486 stem: "bar",
2487 }
2488 `,
2489 packageNameOverride: "",
2490 expected: []string{
2491 "out/soong/.intermediates/foo/android_common/bar.apk",
2492 "out/soong/target/product/test_device/system/app/bar/bar.apk",
2493 },
2494 },
Jaewoong Jung9d22a912019-01-23 16:27:47 -08002495 }
2496
2497 for _, test := range testCases {
2498 t.Run(test.name, func(t *testing.T) {
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00002499 result := android.GroupFixturePreparers(
2500 PrepareForTestWithJavaDefaultModules,
2501 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
2502 if test.packageNameOverride != "" {
2503 variables.PackageNameOverrides = []string{test.packageNameOverride}
2504 }
2505 }),
2506 ).RunTestWithBp(t, test.bp)
Jaewoong Jung9d22a912019-01-23 16:27:47 -08002507
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00002508 foo := result.ModuleForTests("foo", "android_common")
2509
Lukacs T. Berkifbaf7252021-08-19 09:36:42 +02002510 outSoongDir := result.Config.SoongOutDir()
Jaewoong Jung9d22a912019-01-23 16:27:47 -08002511
2512 outputs := foo.AllOutputs()
2513 outputMap := make(map[string]bool)
2514 for _, o := range outputs {
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00002515 outputMap[android.StringPathRelativeToTop(outSoongDir, o)] = true
Jaewoong Jung9d22a912019-01-23 16:27:47 -08002516 }
2517 for _, e := range test.expected {
2518 if _, exist := outputMap[e]; !exist {
2519 t.Errorf("Can't find %q in output files.\nAll outputs:%v", e, outputs)
2520 }
2521 }
2522 })
2523 }
2524}
Jaewoong Jung4102e5d2019-02-27 16:26:28 -08002525
2526func TestInstrumentationTargetOverridden(t *testing.T) {
2527 bp := `
2528 android_app {
2529 name: "foo",
2530 srcs: ["a.java"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09002531 sdk_version: "current",
Jaewoong Jung4102e5d2019-02-27 16:26:28 -08002532 }
2533
2534 android_test {
2535 name: "bar",
2536 instrumentation_for: "foo",
Jeongik Cha538c0d02019-07-11 15:54:27 +09002537 sdk_version: "current",
Jaewoong Jung4102e5d2019-02-27 16:26:28 -08002538 }
2539 `
Jaewoong Jung4102e5d2019-02-27 16:26:28 -08002540
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00002541 result := android.GroupFixturePreparers(
2542 PrepareForTestWithJavaDefaultModules,
2543 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
2544 variables.ManifestPackageNameOverrides = []string{"foo:org.dandroid.bp"}
2545 }),
2546 ).RunTestWithBp(t, bp)
Jaewoong Jung4102e5d2019-02-27 16:26:28 -08002547
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00002548 bar := result.ModuleForTests("bar", "android_common")
Jaewoong Jung4102e5d2019-02-27 16:26:28 -08002549 res := bar.Output("package-res.apk")
2550 aapt2Flags := res.Args["flags"]
2551 e := "--rename-instrumentation-target-package org.dandroid.bp"
2552 if !strings.Contains(aapt2Flags, e) {
2553 t.Errorf("target package renaming flag, %q is missing in aapt2 link flags, %q", e, aapt2Flags)
2554 }
2555}
Jaewoong Jung525443a2019-02-28 15:35:54 -08002556
2557func TestOverrideAndroidApp(t *testing.T) {
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00002558 result := PrepareForTestWithJavaDefaultModules.RunTestWithBp(
2559 t, `
Jaewoong Jung525443a2019-02-28 15:35:54 -08002560 android_app {
2561 name: "foo",
2562 srcs: ["a.java"],
Jaewoong Junga641ee92019-03-27 11:17:14 -07002563 certificate: "expiredkey",
Jaewoong Jungb639a6a2019-05-10 15:16:29 -07002564 overrides: ["qux"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09002565 sdk_version: "current",
Jaewoong Jung525443a2019-02-28 15:35:54 -08002566 }
2567
2568 override_android_app {
2569 name: "bar",
2570 base: "foo",
2571 certificate: ":new_certificate",
Liz Kammere2b27f42020-05-07 13:24:05 -07002572 lineage: "lineage.bin",
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -04002573 rotationMinSdkVersion: "32",
Baligh Uddin5b16dfb2020-02-11 17:27:19 -08002574 logging_parent: "bah",
Jaewoong Jung525443a2019-02-28 15:35:54 -08002575 }
2576
2577 android_app_certificate {
2578 name: "new_certificate",
2579 certificate: "cert/new_cert",
2580 }
Jaewoong Jung6f373f62019-03-13 10:13:24 -07002581
2582 override_android_app {
2583 name: "baz",
2584 base: "foo",
2585 package_name: "org.dandroid.bp",
2586 }
Liz Kammer9f9fd022020-06-18 19:44:06 +00002587
2588 override_android_app {
2589 name: "baz_no_rename_resources",
2590 base: "foo",
2591 package_name: "org.dandroid.bp",
2592 rename_resources_package: false,
2593 }
2594
2595 android_app {
2596 name: "foo_no_rename_resources",
2597 srcs: ["a.java"],
2598 certificate: "expiredkey",
2599 overrides: ["qux"],
2600 rename_resources_package: false,
2601 sdk_version: "current",
2602 }
2603
2604 override_android_app {
2605 name: "baz_base_no_rename_resources",
2606 base: "foo_no_rename_resources",
2607 package_name: "org.dandroid.bp",
2608 }
2609
2610 override_android_app {
2611 name: "baz_override_base_rename_resources",
2612 base: "foo_no_rename_resources",
2613 package_name: "org.dandroid.bp",
2614 rename_resources_package: true,
2615 }
Jaewoong Jung525443a2019-02-28 15:35:54 -08002616 `)
2617
2618 expectedVariants := []struct {
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -04002619 name string
2620 moduleName string
2621 variantName string
2622 apkName string
2623 apkPath string
2624 certFlag string
2625 certSigningFlags string
2626 overrides []string
2627 packageFlag string
2628 renameResources bool
2629 logging_parent string
Jaewoong Jung525443a2019-02-28 15:35:54 -08002630 }{
2631 {
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -04002632 name: "foo",
2633 moduleName: "foo",
2634 variantName: "android_common",
2635 apkPath: "out/soong/target/product/test_device/system/app/foo/foo.apk",
2636 certFlag: "build/make/target/product/security/expiredkey.x509.pem build/make/target/product/security/expiredkey.pk8",
2637 certSigningFlags: "",
2638 overrides: []string{"qux"},
2639 packageFlag: "",
2640 renameResources: false,
2641 logging_parent: "",
Jaewoong Jung525443a2019-02-28 15:35:54 -08002642 },
2643 {
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -04002644 name: "foo",
2645 moduleName: "bar",
2646 variantName: "android_common_bar",
2647 apkPath: "out/soong/target/product/test_device/system/app/bar/bar.apk",
2648 certFlag: "cert/new_cert.x509.pem cert/new_cert.pk8",
2649 certSigningFlags: "--lineage lineage.bin --rotation-min-sdk-version 32",
2650 overrides: []string{"qux", "foo"},
2651 packageFlag: "",
2652 renameResources: false,
2653 logging_parent: "bah",
Jaewoong Jung6f373f62019-03-13 10:13:24 -07002654 },
2655 {
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -04002656 name: "foo",
2657 moduleName: "baz",
2658 variantName: "android_common_baz",
2659 apkPath: "out/soong/target/product/test_device/system/app/baz/baz.apk",
2660 certFlag: "build/make/target/product/security/expiredkey.x509.pem build/make/target/product/security/expiredkey.pk8",
2661 certSigningFlags: "",
2662 overrides: []string{"qux", "foo"},
2663 packageFlag: "org.dandroid.bp",
2664 renameResources: true,
2665 logging_parent: "",
Liz Kammer9f9fd022020-06-18 19:44:06 +00002666 },
2667 {
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -04002668 name: "foo",
2669 moduleName: "baz_no_rename_resources",
2670 variantName: "android_common_baz_no_rename_resources",
2671 apkPath: "out/soong/target/product/test_device/system/app/baz_no_rename_resources/baz_no_rename_resources.apk",
2672 certFlag: "build/make/target/product/security/expiredkey.x509.pem build/make/target/product/security/expiredkey.pk8",
2673 certSigningFlags: "",
2674 overrides: []string{"qux", "foo"},
2675 packageFlag: "org.dandroid.bp",
2676 renameResources: false,
2677 logging_parent: "",
Liz Kammer9f9fd022020-06-18 19:44:06 +00002678 },
2679 {
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -04002680 name: "foo_no_rename_resources",
2681 moduleName: "baz_base_no_rename_resources",
2682 variantName: "android_common_baz_base_no_rename_resources",
2683 apkPath: "out/soong/target/product/test_device/system/app/baz_base_no_rename_resources/baz_base_no_rename_resources.apk",
2684 certFlag: "build/make/target/product/security/expiredkey.x509.pem build/make/target/product/security/expiredkey.pk8",
2685 certSigningFlags: "",
2686 overrides: []string{"qux", "foo_no_rename_resources"},
2687 packageFlag: "org.dandroid.bp",
2688 renameResources: false,
2689 logging_parent: "",
Liz Kammer9f9fd022020-06-18 19:44:06 +00002690 },
2691 {
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -04002692 name: "foo_no_rename_resources",
2693 moduleName: "baz_override_base_rename_resources",
2694 variantName: "android_common_baz_override_base_rename_resources",
2695 apkPath: "out/soong/target/product/test_device/system/app/baz_override_base_rename_resources/baz_override_base_rename_resources.apk",
2696 certFlag: "build/make/target/product/security/expiredkey.x509.pem build/make/target/product/security/expiredkey.pk8",
2697 certSigningFlags: "",
2698 overrides: []string{"qux", "foo_no_rename_resources"},
2699 packageFlag: "org.dandroid.bp",
2700 renameResources: true,
2701 logging_parent: "",
Jaewoong Jung525443a2019-02-28 15:35:54 -08002702 },
2703 }
2704 for _, expected := range expectedVariants {
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00002705 variant := result.ModuleForTests(expected.name, expected.variantName)
Jaewoong Jung525443a2019-02-28 15:35:54 -08002706
2707 // Check the final apk name
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00002708 variant.Output(expected.apkPath)
Jaewoong Jung525443a2019-02-28 15:35:54 -08002709
2710 // Check the certificate paths
Jaewoong Jung5a498812019-11-07 14:14:38 -08002711 signapk := variant.Output(expected.moduleName + ".apk")
Liz Kammere2b27f42020-05-07 13:24:05 -07002712 certFlag := signapk.Args["certificates"]
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00002713 android.AssertStringEquals(t, "certificates flags", expected.certFlag, certFlag)
Liz Kammere2b27f42020-05-07 13:24:05 -07002714
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -04002715 // Check the cert signing flags
2716 certSigningFlags := signapk.Args["flags"]
2717 android.AssertStringEquals(t, "cert signing flags", expected.certSigningFlags, certSigningFlags)
Jaewoong Jung525443a2019-02-28 15:35:54 -08002718
Jaewoong Jung6f373f62019-03-13 10:13:24 -07002719 // Check if the overrides field values are correctly aggregated.
Jaewoong Jung525443a2019-02-28 15:35:54 -08002720 mod := variant.Module().(*AndroidApp)
zhidoua2ce78f2022-02-17 02:33:12 +00002721 android.AssertDeepEquals(t, "overrides property", expected.overrides, mod.overridableAppProperties.Overrides)
Jaewoong Jung6f373f62019-03-13 10:13:24 -07002722
Baligh Uddin5b16dfb2020-02-11 17:27:19 -08002723 // Test Overridable property: Logging_parent
2724 logging_parent := mod.aapt.LoggingParent
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00002725 android.AssertStringEquals(t, "overrides property value for logging parent", expected.logging_parent, logging_parent)
Baligh Uddin5b16dfb2020-02-11 17:27:19 -08002726
Liz Kammer1d5983b2020-05-19 19:15:37 +00002727 // Check the package renaming flag, if exists.
Jaewoong Jung6f373f62019-03-13 10:13:24 -07002728 res := variant.Output("package-res.apk")
2729 aapt2Flags := res.Args["flags"]
Liz Kammer9f9fd022020-06-18 19:44:06 +00002730 checkAapt2LinkFlag(t, aapt2Flags, "rename-manifest-package", expected.packageFlag)
2731 expectedPackage := expected.packageFlag
2732 if !expected.renameResources {
2733 expectedPackage = ""
Liz Kammer1d5983b2020-05-19 19:15:37 +00002734 }
Liz Kammer9f9fd022020-06-18 19:44:06 +00002735 checkAapt2LinkFlag(t, aapt2Flags, "rename-resources-package", expectedPackage)
Jaewoong Jung525443a2019-02-28 15:35:54 -08002736 }
2737}
Jaewoong Jungccbb3932019-04-15 09:48:31 -07002738
zhidoua2ce78f2022-02-17 02:33:12 +00002739func TestOverrideAndroidAppOverrides(t *testing.T) {
2740 ctx, _ := testJava(
2741 t, `
2742 android_app {
2743 name: "foo",
2744 srcs: ["a.java"],
2745 sdk_version: "current",
2746 overrides: ["qux"]
2747 }
2748
2749 android_app {
2750 name: "bar",
2751 srcs: ["b.java"],
2752 sdk_version: "current",
2753 overrides: ["foo"]
2754 }
2755
2756 override_android_app {
2757 name: "foo_override",
2758 base: "foo",
2759 overrides: ["bar"]
2760 }
2761 `)
2762
2763 expectedVariants := []struct {
2764 name string
2765 moduleName string
2766 variantName string
2767 overrides []string
2768 }{
2769 {
2770 name: "foo",
2771 moduleName: "foo",
2772 variantName: "android_common",
2773 overrides: []string{"qux"},
2774 },
2775 {
2776 name: "bar",
2777 moduleName: "bar",
2778 variantName: "android_common",
2779 overrides: []string{"foo"},
2780 },
2781 {
2782 name: "foo",
2783 moduleName: "foo_override",
2784 variantName: "android_common_foo_override",
2785 overrides: []string{"bar", "foo"},
2786 },
2787 }
2788 for _, expected := range expectedVariants {
2789 variant := ctx.ModuleForTests(expected.name, expected.variantName)
2790
2791 // Check if the overrides field values are correctly aggregated.
2792 mod := variant.Module().(*AndroidApp)
2793 android.AssertDeepEquals(t, "overrides property", expected.overrides, mod.overridableAppProperties.Overrides)
2794 }
2795}
2796
Colin Crossaaa0c1f2022-05-16 16:19:54 -07002797func TestOverrideAndroidAppWithPrebuilt(t *testing.T) {
2798 result := PrepareForTestWithJavaDefaultModules.RunTestWithBp(
2799 t, `
2800 android_app {
2801 name: "foo",
2802 srcs: ["a.java"],
2803 sdk_version: "current",
2804 }
2805
2806 override_android_app {
2807 name: "bar",
2808 base: "foo",
2809 }
2810
2811 android_app_import {
2812 name: "bar",
2813 prefer: true,
2814 apk: "bar.apk",
2815 presigned: true,
2816 }
2817 `)
2818
2819 // An app that has an override that also has a prebuilt should not be hidden.
2820 foo := result.ModuleForTests("foo", "android_common")
2821 if foo.Module().IsHideFromMake() {
2822 t.Errorf("expected foo to have HideFromMake false")
2823 }
2824
2825 // An override that also has a prebuilt should be hidden.
2826 barOverride := result.ModuleForTests("foo", "android_common_bar")
2827 if !barOverride.Module().IsHideFromMake() {
2828 t.Errorf("expected bar override variant of foo to have HideFromMake true")
2829 }
2830}
2831
Jooyung Han01d80d82022-01-08 12:16:32 +09002832func TestOverrideAndroidAppStem(t *testing.T) {
2833 ctx, _ := testJava(t, `
2834 android_app {
2835 name: "foo",
2836 srcs: ["a.java"],
2837 sdk_version: "current",
2838 }
2839 override_android_app {
2840 name: "bar",
2841 base: "foo",
2842 }
2843 override_android_app {
2844 name: "baz",
2845 base: "foo",
2846 stem: "baz_stem",
2847 }
2848 android_app {
2849 name: "foo2",
2850 srcs: ["a.java"],
2851 sdk_version: "current",
2852 stem: "foo2_stem",
2853 }
2854 override_android_app {
2855 name: "bar2",
2856 base: "foo2",
2857 }
2858 override_android_app {
2859 name: "baz2",
2860 base: "foo2",
2861 stem: "baz2_stem",
2862 }
2863 `)
2864 for _, expected := range []struct {
2865 moduleName string
2866 variantName string
2867 apkPath string
2868 }{
2869 {
2870 moduleName: "foo",
2871 variantName: "android_common",
2872 apkPath: "out/soong/target/product/test_device/system/app/foo/foo.apk",
2873 },
2874 {
2875 moduleName: "foo",
2876 variantName: "android_common_bar",
2877 apkPath: "out/soong/target/product/test_device/system/app/bar/bar.apk",
2878 },
2879 {
2880 moduleName: "foo",
2881 variantName: "android_common_baz",
2882 apkPath: "out/soong/target/product/test_device/system/app/baz_stem/baz_stem.apk",
2883 },
2884 {
2885 moduleName: "foo2",
2886 variantName: "android_common",
2887 apkPath: "out/soong/target/product/test_device/system/app/foo2_stem/foo2_stem.apk",
2888 },
2889 {
2890 moduleName: "foo2",
2891 variantName: "android_common_bar2",
2892 // Note that this may cause the duplicate output error.
2893 apkPath: "out/soong/target/product/test_device/system/app/foo2_stem/foo2_stem.apk",
2894 },
2895 {
2896 moduleName: "foo2",
2897 variantName: "android_common_baz2",
2898 apkPath: "out/soong/target/product/test_device/system/app/baz2_stem/baz2_stem.apk",
2899 },
2900 } {
2901 variant := ctx.ModuleForTests(expected.moduleName, expected.variantName)
2902 variant.Output(expected.apkPath)
2903 }
2904}
2905
Jaewoong Jungb639a6a2019-05-10 15:16:29 -07002906func TestOverrideAndroidAppDependency(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07002907 ctx, _ := testJava(t, `
Jaewoong Jungb639a6a2019-05-10 15:16:29 -07002908 android_app {
2909 name: "foo",
2910 srcs: ["a.java"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09002911 sdk_version: "current",
Jaewoong Jungb639a6a2019-05-10 15:16:29 -07002912 }
2913
2914 override_android_app {
2915 name: "bar",
2916 base: "foo",
2917 package_name: "org.dandroid.bp",
2918 }
2919
2920 android_test {
2921 name: "baz",
2922 srcs: ["b.java"],
2923 instrumentation_for: "foo",
2924 }
2925
2926 android_test {
2927 name: "qux",
2928 srcs: ["b.java"],
2929 instrumentation_for: "bar",
2930 }
2931 `)
2932
2933 // Verify baz, which depends on the overridden module foo, has the correct classpath javac arg.
Paul Duffina71a67a2021-03-29 00:42:57 +01002934 javac := ctx.ModuleForTests("baz", "android_common").Rule("javac")
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00002935 fooTurbine := "out/soong/.intermediates/foo/android_common/turbine-combined/foo.jar"
Jaewoong Jungb639a6a2019-05-10 15:16:29 -07002936 if !strings.Contains(javac.Args["classpath"], fooTurbine) {
2937 t.Errorf("baz classpath %v does not contain %q", javac.Args["classpath"], fooTurbine)
2938 }
2939
2940 // Verify qux, which depends on the overriding module bar, has the correct classpath javac arg.
Paul Duffina71a67a2021-03-29 00:42:57 +01002941 javac = ctx.ModuleForTests("qux", "android_common").Rule("javac")
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00002942 barTurbine := "out/soong/.intermediates/foo/android_common_bar/turbine-combined/foo.jar"
Jaewoong Jungb639a6a2019-05-10 15:16:29 -07002943 if !strings.Contains(javac.Args["classpath"], barTurbine) {
2944 t.Errorf("qux classpath %v does not contain %q", javac.Args["classpath"], barTurbine)
2945 }
2946}
2947
Jaewoong Jung26dedd32019-06-06 08:45:58 -07002948func TestOverrideAndroidTest(t *testing.T) {
2949 ctx, _ := testJava(t, `
2950 android_app {
2951 name: "foo",
2952 srcs: ["a.java"],
2953 package_name: "com.android.foo",
2954 sdk_version: "current",
2955 }
2956
2957 override_android_app {
2958 name: "bar",
2959 base: "foo",
2960 package_name: "com.android.bar",
2961 }
2962
2963 android_test {
2964 name: "foo_test",
2965 srcs: ["b.java"],
2966 instrumentation_for: "foo",
2967 }
2968
2969 override_android_test {
2970 name: "bar_test",
2971 base: "foo_test",
2972 package_name: "com.android.bar.test",
2973 instrumentation_for: "bar",
2974 instrumentation_target_package: "com.android.bar",
2975 }
2976 `)
2977
2978 expectedVariants := []struct {
2979 moduleName string
2980 variantName string
2981 apkPath string
2982 overrides []string
2983 targetVariant string
2984 packageFlag string
2985 targetPackageFlag string
2986 }{
2987 {
2988 variantName: "android_common",
Jaewoong Jung326a9412019-11-21 10:41:00 -08002989 apkPath: "/target/product/test_device/testcases/foo_test/arm64/foo_test.apk",
Jaewoong Jung26dedd32019-06-06 08:45:58 -07002990 overrides: nil,
2991 targetVariant: "android_common",
2992 packageFlag: "",
2993 targetPackageFlag: "",
2994 },
2995 {
2996 variantName: "android_common_bar_test",
Jaewoong Jung326a9412019-11-21 10:41:00 -08002997 apkPath: "/target/product/test_device/testcases/bar_test/arm64/bar_test.apk",
Jaewoong Jung26dedd32019-06-06 08:45:58 -07002998 overrides: []string{"foo_test"},
2999 targetVariant: "android_common_bar",
3000 packageFlag: "com.android.bar.test",
3001 targetPackageFlag: "com.android.bar",
3002 },
3003 }
3004 for _, expected := range expectedVariants {
3005 variant := ctx.ModuleForTests("foo_test", expected.variantName)
3006
3007 // Check the final apk name
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00003008 variant.Output("out/soong" + expected.apkPath)
Jaewoong Jung26dedd32019-06-06 08:45:58 -07003009
3010 // Check if the overrides field values are correctly aggregated.
3011 mod := variant.Module().(*AndroidTest)
zhidoua2ce78f2022-02-17 02:33:12 +00003012 if !reflect.DeepEqual(expected.overrides, mod.overridableAppProperties.Overrides) {
Jaewoong Jung26dedd32019-06-06 08:45:58 -07003013 t.Errorf("Incorrect overrides property value, expected: %q, got: %q",
zhidoua2ce78f2022-02-17 02:33:12 +00003014 expected.overrides, mod.overridableAppProperties.Overrides)
Jaewoong Jung26dedd32019-06-06 08:45:58 -07003015 }
3016
3017 // Check if javac classpath has the correct jar file path. This checks instrumentation_for overrides.
Paul Duffina71a67a2021-03-29 00:42:57 +01003018 javac := variant.Rule("javac")
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00003019 turbine := filepath.Join("out", "soong", ".intermediates", "foo", expected.targetVariant, "turbine-combined", "foo.jar")
Jaewoong Jung26dedd32019-06-06 08:45:58 -07003020 if !strings.Contains(javac.Args["classpath"], turbine) {
3021 t.Errorf("classpath %q does not contain %q", javac.Args["classpath"], turbine)
3022 }
3023
3024 // Check aapt2 flags.
3025 res := variant.Output("package-res.apk")
3026 aapt2Flags := res.Args["flags"]
3027 checkAapt2LinkFlag(t, aapt2Flags, "rename-manifest-package", expected.packageFlag)
Liz Kammer9f9fd022020-06-18 19:44:06 +00003028 checkAapt2LinkFlag(t, aapt2Flags, "rename-resources-package", expected.packageFlag)
Jaewoong Jung26dedd32019-06-06 08:45:58 -07003029 checkAapt2LinkFlag(t, aapt2Flags, "rename-instrumentation-target-package", expected.targetPackageFlag)
3030 }
3031}
3032
Jaewoong Jung39982342020-01-14 10:27:18 -08003033func TestAndroidTest_FixTestConfig(t *testing.T) {
3034 ctx, _ := testJava(t, `
3035 android_app {
3036 name: "foo",
3037 srcs: ["a.java"],
3038 package_name: "com.android.foo",
3039 sdk_version: "current",
3040 }
3041
3042 android_test {
3043 name: "foo_test",
3044 srcs: ["b.java"],
3045 instrumentation_for: "foo",
3046 }
3047
3048 android_test {
3049 name: "bar_test",
3050 srcs: ["b.java"],
3051 package_name: "com.android.bar.test",
3052 instrumentation_for: "foo",
Seth Moorec6f4b532023-02-02 13:22:26 -08003053 mainline_package_name: "com.android.bar",
Jaewoong Jung39982342020-01-14 10:27:18 -08003054 }
3055
3056 override_android_test {
3057 name: "baz_test",
3058 base: "foo_test",
3059 package_name: "com.android.baz.test",
Seth Moorec6f4b532023-02-02 13:22:26 -08003060 mainline_package_name: "com.android.baz",
Jaewoong Jung39982342020-01-14 10:27:18 -08003061 }
3062 `)
3063
3064 testCases := []struct {
3065 moduleName string
3066 variantName string
3067 expectedFlags []string
3068 }{
3069 {
3070 moduleName: "foo_test",
3071 variantName: "android_common",
3072 },
3073 {
3074 moduleName: "bar_test",
3075 variantName: "android_common",
3076 expectedFlags: []string{
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00003077 "--manifest out/soong/.intermediates/bar_test/android_common/manifest_fixer/AndroidManifest.xml",
Jaewoong Jung39982342020-01-14 10:27:18 -08003078 "--package-name com.android.bar.test",
Seth Moorec6f4b532023-02-02 13:22:26 -08003079 "--mainline-package-name com.android.bar",
Jaewoong Jung39982342020-01-14 10:27:18 -08003080 },
3081 },
3082 {
3083 moduleName: "foo_test",
3084 variantName: "android_common_baz_test",
3085 expectedFlags: []string{
Paul Duffinfb0fe9f2021-03-22 17:31:52 +00003086 "--manifest out/soong/.intermediates/foo_test/android_common_baz_test/manifest_fixer/AndroidManifest.xml",
Jaewoong Jung39982342020-01-14 10:27:18 -08003087 "--package-name com.android.baz.test",
3088 "--test-file-name baz_test.apk",
Seth Moorec6f4b532023-02-02 13:22:26 -08003089 "out/soong/.intermediates/foo_test/android_common_baz_test/test_config_fixer/AndroidTest.xml",
3090 "--mainline-package-name com.android.baz",
Jaewoong Jung39982342020-01-14 10:27:18 -08003091 },
3092 },
3093 }
3094
3095 for _, test := range testCases {
3096 variant := ctx.ModuleForTests(test.moduleName, test.variantName)
Paul Duffina71a67a2021-03-29 00:42:57 +01003097 params := variant.MaybeOutput("test_config_fixer/AndroidTest.xml")
Jaewoong Jung39982342020-01-14 10:27:18 -08003098
3099 if len(test.expectedFlags) > 0 {
3100 if params.Rule == nil {
3101 t.Errorf("test_config_fixer was expected to run, but didn't")
3102 } else {
3103 for _, flag := range test.expectedFlags {
3104 if !strings.Contains(params.RuleParams.Command, flag) {
3105 t.Errorf("Flag %q was not found in command: %q", flag, params.RuleParams.Command)
3106 }
3107 }
3108 }
3109 } else {
3110 if params.Rule != nil {
3111 t.Errorf("test_config_fixer was not expected to run, but did: %q", params.RuleParams.Command)
3112 }
3113 }
Jaewoong Jung39982342020-01-14 10:27:18 -08003114 }
3115}
3116
Paul Duffin53a70a42022-01-11 14:35:55 +00003117func TestInstrumentationTargetPrebuilt(t *testing.T) {
3118 bp := `
3119 android_app_import {
3120 name: "foo",
3121 apk: "foo.apk",
3122 presigned: true,
3123 }
3124
3125 android_test {
3126 name: "bar",
3127 srcs: ["a.java"],
3128 instrumentation_for: "foo",
3129 sdk_version: "current",
3130 }
3131 `
3132
3133 android.GroupFixturePreparers(
3134 PrepareForTestWithJavaDefaultModules,
3135 ).ExtendWithErrorHandler(
3136 android.FixtureExpectsAtLeastOneErrorMatchingPattern(
3137 "instrumentation_for: dependency \"foo\" of type \"android_app_import\" does not provide JavaInfo so is unsuitable for use with this property")).
3138 RunTestWithBp(t, bp)
3139}
3140
Jaewoong Jungbc625cd2019-05-06 15:48:44 -07003141func TestStl(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07003142 ctx, _ := testJava(t, cc.GatherRequiredDepsForTest(android.Android)+`
Jaewoong Jungbc625cd2019-05-06 15:48:44 -07003143 cc_library {
3144 name: "libjni",
Peter Collingbournead84f972019-12-17 16:46:18 -08003145 sdk_version: "current",
3146 stl: "c++_shared",
Jaewoong Jungbc625cd2019-05-06 15:48:44 -07003147 }
3148
3149 android_test {
3150 name: "stl",
3151 jni_libs: ["libjni"],
3152 compile_multilib: "both",
3153 sdk_version: "current",
3154 stl: "c++_shared",
3155 }
3156
3157 android_test {
3158 name: "system",
3159 jni_libs: ["libjni"],
3160 compile_multilib: "both",
3161 sdk_version: "current",
3162 }
3163 `)
3164
3165 testCases := []struct {
3166 name string
3167 jnis []string
3168 }{
3169 {"stl",
3170 []string{
3171 "libjni.so",
Jaewoong Jung710756a2019-06-04 11:53:47 -07003172 "libc++_shared.so",
Jaewoong Jungbc625cd2019-05-06 15:48:44 -07003173 },
3174 },
3175 {"system",
3176 []string{
3177 "libjni.so",
3178 },
3179 },
3180 }
3181
3182 for _, test := range testCases {
3183 t.Run(test.name, func(t *testing.T) {
3184 app := ctx.ModuleForTests(test.name, "android_common")
Colin Crossb3168ba2023-07-26 16:14:56 -07003185 jniLibZip := app.Output("jnilibs.zip")
Jaewoong Jungbc625cd2019-05-06 15:48:44 -07003186 var jnis []string
3187 args := strings.Fields(jniLibZip.Args["jarArgs"])
3188 for i := 0; i < len(args); i++ {
3189 if args[i] == "-f" {
3190 jnis = append(jnis, args[i+1])
3191 i += 1
3192 }
3193 }
3194 jnisJoined := strings.Join(jnis, " ")
3195 for _, jni := range test.jnis {
3196 if !strings.Contains(jnisJoined, jni) {
3197 t.Errorf("missing jni %q in %q", jni, jnis)
3198 }
3199 }
3200 })
3201 }
3202}
Colin Cross50ddcc42019-05-16 12:28:22 -07003203
3204func TestUsesLibraries(t *testing.T) {
3205 bp := `
3206 java_sdk_library {
3207 name: "foo",
3208 srcs: ["a.java"],
3209 api_packages: ["foo"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09003210 sdk_version: "current",
Colin Cross50ddcc42019-05-16 12:28:22 -07003211 }
3212
3213 java_sdk_library {
Paul Duffin859fe962020-05-15 10:20:31 +01003214 name: "qux",
3215 srcs: ["a.java"],
3216 api_packages: ["qux"],
3217 sdk_version: "current",
3218 }
3219
3220 java_sdk_library {
3221 name: "quuz",
3222 srcs: ["a.java"],
3223 api_packages: ["quuz"],
3224 sdk_version: "current",
3225 }
3226
3227 java_sdk_library {
Ulya Trafimovich65b03192020-12-03 16:50:22 +00003228 name: "fred",
3229 srcs: ["a.java"],
3230 api_packages: ["fred"],
3231 sdk_version: "current",
3232 }
3233
3234 java_sdk_library {
Colin Cross50ddcc42019-05-16 12:28:22 -07003235 name: "bar",
3236 srcs: ["a.java"],
3237 api_packages: ["bar"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09003238 sdk_version: "current",
Colin Cross50ddcc42019-05-16 12:28:22 -07003239 }
3240
Ulya Trafimovich4b6d4c12020-08-19 14:58:01 +01003241 java_sdk_library {
3242 name: "runtime-library",
3243 srcs: ["a.java"],
3244 sdk_version: "current",
3245 }
3246
3247 java_library {
3248 name: "static-runtime-helper",
3249 srcs: ["a.java"],
3250 libs: ["runtime-library"],
3251 sdk_version: "current",
3252 }
3253
Ulya Trafimoviche4432872021-08-18 16:57:11 +01003254 java_library {
3255 name: "runtime-required-x",
3256 srcs: ["a.java"],
3257 installable: true,
3258 sdk_version: "current",
3259 }
3260
3261 java_library {
3262 name: "runtime-optional-x",
3263 srcs: ["a.java"],
3264 installable: true,
3265 sdk_version: "current",
3266 }
3267
3268 android_library {
3269 name: "static-x",
3270 uses_libs: ["runtime-required-x"],
3271 optional_uses_libs: ["runtime-optional-x"],
3272 sdk_version: "current",
3273 }
3274
3275 java_library {
3276 name: "runtime-required-y",
3277 srcs: ["a.java"],
3278 installable: true,
3279 sdk_version: "current",
3280 }
3281
3282 java_library {
3283 name: "runtime-optional-y",
3284 srcs: ["a.java"],
3285 installable: true,
3286 sdk_version: "current",
3287 }
3288
3289 java_library {
3290 name: "static-y",
3291 srcs: ["a.java"],
3292 uses_libs: ["runtime-required-y"],
Jiakai Zhang36937082024-04-15 11:15:50 +00003293 optional_uses_libs: [
3294 "runtime-optional-y",
3295 "missing-lib-a",
3296 ],
Ulya Trafimoviche4432872021-08-18 16:57:11 +01003297 sdk_version: "current",
3298 }
3299
Ulya Trafimovich861a8962021-02-26 14:49:07 +00003300 // A library that has to use "provides_uses_lib", because:
3301 // - it is not an SDK library
3302 // - its library name is different from its module name
3303 java_library {
3304 name: "non-sdk-lib",
3305 provides_uses_lib: "com.non.sdk.lib",
3306 installable: true,
3307 srcs: ["a.java"],
3308 }
3309
Colin Cross50ddcc42019-05-16 12:28:22 -07003310 android_app {
3311 name: "app",
3312 srcs: ["a.java"],
Ulya Trafimovich861a8962021-02-26 14:49:07 +00003313 libs: [
3314 "qux",
3315 "quuz.stubs"
3316 ],
Ulya Trafimovich65b03192020-12-03 16:50:22 +00003317 static_libs: [
3318 "static-runtime-helper",
3319 // statically linked component libraries should not pull their SDK libraries,
3320 // so "fred" should not be added to class loader context
3321 "fred.stubs",
Ulya Trafimoviche4432872021-08-18 16:57:11 +01003322 "static-x",
3323 "static-y",
Ulya Trafimovich65b03192020-12-03 16:50:22 +00003324 ],
Ulya Trafimovich861a8962021-02-26 14:49:07 +00003325 uses_libs: [
3326 "foo",
3327 "non-sdk-lib"
3328 ],
Jeongik Cha538c0d02019-07-11 15:54:27 +09003329 sdk_version: "current",
Colin Cross50ddcc42019-05-16 12:28:22 -07003330 optional_uses_libs: [
3331 "bar",
Jiakai Zhangf98da192024-04-15 11:15:41 +00003332 "missing-lib-b",
Colin Cross50ddcc42019-05-16 12:28:22 -07003333 ],
3334 }
3335
3336 android_app_import {
3337 name: "prebuilt",
3338 apk: "prebuilts/apk/app.apk",
3339 certificate: "platform",
Ulya Trafimovich861a8962021-02-26 14:49:07 +00003340 uses_libs: [
3341 "foo",
3342 "non-sdk-lib",
3343 "android.test.runner"
3344 ],
Colin Cross50ddcc42019-05-16 12:28:22 -07003345 optional_uses_libs: [
3346 "bar",
Jiakai Zhangf98da192024-04-15 11:15:41 +00003347 "missing-lib-b",
Colin Cross50ddcc42019-05-16 12:28:22 -07003348 ],
3349 }
3350 `
3351
Paul Duffin71ae5942021-03-22 15:36:52 +00003352 result := android.GroupFixturePreparers(
3353 prepareForJavaTest,
Paul Duffin2645a292021-03-13 02:36:00 +00003354 PrepareForTestWithJavaSdkLibraryFiles,
3355 FixtureWithLastReleaseApis("runtime-library", "foo", "quuz", "qux", "bar", "fred"),
Jiakai Zhang48203e32023-06-02 23:42:21 +01003356 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
3357 variables.BuildWarningBadOptionalUsesLibsAllowlist = []string{"app", "prebuilt"}
3358 }),
Paul Duffind234b412021-03-12 23:04:46 +00003359 ).RunTestWithBp(t, bp)
Colin Cross50ddcc42019-05-16 12:28:22 -07003360
Paul Duffind234b412021-03-12 23:04:46 +00003361 app := result.ModuleForTests("app", "android_common")
3362 prebuilt := result.ModuleForTests("prebuilt", "android_common")
Colin Cross50ddcc42019-05-16 12:28:22 -07003363
Paul Duffin859fe962020-05-15 10:20:31 +01003364 // Test that implicit dependencies on java_sdk_library instances are passed to the manifest.
Ulya Trafimovichf5d91bb2022-05-04 12:00:02 +01003365 // These also include explicit `uses_libs`/`optional_uses_libs` entries, as they may be
3366 // propagated from dependencies.
Ulya Trafimovich2eaf5c52021-02-26 12:05:11 +00003367 actualManifestFixerArgs := app.Output("manifest_fixer/AndroidManifest.xml").Args["args"]
Jiyong Parkd044bb42024-05-15 02:09:54 +09003368 expectManifestFixerArgs := `--extract-native-libs=true ` +
3369 `--uses-library foo ` +
Ulya Trafimovichf5d91bb2022-05-04 12:00:02 +01003370 `--uses-library com.non.sdk.lib ` +
Jiakai Zhangf98da192024-04-15 11:15:41 +00003371 `--uses-library qux ` +
3372 `--uses-library quuz ` +
Ulya Trafimovichf5d91bb2022-05-04 12:00:02 +01003373 `--uses-library runtime-library ` +
3374 `--uses-library runtime-required-x ` +
3375 `--uses-library runtime-required-y ` +
3376 `--optional-uses-library bar ` +
3377 `--optional-uses-library runtime-optional-x ` +
3378 `--optional-uses-library runtime-optional-y`
Gurpreet Singh75d65f32022-01-24 17:44:05 +00003379 android.AssertStringDoesContain(t, "manifest_fixer args", actualManifestFixerArgs, expectManifestFixerArgs)
Paul Duffin859fe962020-05-15 10:20:31 +01003380
Ulya Trafimovich2eaf5c52021-02-26 12:05:11 +00003381 // Test that all libraries are verified (library order matters).
3382 verifyCmd := app.Rule("verify_uses_libraries").RuleParams.Command
3383 verifyArgs := `--uses-library foo ` +
Ulya Trafimovicheea486a2021-02-26 11:38:21 +00003384 `--uses-library com.non.sdk.lib ` +
Ulya Trafimovich2eaf5c52021-02-26 12:05:11 +00003385 `--uses-library qux ` +
3386 `--uses-library quuz ` +
3387 `--uses-library runtime-library ` +
Ulya Trafimoviche4432872021-08-18 16:57:11 +01003388 `--uses-library runtime-required-x ` +
3389 `--uses-library runtime-required-y ` +
Ulya Trafimovich2eaf5c52021-02-26 12:05:11 +00003390 `--optional-uses-library bar ` +
Ulya Trafimoviche4432872021-08-18 16:57:11 +01003391 `--optional-uses-library runtime-optional-x ` +
Jiakai Zhangf98da192024-04-15 11:15:41 +00003392 `--optional-uses-library runtime-optional-y ` +
Jiakai Zhang36937082024-04-15 11:15:50 +00003393 `--missing-optional-uses-library missing-lib-b ` +
3394 `--missing-optional-uses-library missing-lib-a`
Paul Duffind234b412021-03-12 23:04:46 +00003395 android.AssertStringDoesContain(t, "verify cmd args", verifyCmd, verifyArgs)
Colin Cross50ddcc42019-05-16 12:28:22 -07003396
Ulya Trafimovich2eaf5c52021-02-26 12:05:11 +00003397 // Test that all libraries are verified for an APK (library order matters).
3398 verifyApkCmd := prebuilt.Rule("verify_uses_libraries").RuleParams.Command
Ulya Trafimovich0aba2522021-03-03 16:38:37 +00003399 verifyApkArgs := `--uses-library foo ` +
3400 `--uses-library com.non.sdk.lib ` +
3401 `--uses-library android.test.runner ` +
3402 `--optional-uses-library bar ` +
Jiakai Zhangf98da192024-04-15 11:15:41 +00003403 `--missing-optional-uses-library missing-lib-b `
Paul Duffind234b412021-03-12 23:04:46 +00003404 android.AssertStringDoesContain(t, "verify apk cmd args", verifyApkCmd, verifyApkArgs)
Colin Cross50ddcc42019-05-16 12:28:22 -07003405
Jiakai Zhanga4496782023-05-17 16:57:30 +01003406 // Test that necessary args are passed for constructing CLC in Ninja phase.
Ulya Trafimovich2eaf5c52021-02-26 12:05:11 +00003407 cmd := app.Rule("dexpreopt").RuleParams.Command
Jiakai Zhanga4496782023-05-17 16:57:30 +01003408 android.AssertStringDoesContain(t, "dexpreopt app cmd context", cmd, "--context-json=")
3409 android.AssertStringDoesContain(t, "dexpreopt app cmd product_packages", cmd,
Spandan Das2069c3f2023-12-06 19:40:24 +00003410 "--product-packages=out/soong/.intermediates/app/android_common/dexpreopt/app/product_packages.txt")
Colin Cross50ddcc42019-05-16 12:28:22 -07003411}
Jaewoong Jungc27ab662019-05-30 15:51:14 -07003412
Ulya Trafimovich9023b022021-03-22 16:02:28 +00003413func TestDexpreoptBcp(t *testing.T) {
3414 bp := `
3415 java_sdk_library {
3416 name: "foo",
3417 srcs: ["a.java"],
3418 api_packages: ["foo"],
3419 sdk_version: "current",
3420 }
3421
3422 java_sdk_library {
3423 name: "bar",
3424 srcs: ["a.java"],
3425 api_packages: ["bar"],
3426 permitted_packages: ["bar"],
3427 sdk_version: "current",
3428 }
3429
3430 android_app {
3431 name: "app",
3432 srcs: ["a.java"],
3433 sdk_version: "current",
3434 }
3435 `
3436
3437 testCases := []struct {
3438 name string
3439 with bool
3440 expect string
3441 }{
3442 {
3443 name: "with updatable bcp",
3444 with: true,
3445 expect: "/system/framework/foo.jar:/system/framework/bar.jar",
3446 },
3447 {
3448 name: "without updatable bcp",
3449 with: false,
3450 expect: "/system/framework/foo.jar",
3451 },
3452 }
3453
3454 for _, test := range testCases {
3455 t.Run(test.name, func(t *testing.T) {
3456 result := android.GroupFixturePreparers(
3457 prepareForJavaTest,
3458 PrepareForTestWithJavaSdkLibraryFiles,
3459 FixtureWithLastReleaseApis("runtime-library", "foo", "bar"),
3460 dexpreopt.FixtureSetBootJars("platform:foo"),
satayevd604b212021-07-21 14:23:52 +01003461 dexpreopt.FixtureSetApexBootJars("platform:bar"),
Ulya Trafimovich9023b022021-03-22 16:02:28 +00003462 dexpreopt.FixtureSetPreoptWithUpdatableBcp(test.with),
3463 ).RunTestWithBp(t, bp)
3464
3465 app := result.ModuleForTests("app", "android_common")
3466 cmd := app.Rule("dexpreopt").RuleParams.Command
3467 bcp := " -Xbootclasspath-locations:" + test.expect + " " // space at the end matters
3468 android.AssertStringDoesContain(t, "dexpreopt app bcp", cmd, bcp)
3469 })
3470 }
3471}
3472
Jaewoong Jungc27ab662019-05-30 15:51:14 -07003473func TestCodelessApp(t *testing.T) {
3474 testCases := []struct {
3475 name string
3476 bp string
3477 noCode bool
3478 }{
3479 {
3480 name: "normal",
3481 bp: `
3482 android_app {
3483 name: "foo",
3484 srcs: ["a.java"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09003485 sdk_version: "current",
Jaewoong Jungc27ab662019-05-30 15:51:14 -07003486 }
3487 `,
3488 noCode: false,
3489 },
3490 {
3491 name: "app without sources",
3492 bp: `
3493 android_app {
3494 name: "foo",
Jeongik Cha538c0d02019-07-11 15:54:27 +09003495 sdk_version: "current",
Jaewoong Jungc27ab662019-05-30 15:51:14 -07003496 }
3497 `,
3498 noCode: true,
3499 },
3500 {
3501 name: "app with libraries",
3502 bp: `
3503 android_app {
3504 name: "foo",
3505 static_libs: ["lib"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09003506 sdk_version: "current",
Jaewoong Jungc27ab662019-05-30 15:51:14 -07003507 }
3508
3509 java_library {
3510 name: "lib",
3511 srcs: ["a.java"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09003512 sdk_version: "current",
Jaewoong Jungc27ab662019-05-30 15:51:14 -07003513 }
3514 `,
3515 noCode: false,
3516 },
3517 {
3518 name: "app with sourceless libraries",
3519 bp: `
3520 android_app {
3521 name: "foo",
3522 static_libs: ["lib"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09003523 sdk_version: "current",
Jaewoong Jungc27ab662019-05-30 15:51:14 -07003524 }
3525
3526 java_library {
3527 name: "lib",
Jeongik Cha538c0d02019-07-11 15:54:27 +09003528 sdk_version: "current",
Jaewoong Jungc27ab662019-05-30 15:51:14 -07003529 }
3530 `,
3531 // TODO(jungjw): this should probably be true
3532 noCode: false,
3533 },
3534 }
3535
3536 for _, test := range testCases {
3537 t.Run(test.name, func(t *testing.T) {
3538 ctx := testApp(t, test.bp)
3539
3540 foo := ctx.ModuleForTests("foo", "android_common")
3541 manifestFixerArgs := foo.Output("manifest_fixer/AndroidManifest.xml").Args["args"]
3542 if strings.Contains(manifestFixerArgs, "--has-no-code") != test.noCode {
3543 t.Errorf("unexpected manifest_fixer args: %q", manifestFixerArgs)
3544 }
3545 })
3546 }
3547}
Jaewoong Jung5b425e22019-06-17 17:40:56 -07003548
Colin Cross53a87f52019-06-25 13:35:30 -07003549func TestUncompressDex(t *testing.T) {
3550 testCases := []struct {
3551 name string
3552 bp string
3553
3554 uncompressedPlatform bool
3555 uncompressedUnbundled bool
3556 }{
3557 {
3558 name: "normal",
3559 bp: `
3560 android_app {
3561 name: "foo",
3562 srcs: ["a.java"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09003563 sdk_version: "current",
Colin Cross53a87f52019-06-25 13:35:30 -07003564 }
3565 `,
3566 uncompressedPlatform: true,
3567 uncompressedUnbundled: false,
3568 },
3569 {
3570 name: "use_embedded_dex",
3571 bp: `
3572 android_app {
3573 name: "foo",
3574 use_embedded_dex: true,
3575 srcs: ["a.java"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09003576 sdk_version: "current",
Colin Cross53a87f52019-06-25 13:35:30 -07003577 }
3578 `,
3579 uncompressedPlatform: true,
3580 uncompressedUnbundled: true,
3581 },
3582 {
3583 name: "privileged",
3584 bp: `
3585 android_app {
3586 name: "foo",
3587 privileged: true,
3588 srcs: ["a.java"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09003589 sdk_version: "current",
Colin Cross53a87f52019-06-25 13:35:30 -07003590 }
3591 `,
3592 uncompressedPlatform: true,
3593 uncompressedUnbundled: true,
3594 },
David Srbeckye033cba2020-05-20 22:20:28 +01003595 {
3596 name: "normal_uncompress_dex_true",
3597 bp: `
3598 android_app {
3599 name: "foo",
3600 srcs: ["a.java"],
3601 sdk_version: "current",
3602 uncompress_dex: true,
3603 }
3604 `,
3605 uncompressedPlatform: true,
3606 uncompressedUnbundled: true,
3607 },
3608 {
3609 name: "normal_uncompress_dex_false",
3610 bp: `
3611 android_app {
3612 name: "foo",
3613 srcs: ["a.java"],
3614 sdk_version: "current",
3615 uncompress_dex: false,
3616 }
3617 `,
3618 uncompressedPlatform: false,
3619 uncompressedUnbundled: false,
3620 },
Colin Cross53a87f52019-06-25 13:35:30 -07003621 }
3622
3623 test := func(t *testing.T, bp string, want bool, unbundled bool) {
3624 t.Helper()
3625
Paul Duffin71ae5942021-03-22 15:36:52 +00003626 result := android.GroupFixturePreparers(
3627 prepareForJavaTest,
Paul Duffin2645a292021-03-13 02:36:00 +00003628 PrepareForTestWithPrebuiltsOfCurrentApi,
Paul Duffincdb88a92021-03-14 00:36:50 +00003629 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
3630 if unbundled {
3631 variables.Unbundled_build = proptools.BoolPtr(true)
3632 variables.Always_use_prebuilt_sdks = proptools.BoolPtr(true)
3633 }
3634 }),
3635 ).RunTestWithBp(t, bp)
Colin Cross53a87f52019-06-25 13:35:30 -07003636
Paul Duffincdb88a92021-03-14 00:36:50 +00003637 foo := result.ModuleForTests("foo", "android_common")
Colin Cross53a87f52019-06-25 13:35:30 -07003638 dex := foo.Rule("r8")
3639 uncompressedInDexJar := strings.Contains(dex.Args["zipFlags"], "-L 0")
3640 aligned := foo.MaybeRule("zipalign").Rule != nil
3641
Paul Duffincdb88a92021-03-14 00:36:50 +00003642 android.AssertBoolEquals(t, "uncompressed in dex", want, uncompressedInDexJar)
Colin Cross53a87f52019-06-25 13:35:30 -07003643
Paul Duffincdb88a92021-03-14 00:36:50 +00003644 android.AssertBoolEquals(t, "aligne", want, aligned)
Colin Cross53a87f52019-06-25 13:35:30 -07003645 }
3646
3647 for _, tt := range testCases {
3648 t.Run(tt.name, func(t *testing.T) {
3649 t.Run("platform", func(t *testing.T) {
3650 test(t, tt.bp, tt.uncompressedPlatform, false)
3651 })
3652 t.Run("unbundled", func(t *testing.T) {
3653 test(t, tt.bp, tt.uncompressedUnbundled, true)
3654 })
3655 })
3656 }
3657}
Jaewoong Jung26dedd32019-06-06 08:45:58 -07003658
3659func checkAapt2LinkFlag(t *testing.T, aapt2Flags, flagName, expectedValue string) {
3660 if expectedValue != "" {
3661 expectedFlag := "--" + flagName + " " + expectedValue
3662 if !strings.Contains(aapt2Flags, expectedFlag) {
3663 t.Errorf("%q is missing in aapt2 link flags, %q", expectedFlag, aapt2Flags)
3664 }
3665 } else {
3666 unexpectedFlag := "--" + flagName
3667 if strings.Contains(aapt2Flags, unexpectedFlag) {
3668 t.Errorf("unexpected flag, %q is found in aapt2 link flags, %q", unexpectedFlag, aapt2Flags)
3669 }
3670 }
3671}
Jaewoong Jung9befb0c2020-01-18 10:33:43 -08003672
Cole Faust9a631312020-10-22 21:05:24 +00003673func TestExportedProguardFlagFiles(t *testing.T) {
3674 ctx, _ := testJava(t, `
3675 android_app {
3676 name: "foo",
3677 sdk_version: "current",
Colin Crosscde55342024-03-27 14:11:51 -07003678 static_libs: [
3679 "lib1",
3680 "lib3",
3681 ],
Cole Faust9a631312020-10-22 21:05:24 +00003682 }
3683
3684 android_library {
3685 name: "lib1",
3686 sdk_version: "current",
3687 optimize: {
3688 proguard_flags_files: ["lib1proguard.cfg"],
Colin Crosscde55342024-03-27 14:11:51 -07003689 },
3690 static_libs: ["lib2"],
3691 }
3692
3693 android_library {
3694 name: "lib2",
3695 sdk_version: "current",
3696 optimize: {
3697 proguard_flags_files: ["lib2proguard.cfg"],
Cole Faust9a631312020-10-22 21:05:24 +00003698 }
3699 }
Colin Crosscde55342024-03-27 14:11:51 -07003700
3701 android_library_import {
3702 name: "lib3",
3703 sdk_version: "current",
3704 aars: ["lib3.aar"],
3705 static_libs: ["lib4"],
3706 }
3707
3708 android_library {
3709 name: "lib4",
3710 sdk_version: "current",
3711 optimize: {
3712 proguard_flags_files: ["lib4proguard.cfg"],
3713 }
3714 }
3715
3716
Cole Faust9a631312020-10-22 21:05:24 +00003717 `)
3718
3719 m := ctx.ModuleForTests("foo", "android_common")
Colin Crosscde55342024-03-27 14:11:51 -07003720 r8 := m.Rule("java.r8")
3721 implicits := r8.Implicits.RelativeToTop().Strings()
3722 android.AssertStringListContains(t, "r8 implicits", implicits, "lib1proguard.cfg")
3723 android.AssertStringListContains(t, "r8 implicits", implicits, "lib2proguard.cfg")
3724 android.AssertStringListContains(t, "r8 implicits", implicits, "lib4proguard.cfg")
3725 android.AssertStringListContains(t, "r8 implicits", implicits, "out/soong/.intermediates/lib3/android_common/aar/proguard.txt")
Cole Faust9a631312020-10-22 21:05:24 +00003726
Colin Crosscde55342024-03-27 14:11:51 -07003727 flags := r8.Args["r8Flags"]
3728 android.AssertStringDoesContain(t, "r8 flags", flags, "-include lib1proguard.cfg")
3729 android.AssertStringDoesContain(t, "r8 flags", flags, "-include lib2proguard.cfg")
3730 android.AssertStringDoesContain(t, "r8 flags", flags, "-include lib4proguard.cfg")
3731 android.AssertStringDoesContain(t, "r8 flags", flags, "-include out/soong/.intermediates/lib3/android_common/aar/proguard.txt")
Cole Faust9a631312020-10-22 21:05:24 +00003732}
Spandan Dasc8cb0c32021-12-08 17:00:38 +00003733
3734func TestTargetSdkVersionManifestFixer(t *testing.T) {
3735 platform_sdk_codename := "Tiramisu"
Spandan Dasfb6a1ee2023-04-27 16:08:26 +00003736 platform_sdk_version := 33
Spandan Dasc8cb0c32021-12-08 17:00:38 +00003737 testCases := []struct {
3738 name string
3739 targetSdkVersionInBp string
3740 targetSdkVersionExpected string
3741 unbundledBuild bool
Spandan Dasfb6a1ee2023-04-27 16:08:26 +00003742 platformSdkFinal bool
Spandan Dasc8cb0c32021-12-08 17:00:38 +00003743 }{
3744 {
3745 name: "Non-Unbundled build: Android.bp has targetSdkVersion",
3746 targetSdkVersionInBp: "30",
3747 targetSdkVersionExpected: "30",
3748 unbundledBuild: false,
3749 },
3750 {
3751 name: "Unbundled build: Android.bp has targetSdkVersion",
3752 targetSdkVersionInBp: "30",
3753 targetSdkVersionExpected: "30",
3754 unbundledBuild: true,
3755 },
3756 {
3757 name: "Non-Unbundled build: Android.bp has targetSdkVersion equal to platform_sdk_codename",
3758 targetSdkVersionInBp: platform_sdk_codename,
3759 targetSdkVersionExpected: platform_sdk_codename,
3760 unbundledBuild: false,
3761 },
3762 {
3763 name: "Unbundled build: Android.bp has targetSdkVersion equal to platform_sdk_codename",
3764 targetSdkVersionInBp: platform_sdk_codename,
3765 targetSdkVersionExpected: "10000",
3766 unbundledBuild: true,
3767 },
3768
3769 {
3770 name: "Non-Unbundled build: Android.bp has no targetSdkVersion",
3771 targetSdkVersionExpected: platform_sdk_codename,
3772 unbundledBuild: false,
3773 },
3774 {
3775 name: "Unbundled build: Android.bp has no targetSdkVersion",
3776 targetSdkVersionExpected: "10000",
3777 unbundledBuild: true,
3778 },
Spandan Dasfb6a1ee2023-04-27 16:08:26 +00003779 {
3780 name: "Bundled build in REL branches",
3781 targetSdkVersionExpected: "33",
3782 unbundledBuild: false,
3783 platformSdkFinal: true,
3784 },
Spandan Dasc8cb0c32021-12-08 17:00:38 +00003785 }
3786 for _, testCase := range testCases {
Spandan Dasca70fc42023-03-01 23:38:49 +00003787 targetSdkVersionTemplate := ""
3788 if testCase.targetSdkVersionInBp != "" {
3789 targetSdkVersionTemplate = fmt.Sprintf(`target_sdk_version: "%s",`, testCase.targetSdkVersionInBp)
3790 }
Spandan Dasc8cb0c32021-12-08 17:00:38 +00003791 bp := fmt.Sprintf(`
3792 android_app {
3793 name: "foo",
3794 sdk_version: "current",
Spandan Dasca70fc42023-03-01 23:38:49 +00003795 %s
Spandan Dasc8cb0c32021-12-08 17:00:38 +00003796 }
Spandan Dasca70fc42023-03-01 23:38:49 +00003797 `, targetSdkVersionTemplate)
Spandan Dasc8cb0c32021-12-08 17:00:38 +00003798 fixture := android.GroupFixturePreparers(
3799 prepareForJavaTest,
3800 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
Spandan Dasfb6a1ee2023-04-27 16:08:26 +00003801 if testCase.platformSdkFinal {
3802 variables.Platform_sdk_final = proptools.BoolPtr(true)
3803 }
Spandan Dasc8cb0c32021-12-08 17:00:38 +00003804 // explicitly set platform_sdk_codename to make the test deterministic
3805 variables.Platform_sdk_codename = &platform_sdk_codename
Spandan Dasfb6a1ee2023-04-27 16:08:26 +00003806 variables.Platform_sdk_version = &platform_sdk_version
Spandan Dasc8cb0c32021-12-08 17:00:38 +00003807 variables.Platform_version_active_codenames = []string{platform_sdk_codename}
3808 // create a non-empty list if unbundledBuild==true
3809 if testCase.unbundledBuild {
3810 variables.Unbundled_build_apps = []string{"apex_a", "apex_b"}
3811 }
3812 }),
3813 )
3814
3815 result := fixture.RunTestWithBp(t, bp)
3816 foo := result.ModuleForTests("foo", "android_common")
3817
Gurpreet Singh75d65f32022-01-24 17:44:05 +00003818 manifestFixerArgs := foo.Output("manifest_fixer/AndroidManifest.xml").Args["args"]
3819 android.AssertStringDoesContain(t, testCase.name, manifestFixerArgs, "--targetSdkVersion "+testCase.targetSdkVersionExpected)
Spandan Dasc8cb0c32021-12-08 17:00:38 +00003820 }
3821}
Colin Cross412436f2022-04-07 17:40:07 -07003822
Harshit Mahajan5b8b7302022-06-10 11:24:05 +00003823func TestDefaultAppTargetSdkVersionForUpdatableModules(t *testing.T) {
3824 platform_sdk_codename := "Tiramisu"
3825 platform_sdk_version := 33
3826 testCases := []struct {
3827 name string
3828 platform_sdk_final bool
3829 targetSdkVersionInBp *string
3830 targetSdkVersionExpected *string
3831 updatable bool
3832 }{
3833 {
3834 name: "Non-Updatable Module: Android.bp has older targetSdkVersion",
3835 targetSdkVersionInBp: proptools.StringPtr("29"),
3836 targetSdkVersionExpected: proptools.StringPtr("29"),
3837 updatable: false,
3838 },
3839 {
3840 name: "Updatable Module: Android.bp has older targetSdkVersion",
3841 targetSdkVersionInBp: proptools.StringPtr("30"),
3842 targetSdkVersionExpected: proptools.StringPtr("30"),
3843 updatable: true,
3844 },
3845 {
3846 name: "Updatable Module: Android.bp has no targetSdkVersion",
3847 targetSdkVersionExpected: proptools.StringPtr("10000"),
3848 updatable: true,
3849 },
3850 {
3851 name: "[SDK finalised] Non-Updatable Module: Android.bp has older targetSdkVersion",
3852 platform_sdk_final: true,
3853 targetSdkVersionInBp: proptools.StringPtr("30"),
3854 targetSdkVersionExpected: proptools.StringPtr("30"),
3855 updatable: false,
3856 },
3857 {
3858 name: "[SDK finalised] Updatable Module: Android.bp has older targetSdkVersion",
3859 platform_sdk_final: true,
3860 targetSdkVersionInBp: proptools.StringPtr("30"),
3861 targetSdkVersionExpected: proptools.StringPtr("30"),
3862 updatable: true,
3863 },
3864 {
3865 name: "[SDK finalised] Updatable Module: Android.bp has targetSdkVersion as platform sdk codename",
3866 platform_sdk_final: true,
3867 targetSdkVersionInBp: proptools.StringPtr(platform_sdk_codename),
3868 targetSdkVersionExpected: proptools.StringPtr("33"),
3869 updatable: true,
3870 },
3871 {
3872 name: "[SDK finalised] Updatable Module: Android.bp has no targetSdkVersion",
3873 platform_sdk_final: true,
3874 targetSdkVersionExpected: proptools.StringPtr("33"),
3875 updatable: true,
3876 },
3877 }
3878 for _, testCase := range testCases {
Spandan Dasca70fc42023-03-01 23:38:49 +00003879 targetSdkVersionTemplate := ""
3880 if testCase.targetSdkVersionInBp != nil {
3881 targetSdkVersionTemplate = fmt.Sprintf(`target_sdk_version: "%s",`, *testCase.targetSdkVersionInBp)
3882 }
Harshit Mahajan5b8b7302022-06-10 11:24:05 +00003883 bp := fmt.Sprintf(`
3884 android_app {
3885 name: "foo",
3886 sdk_version: "current",
3887 min_sdk_version: "29",
Spandan Dasca70fc42023-03-01 23:38:49 +00003888 %s
Harshit Mahajan5b8b7302022-06-10 11:24:05 +00003889 updatable: %t,
3890 enforce_default_target_sdk_version: %t
3891 }
Spandan Dasca70fc42023-03-01 23:38:49 +00003892 `, targetSdkVersionTemplate, testCase.updatable, testCase.updatable) // enforce default target sdk version if app is updatable
Harshit Mahajan5b8b7302022-06-10 11:24:05 +00003893
3894 fixture := android.GroupFixturePreparers(
3895 PrepareForTestWithJavaDefaultModules,
3896 android.PrepareForTestWithAllowMissingDependencies,
3897 android.PrepareForTestWithAndroidMk,
3898 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
3899 // explicitly set following platform variables to make the test deterministic
3900 variables.Platform_sdk_final = &testCase.platform_sdk_final
3901 variables.Platform_sdk_version = &platform_sdk_version
3902 variables.Platform_sdk_codename = &platform_sdk_codename
3903 variables.Platform_version_active_codenames = []string{platform_sdk_codename}
Harshit Mahajan18845d02023-01-11 18:39:12 +00003904 variables.Unbundled_build = proptools.BoolPtr(true)
Harshit Mahajan5b8b7302022-06-10 11:24:05 +00003905 variables.Unbundled_build_apps = []string{"sampleModule"}
3906 }),
3907 )
3908
3909 result := fixture.RunTestWithBp(t, bp)
3910 foo := result.ModuleForTests("foo", "android_common")
3911
3912 manifestFixerArgs := foo.Output("manifest_fixer/AndroidManifest.xml").Args["args"]
3913 android.AssertStringDoesContain(t, testCase.name, manifestFixerArgs, "--targetSdkVersion "+*testCase.targetSdkVersionExpected)
3914 }
3915}
3916
3917func TestEnforceDefaultAppTargetSdkVersionFlag(t *testing.T) {
3918 platform_sdk_codename := "Tiramisu"
3919 platform_sdk_version := 33
3920 testCases := []struct {
3921 name string
3922 enforceDefaultTargetSdkVersion bool
3923 expectedError string
3924 platform_sdk_final bool
3925 targetSdkVersionInBp string
3926 targetSdkVersionExpected string
3927 updatable bool
3928 }{
3929 {
3930 name: "Not enforcing Target SDK Version: Android.bp has older targetSdkVersion",
3931 enforceDefaultTargetSdkVersion: false,
3932 targetSdkVersionInBp: "29",
3933 targetSdkVersionExpected: "29",
3934 updatable: false,
3935 },
3936 {
3937 name: "[SDK finalised] Enforce Target SDK Version: Android.bp has current targetSdkVersion",
3938 enforceDefaultTargetSdkVersion: true,
3939 platform_sdk_final: true,
3940 targetSdkVersionInBp: "current",
3941 targetSdkVersionExpected: "33",
3942 updatable: true,
3943 },
3944 {
Harshit Mahajan8f202ad2023-01-09 20:45:55 +00003945 name: "Enforce Target SDK Version: Android.bp has current targetSdkVersion",
Harshit Mahajan5b8b7302022-06-10 11:24:05 +00003946 enforceDefaultTargetSdkVersion: true,
3947 platform_sdk_final: false,
3948 targetSdkVersionInBp: "current",
3949 targetSdkVersionExpected: "10000",
3950 updatable: false,
3951 },
3952 {
3953 name: "Not enforcing Target SDK Version for Updatable app",
3954 enforceDefaultTargetSdkVersion: false,
3955 expectedError: "Updatable apps must enforce default target sdk version",
3956 targetSdkVersionInBp: "29",
3957 targetSdkVersionExpected: "29",
3958 updatable: true,
3959 },
3960 }
3961 for _, testCase := range testCases {
3962 errExpected := testCase.expectedError != ""
3963 bp := fmt.Sprintf(`
3964 android_app {
3965 name: "foo",
3966 enforce_default_target_sdk_version: %t,
3967 sdk_version: "current",
3968 min_sdk_version: "29",
3969 target_sdk_version: "%v",
3970 updatable: %t
3971 }
3972 `, testCase.enforceDefaultTargetSdkVersion, testCase.targetSdkVersionInBp, testCase.updatable)
3973
3974 fixture := android.GroupFixturePreparers(
3975 PrepareForTestWithJavaDefaultModules,
3976 android.PrepareForTestWithAllowMissingDependencies,
3977 android.PrepareForTestWithAndroidMk,
3978 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
3979 // explicitly set following platform variables to make the test deterministic
3980 variables.Platform_sdk_final = &testCase.platform_sdk_final
3981 variables.Platform_sdk_version = &platform_sdk_version
3982 variables.Platform_sdk_codename = &platform_sdk_codename
Harshit Mahajan18845d02023-01-11 18:39:12 +00003983 variables.Unbundled_build = proptools.BoolPtr(true)
Harshit Mahajan5b8b7302022-06-10 11:24:05 +00003984 variables.Unbundled_build_apps = []string{"sampleModule"}
3985 }),
3986 )
3987
3988 errorHandler := android.FixtureExpectsNoErrors
3989 if errExpected {
3990 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(testCase.expectedError)
3991 }
3992 result := fixture.ExtendWithErrorHandler(errorHandler).RunTestWithBp(t, bp)
3993
3994 if !errExpected {
3995 foo := result.ModuleForTests("foo", "android_common")
3996 manifestFixerArgs := foo.Output("manifest_fixer/AndroidManifest.xml").Args["args"]
3997 android.AssertStringDoesContain(t, testCase.name, manifestFixerArgs, "--targetSdkVersion "+testCase.targetSdkVersionExpected)
3998 }
3999 }
4000}
4001
Harshit Mahajan8f202ad2023-01-09 20:45:55 +00004002func TestEnforceDefaultAppTargetSdkVersionFlagForTests(t *testing.T) {
4003 platform_sdk_codename := "Tiramisu"
4004 platform_sdk_version := 33
4005 testCases := []struct {
4006 name string
4007 enforceDefaultTargetSdkVersion bool
4008 expectedError string
4009 platform_sdk_final bool
4010 targetSdkVersionInBp string
4011 targetSdkVersionExpected string
4012 }{
4013 {
4014 name: "Not enforcing Target SDK Version: Android.bp has older targetSdkVersion",
4015 enforceDefaultTargetSdkVersion: false,
4016 targetSdkVersionInBp: "29",
4017 targetSdkVersionExpected: "29",
4018 },
4019 {
4020 name: "[SDK finalised] Enforce Target SDK Version: Android.bp has current targetSdkVersion",
4021 enforceDefaultTargetSdkVersion: true,
4022 platform_sdk_final: true,
4023 targetSdkVersionInBp: "current",
4024 targetSdkVersionExpected: "33",
4025 },
4026 {
4027 name: "Enforce Target SDK Version: Android.bp has current targetSdkVersion",
4028 enforceDefaultTargetSdkVersion: true,
4029 platform_sdk_final: false,
4030 targetSdkVersionInBp: "current",
4031 targetSdkVersionExpected: "10000",
4032 },
4033 }
4034 for _, testCase := range testCases {
4035 errExpected := testCase.expectedError != ""
4036 bp := fmt.Sprintf(`
4037 android_test {
4038 name: "foo",
4039 enforce_default_target_sdk_version: %t,
4040 min_sdk_version: "29",
4041 target_sdk_version: "%v",
4042 }
4043 `, testCase.enforceDefaultTargetSdkVersion, testCase.targetSdkVersionInBp)
4044
4045 fixture := android.GroupFixturePreparers(
4046 PrepareForTestWithJavaDefaultModules,
4047 android.PrepareForTestWithAllowMissingDependencies,
4048 android.PrepareForTestWithAndroidMk,
4049 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
4050 // explicitly set following platform variables to make the test deterministic
4051 variables.Platform_sdk_final = &testCase.platform_sdk_final
4052 variables.Platform_sdk_version = &platform_sdk_version
4053 variables.Platform_sdk_codename = &platform_sdk_codename
Harshit Mahajan18845d02023-01-11 18:39:12 +00004054 variables.Unbundled_build = proptools.BoolPtr(true)
Harshit Mahajan8f202ad2023-01-09 20:45:55 +00004055 variables.Unbundled_build_apps = []string{"sampleModule"}
4056 }),
4057 )
4058
4059 errorHandler := android.FixtureExpectsNoErrors
4060 if errExpected {
4061 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(testCase.expectedError)
4062 }
4063 result := fixture.ExtendWithErrorHandler(errorHandler).RunTestWithBp(t, bp)
4064
4065 if !errExpected {
4066 foo := result.ModuleForTests("foo", "android_common")
4067 manifestFixerArgs := foo.Output("manifest_fixer/AndroidManifest.xml").Args["args"]
4068 android.AssertStringDoesContain(t, testCase.name, manifestFixerArgs, "--targetSdkVersion "+testCase.targetSdkVersionExpected)
4069 }
4070 }
4071}
4072
Colin Cross412436f2022-04-07 17:40:07 -07004073func TestAppMissingCertificateAllowMissingDependencies(t *testing.T) {
4074 result := android.GroupFixturePreparers(
4075 PrepareForTestWithJavaDefaultModules,
4076 android.PrepareForTestWithAllowMissingDependencies,
4077 android.PrepareForTestWithAndroidMk,
4078 ).RunTestWithBp(t, `
4079 android_app {
4080 name: "foo",
4081 srcs: ["a.java"],
4082 certificate: ":missing_certificate",
4083 sdk_version: "current",
Zyan Wub7550aa2023-05-18 15:46:31 +08004084 }
4085
4086 android_app {
4087 name: "bar",
4088 srcs: ["a.java"],
4089 certificate: ":missing_certificate",
4090 product_specific: true,
4091 sdk_version: "current",
Colin Cross412436f2022-04-07 17:40:07 -07004092 }`)
4093
4094 foo := result.ModuleForTests("foo", "android_common")
4095 fooApk := foo.Output("foo.apk")
4096 if fooApk.Rule != android.ErrorRule {
4097 t.Fatalf("expected ErrorRule for foo.apk, got %s", fooApk.Rule.String())
4098 }
4099 android.AssertStringDoesContain(t, "expected error rule message", fooApk.Args["error"], "missing dependencies: missing_certificate\n")
4100}
Sam Delmerico82602492022-06-10 17:05:42 +00004101
4102func TestAppIncludesJniPackages(t *testing.T) {
4103 ctx := android.GroupFixturePreparers(
4104 PrepareForTestWithJavaDefaultModules,
4105 ).RunTestWithBp(t, `
4106 android_library_import {
4107 name: "aary-nodeps",
4108 aars: ["aary.aar"],
4109 extract_jni: true,
4110 }
4111
4112 android_library {
4113 name: "aary-lib",
4114 sdk_version: "current",
4115 min_sdk_version: "21",
4116 static_libs: ["aary-nodeps"],
4117 }
4118
4119 android_app {
4120 name: "aary-lib-dep",
4121 sdk_version: "current",
4122 min_sdk_version: "21",
4123 manifest: "AndroidManifest.xml",
4124 static_libs: ["aary-lib"],
4125 use_embedded_native_libs: true,
4126 }
4127
4128 android_app {
4129 name: "aary-import-dep",
4130 sdk_version: "current",
4131 min_sdk_version: "21",
4132 manifest: "AndroidManifest.xml",
4133 static_libs: ["aary-nodeps"],
4134 use_embedded_native_libs: true,
4135 }
4136
4137 android_app {
4138 name: "aary-no-use-embedded",
4139 sdk_version: "current",
4140 min_sdk_version: "21",
4141 manifest: "AndroidManifest.xml",
4142 static_libs: ["aary-nodeps"],
4143 }`)
4144
4145 testCases := []struct {
4146 name string
4147 hasPackage bool
4148 }{
4149 {
4150 name: "aary-import-dep",
4151 hasPackage: true,
4152 },
4153 {
4154 name: "aary-lib-dep",
4155 hasPackage: true,
4156 },
4157 {
4158 name: "aary-no-use-embedded",
Jiyong Parkd044bb42024-05-15 02:09:54 +09004159 hasPackage: false,
Sam Delmerico82602492022-06-10 17:05:42 +00004160 },
4161 }
4162
4163 for _, tc := range testCases {
4164 t.Run(tc.name, func(t *testing.T) {
4165 app := ctx.ModuleForTests(tc.name, "android_common")
4166
4167 outputFile := "jnilibs.zip"
4168 jniOutputLibZip := app.MaybeOutput(outputFile)
4169 if jniOutputLibZip.Rule == nil && !tc.hasPackage {
4170 return
4171 }
4172
4173 jniPackage := "arm64-v8a_jni.zip"
4174 inputs := jniOutputLibZip.Inputs
4175 foundPackage := false
4176 for i := 0; i < len(inputs); i++ {
4177 if strings.Contains(inputs[i].String(), jniPackage) {
4178 foundPackage = true
4179 }
4180 }
4181 if foundPackage != tc.hasPackage {
4182 t.Errorf("expected to find %v in %v inputs; inputs = %v", jniPackage, outputFile, inputs)
4183 }
4184 })
4185 }
4186}
Spandan Das9f7ae7f2022-07-25 00:34:18 +00004187
4188func TestTargetSdkVersionMtsTests(t *testing.T) {
4189 platformSdkCodename := "Tiramisu"
4190 android_test := "android_test"
4191 android_test_helper_app := "android_test_helper_app"
4192 bpTemplate := `
4193 %v {
4194 name: "mytest",
4195 target_sdk_version: "%v",
4196 test_suites: ["othersuite", "%v"],
4197 }
4198 `
4199 testCases := []struct {
4200 desc string
4201 moduleType string
4202 targetSdkVersionInBp string
4203 targetSdkVersionExpected string
4204 testSuites string
4205 }{
4206 {
4207 desc: "Non-MTS android_test_apps targeting current should not be upgraded to 10000",
4208 moduleType: android_test,
4209 targetSdkVersionInBp: "current",
4210 targetSdkVersionExpected: platformSdkCodename,
4211 testSuites: "non-mts-suite",
4212 },
4213 {
4214 desc: "MTS android_test_apps targeting released sdks should not be upgraded to 10000",
4215 moduleType: android_test,
4216 targetSdkVersionInBp: "29",
4217 targetSdkVersionExpected: "29",
4218 testSuites: "mts-suite",
4219 },
4220 {
4221 desc: "MTS android_test_apps targeting current should be upgraded to 10000",
4222 moduleType: android_test,
4223 targetSdkVersionInBp: "current",
4224 targetSdkVersionExpected: "10000",
4225 testSuites: "mts-suite",
4226 },
4227 {
4228 desc: "MTS android_test_helper_apps targeting current should be upgraded to 10000",
4229 moduleType: android_test_helper_app,
4230 targetSdkVersionInBp: "current",
4231 targetSdkVersionExpected: "10000",
4232 testSuites: "mts-suite",
4233 },
4234 }
4235 fixture := android.GroupFixturePreparers(
4236 prepareForJavaTest,
4237 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
4238 variables.Platform_sdk_codename = &platformSdkCodename
4239 variables.Platform_version_active_codenames = []string{platformSdkCodename}
4240 }),
4241 )
4242 for _, testCase := range testCases {
4243 result := fixture.RunTestWithBp(t, fmt.Sprintf(bpTemplate, testCase.moduleType, testCase.targetSdkVersionInBp, testCase.testSuites))
4244 mytest := result.ModuleForTests("mytest", "android_common")
4245 manifestFixerArgs := mytest.Output("manifest_fixer/AndroidManifest.xml").Args["args"]
4246 android.AssertStringDoesContain(t, testCase.desc, manifestFixerArgs, "--targetSdkVersion "+testCase.targetSdkVersionExpected)
4247 }
4248}
Andrei Onea580636b2022-08-17 16:53:46 +00004249
4250func TestPrivappAllowlist(t *testing.T) {
4251 testJavaError(t, "privileged must be set in order to use privapp_allowlist", `
4252 android_app {
4253 name: "foo",
4254 srcs: ["a.java"],
4255 privapp_allowlist: "perms.xml",
4256 }
4257 `)
4258
4259 result := PrepareForTestWithJavaDefaultModules.RunTestWithBp(
4260 t,
4261 `
4262 android_app {
4263 name: "foo",
4264 srcs: ["a.java"],
Sam Delmerico15809f82023-05-15 17:21:47 -04004265 privapp_allowlist: "privapp_allowlist_com.android.foo.xml",
Andrei Onea580636b2022-08-17 16:53:46 +00004266 privileged: true,
Andrei Onea580636b2022-08-17 16:53:46 +00004267 sdk_version: "current",
4268 }
4269 override_android_app {
4270 name: "bar",
4271 base: "foo",
4272 package_name: "com.google.android.foo",
4273 }
4274 `,
4275 )
4276 app := result.ModuleForTests("foo", "android_common")
4277 overrideApp := result.ModuleForTests("foo", "android_common_bar")
4278
Sam Delmerico15809f82023-05-15 17:21:47 -04004279 // verify that privapp allowlist is created for override apps
Andrei Onea580636b2022-08-17 16:53:46 +00004280 overrideApp.Output("out/soong/.intermediates/foo/android_common_bar/privapp_allowlist_com.google.android.foo.xml")
Sam Delmerico15809f82023-05-15 17:21:47 -04004281 expectedAllowlistInput := "privapp_allowlist_com.android.foo.xml"
4282 overrideActualAllowlistInput := overrideApp.Rule("modifyAllowlist").Input.String()
4283 if expectedAllowlistInput != overrideActualAllowlistInput {
4284 t.Errorf("expected override allowlist to be %q; got %q", expectedAllowlistInput, overrideActualAllowlistInput)
Andrei Onea580636b2022-08-17 16:53:46 +00004285 }
4286
4287 // verify that permissions are copied to device
Anton Hansson0e486a42023-06-01 16:38:35 +00004288 app.Output("out/soong/target/product/test_device/system/etc/permissions/foo.xml")
4289 overrideApp.Output("out/soong/target/product/test_device/system/etc/permissions/bar.xml")
Andrei Onea580636b2022-08-17 16:53:46 +00004290}
Sam Delmericob1daccd2023-05-25 14:45:30 -04004291
4292func TestPrivappAllowlistAndroidMk(t *testing.T) {
4293 result := android.GroupFixturePreparers(
4294 PrepareForTestWithJavaDefaultModules,
4295 android.PrepareForTestWithAndroidMk,
4296 ).RunTestWithBp(
4297 t,
4298 `
4299 android_app {
4300 name: "foo",
4301 srcs: ["a.java"],
4302 privapp_allowlist: "privapp_allowlist_com.android.foo.xml",
4303 privileged: true,
4304 sdk_version: "current",
4305 }
4306 override_android_app {
4307 name: "bar",
4308 base: "foo",
4309 package_name: "com.google.android.foo",
4310 }
4311 `,
4312 )
4313 baseApp := result.ModuleForTests("foo", "android_common")
4314 overrideApp := result.ModuleForTests("foo", "android_common_bar")
4315
4316 baseAndroidApp := baseApp.Module().(*AndroidApp)
4317 baseEntries := android.AndroidMkEntriesForTest(t, result.TestContext, baseAndroidApp)[0]
4318 android.AssertStringMatches(
4319 t,
4320 "androidmk has incorrect LOCAL_SOONG_INSTALLED_MODULE; expected to find foo.apk",
4321 baseEntries.EntryMap["LOCAL_SOONG_INSTALLED_MODULE"][0],
4322 "\\S+foo.apk",
4323 )
4324 android.AssertStringMatches(
4325 t,
4326 "androidmk has incorrect LOCAL_SOONG_INSTALL_PAIRS; expected to it to include foo.apk",
4327 baseEntries.EntryMap["LOCAL_SOONG_INSTALL_PAIRS"][0],
4328 "\\S+foo.apk",
4329 )
4330 android.AssertStringMatches(
4331 t,
4332 "androidmk has incorrect LOCAL_SOONG_INSTALL_PAIRS; expected to it to include app",
4333 baseEntries.EntryMap["LOCAL_SOONG_INSTALL_PAIRS"][0],
4334 "\\S+foo.apk:\\S+/target/product/test_device/system/priv-app/foo/foo.apk",
4335 )
4336 android.AssertStringMatches(
4337 t,
4338 "androidmk has incorrect LOCAL_SOONG_INSTALL_PAIRS; expected to it to include privapp_allowlist",
4339 baseEntries.EntryMap["LOCAL_SOONG_INSTALL_PAIRS"][0],
Anton Hansson0e486a42023-06-01 16:38:35 +00004340 "privapp_allowlist_com.android.foo.xml:\\S+/target/product/test_device/system/etc/permissions/foo.xml",
Sam Delmericob1daccd2023-05-25 14:45:30 -04004341 )
4342
4343 overrideAndroidApp := overrideApp.Module().(*AndroidApp)
4344 overrideEntries := android.AndroidMkEntriesForTest(t, result.TestContext, overrideAndroidApp)[0]
4345 android.AssertStringMatches(
4346 t,
4347 "androidmk has incorrect LOCAL_SOONG_INSTALLED_MODULE; expected to find bar.apk",
4348 overrideEntries.EntryMap["LOCAL_SOONG_INSTALLED_MODULE"][0],
4349 "\\S+bar.apk",
4350 )
4351 android.AssertStringMatches(
4352 t,
4353 "androidmk has incorrect LOCAL_SOONG_INSTALL_PAIRS; expected to it to include bar.apk",
4354 overrideEntries.EntryMap["LOCAL_SOONG_INSTALL_PAIRS"][0],
4355 "\\S+bar.apk",
4356 )
4357 android.AssertStringMatches(
4358 t,
4359 "androidmk has incorrect LOCAL_SOONG_INSTALL_PAIRS; expected to it to include app",
4360 overrideEntries.EntryMap["LOCAL_SOONG_INSTALL_PAIRS"][0],
4361 "\\S+bar.apk:\\S+/target/product/test_device/system/priv-app/bar/bar.apk",
4362 )
4363 android.AssertStringMatches(
4364 t,
4365 "androidmk has incorrect LOCAL_SOONG_INSTALL_PAIRS; expected to it to include privapp_allowlist",
4366 overrideEntries.EntryMap["LOCAL_SOONG_INSTALL_PAIRS"][0],
Anton Hansson0e486a42023-06-01 16:38:35 +00004367 "\\S+soong/.intermediates/foo/android_common_bar/privapp_allowlist_com.google.android.foo.xml:\\S+/target/product/test_device/system/etc/permissions/bar.xml",
Sam Delmericob1daccd2023-05-25 14:45:30 -04004368 )
4369}
Sam Delmerico0e0d96e2023-08-18 22:43:28 +00004370
Jihoon Kang84b25892023-12-01 22:01:06 +00004371func TestAppFlagsPackages(t *testing.T) {
4372 ctx := testApp(t, `
4373 android_app {
4374 name: "foo",
4375 srcs: ["a.java"],
4376 sdk_version: "current",
4377 flags_packages: [
4378 "bar",
4379 "baz",
4380 ],
4381 }
4382 aconfig_declarations {
4383 name: "bar",
Dennis Shen4e7773d2024-01-05 19:06:50 +00004384 package: "com.example.package.bar",
Yu Liu315a53c2024-04-24 16:41:57 +00004385 container: "com.android.foo",
Jihoon Kang84b25892023-12-01 22:01:06 +00004386 srcs: [
4387 "bar.aconfig",
4388 ],
4389 }
4390 aconfig_declarations {
4391 name: "baz",
Dennis Shen4e7773d2024-01-05 19:06:50 +00004392 package: "com.example.package.baz",
Yu Liu315a53c2024-04-24 16:41:57 +00004393 container: "com.android.foo",
Jihoon Kang84b25892023-12-01 22:01:06 +00004394 srcs: [
4395 "baz.aconfig",
4396 ],
4397 }
4398 `)
4399
4400 foo := ctx.ModuleForTests("foo", "android_common")
4401
4402 // android_app module depends on aconfig_declarations listed in flags_packages
4403 android.AssertBoolEquals(t, "foo expected to depend on bar", true,
4404 CheckModuleHasDependency(t, ctx, "foo", "android_common", "bar"))
4405
4406 android.AssertBoolEquals(t, "foo expected to depend on baz", true,
4407 CheckModuleHasDependency(t, ctx, "foo", "android_common", "baz"))
4408
4409 aapt2LinkRule := foo.Rule("android/soong/java.aapt2Link")
4410 linkInFlags := aapt2LinkRule.Args["inFlags"]
4411 android.AssertStringDoesContain(t,
4412 "aapt2 link command expected to pass feature flags arguments",
4413 linkInFlags,
4414 "--feature-flags @out/soong/.intermediates/bar/intermediate.txt --feature-flags @out/soong/.intermediates/baz/intermediate.txt",
4415 )
4416}
Spandan Das0727ba72024-02-13 16:37:43 +00004417
4418// Test that dexpreopt is disabled if an optional_uses_libs exists, but does not provide an implementation.
4419func TestNoDexpreoptOptionalUsesLibDoesNotHaveImpl(t *testing.T) {
4420 bp := `
4421 java_sdk_library_import {
4422 name: "sdklib_noimpl",
4423 public: {
4424 jars: ["stub.jar"],
4425 },
4426 }
4427 android_app {
4428 name: "app",
4429 srcs: ["a.java"],
4430 sdk_version: "current",
4431 optional_uses_libs: [
4432 "sdklib_noimpl",
4433 ],
4434 }
4435 `
4436 result := prepareForJavaTest.RunTestWithBp(t, bp)
4437 dexpreopt := result.ModuleForTests("app", "android_common").MaybeRule("dexpreopt").Rule
4438 android.AssertBoolEquals(t, "dexpreopt should be disabled if optional_uses_libs does not have an implementation", true, dexpreopt == nil)
4439}
yangbill2af0b6e2024-03-15 09:29:29 +00004440
Ronald Braunsteincdc66f42024-04-12 11:23:19 -07004441func TestTestOnlyApp(t *testing.T) {
4442 t.Parallel()
4443 ctx := android.GroupFixturePreparers(
4444 prepareForJavaTest,
4445 ).RunTestWithBp(t, `
4446 // These should be test-only
4447 android_test {
4448 name: "android-test",
4449 }
4450 android_test_helper_app {
4451 name: "helper-app",
4452 }
4453 override_android_test {
4454 name: "override-test",
4455 base: "android-app",
4456 }
4457 // And these should not be
4458 android_app {
4459 name: "android-app",
4460 srcs: ["b.java"],
4461 sdk_version: "current",
4462 }
4463 `)
4464
4465 expectedTestOnly := []string{
4466 "android-test",
4467 "helper-app",
4468 "override-test",
4469 }
4470
4471 expectedTopLevel := []string{
4472 "android-test",
4473 "override-test",
4474 }
4475
4476 assertTestOnlyAndTopLevel(t, ctx, expectedTestOnly, expectedTopLevel)
4477}
4478
yangbill2af0b6e2024-03-15 09:29:29 +00004479func TestAppStem(t *testing.T) {
4480 ctx := testApp(t, `
4481 android_app {
4482 name: "foo",
4483 srcs: ["a.java"],
4484 stem: "foo-new",
4485 sdk_version: "current",
4486 }`)
4487
4488 foo := ctx.ModuleForTests("foo", "android_common")
4489
4490 outputs := fmt.Sprint(foo.AllOutputs())
4491 if !strings.Contains(outputs, "foo-new.apk") {
4492 t.Errorf("Module output does not contain expected apk %s", "foo-new.apk")
4493 }
4494}
Spandan Dasb9c58352024-05-13 18:29:45 +00004495
4496func TestAppMinSdkVersionOverride(t *testing.T) {
4497 result := android.GroupFixturePreparers(
4498 PrepareForTestWithJavaDefaultModules,
4499 ).RunTestWithBp(t, `
4500 android_app {
4501 name: "com.android.foo",
4502 srcs: ["a.java"],
4503 sdk_version: "current",
4504 min_sdk_version: "31",
4505 updatable: true,
4506 }
4507 override_android_app {
4508 name: "com.android.go.foo",
4509 base: "com.android.foo",
4510 min_sdk_version: "33",
4511 }
4512 `)
4513 foo := result.ModuleForTests("com.android.foo", "android_common").Rule("manifestFixer")
4514 fooOverride := result.ModuleForTests("com.android.foo", "android_common_com.android.go.foo").Rule("manifestFixer")
4515
4516 android.AssertStringDoesContain(t,
4517 "com.android.foo: expected manifest fixer to set minSdkVersion to T",
4518 foo.BuildParams.Args["args"],
4519 "--minSdkVersion 31",
4520 )
4521 android.AssertStringDoesContain(t,
4522 "com.android.go.foo: expected manifest fixer to set minSdkVersion to T",
4523 fooOverride.BuildParams.Args["args"],
4524 "--minSdkVersion 33",
4525 )
4526
4527}
Alyssa Ketpreechasawat02774e82024-07-04 10:45:04 +00004528
4529func TestNotApplyDefaultUpdatableModuleVersion(t *testing.T) {
4530 result := android.GroupFixturePreparers(
4531 PrepareForTestWithJavaDefaultModules,
4532 ).RunTestWithBp(t, `
4533 android_app {
4534 name: "com.android.foo",
4535 srcs: ["a.java"],
4536 sdk_version: "current",
4537 min_sdk_version: "31",
4538 }
4539 `)
4540 foo := result.ModuleForTests("com.android.foo", "android_common").Rule("manifestFixer")
4541 android.AssertStringDoesNotContain(t,
4542 "com.android.foo: expected manifest fixer to not set override-placeholder-version",
4543 foo.BuildParams.Args["args"],
4544 "--override-placeholder-version",
4545 )
4546}
4547
4548func TestNotApplyOverrideApexManifestDefaultVersion(t *testing.T) {
4549 result := android.GroupFixturePreparers(
4550 PrepareForTestWithJavaDefaultModules,
4551 android.FixtureMergeEnv(map[string]string{
4552 "OVERRIDE_APEX_MANIFEST_DEFAULT_VERSION": "1234",
4553 }),
4554 ).RunTestWithBp(t, `
4555 android_app {
4556 name: "com.android.foo",
4557 srcs: ["a.java"],
4558 sdk_version: "current",
4559 min_sdk_version: "31",
4560 }
4561 `)
4562 foo := result.ModuleForTests("com.android.foo", "android_common").Rule("manifestFixer")
4563 android.AssertStringDoesNotContain(t,
4564 "com.android.foo: expected manifest fixer to not set override-placeholder-version",
4565 foo.BuildParams.Args["args"],
4566 "--override-placeholder-version",
4567 )
4568}