aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJerome Gaillard <jgaillard@google.com>2022-02-02 12:46:52 +0000
committerJerome Gaillard <jgaillard@google.com>2022-02-02 14:43:31 +0000
commit806f89e79b0b080aa7b4c8a6758b56c39742d34a (patch)
treee878385cc4b400b6949f2b31a1edd4cbcc9c73db
parenta3bb0ac8e843dbd5e1f5b5c2d295d9e2fc7db7f5 (diff)
downloadlayoutlib-806f89e79b0b080aa7b4c8a6758b56c39742d34a.tar.gz
Better errors when failing to find custom preference
When inflating preferences, if a custom class is not found, log the error to better inform the user what the problem is. Right now, this causes an error later on which is confusing as the root cause is not clear. Fixes: 37132984 Test: checked from Studio Change-Id: I81e521ac1f325b473fdc6fdc251f4ff78db73324 (cherry picked from commit 2c7c59d037e0123967d4a859c1fceadcb8096b7e)
-rw-r--r--bridge/src/android/preference/BridgePreferenceInflater.java8
-rw-r--r--bridge/src/com/android/layoutlib/bridge/android/support/SupportPreferencesUtil.java19
2 files changed, 26 insertions, 1 deletions
diff --git a/bridge/src/android/preference/BridgePreferenceInflater.java b/bridge/src/android/preference/BridgePreferenceInflater.java
index 3665d86134..c17313bcdf 100644
--- a/bridge/src/android/preference/BridgePreferenceInflater.java
+++ b/bridge/src/android/preference/BridgePreferenceInflater.java
@@ -16,6 +16,7 @@
package android.preference;
+import com.android.ide.common.rendering.api.ILayoutLog;
import com.android.layoutlib.bridge.android.BridgeContext;
import com.android.layoutlib.bridge.android.BridgeXmlBlockParser;
@@ -23,6 +24,8 @@ import android.content.Context;
import android.util.AttributeSet;
import android.view.InflateException;
+import static com.android.layoutlib.bridge.Bridge.getLog;
+
public class BridgePreferenceInflater extends PreferenceInflater {
public BridgePreferenceInflater(Context context, PreferenceManager preferenceManager) {
@@ -53,6 +56,11 @@ public class BridgePreferenceInflater extends PreferenceInflater {
"androidx.preference".equals(prefix)) &&
"SwitchPreferenceCompat".equals(name)) {
preference = super.createItem("SwitchPreference", prefix, attrs);
+ } else {
+ // Log the error and rethrow the exception as returning null would later result in
+ // a NullPointerException without a good error message for the user.
+ getLog().error(ILayoutLog.TAG_INFLATE, exception.getMessage(), null, null);
+ throw exception;
}
}
diff --git a/bridge/src/com/android/layoutlib/bridge/android/support/SupportPreferencesUtil.java b/bridge/src/com/android/layoutlib/bridge/android/support/SupportPreferencesUtil.java
index 870a0f9666..6876312f40 100644
--- a/bridge/src/com/android/layoutlib/bridge/android/support/SupportPreferencesUtil.java
+++ b/bridge/src/com/android/layoutlib/bridge/android/support/SupportPreferencesUtil.java
@@ -16,6 +16,7 @@
package com.android.layoutlib.bridge.android.support;
+import com.android.ide.common.rendering.api.ILayoutLog;
import com.android.ide.common.rendering.api.LayoutlibCallback;
import com.android.ide.common.rendering.api.RenderResources;
import com.android.ide.common.rendering.api.ResourceValue;
@@ -43,6 +44,7 @@ import java.io.IOException;
import java.lang.reflect.Method;
import java.util.ArrayList;
+import static com.android.layoutlib.bridge.Bridge.getLog;
import static com.android.layoutlib.common.util.ReflectionUtils.getAccessibleMethod;
import static com.android.layoutlib.common.util.ReflectionUtils.getClassInstance;
import static com.android.layoutlib.common.util.ReflectionUtils.getMethod;
@@ -213,7 +215,7 @@ public class SupportPreferencesUtil {
*/
@Nullable
public static View inflatePreference(@NonNull BridgeContext bridgeContext,
- @NonNull XmlPullParser parser, @Nullable ViewGroup root) {
+ @NonNull XmlPullParser parser, @Nullable ViewGroup root) throws Throwable {
String preferencePackageName = null;
String preferenceManagerClassName = null;
// Find the correct package for the classes
@@ -306,6 +308,21 @@ public class SupportPreferencesUtil {
return scrollView;
} catch (ReflectionException e) {
+ Throwable t = e;
+ while (t.getCause() != null) {
+ t = t.getCause();
+ }
+ if (t instanceof ClassNotFoundException) {
+ String message = t.getMessage();
+ if (message != null && !message.contains(preferencePackageName)) {
+ // If the class not found is not part of the preference library, then it
+ // must be a custom preference. Log the error and throw the exception, which
+ // will prevent trying to inflate with the Android framework preference
+ // installer
+ getLog().error(ILayoutLog.TAG_INFLATE, t.getMessage(), null, null);
+ throw t;
+ }
+ }
return null;
}
}