android_app_package,android - Install APK as system app with INSTALL_PACKAGES - Stack Overflow

This question already has answers here:

Closed 2 years ago.

I have developed an app that is running with system permissions (it is built into the image of AOSP) and has the INSTALL_PACKAGES permission.

My goal is to install a APK silently in the background without user interaction. I read different approaches on stackoverflow, but most of them were either outdated or simply didn't work.

For example if I try to use the PackageManager (either via reflection or via running as a shell command from within my app) I get the following error and that's where I got stuck currently.

11-20 19:06:10.999 4965 4965 D AndroidRuntime: >>>>>> START

com.android.internal.os.RuntimeInit uid 10039 <<<<<<

11-20 19:06:11.199 4965 4965 D AndroidRuntime: Calling main entry com.android.commands.pm.Pm

11-20 19:06:11.268 4965 4965 E Pm : Error

11-20 19:06:11.268 4965 4965 E Pm : java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.AppOpsManager.checkPackage(int, java.lang.String)' on a null object reference

11-20 19:06:11.268 4965 4965 E Pm : at android.os.Parcel.readException(Parcel.java:2010)

11-20 19:06:11.268 4965 4965 E Pm : at android.os.Parcel.readException(Parcel.java:1950)

11-20 19:06:11.268 4965 4965 E Pm : at android.content.pm.IPackageInstaller$Stub$Proxy.createSession(IPackageInstaller.java:254)

11-20 19:06:11.268 4965 4965 E Pm : at com.android.commands.pm.Pm.doCreateSession(Pm.java:607)

11-20 19:06:11.268 4965 4965 E Pm : at com.android.commands.pm.Pm.runInstall(Pm.java:431)

11-20 19:06:11.268 4965 4965 E Pm : at com.android.commands.pm.Pm.run(Pm.java:150)

11-20 19:06:11.268 4965 4965 E Pm : at com.android.commands.pm.Pm.main(Pm.java:107)

11-20 19:06:11.268 4965 4965 E Pm : at com.android.internal.os.RuntimeInit.nativeFinishInit(Native Method)

11-20 19:06:11.268 4965 4965 E Pm : at com.android.internal.os.RuntimeInit.main(RuntimeInit.java:285)

11-20 19:06:11.270 4965 4965 I app_process: System.exit called, status: 1

11-20 19:06:11.270 4965 4965 I AndroidRuntime: VM exiting with result code 1.

Can anybody tell me why this error is occurring or alternatively other solutions or the "modern" way to install APKS silently with INSTALL_PACKAGES?

Any help appreciated.

EDIT:

To the guy who marked this as a duplicate of nullpointer exception - how do you make the connection that would be a duplicate? I am specifically asking for the reasons behind it and not about the nullpointer exception itself. Your duplicate suggestion makes no sense at all.

EDIT 2:

My code that tries to install the APK (copied from the package installer app of AOSP sources):

public boolean install(Context context) {

Log.d(TAG, "Start Installation");

String packageName = "com.spotify.music";

String apkPath = Environment.getExternalStorageDirectory().toString() + "/APKs/spotify.apk";

PackageInstaller.Session session;

int sessionId;

PackageInstaller.SessionParams params = new PackageInstaller.SessionParams(PackageInstaller.SessionParams.MODE_FULL_INSTALL);

params.setAppPackageName(packageName);

try {

sessionId = context.getPackageManager().getPackageInstaller().createSession(params);

session = context.getPackageManager().getPackageInstaller().openSession(sessionId);

} catch (IOException e) {

Log.w(TAG, "Error creating/opening the installing session", e);

return false;

}

try {

File file = new File(apkPath);

try (InputStream in = new FileInputStream(file)) {

long sizeBytes = file.length();

try (OutputStream out = session.openWrite("PackageInstaller", 0, sizeBytes)) {

byte[] buffer = new byte[1024 * 1024];

while (true) {

int numRead = in.read(buffer);

if (numRead == -1) {

session.fsync(out);

break;

}

out.write(buffer, 0, numRead);

if (sizeBytes > 0) {

float fraction = ((float) numRead / (float) sizeBytes);

Log.d(TAG, "Install progress: " + fraction);

}

}

}

}

Log.d(TAG, "Install finished");

session.commit(PendingIntent.getBroadcast(context, sessionId, new Intent("android.intent.action.MAIN"), 0).getIntentSender());

return true;

} catch (IOException | SecurityException e) {

Log.e(TAG, "Could not write package", e);

session.close();

return false;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值