转载自:RK3399 Android8.1静默安装APK,部分修改.
在RK3399 Android8.1上面,实现APK静默安装功能,下面是我们的测试验试过程:
#!/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
执行后会提示如下内容:
删除文件systemkjd.jks,再次执行./build_system_jks2.sh 生成的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时是对应的,stxinu为生成systemkjd.jks时你自己输入的密码。然后修改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配置,如下加粗部分为需要添加的内容:
/**
* 安装下载的新版本.静默安装
*/
public static boolean silenceInstallApk(String apkPath) {
Process process = null;
BufferedReader successResult = null;
BufferedReader errorResult = null;
StringBuilder successMsg = new StringBuilder();
StringBuilder errorMsg = new StringBuilder();
try {
// process = new ProcessBuilder("pm", "install", "-i", "com.xh.hwcontroller", "-r", apkPath).start();
process = new ProcessBuilder("pm", "install","-i",BuildConfig.APPLICATION_ID, "-r", apkPath).start();
successResult = new BufferedReader(new InputStreamReader(process.getInputStream()));
errorResult = new BufferedReader(new InputStreamReader(process.getErrorStream()));
String s;
while ((s = successResult.readLine()) != null) {
successMsg.append(s);
}
while ((s = errorResult.readLine()) != null) {
errorMsg.append(s);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (successResult != null) {
successResult.close();
}
if (errorResult != null) {
errorResult.close();
}
} catch (Exception e) {
e.printStackTrace();
}
if (process != null) {
process.destroy();
}
}
//如果含有“success”单词则认为安装成功
return successMsg.toString().equalsIgnoreCase("success");
}