blob: 6a1778cfd3f669c3ba2ec8735e917ffe42575493 [file] [log] [blame]
Paul Duffina3b69212018-01-25 09:58:32 +00001/*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package android.content.pm;
17
18import static android.content.pm.SharedLibraryNames.ANDROID_TEST_BASE;
19import static android.content.pm.SharedLibraryNames.ANDROID_TEST_RUNNER;
20
21import android.content.pm.PackageParser.Package;
Paul Duffin5d70cdf2018-05-16 13:06:33 +010022import android.os.Build;
Paul Duffina3b69212018-01-25 09:58:32 +000023
24import com.android.internal.annotations.VisibleForTesting;
25
26/**
Paul Duffin5d70cdf2018-05-16 13:06:33 +010027 * Updates a package to ensure that if it targets <= P that the android.test.base library is
Paul Duffina3b69212018-01-25 09:58:32 +000028 * included by default.
29 *
30 * <p>This is separated out so that it can be conditionally included at build time depending on
31 * whether android.test.base is on the bootclasspath or not. In order to include this at
32 * build time, and remove android.test.base from the bootclasspath pass
33 * REMOVE_ATB_FROM_BCP=true on the build command line, otherwise this class will not be included
34 * and the
35 *
36 * @hide
37 */
38@VisibleForTesting
39public class AndroidTestBaseUpdater extends PackageSharedLibraryUpdater {
40
Paul Duffin5d70cdf2018-05-16 13:06:33 +010041 private static boolean apkTargetsApiLevelLessThanOrEqualToP(Package pkg) {
42 int targetSdkVersion = pkg.applicationInfo.targetSdkVersion;
43 return targetSdkVersion <= Build.VERSION_CODES.P;
44 }
45
Paul Duffina3b69212018-01-25 09:58:32 +000046 @Override
47 public void updatePackage(Package pkg) {
Paul Duffin5d70cdf2018-05-16 13:06:33 +010048 // Packages targeted at <= P expect the classes in the android.test.base library
Paul Duffina3b69212018-01-25 09:58:32 +000049 // to be accessible so this maintains backward compatibility by adding the
50 // android.test.base library to those packages.
Paul Duffin5d70cdf2018-05-16 13:06:33 +010051 if (apkTargetsApiLevelLessThanOrEqualToP(pkg)) {
Paul Duffina3b69212018-01-25 09:58:32 +000052 prefixRequiredLibrary(pkg, ANDROID_TEST_BASE);
53 } else {
54 // If a package already depends on android.test.runner then add a dependency on
55 // android.test.base because android.test.runner depends on classes from the
56 // android.test.base library.
57 prefixImplicitDependency(pkg, ANDROID_TEST_RUNNER, ANDROID_TEST_BASE);
58 }
59 }
60}