在RK3399 Android8.1上面,实现APK静默安装功能,下面是我们的测试验试过程:
1.在RK3399 Android8.1源码目录下生成系统签名KEY:
进入build/target/product/security目录:
cd build/target/product/security
创建build_system_jks2.sh文件,内容如下:
#!/bin/bash
echo "生成系统签名文件中..."
openssl pkcs8 -inform DER -nocrypt -in platform.pk8 -out platform.pem
openssl pkcs12 -export \
-in platform.x509.pem \
-out platform.p12 \
-inkey platform.pem \
-password pass:android \
-name system
keytool -importkeystore \
-destkeystore ./systemkjd.jks \
-srckeystore ./platform.p12 \
-srcstoretype PKCS12 \
-srcstorepass android
echo "生成完毕!"
然后保存后执行如下命令:
chmod +x build_system_jks2.sh
./build_system_jks2.sh
执行后会提示如下内容:
生成系统签名文件中...
Importing keystore ./platform.p12 to ./systemkjd.jks...
Enter destination keystore password:此处输入自己保存KEY的密码,我输入的是stxinu,然后回车后提示:
Re-enter new password:
再输入一次stxinu回车后提示:
Entry for alias system successfully imported.
Import command completed: 1 entries successfully imported,0 entries failed or cancelled
Warning:
The JKS keystore uses a proprietary format. It is recommended to migrate to PKCS12 which is an industry standard format using "keytool -importkeystore -srckeystore ./systemkjd.jks -destkeystore ./systemkjd.jks -deststoretype pkcs12".
生成完毕!
执行至此,在当前目录下会生成systemkjd.jks文件,该文件为Android Studio工程需要用的系统签名KEY。
2.Android Studio工程相关修改:
我们创建的工程为SilentInstallAPK,进入工程根目录下创建signature目录,将上面生成的systemkjd.jks文件复制到该目录下。
修改app的build.gradle文件,在android配置项里添加如下内容:
signingConfigs {
rk3399_8 {
keyAlias 'system'
keyPassword 'android'
storeFile file('../signature/systemkjd.jks')
storePassword 'stxinu'
}
}
此处的system、android和stxinu与上面生成KEY时是对应的,都加粗了。然后修改buildTypes项,改为如下内容:
buildTypes {
release {
minifyEnabledfalse
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'),'proguard-rules.pro'
signingConfig signingConfigs.rk3399_8
}
debug {
minifyEnabledfalse
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'),'proguard-rules.pro'
signingConfig signingConfigs.rk3399_8
}
}
接下来在AndroidManifest.xml里面加上UID以及Permission配置,如下加粗部分为需要添加的内容:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
android:sharedUserId="android.uid.system"
package="com.slam.test"><uses-permission android:name="android.permission.INSTALL_PACKAGES"/>
再接下来就是代码内容了,关键代码封装如下:
public static boolean silentInstall(PackageManager packageManager, String apkPath) {
Class pmClz = packageManager.getClass();
try {
if (Build.VERSION.SDK_INT >=21) {
Class aClass = Class.forName("android.app.PackageInstallObserver");
Constructor constructor = aClass.getDeclaredConstructor();
constructor.setAccessible(true);
Object installObserver = constructor.newInstance();
Method method = pmClz.getDeclaredMethod("installPackage", Uri.class, aClass, int.class, String.class);
method.setAccessible(true);
method.invoke(packageManager, Uri.fromFile(new File(apkPath)), installObserver, 2, null);
}else {
Method method = pmClz.getDeclaredMethod("installPackage", Uri.class, Class.forName("android.content.pm.IPackageInstallObserver"), int.class, String.class);
method.setAccessible(true);
method.invoke(packageManager, Uri.fromFile(new File(apkPath)), null, 2, null);
}
return true;
}catch (Exception e) {
Log.e("stxinu", e.toString());
}
return false;
}
然后在我们需要的地方调用上面函数即可,下面是我们的调用例子代码片段:
PackageManager mPm = getApplicationContext().getPackageManager();
if (silentInstall(mPm, "/data/test.apk")) {
Toast.makeText(getApplicationContext(), "silent install success!", Toast.LENGTH_LONG).show();
}else {
Toast.makeText(getApplicationContext(), "silent install fail!", Toast.LENGTH_LONG).show();
}
接下来,我们将要静默安装的APK放到Android机器的/data目录下,名字为test.apk,这由上面测试代码的标粗内容决定。
运行这段测试代码后,就会自动将/data/test.apk安装到系统里面,过程无任何界面变化。
参考资料:https://blog.csdn.net/u013270171/article/details/89320824
https://blog.csdn.net/tjj93622/article/details/54964382
网友评论