启用严格模式后,当我尝试在运行于Android Jellybean(4.3)的设备上安装应用程序时,我可以看到以下内存泄漏消息,但是如果我安装运行于android 7.1.2(Android N)的应用程序,则会出现此问题不来 我需要在Android 4.3(即软糖)上使用我的应用程序
附加日志和示例应用代码: 这只是警告,而不是内存泄漏。 日志:
09-21 11:30:49.951 E/StrictMode(30431): A resource was acquired at attached stack trace but never released. See java.io.Closeable for information on avoiding resource leaks.
09-21 11:30:49.951 E/StrictMode(30431): java.lang.Throwable: Explicit termination method 'close' not called
09-21 11:30:49.951 E/StrictMode(30431): at dalvik.system.CloseGuard.open(CloseGuard.java:184)
09-21 11:30:49.951 E/StrictMode(30431): at dalvik.system.DexFile.<init>(DexFile.java:80)
09-21 11:30:49.951 E/StrictMode(30431): at dalvik.system.DexFile.<init>(DexFile.java:57)
09-21 11:30:49.951 E/StrictMode(30431): at dalvik.system.DexPathList.loadDexFile(DexPathList.java:256)
application1
for (int i =0 ; i<10 ; i++)
{
View view = loadExternalView(packageName,className);
}
public synchronized Context loadExternalContext(String packageName)
throws PackageManager.NameNotFoundException, NullPointerException {
if (!mCachedContexts.containsKey(packageName)) {
Context context = mContext.createPackageContext(packageName, CONTEXT_INCLUDE_CODE | CONTEXT_IGNORE_SECURITY);
mCachedContexts.put(packageName, context);
}
return mCachedContexts.get(packageName);
}
public synchronized View loadExternalView(String packageName, String className)
throws PackageManager.NameNotFoundException, ClassNotFoundException, ClassCastException, IllegalArgumentException, InstantiationException,
IllegalAccessException, InvocationTargetException, NoSuchMethodException, NullPointerException {
Context apkContext = loadExternalContext(packageName);
Class<?> cls = loadExternalClass(packageName, className);
return (View) cls.getConstructor(Context.class).newInstance(apkContext);
}
public Class<?> loadExternalClass(String packageName, String className)
throws PackageManager.NameNotFoundException, ClassNotFoundException {
String key = packageName + "/" + className;
if (!mCachedClasses.containsKey(key)) {
Context apkContext = loadExternalContext(packageName);
String apkName = apkContext.getPackageManager().getApplicationInfo(packageName, 0).sourceDir;
PathClassLoader pathClassLoader = new PathClassLoader(apkName, apkContext.getClassLoader()); //actual
try {
Class<?> clazz = Class.forName(className, true, pathClassLoader);
mCachedClasses.put(key, clazz);
} catch (ClassNotFoundException e) {
}
}
return mCachedClasses.get(key);
}
应用程序2:
public class DummyView_1 extends RelativeLayout {
public DummyView_1(Context context){
super(context);
loadview();
}
public DummyView_1(Context context, AttributeSet attrs){
super(context, attrs);
loadview();
}
public DummyView_1(Context context, AttributeSet attrs, int defStyle){
super(context, attrs, defStyle);
loadview();
}
public void loadview(){
LayoutInflater inflater = LayoutInflater.from(this.getContext());
inflater.inflate(R.layout.activity_dummy_1, this);
}
}