Android P使用pm install安装apk报错
PX30_Android10:/sdcard # pm install SU70_1.apk
avc: denied { read } for scontext=u:r:system_server:s0 tcontext=u:object_r:sdcardfs:s0 tclass=file permissive=0
System server has no access to read file context u:object_r:sdcardfs:s0 (from path /storage/emulated/0/SU70_1.apk, context u:r:system_server:s0)
Error: Unable to open file: SU70_1.apk
Consider using a file under /data/local/tmp/
Error: Can't open file: SU70_1.apk
Exception occurred while executing:
java.lang.IllegalArgumentException: Error: Can't open file: SU70_1.apk
at com.android.server.pm.PackageManagerShellCommand.setParamsSize(PackageManagerShellCommand.java:461)
at com.android.server.pm.PackageManagerShellCommand.runInstall(PackageManagerShellCommand.java:1060)
at com.android.server.pm.PackageManagerShellCommand.onCommand(PackageManagerShellCommand.java:169)
at android.os.ShellCommand.exec(ShellCommand.java:104)
at com.android.server.pm.PackageManagerService.onShellCommand(PackageManagerService.java:22086)
at android.os.Binder.shellCommand(Binder.java:881)
at android.os.Binder.onTransact(Binder.java:765)
at android.content.pm.IPackageManager$Stub.onTransact(IPackageManager.java:4952)
at com.android.server.pm.PackageManagerService.onTransact(PackageManagerService.java:4086)
at android.os.Binder.execTransactInternal(Binder.java:1021)
at android.os.Binder.execTransact(Binder.java:994)
解决方法
在android P以前可以使用pm install安装apk,但是在android P出现报以上错误,有以下两种解决方法
1.将文件拷贝到/data/local/tmp/下再进行安装
String path = “/data/local/tmp/”;
String fileName = PathData.utfApkName;
File apkFile = new File(path + fileName);
String command = "pm install " + apkFile.getAbsolutePath();
2.使用cat $apkfile.apk | pm install -S $apkfile.length
String installCommand = " cat " + apkFile.getAbsolutePath() + " | pm install -S "+ apkLength;
3.根据提示修改selinux权限:
avc: denied { read } for scontext=u:r:system_server:s0 tcontext=u:object_r:sdcardfs:s0 tclass=file permissive=0
System server has no access to read file context u:object_r:sdcardfs:s0 (from path /storage/emulated/0/SU70_1.apk, context u:r:system_server:s0)