參考地址
?接上文: 幾個(gè)錯(cuò)誤解決如下
1.Adding support for Java 8 language features could solve this issue.
kotlinOptions { jvmTarget = 1.8}

2. 文件的copy 與命名?
工具類FileUtils?
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* @author as752497576@gmail.com
* @description
* @time 2023/3/12
*/
public class FileUtils {
? ? /**
? ? * 創(chuàng)建文件夾
? ? */
? ? public static boolean createDirs(String dirPath) {
? ? ? ? File file = new File(dirPath);
? ? ? ? if (!file.exists() || !file.isDirectory()) {
? ? ? ? ? ? return file.mkdirs();
? ? ? ? }
? ? ? ? return true;
? ? }
? ? /**
? ? * 文件的復(fù)制操作方法
? ? *
? ? * @param fromFile 準(zhǔn)備復(fù)制的文件
? ? * @param toFile? 要復(fù)制的文件的目錄
? ? */
? ? public static void copyfile(File fromFile, File toFile) {
? ? ? ? if (!fromFile.exists()) {
? ? ? ? ? ? return;
? ? ? ? }
? ? ? ? if (!fromFile.isFile()) {
? ? ? ? ? ? return;
? ? ? ? }
? ? ? ? if (!fromFile.canRead()) {
? ? ? ? ? ? return;
? ? ? ? }
? ? ? ? if (!toFile.getParentFile().exists()) {
? ? ? ? ? ? toFile.getParentFile().mkdirs();
? ? ? ? }
? ? ? ? if (toFile.exists()) {
? ? ? ? ? ? toFile.delete();
? ? ? ? }
? ? ? ? try {
? ? ? ? ? ? FileInputStream fosfrom = new FileInputStream(fromFile);
? ? ? ? ? ? FileOutputStream fosto = new FileOutputStream(toFile);
? ? ? ? ? ? byte[] bt = new byte[1024];
? ? ? ? ? ? int c;
? ? ? ? ? ? while ((c = fosfrom.read(bt)) > 0) {
? ? ? ? ? ? ? ? fosto.write(bt, 0, c);
? ? ? ? ? ? }
? ? ? ? ? ? //關(guān)閉輸入、輸出流
? ? ? ? ? ? fosfrom.close();
? ? ? ? ? ? fosto.close();
? ? ? ? } catch (FileNotFoundException e) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? } catch (IOException e) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
? ? }
? ? /**
? ? * 重命名文件
? ? *
? ? * @param oldPath 原來的文件地址
? ? * @param newPath 新的文件地址
? ? */
? ? public static void renameFile(String oldPath, String newPath) {
? ? ? ? File oleFile = new File(oldPath);
? ? ? ? File newFile = new File(newPath);
? ? ? ? //執(zhí)行重命名
? ? ? ? oleFile.renameTo(newFile);
? ? }
? ? /**
? ? * 刪除文件夾
? ? *
? ? * @return boolean
? ? */
? ? public static void delFolder(String folderPath) {
? ? ? ? try {
? ? ? ? ? ? delAllFile(folderPath); //刪除完里面所有內(nèi)容
? ? ? ? ? ? String filePath = folderPath;
? ? ? ? ? ? filePath = filePath.toString();
? ? ? ? ? ? File myFilePath = new File(filePath);
? ? ? ? ? ? myFilePath.delete(); //刪除空文件夾
? ? ? ? } catch (Exception e) {
? ? ? ? ? ? System.out.println("刪除文件夾操作出錯(cuò)");
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
? ? }
? ? /**
? ? * 刪除文件夾里面的所有文件
? ? *
? ? * @param path String 文件夾路徑 如 c:/fqf
? ? */
? ? public static void delAllFile(String path) {
? ? ? ? File file = new File(path);
? ? ? ? if (!file.exists()) {
? ? ? ? ? ? return;
? ? ? ? }
? ? ? ? if (!file.isDirectory()) {
? ? ? ? ? ? return;
? ? ? ? }
? ? ? ? String[] tempList = file.list();
? ? ? ? File temp = null;
? ? ? ? for (int i = 0; i < tempList.length; i++) {
? ? ? ? ? ? if (path.endsWith(File.separator)) {
? ? ? ? ? ? ? ? temp = new File(path + tempList[i]);
? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? temp = new File(path + File.separator + tempList[i]);
? ? ? ? ? ? }
? ? ? ? ? ? if (temp.isFile()) {
? ? ? ? ? ? ? ? temp.delete();
? ? ? ? ? ? }
? ? ? ? ? ? if (temp.isDirectory()) {
? ? ? ? ? ? ? ? delAllFile(path + "/" + tempList[i]);//先刪除文件夾里面的文件
? ? ? ? ? ? ? ? delFolder(path + "/" + tempList[i]);//再刪除空文件夾
? ? ? ? ? ? }
? ? ? ? }
? ? }
}
val fileName ="huipijiang" +".apk"
val cacheDir = BaseApplication.mContext.getDownLoadPath()
FileUtils.createDirs(cacheDir)
var? path? =? ? File(cacheDir + File.separator + fileName)
FileUtils.copyfile(resource,path? )
FileUtils.renameFile(resource.toString(),path.toString()? )?
FileUtils.delFolder(resource.toString()); // 刪除下載的文件
SilentInstall.install(File(cacheDir + File.separator + fileName),BaseApplication.getContext())? //安裝apk?
getDownLoadPath()代碼
fun getDownLoadPath(): String {
return if (Build.VERSION.SDK_INT >29) {
getExternalFilesDir(null)?.absolutePath + File.separator +"BifrostApkDownload"
? ? }else {
Environment.getExternalStorageDirectory().absolutePath + File.separator +"BifrostApkDownload"
? ? }
}

安裝代碼:?
Intent intent =new Intent(Intent.ACTION_VIEW);?
intent.setAction(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);?
intent.addCategory(Intent.CATEGORY_DEFAULT);
Uri uri;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {?
? ? uri = FileProvider.getUriForFile(context, context.getApplicationContext().getPackageName() +".fileProvider", apk);
? ? intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
}else {?
? ? uri = Uri.fromFile(apk);
}
intent.setDataAndType(uri, "application/vnd.android.package-archive");
context.startActivity(intent);