From 035820cbda4c208bec8129d0e1e43f986d2dff87 Mon Sep 17 00:00:00 2001 From: Paul Duffin Date: Mon, 2 Oct 2017 11:16:25 +0100 Subject: [PATCH] Insert org.apache.http.legacy at the start of the shared library list Ensure consistent behaviour depending on whether OAHL is on the bootclasspath or not. When OAHL is on the bootclasspath the search order is (where ... is the other libraries on the bootclasspath): OAHL ... shared libraries optional shared libraries APK Prior to this change the OAHL was added to the end of the shared library list which meant the search order (when OAHL is not on the bootclasspath) would be: ... shared libraries OAHL optional shared libraries APK After this change the order will be: ... OAHL shared libraries optional shared libraries APK The slight difference at the beginning is not an issue because there are no conflicting resources or class files between OAHL and the other boot libraries. Bug: 65552462 Bug: 18027885 Test: build, flash, check systrace when starting GoogleDialer Change-Id: Ifcb4d50c13e35eebac4d18f8f0f10dd0734e8896 --- .../content/pm/PackageBackwardCompatibility.java | 12 +++++++++++- .../pm/PackageBackwardCompatibilityTest.java | 14 ++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/core/java/android/content/pm/PackageBackwardCompatibility.java b/core/java/android/content/pm/PackageBackwardCompatibility.java index cee25994a2719..8014c94d198df 100644 --- a/core/java/android/content/pm/PackageBackwardCompatibility.java +++ b/core/java/android/content/pm/PackageBackwardCompatibility.java @@ -16,6 +16,8 @@ package android.content.pm; +import android.annotation.NonNull; +import android.annotation.Nullable; import android.content.pm.PackageParser.Package; import android.os.Build; @@ -56,7 +58,7 @@ public class PackageBackwardCompatibility { boolean apacheHttpLegacyPresent = isLibraryPresent( usesLibraries, usesOptionalLibraries, APACHE_HTTP_LEGACY); if (!apacheHttpLegacyPresent) { - usesLibraries = ArrayUtils.add(usesLibraries, APACHE_HTTP_LEGACY); + usesLibraries = prefix(usesLibraries, APACHE_HTTP_LEGACY); } } @@ -86,4 +88,12 @@ public class PackageBackwardCompatibility { return ArrayUtils.contains(usesLibraries, apacheHttpLegacy) || ArrayUtils.contains(usesOptionalLibraries, apacheHttpLegacy); } + + private static @NonNull ArrayList prefix(@Nullable ArrayList cur, T val) { + if (cur == null) { + cur = new ArrayList<>(); + } + cur.add(0, val); + return cur; + } } diff --git a/core/tests/coretests/src/android/content/pm/PackageBackwardCompatibilityTest.java b/core/tests/coretests/src/android/content/pm/PackageBackwardCompatibilityTest.java index 1a54bd608d48b..63a5e4cc1dd8a 100644 --- a/core/tests/coretests/src/android/content/pm/PackageBackwardCompatibilityTest.java +++ b/core/tests/coretests/src/android/content/pm/PackageBackwardCompatibilityTest.java @@ -41,6 +41,8 @@ public class PackageBackwardCompatibilityTest { private static final String ANDROID_TEST_MOCK = "android.test.mock"; + private static final String OTHER_LIBRARY = "other.library"; + private Package mPackage; private static ArrayList arrayList(String... strings) { @@ -77,6 +79,18 @@ public class PackageBackwardCompatibilityTest { assertNull("usesOptionalLibraries not updated correctly", mPackage.usesOptionalLibraries); } + @Test + public void targeted_at_O_not_empty_usesLibraries() { + mPackage.applicationInfo.targetSdkVersion = Build.VERSION_CODES.O; + mPackage.usesLibraries = arrayList(OTHER_LIBRARY); + PackageBackwardCompatibility.modifySharedLibraries(mPackage); + // The org.apache.http.legacy jar should be added at the start of the list. + assertEquals("usesLibraries not updated correctly", + arrayList(ORG_APACHE_HTTP_LEGACY, OTHER_LIBRARY), + mPackage.usesLibraries); + assertNull("usesOptionalLibraries not updated correctly", mPackage.usesOptionalLibraries); + } + @Test public void targeted_at_O_org_apache_http_legacy_in_usesLibraries() { mPackage.applicationInfo.targetSdkVersion = Build.VERSION_CODES.O;