Fix dependencies of packages that target earlier releases

A previous change added legacy-android-test as a static dependency to
all packages that build against the current, test_current or
system_current and failed to compile when the junit and android.test
classes were removed from the API. Unfortunately, those changes did not
take into account that some of those packages target earlier API
versions and so will always have the classes available at runtime.

This change replaces those static dependencies with dynamic dependencies
for any package that targets an earlier API version. The file changes
were made automatically by a tool that constructed and then analyzed a
full dependency graph of all the Android Java modules. The individual
changes were checked manually to ensure that the changes matched the
intent. The affected modules were built against an API with the junit
and android.test classes removed. Any issues found during this process
resulted in either the tool being updated to address the issue or a
separate change being made to fix an existing problem with the build. A
sample of the affected packages were run to ensure that they worked as
expected at runtime; no issues were found during testing.

The following change descriptions were generated automatically and so
may be a little repetitive. They are provided to give the reviewer
enough information to check the comments match what has actually been
changed and check the reasoning behind the changes.

* apps/CtsVerifier/Android.mk
    Added 'legacy-android-test' to LOCAL_JAVA_LIBRARIES because module
    CtsVerifier builds against 'LOCAL_SDK_VERSION := test_current' and
    uses classes from package android.test (possibly indirectly) so
    will no longer compile once they are removed from the API.

    It was not added to LOCAL_STATIC_JAVA_LIBRARIES because
    CtsVerifier's manifest file (AndroidManifest.xml) targets API level
    26 and uses the android.test.runner library which will provide the
    classes dynamically at runtime.

* hostsidetests/appsecurity/test-apps/UsePermissionApp22/Android.mk
    Removed legacy-android-test from LOCAL_STATIC_JAVA_LIBRARIES
    because CtsUsePermissionApp22's manifest file (AndroidManifest.xml)
    targets API level 22 and dynamically includes the
    android.test.runner library at runtime so there is no point in
    statically including the classes.

    Added 'legacy-android-test' to LOCAL_JAVA_LIBRARIES because module
    CtsUsePermissionApp22 uses classes from package android.test
    (possible indirectly) and needs them available at compile time.

    Dependency 'legacy-android-test' is used instead of
    'android.test.runner' because the latter will conflict with
    dependencies on junit.

* hostsidetests/appsecurity/test-apps/UsePermissionApp23/Android.mk
    Removed legacy-android-test from LOCAL_STATIC_JAVA_LIBRARIES
    because CtsUsePermissionApp23's manifest file (AndroidManifest.xml)
    targets API level 23 and dynamically includes the
    android.test.runner library at runtime so there is no point in
    statically including the classes.

    Added 'legacy-android-test' to LOCAL_JAVA_LIBRARIES because module
    CtsUsePermissionApp23 uses classes from package android.test
    (possible indirectly) and needs them available at compile time.

    Dependency 'legacy-android-test' is used instead of
    'android.test.runner' because the latter will conflict with
    dependencies on junit.

* hostsidetests/devicepolicy/app/AccountCheck/Auth/Android.mk
    Removed legacy-android-test from LOCAL_STATIC_JAVA_LIBRARIES
    because CtsAccountCheckAuthApp's manifest file
    (AndroidManifest.xml) targets API level 25 and dynamically includes
    the android.test.runner library at runtime so there is no point in
    statically including the classes.

    Added 'legacy-android-test' to LOCAL_JAVA_LIBRARIES because module
    CtsAccountCheckAuthApp builds against 'LOCAL_SDK_VERSION :=
    test_current' and uses classes from package android.test (possibly
    indirectly) so will no longer compile once they are removed from
    the API.

* hostsidetests/devicepolicy/app/DeviceAdmin/api23/Android.mk
    Removed legacy-android-test from LOCAL_STATIC_JAVA_LIBRARIES
    because CtsDeviceAdminApp23's manifest file (AndroidManifest.xml)
    targets API level 23 and dynamically includes the
    android.test.runner library at runtime so there is no point in
    statically including the classes.

    Added 'legacy-android-test' to LOCAL_JAVA_LIBRARIES because module
    CtsDeviceAdminApp23 builds against 'LOCAL_SDK_VERSION := current'
    and uses classes from package android.test (possibly indirectly) so
    will no longer compile once they are removed from the API.

* hostsidetests/devicepolicy/app/DeviceAdmin/api24/Android.mk
    Removed legacy-android-test from LOCAL_STATIC_JAVA_LIBRARIES
    because CtsDeviceAdminApp24's manifest file (AndroidManifest.xml)
    targets API level 24 and dynamically includes the
    android.test.runner library at runtime so there is no point in
    statically including the classes.

    Added 'legacy-android-test' to LOCAL_JAVA_LIBRARIES because module
    CtsDeviceAdminApp24 builds against 'LOCAL_SDK_VERSION := current'
    and uses classes from package android.test (possibly indirectly) so
    will no longer compile once they are removed from the API.

* hostsidetests/devicepolicy/app/DeviceAndProfileOwner/api23/Android.mk
    Added 'legacy-android-test' to LOCAL_JAVA_LIBRARIES because module
    CtsDeviceAndProfileOwnerApp23 builds against 'LOCAL_SDK_VERSION :=
    test_current' and uses classes from package android.test (possibly
    indirectly) so will no longer compile once they are removed from
    the API.

    It was not added to LOCAL_STATIC_JAVA_LIBRARIES because
    CtsDeviceAndProfileOwnerApp23's manifest file (AndroidManifest.xml)
    targets API level 23 and uses the android.test.runner library which
    will provide the classes dynamically at runtime.

* hostsidetests/devicepolicy/app/DeviceAndProfileOwner/api25/Android.mk
    Added 'legacy-android-test' to LOCAL_JAVA_LIBRARIES because module
    CtsDeviceAndProfileOwnerApp25 builds against 'LOCAL_SDK_VERSION :=
    test_current' and uses classes from package android.test (possibly
    indirectly) so will no longer compile once they are removed from
    the API.

    It was not added to LOCAL_STATIC_JAVA_LIBRARIES because
    CtsDeviceAndProfileOwnerApp25's manifest file (AndroidManifest.xml)
    targets API level 25 and uses the android.test.runner library which
    will provide the classes dynamically at runtime.

* hostsidetests/devicepolicy/app/LauncherTests/Android.mk
    Replaced 'android.test.runner' with 'legacy-android-test' in
    LOCAL_JAVA_LIBRARIES because module CtsLauncherAppsTests builds
    against 'LOCAL_SDK_VERSION := current' and uses classes from
    package android.test (possibly indirectly) so will no longer
    compile once they are removed from the API.

    Removed legacy-android-test from LOCAL_STATIC_JAVA_LIBRARIES
    because CtsLauncherAppsTests's manifest file (AndroidManifest.xml)
    targets API level 25 and dynamically includes the
    android.test.runner library at runtime so there is no point in
    statically including the classes.

* hostsidetests/devicepolicy/app/LauncherTestsSupport/Android.mk
    Removed android.test.runner from LOCAL_JAVA_LIBRARIES because
    module CtsLauncherAppsTestsSupport does not use any classes from
    android.test.runner at compile time.

* hostsidetests/dumpsys/apps/ProcStatsHelperApp/Android.mk
    Removed android.test.runner from LOCAL_JAVA_LIBRARIES because
    module CtsProcStatsHelperApp does not use any classes from
    android.test.runner at compile time.

* tests/tests/accounts/Android.mk
    Removed legacy-android-test from LOCAL_STATIC_JAVA_LIBRARIES
    because CtsAccountManagerTestCases's manifest file
    (AndroidManifest.xml) targets API level 26 and dynamically includes
    the android.test.runner library at runtime so there is no point in
    statically including the classes.

    Added 'legacy-android-test' to LOCAL_JAVA_LIBRARIES because module
    CtsAccountManagerTestCases builds against 'LOCAL_SDK_VERSION :=
    current' and uses classes from package android.test (possibly
    indirectly) so will no longer compile once they are removed from
    the API.

* tests/tests/calendarcommon/Android.mk
    Removed legacy-android-test from LOCAL_STATIC_JAVA_LIBRARIES
    because CtsCalendarcommon2TestCases's manifest file
    (AndroidManifest.xml) targets API level 15 and dynamically includes
    the android.test.runner library at runtime so there is no point in
    statically including the classes.

    Added 'legacy-android-test' to LOCAL_JAVA_LIBRARIES because module
    CtsCalendarcommon2TestCases builds against 'LOCAL_SDK_VERSION :=
    current' and uses classes from package android.test (possibly
    indirectly) so will no longer compile once they are removed from
    the API.

* tests/tests/dpi2/Android.mk
    Added 'legacy-android-test' to LOCAL_JAVA_LIBRARIES because module
    CtsDpiTestCases2 builds against 'LOCAL_SDK_VERSION := current' and
    uses classes from package android.test (possibly indirectly) so
    will no longer compile once they are removed from the API.

    It was not added to LOCAL_STATIC_JAVA_LIBRARIES because
    CtsDpiTestCases2's manifest file (AndroidManifest.xml) targets API
    level 3 and uses the android.test.runner library which will provide
    the classes dynamically at runtime.

* tests/tests/provider/Android.mk
    Replaced 'android.test.runner' with 'android.test.mock' and
    'legacy-android-test' in LOCAL_JAVA_LIBRARIES because module
    CtsProviderTestCases uses classes from packages android.test and
    android.test.mock (possible indirectly) and needs them available at
    compile time.

    Dependency 'legacy-android-test' is used instead of
    'android.test.runner' because the latter will conflict with
    dependencies on junit.

    Dependency 'android.test.mock.sdk' is used instead of
    'android.test.mock' because module CtsProviderTestCases builds
    against internal jars not the API and so should use libraries that
    build against internal jars not the API.

    Removed legacy-android-test from LOCAL_STATIC_JAVA_LIBRARIES
    because CtsProviderTestCases's manifest file (AndroidManifest.xml)
    targets API level 21 and dynamically includes the
    android.test.runner library at runtime so there is no point in
    statically including the classes.

* tests/tests/selinux/selinuxTargetSdk/Android.mk
    Removed legacy-android-test from LOCAL_STATIC_JAVA_LIBRARIES
    because CtsSelinuxTargetSdkTestCases's manifest file
    (AndroidManifest.xml) targets API level 25 and dynamically includes
    the android.test.runner library at runtime so there is no point in
    statically including the classes.

    Added 'legacy-android-test' to LOCAL_JAVA_LIBRARIES because module
    CtsSelinuxTargetSdkTestCases builds against 'LOCAL_SDK_VERSION :=
    current' and uses classes from package android.test (possibly
    indirectly) so will no longer compile once they are removed from
    the API.

Bug: 30188076
Test: make checkbuild and ran a sample of tests
Change-Id: I4744db5f5c9b33b2cb13e8a0edc46881d3aa8aae
diff --git a/apps/CtsVerifier/Android.mk b/apps/CtsVerifier/Android.mk
index d82c24c..d5ea50c 100644
--- a/apps/CtsVerifier/Android.mk
+++ b/apps/CtsVerifier/Android.mk
@@ -40,6 +40,8 @@
                                compatibility-device-util \
                                platform-test-annotations
 
+LOCAL_JAVA_LIBRARIES := legacy-android-test
+
 LOCAL_PACKAGE_NAME := CtsVerifier
 
 LOCAL_JNI_SHARED_LIBRARIES := libctsverifier_jni \
diff --git a/hostsidetests/appsecurity/test-apps/UsePermissionApp22/Android.mk b/hostsidetests/appsecurity/test-apps/UsePermissionApp22/Android.mk
index 8e51f22..e4fba42 100644
--- a/hostsidetests/appsecurity/test-apps/UsePermissionApp22/Android.mk
+++ b/hostsidetests/appsecurity/test-apps/UsePermissionApp22/Android.mk
@@ -23,8 +23,9 @@
     android-support-test \
     compatibility-device-util \
     ctstestrunner \
-    ub-uiautomator \
-    legacy-android-test
+    ub-uiautomator
+
+LOCAL_JAVA_LIBRARIES := legacy-android-test
 
 LOCAL_SRC_FILES := $(call all-java-files-under, src) \
     ../ExternalStorageApp/src/com/android/cts/externalstorageapp/CommonExternalStorageTest.java \
diff --git a/hostsidetests/appsecurity/test-apps/UsePermissionApp23/Android.mk b/hostsidetests/appsecurity/test-apps/UsePermissionApp23/Android.mk
index 5e623d8..58a639d 100644
--- a/hostsidetests/appsecurity/test-apps/UsePermissionApp23/Android.mk
+++ b/hostsidetests/appsecurity/test-apps/UsePermissionApp23/Android.mk
@@ -23,8 +23,9 @@
     android-support-test \
     compatibility-device-util \
     ctstestrunner \
-    ub-uiautomator \
-    legacy-android-test
+    ub-uiautomator
+
+LOCAL_JAVA_LIBRARIES := legacy-android-test
 
 LOCAL_SRC_FILES := $(call all-java-files-under, src) \
     ../ExternalStorageApp/src/com/android/cts/externalstorageapp/CommonExternalStorageTest.java
diff --git a/hostsidetests/devicepolicy/app/AccountCheck/Auth/Android.mk b/hostsidetests/devicepolicy/app/AccountCheck/Auth/Android.mk
index c9bd048..be51112 100644
--- a/hostsidetests/devicepolicy/app/AccountCheck/Auth/Android.mk
+++ b/hostsidetests/devicepolicy/app/AccountCheck/Auth/Android.mk
@@ -34,6 +34,8 @@
     android-support-test \
     legacy-android-test
 
+LOCAL_JAVA_LIBRARIES := legacy-android-test
+
 LOCAL_SDK_VERSION := test_current
 
 include $(BUILD_CTS_PACKAGE)
diff --git a/hostsidetests/devicepolicy/app/DeviceAdmin/api23/Android.mk b/hostsidetests/devicepolicy/app/DeviceAdmin/api23/Android.mk
index 7d277d9..cee19e8 100644
--- a/hostsidetests/devicepolicy/app/DeviceAdmin/api23/Android.mk
+++ b/hostsidetests/devicepolicy/app/DeviceAdmin/api23/Android.mk
@@ -31,6 +31,8 @@
     compatibility-device-util \
     legacy-android-test
 
+LOCAL_JAVA_LIBRARIES := legacy-android-test
+
 LOCAL_SDK_VERSION := current
 
 # tag this module as a cts test artifact
diff --git a/hostsidetests/devicepolicy/app/DeviceAdmin/api24/Android.mk b/hostsidetests/devicepolicy/app/DeviceAdmin/api24/Android.mk
index ab38265..7b21c3c 100644
--- a/hostsidetests/devicepolicy/app/DeviceAdmin/api24/Android.mk
+++ b/hostsidetests/devicepolicy/app/DeviceAdmin/api24/Android.mk
@@ -31,6 +31,8 @@
     compatibility-device-util \
     legacy-android-test
 
+LOCAL_JAVA_LIBRARIES := legacy-android-test
+
 LOCAL_SDK_VERSION := current
 
 # tag this module as a cts test artifact
diff --git a/hostsidetests/devicepolicy/app/DeviceAndProfileOwner/api23/Android.mk b/hostsidetests/devicepolicy/app/DeviceAndProfileOwner/api23/Android.mk
index f8467c1..dfe3f21 100644
--- a/hostsidetests/devicepolicy/app/DeviceAndProfileOwner/api23/Android.mk
+++ b/hostsidetests/devicepolicy/app/DeviceAndProfileOwner/api23/Android.mk
@@ -24,7 +24,7 @@
 
 LOCAL_SRC_FILES := $(call all-java-files-under, ../src)
 
-LOCAL_JAVA_LIBRARIES = conscrypt
+LOCAL_JAVA_LIBRARIES = conscrypt legacy-android-test
 
 LOCAL_STATIC_JAVA_LIBRARIES = android-support-v4 compatibility-device-util ctstestrunner ub-uiautomator
 
diff --git a/hostsidetests/devicepolicy/app/DeviceAndProfileOwner/api25/Android.mk b/hostsidetests/devicepolicy/app/DeviceAndProfileOwner/api25/Android.mk
index 8a0d559..f8de3cf 100644
--- a/hostsidetests/devicepolicy/app/DeviceAndProfileOwner/api25/Android.mk
+++ b/hostsidetests/devicepolicy/app/DeviceAndProfileOwner/api25/Android.mk
@@ -24,7 +24,7 @@
 
 LOCAL_SRC_FILES := $(call all-java-files-under, ../src)
 
-LOCAL_JAVA_LIBRARIES = conscrypt
+LOCAL_JAVA_LIBRARIES = conscrypt legacy-android-test
 
 LOCAL_STATIC_JAVA_LIBRARIES = android-support-v4 compatibility-device-util ctstestrunner ub-uiautomator
 
diff --git a/hostsidetests/devicepolicy/app/LauncherTests/Android.mk b/hostsidetests/devicepolicy/app/LauncherTests/Android.mk
index 5bb5c80..bad1d41 100644
--- a/hostsidetests/devicepolicy/app/LauncherTests/Android.mk
+++ b/hostsidetests/devicepolicy/app/LauncherTests/Android.mk
@@ -24,7 +24,7 @@
 
 LOCAL_SRC_FILES := $(call all-java-files-under, src)
 
-LOCAL_JAVA_LIBRARIES := android.test.runner cts-junit
+LOCAL_JAVA_LIBRARIES := legacy-android-test cts-junit
 
 LOCAL_STATIC_JAVA_LIBRARIES = \
 	android-support-v4 \
diff --git a/hostsidetests/devicepolicy/app/LauncherTestsSupport/Android.mk b/hostsidetests/devicepolicy/app/LauncherTestsSupport/Android.mk
index 95b3e27..3c3adcf 100644
--- a/hostsidetests/devicepolicy/app/LauncherTestsSupport/Android.mk
+++ b/hostsidetests/devicepolicy/app/LauncherTestsSupport/Android.mk
@@ -24,7 +24,7 @@
 
 LOCAL_SRC_FILES := $(call all-java-files-under, src)
 
-LOCAL_JAVA_LIBRARIES := android.test.runner cts-junit
+LOCAL_JAVA_LIBRARIES := cts-junit
 
 LOCAL_SDK_VERSION := current
 
diff --git a/hostsidetests/dumpsys/apps/ProcStatsHelperApp/Android.mk b/hostsidetests/dumpsys/apps/ProcStatsHelperApp/Android.mk
index f438d83..8896f6a 100644
--- a/hostsidetests/dumpsys/apps/ProcStatsHelperApp/Android.mk
+++ b/hostsidetests/dumpsys/apps/ProcStatsHelperApp/Android.mk
@@ -24,7 +24,7 @@
 
 LOCAL_SRC_FILES := $(call all-java-files-under, src)
 
-LOCAL_JAVA_LIBRARIES := android.test.runner cts-junit
+LOCAL_JAVA_LIBRARIES := cts-junit
 
 LOCAL_STATIC_JAVA_LIBRARIES := \
     ctstestrunner \
diff --git a/tests/tests/accounts/Android.mk b/tests/tests/accounts/Android.mk
index a3c1f92..501feae 100644
--- a/tests/tests/accounts/Android.mk
+++ b/tests/tests/accounts/Android.mk
@@ -24,6 +24,8 @@
 LOCAL_STATIC_JAVA_LIBRARIES := \
     CtsAccountTestsCommon ctstestrunner legacy-android-test
 
+LOCAL_JAVA_LIBRARIES := legacy-android-test
+
 LOCAL_SRC_FILES := $(call all-java-files-under, src)
 
 LOCAL_CERTIFICATE := cts/hostsidetests/appsecurity/certs/cts-testkey1
diff --git a/tests/tests/calendarcommon/Android.mk b/tests/tests/calendarcommon/Android.mk
index 042e260..bca60b7 100644
--- a/tests/tests/calendarcommon/Android.mk
+++ b/tests/tests/calendarcommon/Android.mk
@@ -27,6 +27,8 @@
 
 LOCAL_STATIC_JAVA_LIBRARIES := ctstestrunner legacy-android-test
 
+LOCAL_JAVA_LIBRARIES := legacy-android-test
+
 LOCAL_SRC_FILES := $(call all-java-files-under, src)
 
 LOCAL_SDK_VERSION := current
diff --git a/tests/tests/dpi2/Android.mk b/tests/tests/dpi2/Android.mk
index 1fcedf1..3819d01 100644
--- a/tests/tests/dpi2/Android.mk
+++ b/tests/tests/dpi2/Android.mk
@@ -20,6 +20,8 @@
 # We use the DefaultManifestAttributesTest from the android.cts.dpi package.
 LOCAL_STATIC_JAVA_LIBRARIES := android.cts.dpi ctstestrunner junit
 
+LOCAL_JAVA_LIBRARIES := legacy-android-test
+
 LOCAL_SRC_FILES := $(call all-java-files-under, src)
 
 LOCAL_PACKAGE_NAME := CtsDpiTestCases2
diff --git a/tests/tests/provider/Android.mk b/tests/tests/provider/Android.mk
index 3b71035..c1d1058 100644
--- a/tests/tests/provider/Android.mk
+++ b/tests/tests/provider/Android.mk
@@ -28,15 +28,14 @@
 # Tag this module as a cts test artifact
 LOCAL_COMPATIBILITY_SUITE := cts general-tests
 
-LOCAL_JAVA_LIBRARIES := android.test.runner telephony-common
+LOCAL_JAVA_LIBRARIES := android.test.mock legacy-android-test telephony-common
 
 LOCAL_STATIC_JAVA_LIBRARIES := \
     android-support-v4 \
     compatibility-device-util \
     ctstestrunner \
     ub-uiautomator \
-    junit \
-    legacy-android-test
+    junit
 
 LOCAL_JNI_SHARED_LIBRARIES := libcts_jni libnativehelper_compat_libc++
 
diff --git a/tests/tests/selinux/selinuxTargetSdk/Android.mk b/tests/tests/selinux/selinuxTargetSdk/Android.mk
index 138da59..d2a69f8 100755
--- a/tests/tests/selinux/selinuxTargetSdk/Android.mk
+++ b/tests/tests/selinux/selinuxTargetSdk/Android.mk
@@ -20,8 +20,8 @@
 LOCAL_COMPATIBILITY_SUITE := cts general-tests
 LOCAL_STATIC_JAVA_LIBRARIES := \
     compatibility-device-util \
-    ctstestrunner \
-    legacy-android-test
+    ctstestrunner
+LOCAL_JAVA_LIBRARIES := legacy-android-test
 LOCAL_SRC_FILES := $(call all-java-files-under, src)
 LOCAL_PACKAGE_NAME := CtsSelinuxTargetSdkTestCases
 LOCAL_SDK_VERSION := current