This is my first post on StackOverflow
.
I have used the reflection in my boosting app like below. It works well in sdk<26 but in sdk 26 I get the java.lang.reflect.InvocationTargetException exception. How can I bring it back to work in android 8?
My code is like:
Method pkgMethod = getPackageManager().getClass().getMethod("getPackageSizeInfo", String.class, IPackageStatsObserver.class);
pkgMethod.invoke(getPackageManager(), pkg.packageName, new IPackageStatsObserver.Stub() {
@Override
public void onGetStatsCompleted(PackageStats pStats, boolean succeeded) throws RemoteException {
long cacheSize = pStats.externalCacheSize;
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M)
cacheSize += pStats.cacheSize;
if (cacheSize > 0) {
Drawable icon = null;
try {
icon = getPackageManager().getApplicationIcon(pkg.packageName);
} catch (PackageManager.NameNotFoundException ignored) {
}
String name = pkg.packageName;
try {
PackageManager pm = getApplicationContext().getPackageManager();
name = (String) pm.getApplicationLabel(pm.getApplicationInfo(pkg.packageName, PackageManager.GET_META_DATA));
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
totalSize += cacheSize;
appList.add(new Items(icon, name, pkg.packageName, cacheSize, true, ""));
publishProgress();
}
countDownLatch.countDown();
}
});
And here is the exception I get:
java.lang.reflect.InvocationTargetException
04-05 05:20:14.027 2701-4724/com.mydidantivirus.android W/System.err:
at java.lang.reflect.Method.invoke(Native Method)
04-05 05:20:14.027 2701-4724/com.myantivirus.android W/System.err:
at com.my.antivirus.ui.JunkFileActivity$ScanTask.doInBackground(JunkFileActivity.java:285)
04-05 05:20:14.028 2701-4724/com.myantivirus.android W/System.err: at com.my.antivirus.ui.JunkFileActivity$ScanTask.doInBackground(JunkFileActivity.java:260)
04-05 05:20:14.028 2701-4724/com.myantivirus.android W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:333)
04-05 05:20:14.029 2701-4724/com.myantivirus.android W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:266)
04-05 05:20:14.029 2701-4724/com.myantivirus.android W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
04-05 05:20:14.029 2701-4724/com.myantivirus.android W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
04-05 05:20:14.029 2701-4724/com.myantivirus.android W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
04-05 05:20:14.029 2701-4724/com.myantivirus.android W/System.err: at java.lang.Thread.run(Thread.java:764)
04-05 05:20:14.030 2701-4724/com.myantivirus.android W/System.err: Caused by: java.lang.NullPointerException: Attempt to read from field 'long android.content.pm.PackageStats.externalCacheSize' on a null object reference
04-05 05:20:14.030 2701-4724/com.myantivirus.android W/System.err: at com.my.antivirus.ui.JunkFileActivity$ScanTask$1.onGetStatsCompleted(JunkFileActivity.java:288)
04-05 05:20:14.030 2701-4724/com.myantivirus.android W/System.err: at android.app.ApplicationPackageManager.getPackageSizeInfoAsUser(ApplicationPackageManager.java:2167)
04-05 05:20:14.030 2701-4724/com.myantivirus.android W/System.err: at android.content.pm.PackageManager.getPackageSizeInfo(PackageManager.java:5065)
04-05 05:20:14.030 2701-4724/com.myantivirus.android W/System.err: ... 9 more
I also tried a cache cleaner library on Android Oreo and got this error:
java.lang.reflect.InvocationTargetException
04-05 06:50:46.590 8639-8741/org.mazhuang.cleanexpert W/System.err: at
java.lang.reflect.Method.invoke(Native Method)
04-05 06:50:46.590 8639-8741/org.mazhuang.cleanexpert W/System.err: at org.mazhuang.cleanexpert.util.CleanUtil.freeAllAppsCache(CleanUtil.java:96)
04-05 06:50:46.591 8639-8741/org.mazhuang.cleanexpert W/System.err: at org.mazhuang.cleanexpert.ui.JunkCleanActivity$5.run(JunkCleanActivity.java:306)
04-05 06:50:46.591 8639-8741/org.mazhuang.cleanexpert W/System.err: at java.lang.Thread.run(Thread.java:764)
04-05 06:50:46.591 8639-8741/org.mazhuang.cleanexpert W/System.err: Caused by: java.lang.SecurityException: Neither user 10087 nor current process has android.permission.CLEAR_APP_CACHE.
04-05 06:50:46.591 8639-8741/org.mazhuang.cleanexpert W/System.err: at android.os.Parcel.readException(Parcel.java:1942)
04-05 06:50:46.591 8639-8741/org.mazhuang.cleanexpert W/System.err: at android.os.Parcel.readException(Parcel.java:1888)
04-05 06:50:46.591 8639-8741/org.mazhuang.cleanexpert W/System.err: at android.content.pm.IPackageManager$Stub$Proxy.freeStorageAndNotify(IPackageManager.java:4865)
04-05 06:50:46.591 8639-8741/org.mazhuang.cleanexpert W/System.err: at android.app.ApplicationPackageManager.freeStorageAndNotify(ApplicationPackageManager.java:2112)
04-05 06:50:46.592 8639-8741/org.mazhuang.cleanexpert W/System.err: at android.content.pm.PackageManager.freeStorageAndNotify(PackageManager.java:4996)
04-05 06:50:46.593 8639-8741/org.mazhuang.cleanexpert W/System.err: ... 4 more
Aucun commentaire:
Enregistrer un commentaire