ClassCastException: com.example.interf.JarLoader cannot be cast to com.example.impl.ILoader,接口類名相同但加載器不同。
jar { exclude 'com/example/interf/**' }。public class PluginClassLoader extends DexClassLoader {
public PluginClassLoader(...) { super(...); }
@Override
protected Class> loadClass(String name, boolean resolve) throws ClassNotFoundException {
if (name.startsWith("com.example.interf.")) // 接口包
return getParent().loadClass(name); // 委派給宿主加載器
return super.loadClass(name, resolve);
}
}
若插件已安裝為獨立 APK,宿主用 `context.createPackageContext(pkg, CONTEXT_INCLUDE_CODE).getClassLoader()` 直接獲取插件 ClassLoader,避免雙份加載。