Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(686)

Unified Diff: android_webview/java/src/org/chromium/android_webview/AndroidProtocolHandler.java

Issue 2075753002: aw: Try strip out last component of app name for android_res (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: minor clean up Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: android_webview/java/src/org/chromium/android_webview/AndroidProtocolHandler.java
diff --git a/android_webview/java/src/org/chromium/android_webview/AndroidProtocolHandler.java b/android_webview/java/src/org/chromium/android_webview/AndroidProtocolHandler.java
index 3db25bb9d57073a37cc4b1bff3a31fd29a0ef16f..20717b6e536f2a825f6f41d6319e886957f4a5ba 100644
--- a/android_webview/java/src/org/chromium/android_webview/AndroidProtocolHandler.java
+++ b/android_webview/java/src/org/chromium/android_webview/AndroidProtocolHandler.java
@@ -60,11 +60,42 @@ public class AndroidProtocolHandler {
return null;
}
+ // Assumes input string is in the format "foo.bar.baz" and strips out the last component.
+ // Returns null on failure.
+ private static String removeOneSuffix(String input) {
+ if (input == null) return null;
+ int lastDotIndex = input.lastIndexOf('.');
+ if (lastDotIndex == -1) return null;
+ return input.substring(0, lastDotIndex);
+ }
+
+ private static Class<?> getClazz(Context context, String packageName, String assetType)
+ throws ClassNotFoundException {
+ return context.getClassLoader().loadClass(packageName + ".R$" + assetType);
+ }
+
private static int getFieldId(Context context, String assetType, String assetName)
throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException {
- Class<?> d = context.getClassLoader()
- .loadClass(context.getPackageName() + ".R$" + assetType);
- java.lang.reflect.Field field = d.getField(assetName);
+ Class<?> clazz = null;
+ try {
+ clazz = getClazz(context, context.getPackageName(), assetType);
+ } catch (ClassNotFoundException e) {
+ // Strip out components from the end so gradle generated application suffix such as
+ // com.example.my.pkg.pro works. This is by no means bulletproof.
+ String packageName = context.getPackageName();
+ while (clazz == null) {
+ packageName = removeOneSuffix(packageName);
+ // Throw original exception which contains the entire package id.
+ if (packageName == null) throw e;
+ try {
+ clazz = getClazz(context, packageName, assetType);
+ } catch (ClassNotFoundException ignored) {
+ // Strip and try again.
+ }
+ }
+ }
+
+ java.lang.reflect.Field field = clazz.getField(assetName);
int id = field.getInt(null);
return id;
}
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698