dart腳本實(shí)戰(zhàn) -- apk對(duì)齊、簽名、注入渠道

一、背景

看了張風(fēng)捷特烈大佬的《Dart 開(kāi)發(fā)命令行工具》
覺(jué)得很有意思,然后想到我們項(xiàng)目中的apk,需要通過(guò)騰訊進(jìn)行加固、4字節(jié)對(duì)齊、簽名、注入渠道等操作。

雖然只有三大步操作,但是每個(gè)操作都需要生產(chǎn)apk,然后去對(duì)應(yīng)的文件中查找,加固命令復(fù)雜,容易出錯(cuò),還要執(zhí)行腳本,復(fù)制備份,以及不需要自己找文件夾,直接在程序中打開(kāi)文件夾等。

于是就寫了下面的腳本,解放雙手并且不會(huì)出錯(cuò)。

完整代碼如下:

import 'dart:io';

Future<void> main(List<String> arguments) async {
  // 調(diào)用 zipalign 命令
  if (arguments.isEmpty || arguments.length != 2) {
    print('請(qǐng)傳入apk完整路徑和版本');
    return;
  }
  String path = arguments[0];
  String version = arguments[1];
  ///切割,拼接路徑
  String path4kb = generate4Kb(path);
  await runZipAlign(path, path4kb);

  String directoryPath = '/Users/xxx/Documents/packer-tool-v2.2/parent';
  String directoryOutPath = '/Users/xxx/Documents/packer-tool-v2.2/output';
  // 刪除其內(nèi)容
  await deleteDirectory(Directory(directoryPath));
  await deleteDirectory(Directory(directoryOutPath));
  print('Directory deleted successfully.');

  await runApkSigner(
      path4kb,
      '/Users/x x x/Documents/packer-tool-v2.2/parent/app-release-$version.apk');
  // 進(jìn)入目錄
  Directory.current = '/Users/xxx/Documents/packer-tool-v2.2';

  // 執(zhí)行 shell 腳本
  await runShellScript('sh', ['packer.sh']);

  // String sourcePath = '/Users/xxx/Documents/packer-tool-v2.2/parent';
  String destinationPath = '/Users/xxx/Desktop/keep_release';
  // 復(fù)制目錄內(nèi)容
  await copyDirectory(Directory(directoryOutPath), Directory(destinationPath));
  print('Directory copied successfully.');
  openDirectory(directoryOutPath);
}

Future<void> runZipAlign(String inputApk, String outputApk) async {
  // 請(qǐng)確保 zipalign 工具在系統(tǒng)路徑中可用
  ProcessResult result =
      await Process.run('zipalign', ['-v', '4', inputApk, outputApk]);

  // 處理標(biāo)準(zhǔn)輸出
  print('stdout: ${result.stdout}');

  // 處理標(biāo)準(zhǔn)錯(cuò)誤輸出
  print('stderr: ${result.stderr}');

  // 輸出命令的退出碼
  print('Exit code: ${result.exitCode}');
}

Future<void> runApkSigner(String inputApk, String outputApk) async {
  ProcessResult result = await Process.run(
    'apksigner',
    [
      'sign',
      '--verbose',
      '--v1-signing-enabled',
      'true',
      '--v2-signing-enabled',
      'true',
      '--v3-signing-enabled',
      'false',
      '--ks',
      '/Users/xxx/Desktop/writerassistant/writer_assistant/android/xx',
      '--ks-key-alias',
      'xxx',
      '--ks-pass',
      'pass:xxx',
      '--key-pass',
      'pass:xxx',
      '--out',
      outputApk,
      inputApk,
    ],
  );
  print('apksigner stdout: ${result.stdout}');
  print('apksigner stderr: ${result.stderr}');
  print('apksigner exit code: ${result.exitCode}');
}

Future<void> runShellScript(String command, List<String> arguments) async {
  ProcessResult result = await Process.run(command, arguments);

  // 處理標(biāo)準(zhǔn)輸出
  print('stdout: ${result.stdout}');

  // 處理標(biāo)準(zhǔn)錯(cuò)誤輸出
  print('stderr: ${result.stderr}');

  // 輸出命令的退出碼
  print('Exit code: ${result.exitCode}');
}
String generate4Kb(String path) {
  if (path.endsWith(".apk")) {
    var paths = path.split(".apk");
    return '${paths[0]}_4kb.apk';
  } else {
    throw Exception("路徑不對(duì),需要 .apk結(jié)尾");
  }
}

Future<void> deleteDirectory(Directory directory) async {
  if (await directory.exists()) {
    await for (var entity in directory.list(recursive: true)) {
      if (entity is File) {
        await entity.delete();
      } else if (entity is Directory) {
        await entity.delete(recursive: true);
      }
    }

    // await directory.delete();
  }
}

Future<void> copyDirectory(Directory source, Directory destination) async {
  if (await source.exists()) {
    await destination.create(recursive: true);

    await for (var entity in source.list(recursive: true)) {
      if (entity is File) {
        String newPath = destination.path + entity.path.substring(source.path.length);
        await entity.copy(newPath);
      } else if (entity is Directory) {
        String newPath = destination.path + entity.path.substring(source.path.length);
        await Directory(newPath).create(recursive: true);
      }
    }
  }
}

Future<void> openDirectory(String folderPath) async{
  // 創(chuàng)建一個(gè) ProcessResult 對(duì)象
  ProcessResult result = await Process.run('open', [folderPath]);

  // 檢查命令是否成功執(zhí)行
  if (result.exitCode == 0) {
    print('Folder opened successfully: $folderPath');
  } else {
    print('Failed to open folder: $folderPath');
    print('Error: ${result.stderr}');
  }
}

這個(gè)代碼的main函數(shù)是需要兩個(gè)參數(shù)的,一個(gè)apk的完整路徑,一個(gè)是產(chǎn)物apk的版本號(hào)。
參數(shù)可以從運(yùn)行設(shè)置中的Program arguments傳入;也可以通過(guò)命令行執(zhí)行:參數(shù)直接放到命令行后面。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容