From 4d01a45c44da726927a736c81d71c72de04e23d7 Mon Sep 17 00:00:00 2001 From: Hai Zhang Date: Tue, 11 May 2021 00:48:44 -0700 Subject: [PATCH] Remove workaround for R class conflict in Settings unit test. The workaround is accessing the resources dynamically with Resources.getIdentifier(), however this makes it incompatible with R8 which will think the resource is unused and remove it, resulting in a runtime exception. There is currently no way to have consumer ProGuard texts in Soong, and we cannot disable R8 for PermissionController because it would severely regress our APK size and make build unhappy. The real cause for the original test failure is that, Settings unit test used to include SettingsLib libraries in its static_libs, and this caused duplicate R classes to be included in its test APK (which is already in Settings APK) and resulted in the wrong R class being loaded when accessing resources from Settings context. So we should remove the SettingsLib dependencies in Settings unit test and remove the workaround here, to correctly fix the issue and unblock R8 usage. Bug: 183340630 Bug: 177638625 Bug: 183342958 Test: atest SettingsSliderPreferenceControllerTest Test: atest PrivateVolumeForgetTest Change-Id: I404892ccb7c2004ec3f81df69c1dc4bceed150df --- .../settingslib/widget/MainSwitchBar.java | 16 ++++------------ .../settingslib/widget/TwoTargetPreference.java | 8 ++------ 2 files changed, 6 insertions(+), 18 deletions(-) diff --git a/packages/SettingsLib/MainSwitchPreference/src/com/android/settingslib/widget/MainSwitchBar.java b/packages/SettingsLib/MainSwitchPreference/src/com/android/settingslib/widget/MainSwitchBar.java index 0748acd4d7305..123c47727f4d9 100644 --- a/packages/SettingsLib/MainSwitchPreference/src/com/android/settingslib/widget/MainSwitchBar.java +++ b/packages/SettingsLib/MainSwitchPreference/src/com/android/settingslib/widget/MainSwitchBar.java @@ -72,9 +72,7 @@ public class MainSwitchBar extends LinearLayout implements CompoundButton.OnChec int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); - LayoutInflater.from(context).inflate( - resourceId(context, "layout", "settingslib_main_switch_bar"), - this); + LayoutInflater.from(context).inflate(R.layout.settingslib_main_switch_bar, this); if (!BuildCompat.isAtLeastS()) { final TypedArray a = context.obtainStyledAttributes( @@ -90,12 +88,10 @@ public class MainSwitchBar extends LinearLayout implements CompoundButton.OnChec mFrameView = findViewById(R.id.frame); mTextView = (TextView) findViewById(R.id.switch_text); mSwitch = (Switch) findViewById(android.R.id.switch_widget); - mBackgroundOn = getContext().getDrawable( - resourceId(context, "drawable", "settingslib_switch_bar_bg_on")); - mBackgroundOff = getContext().getDrawable( - resourceId(context, "drawable", "settingslib_switch_bar_bg_off")); + mBackgroundOn = getContext().getDrawable(R.drawable.settingslib_switch_bar_bg_on); + mBackgroundOff = getContext().getDrawable(R.drawable.settingslib_switch_bar_bg_off); mBackgroundDisabled = getContext().getDrawable( - resourceId(context, "drawable", "settingslib_switch_bar_bg_disabled")); + R.drawable.settingslib_switch_bar_bg_disabled); addOnSwitchChangeListener((switchView, isChecked) -> setChecked(isChecked)); @@ -302,8 +298,4 @@ public class MainSwitchBar extends LinearLayout implements CompoundButton.OnChec requestLayout(); } - - private int resourceId(Context context, String type, String name) { - return context.getResources().getIdentifier(name, type, context.getPackageName()); - } } diff --git a/packages/SettingsLib/TwoTargetPreference/src/com/android/settingslib/widget/TwoTargetPreference.java b/packages/SettingsLib/TwoTargetPreference/src/com/android/settingslib/widget/TwoTargetPreference.java index 17f257d3758ab..9130662021d54 100644 --- a/packages/SettingsLib/TwoTargetPreference/src/com/android/settingslib/widget/TwoTargetPreference.java +++ b/packages/SettingsLib/TwoTargetPreference/src/com/android/settingslib/widget/TwoTargetPreference.java @@ -72,9 +72,9 @@ public class TwoTargetPreference extends Preference { private void init(Context context) { setLayoutResource(R.layout.preference_two_target); mSmallIconSize = context.getResources().getDimensionPixelSize( - resourceId(context, "dimen", "two_target_pref_small_icon_size")); + R.dimen.two_target_pref_small_icon_size); mMediumIconSize = context.getResources().getDimensionPixelSize( - resourceId(context, "dimen", "two_target_pref_medium_icon_size")); + R.dimen.two_target_pref_medium_icon_size); final int secondTargetResId = getSecondTargetResId(); if (secondTargetResId != 0) { setWidgetLayoutResource(secondTargetResId); @@ -116,8 +116,4 @@ public class TwoTargetPreference extends Preference { protected int getSecondTargetResId() { return 0; } - - private int resourceId(Context context, String type, String name) { - return context.getResources().getIdentifier(name, type, context.getPackageName()); - } }