東川印記

一本東川,笑看爭龍斗虎;寰茫兦者,度橫佰昧人生。

Android APK 打包系统签名方式实现静默安装卸载

2021年8月21日星期六



需要用到静默安装、卸载升级其他app....

1,控制app调整

增加 

android:sharedUserId="android.uid.system"

找到系统ROM所用签名文件

platform.pk8

platform.x509.pem

不加系统签名会报错:

adb: failed to install signed.apk: Failure [INSTALL_FAILED_UPDATE_INCOMPATIBLE: Package xxxxapps.rainbow signatures do not match previously installed version; ignoring!]

加系统签名:

SENRSL:Downloads senrsl$ java -jar signapk.jar 9.0系统签名/platform.x509.pem 9.0系统签名/platform.pk8 a_displayer/build/outputs/apk/df/debug/a_displayer-df-debug.apk sing-rainbow.apk
SENRSL:Downloads senrsl$ adb install -t sing-rainbow.apk
Success
SENRSL:Downloads senrsl$

2,安三方应用

avc:  denied  { read } for  scontext=u:r:system_server:s0 tcontext=u:object_r:sdcardfs:s0 tclass=file permissive=1

在android P以后,不能随便安了,只能直接安装 固定目录下的

K10Pro:/ # pm install -r /data/local/tmp/test.apk                                                                                                     
Success
K10Pro:/ #

或者

cat $apkfile.apk | pm install -S $apkfile.length

3,Android 9.0

发现在android 9.0 P上,不能再用pm来操作了。。。。

一个开源的系统,逐渐走向封闭。。。。

找到的能用的,只能通过反射解决。。。。

4,在Android 9 上静默安装

public static boolean install(Context context, String apkPath) { if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.LOLLIPOP) return false; PackageInstaller packageInstaller = context.getPackageManager().getPackageInstaller(); PackageInstaller.SessionParams params = new PackageInstaller.SessionParams(PackageInstaller.SessionParams.MODE_FULL_INSTALL); String pkgName = getApkPackageName(context, apkPath); if (pkgName == null) { return false; } params.setAppPackageName(pkgName); try { Method allowDowngrade = PackageInstaller.SessionParams.class.getMethod("setAllowDowngrade", boolean.class); allowDowngrade.setAccessible(true); allowDowngrade.invoke(params, true); } catch (Exception e) { e.printStackTrace(); } OutputStream os = null; InputStream is = null; try { int sessionId = packageInstaller.createSession(params); PackageInstaller.Session session = packageInstaller.openSession(sessionId); os = session.openWrite(pkgName, 0, -1); is = new FileInputStream(apkPath); byte[] buffer = new byte[1024]; int len; while ((len = is.read(buffer)) != -1) { os.write(buffer, 0, len); } session.fsync(os); os.close(); os = null; is.close(); is = null; Intent intent = new Intent(getContext(), DistributeStatusReceiver.class);//new Intent(Intent.ACTION_MAIN) session.commit(PendingIntent.getBroadcast(context, sessionId, intent, 0).getIntentSender()); } catch (Exception e) { Logger.w("" + e.getMessage()); return false; } finally { if (os != null) { try { os.close(); } catch (IOException e) { e.printStackTrace(); } } if (is != null) { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } } return true; } /** * 获取apk的包名 */ public static String getApkPackageName(Context context, String apkPath) { PackageManager pm = context.getPackageManager(); PackageInfo info = pm.getPackageArchiveInfo(apkPath, 0); if (info != null) { return info.packageName; } else { return null; } }


5,在Android 9 上静默卸载

/** * 根据包名卸载应用 * * @param packageName */ public static void uninstall(String packageName) { if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.LOLLIPOP) return; try { Intent broadcastIntent = new Intent(getContext(), DistributeStatusReceiver.class); PendingIntent pendingIntent = PendingIntent.getBroadcast(getContext(), 1, broadcastIntent, PendingIntent.FLAG_UPDATE_CURRENT); PackageInstaller packageInstaller = getContext().getPackageManager().getPackageInstaller(); packageInstaller.uninstall(packageName, pendingIntent.getIntentSender()); } catch (Exception e) { e.printStackTrace(); } }


6,打开三方应用

public static void start(@NonNull String packageName, @NonNull String className) throws ActivityNotFoundException { Intent intent = new Intent(); intent.setClassName(packageName, className); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); getContext().startActivity(intent); }

感觉google马上就把android玩死了,毕竟是一贯的风格。。。。

--
senRsl
2021年08月21日15:13:36

没有评论 :

发表评论