#FIX #plugin-lib 解决自定义PluginResource的getIdentifier方法被gradle替换引起的webview系统资源找不到崩溃

This commit is contained in:
raotao 2020-12-04 15:21:34 +08:00
parent 947d2e1625
commit b3d9c29fcd
9 changed files with 36 additions and 21 deletions

View File

@ -1,6 +1,7 @@
package com.qihoo360.replugin.loader;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.content.res.ColorStateList;
import android.content.res.Configuration;
@ -20,14 +21,19 @@ import java.io.InputStream;
public class PluginResource extends Resources {
private Context mContext;
private Resources mPluginResource;
private Resources mHostResources;
public PluginResource(Resources resources) {
super(resources.getAssets(), resources.getDisplayMetrics(), resources.getConfiguration());
this.mPluginResource = resources;
public PluginResource(Context context) {
super(context.getResources().getAssets(), context.getResources().getDisplayMetrics(),
context.getResources().getConfiguration());
this.mContext = context;
this.mPluginResource = context.getResources();
if (RePlugin.isHostInitialized()) {
mHostResources = RePlugin.getHostContext().getResources();
} else {
mHostResources = context.getResources();
}
}
@ -411,17 +417,26 @@ public class PluginResource extends Resources {
@Override
public int getIdentifier(String name, String defType, String defPackage) {
try {
//当defPackage不是插件包名也不是宿主包名时比如androidvivo等
// 此时需要用宿主的resources进行加载
//plugin gradle会替换getIdentifier的defPackage所以此处用反射方式进行处理
if (!TextUtils.equals(RePlugin.getPluginContext().getPackageName(), defPackage)
&& !TextUtils.equals(RePlugin.getHostContext().getPackageName(), defPackage)) {
return Integer.parseInt(String.valueOf(ReflectUtils.invokeMethod(mHostResources.getClass().getClassLoader(),
"android.content.res.Resources", "getIdentifier", mHostResources,
new Class[]{String.class, String.class, String.class},
name, defType, defPackage)));
if (RePlugin.isHostInitialized()) {
//当defPackage不是插件包名也不是宿主包名时比如androidvivo等
// 此时需要用宿主的resources进行加载
//plugin gradle会替换getIdentifier的defPackage所以此处用反射方式进行处理
if (!TextUtils.equals(RePlugin.getPluginContext().getPackageName(), defPackage)
&& !TextUtils.equals(RePlugin.getHostContext().getPackageName(), defPackage)) {
return Integer.parseInt(String.valueOf(ReflectUtils.invokeMethod(mHostResources.getClass().getClassLoader(),
"android.content.res.Resources", "getIdentifier", mHostResources,
new Class[]{String.class, String.class, String.class},
name, defType, defPackage)));
}
} else {
if (!TextUtils.equals(mContext.getPackageName(), defPackage)) {
return Integer.parseInt(String.valueOf(ReflectUtils.invokeMethod(mHostResources.getClass().getClassLoader(),
"android.content.res.Resources", "getIdentifier", mHostResources,
new Class[]{String.class, String.class, String.class},
name, defType, defPackage)));
}
}
return mHostResources.getIdentifier(name, defType, defPackage);
return mPluginResource.getIdentifier(name, defType, defPackage);
} catch (Exception e) {
e.printStackTrace();
}

View File

@ -39,7 +39,7 @@ public abstract class PluginActivity extends Activity {
@Override
protected void attachBaseContext(Context newBase) {
newBase = RePluginInternal.createActivityContext(this, newBase);
pluginResource = new PluginResource(newBase.getResources());
pluginResource = new PluginResource(newBase);
super.attachBaseContext(newBase);
}

View File

@ -36,7 +36,7 @@ public abstract class PluginActivityGroup extends ActivityGroup {
@Override
protected void attachBaseContext(Context newBase) {
newBase = RePluginInternal.createActivityContext(this, newBase);
pluginResource = new PluginResource(newBase.getResources());
pluginResource = new PluginResource(newBase);
super.attachBaseContext(newBase);
}

View File

@ -40,7 +40,7 @@ public abstract class PluginAppCompatActivity extends AppCompatActivity {
@Override
protected void attachBaseContext(Context newBase) {
newBase = RePluginInternal.createActivityContext(this, newBase);
pluginResource = new PluginResource(newBase.getResources());
pluginResource = new PluginResource(newBase);
super.attachBaseContext(newBase);
}

View File

@ -36,7 +36,7 @@ public abstract class PluginExpandableListActivity extends ExpandableListActivit
@Override
protected void attachBaseContext(Context newBase) {
newBase = RePluginInternal.createActivityContext(this, newBase);
pluginResource = new PluginResource(newBase.getResources());
pluginResource = new PluginResource(newBase);
super.attachBaseContext(newBase);
}

View File

@ -40,7 +40,7 @@ public abstract class PluginFragmentActivity extends FragmentActivity {
@Override
protected void attachBaseContext(Context newBase) {
newBase = RePluginInternal.createActivityContext(this, newBase);
pluginResource = new PluginResource(newBase.getResources());
pluginResource = new PluginResource(newBase);
super.attachBaseContext(newBase);
}

View File

@ -36,7 +36,7 @@ public abstract class PluginListActivity extends ListActivity {
@Override
protected void attachBaseContext(Context newBase) {
newBase = RePluginInternal.createActivityContext(this, newBase);
pluginResource = new PluginResource(newBase.getResources());
pluginResource = new PluginResource(newBase);
super.attachBaseContext(newBase);
}

View File

@ -37,7 +37,7 @@ public class PluginPreferenceActivity extends PreferenceActivity {
@Override
protected void attachBaseContext(Context newBase) {
newBase = RePluginInternal.createActivityContext(this, newBase);
pluginResource = new PluginResource(newBase.getResources());
pluginResource = new PluginResource(newBase);
super.attachBaseContext(newBase);
}

View File

@ -36,7 +36,7 @@ public abstract class PluginTabActivity extends TabActivity {
@Override
protected void attachBaseContext(Context newBase) {
newBase = RePluginInternal.createActivityContext(this, newBase);
pluginResource = new PluginResource(newBase.getResources());
pluginResource = new PluginResource(newBase);
super.attachBaseContext(newBase);
}