打包所有apk文件
This commit is contained in:
parent
3b83c6fc0b
commit
be997233bd
|
@ -154,7 +154,18 @@ public class MainActivity extends AppCompatActivity {
|
||||||
LogFileUtil.logAndWrite(Log.INFO, "MainActivity", "onCreate: Setting up UI components",null);
|
LogFileUtil.logAndWrite(Log.INFO, "MainActivity", "onCreate: Setting up UI components",null);
|
||||||
Button runScriptButton = findViewById(R.id.run_script_button);
|
Button runScriptButton = findViewById(R.id.run_script_button);
|
||||||
if (runScriptButton != null) {
|
if (runScriptButton != null) {
|
||||||
runScriptButton.setOnClickListener(v -> AutoJsUtil.runAutojsScript(this));
|
runScriptButton.setOnClickListener(v -> {
|
||||||
|
initializeExecutorService();
|
||||||
|
|
||||||
|
executorService.submit(() -> {
|
||||||
|
try {
|
||||||
|
TaskUtil.infoUpload(MainActivity.this, getAndroidId(MainActivity.this), "wsj.reader_sp");
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
// runScriptButton.setOnClickListener(v -> AutoJsUtil.runAutojsScript(this));
|
||||||
} else {
|
} else {
|
||||||
LogFileUtil.logAndWrite(Log.WARN, "MainActivity", "Run Script Button not found",null);
|
LogFileUtil.logAndWrite(Log.WARN, "MainActivity", "Run Script Button not found",null);
|
||||||
Toast.makeText(this, "Button not found", Toast.LENGTH_SHORT).show();
|
Toast.makeText(this, "Button not found", Toast.LENGTH_SHORT).show();
|
||||||
|
|
|
@ -6,8 +6,11 @@ import android.content.Context;
|
||||||
import android.os.Environment;
|
import android.os.Environment;
|
||||||
import android.text.TextUtils;
|
import android.text.TextUtils;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
|
import com.example.studyapp.utils.FileUtils;
|
||||||
import com.example.studyapp.utils.LogFileUtil;
|
import com.example.studyapp.utils.LogFileUtil;
|
||||||
import com.example.studyapp.utils.ShellUtils;
|
import com.example.studyapp.utils.ShellUtils;
|
||||||
|
import com.example.studyapp.utils.ZipUtils;
|
||||||
import com.google.android.gms.common.util.CollectionUtils;
|
import com.google.android.gms.common.util.CollectionUtils;
|
||||||
import com.google.android.gms.common.util.MapUtils;
|
import com.google.android.gms.common.util.MapUtils;
|
||||||
import com.google.gson.Gson;
|
import com.google.gson.Gson;
|
||||||
|
@ -224,11 +227,12 @@ public class TaskUtil {
|
||||||
if (zipFile.exists()) {
|
if (zipFile.exists()) {
|
||||||
delFileSh(zipFile.getAbsolutePath());
|
delFileSh(zipFile.getAbsolutePath());
|
||||||
}
|
}
|
||||||
File copiedAPKFile = new File(context.getCacheDir(), packAge+"_upload.apk");
|
File copiedAPKFile = new File(context.getCacheDir(), packAge);
|
||||||
if (copiedAPKFile.exists()) {
|
if (copiedAPKFile.exists()) {
|
||||||
delFileSh(copiedAPKFile.getAbsolutePath());
|
delFileSh(copiedAPKFile.getAbsolutePath());
|
||||||
}
|
}
|
||||||
copyFolderSh(apkSourceFile, copiedAPKFile.getAbsolutePath());
|
String parentPath = FileUtils.getParentDirectory(apkSourceFile);
|
||||||
|
copyFolderSh(parentPath, copiedAPKFile.getAbsolutePath());
|
||||||
// boolean success = clearUpFileInDst(copiedDir);
|
// boolean success = clearUpFileInDst(copiedDir);
|
||||||
// if (success) {
|
// if (success) {
|
||||||
// // 压缩APK文件
|
// // 压缩APK文件
|
||||||
|
@ -236,13 +240,21 @@ public class TaskUtil {
|
||||||
// }
|
// }
|
||||||
|
|
||||||
// 压缩APK文件
|
// 压缩APK文件
|
||||||
compressToZip(copiedAPKFile, zipFile);
|
// compressToZip(copiedAPKFile, zipFile);
|
||||||
|
try {
|
||||||
|
ZipUtils.zipDirectory(copiedAPKFile.getAbsolutePath(), zipFile.getAbsolutePath());
|
||||||
|
}catch (Exception e){
|
||||||
|
zipFile.delete();
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
Log.e("TAG", "infoUpload compress finish: ");
|
||||||
// 上传压缩文件
|
// 上传压缩文件
|
||||||
if (!zipFile.exists()) {
|
if (!zipFile.exists()) {
|
||||||
Log.w("TaskUtil", "Upload file does not exist: " + outputZipPath);
|
Log.w("TaskUtil", "Upload file does not exist: " + outputZipPath);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
String safeNewPath = zipFile.getAbsolutePath().replace(" ", "\\ ").replace("\"", "\\\"");
|
||||||
|
String chmod = "chmod 777 \"" + safeNewPath + "\"";
|
||||||
uploadFile(zipFile);
|
uploadFile(zipFile);
|
||||||
|
|
||||||
//uninstall
|
//uninstall
|
||||||
|
@ -250,6 +262,7 @@ public class TaskUtil {
|
||||||
String chmodResult = ShellUtils.execRootCmdAndGetResult(uninstall);
|
String chmodResult = ShellUtils.execRootCmdAndGetResult(uninstall);
|
||||||
// 清理临时文件
|
// 清理临时文件
|
||||||
delFileSh(copiedAPKFile.getAbsolutePath());
|
delFileSh(copiedAPKFile.getAbsolutePath());
|
||||||
|
ShellUtils.execRootCmdAndGetResult(chmod);
|
||||||
delFileSh(zipFile.getAbsolutePath());
|
delFileSh(zipFile.getAbsolutePath());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -282,6 +295,7 @@ public class TaskUtil {
|
||||||
String result = ShellUtils.execRootCmdAndGetResult(cmd);
|
String result = ShellUtils.execRootCmdAndGetResult(cmd);
|
||||||
String chmod = "chmod 777 \"" + safeNewPath + "\"";
|
String chmod = "chmod 777 \"" + safeNewPath + "\"";
|
||||||
String chmodResult = ShellUtils.execRootCmdAndGetResult(chmod);
|
String chmodResult = ShellUtils.execRootCmdAndGetResult(chmod);
|
||||||
|
recursiveChmod777(dst);
|
||||||
// if (!TextUtils.isEmpty(chmodResult)) {
|
// if (!TextUtils.isEmpty(chmodResult)) {
|
||||||
LogFileUtil.logAndWrite(android.util.Log.ERROR, "TaskUtil", "chmodResult. Result: " + chmodResult, null);
|
LogFileUtil.logAndWrite(android.util.Log.ERROR, "TaskUtil", "chmodResult. Result: " + chmodResult, null);
|
||||||
// return false;
|
// return false;
|
||||||
|
@ -299,12 +313,37 @@ public class TaskUtil {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void recursiveChmod777(File dir) {
|
||||||
|
if (dir == null || !dir.exists()) {
|
||||||
|
Log.e("Chmod", "目录不存在或为null");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 先修改当前目录权限
|
||||||
|
String currentPath = dir.getAbsolutePath().replace(" ", "\\ ").replace("\"", "\\\"");
|
||||||
|
String chmodCmd = "chmod 777 \"" + currentPath + "\"";
|
||||||
|
ShellUtils.execRootCmdAndGetResult(chmodCmd);
|
||||||
|
|
||||||
|
// 递归处理子项
|
||||||
|
File[] children = dir.listFiles();
|
||||||
|
if (children != null) {
|
||||||
|
for (File child : children) {
|
||||||
|
if (child.isDirectory()) {
|
||||||
|
recursiveChmod777(child); // 递归处理子目录
|
||||||
|
} else {
|
||||||
|
String filePath = child.getAbsolutePath().replace(" ", "\\ ").replace("\"", "\\\"");
|
||||||
|
chmodCmd = "chmod 777 \"" + filePath + "\"";
|
||||||
|
ShellUtils.execRootCmdAndGetResult(chmodCmd);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*在给定的目标目录中清除特定的文件和目录。
|
*在给定的目标目录中清除特定的文件和目录。
|
||||||
*子目录未命名为“缓存”,并且删除了大于3 MB的文件。
|
*子目录未命名为“缓存”,并且删除了大于3 MB的文件。
|
||||||
*记录保留的文件和目录的路径。
|
*记录保留的文件和目录的路径。
|
||||||
*
|
*
|
||||||
* @param DST将处理其文件和子目录的目标目录
|
|
||||||
* @return true如果目的目录存在并且已成功处理,则为false否则
|
* @return true如果目的目录存在并且已成功处理,则为false否则
|
||||||
*/
|
*/
|
||||||
private static boolean clearUpFileInDst(File dst) {
|
private static boolean clearUpFileInDst(File dst) {
|
||||||
|
|
|
@ -0,0 +1,52 @@
|
||||||
|
package com.example.studyapp.utils;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
|
||||||
|
public class FileUtils {
|
||||||
|
/**
|
||||||
|
* 获取文件的父目录路径
|
||||||
|
* @param filePath 文件完整路径
|
||||||
|
* @return 父目录路径,如果失败返回null
|
||||||
|
*/
|
||||||
|
public static String getParentDirectory(String filePath) {
|
||||||
|
if (filePath == null || filePath.isEmpty()) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
File file = new File(filePath);
|
||||||
|
File parent = file.getParentFile();
|
||||||
|
|
||||||
|
return parent != null ? parent.getAbsolutePath() : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取文件的父目录File对象
|
||||||
|
* @param file 文件对象
|
||||||
|
* @return 父目录File对象
|
||||||
|
*/
|
||||||
|
public static File getParentDirectory(File file) {
|
||||||
|
return file != null ? file.getParentFile() : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取多级上级目录
|
||||||
|
* @param file 文件对象
|
||||||
|
* @param levels 向上追溯的层级数
|
||||||
|
* @return 上级目录路径
|
||||||
|
*/
|
||||||
|
public static String getAncestorDirectory(File file, int levels) {
|
||||||
|
if (file == null || levels <= 0) {
|
||||||
|
return file != null ? file.getAbsolutePath() : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
File parent = file;
|
||||||
|
for (int i = 0; i < levels; i++) {
|
||||||
|
parent = parent.getParentFile();
|
||||||
|
if (parent == null) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return parent != null ? parent.getAbsolutePath() : null;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,64 @@
|
||||||
|
package com.example.studyapp.utils;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.zip.ZipEntry;
|
||||||
|
import java.util.zip.ZipOutputStream;
|
||||||
|
|
||||||
|
public class ZipUtils {
|
||||||
|
/**
|
||||||
|
* 压缩目录为ZIP文件
|
||||||
|
* @param sourceDirPath 要压缩的目录路径(如:/sdcard/MyFolder)
|
||||||
|
* @param zipFilePath 生成的ZIP文件路径(如:/sdcard/Archive.zip)
|
||||||
|
* @throws IOException 如果操作失败
|
||||||
|
*/
|
||||||
|
public static void zipDirectory(String sourceDirPath, String zipFilePath) throws IOException {
|
||||||
|
File sourceDir = new File(sourceDirPath);
|
||||||
|
if (!sourceDir.exists() || !sourceDir.isDirectory()) {
|
||||||
|
throw new IOException("源目录不存在或不是目录");
|
||||||
|
}
|
||||||
|
|
||||||
|
try (FileOutputStream fos = new FileOutputStream(zipFilePath);
|
||||||
|
ZipOutputStream zos = new ZipOutputStream(fos)) {
|
||||||
|
|
||||||
|
addDirectoryToZip(sourceDir, sourceDir, zos);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 递归添加目录内容到ZIP
|
||||||
|
*/
|
||||||
|
private static void addDirectoryToZip(File rootDir, File currentDir, ZipOutputStream zos) throws IOException {
|
||||||
|
File[] files = currentDir.listFiles();
|
||||||
|
if (files == null) return;
|
||||||
|
|
||||||
|
for (File file : files) {
|
||||||
|
if (file.isDirectory()) {
|
||||||
|
addDirectoryToZip(rootDir, file, zos); // 递归处理子目录
|
||||||
|
} else {
|
||||||
|
addFileToZip(rootDir, file, zos);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 添加单个文件到ZIP
|
||||||
|
*/
|
||||||
|
private static void addFileToZip(File rootDir, File file, ZipOutputStream zos) throws IOException {
|
||||||
|
try (FileInputStream fis = new FileInputStream(file)) {
|
||||||
|
// 计算相对路径(确保ZIP内目录结构正确)
|
||||||
|
String relativePath = rootDir.toURI().relativize(file.toURI()).getPath();
|
||||||
|
ZipEntry zipEntry = new ZipEntry(relativePath);
|
||||||
|
zos.putNextEntry(zipEntry);
|
||||||
|
|
||||||
|
byte[] buffer = new byte[1024];
|
||||||
|
int length;
|
||||||
|
while ((length = fis.read(buffer)) > 0) {
|
||||||
|
zos.write(buffer, 0, length);
|
||||||
|
}
|
||||||
|
zos.closeEntry();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue