背景
打包,mapping,上传,这些是不是都困扰了开发很多。苦于复制粘贴?反正我是。于是,有了这个
最终效果
最终的效果,就是点击项目的打包gradle,复制mapping文件且进行bugly混淆的自动上报。
环境
win10
gradle7.5
jdk8,jdk11
as2022+
特别说明
jdk是要有jdk8和jdk11的。因为bugly的上报jar包,只能用jdk8运行。
上图是bugly符号表上传的官方说明,已经两年没更新了,可能已经稳定了吧,反正只能用jdk8。
对于上传符号表不懂的朋友,可以看我这篇文章。
https://blog.csdn.net/motosheep/article/details/130398365
开始编码
首先,写grovvy类似的脚本,需要有一定的java基础。这里就不在太多细节上面叙述了,麻烦自行研究。
这里主要分为两大块:
(一)mapping文件的复制
(二)mapping文件的上传
开始!!这里由于过于简单,就两个步骤一起讲。
文件复制,来来去去,都是一个流的操作,而在grovvy中,只需要通过Files.copy即可完成操作!方便吧!
核心代码讲完了,可以关网页了。
好了,言归正传。
首先大致的流程如下:
找到release包生成的mapping目录–>复制mapping文件到自定义的目录–>上传mapping文件到bugly。对于上传,这里我写了一个bat脚本,直接在代码里面调用即可!
!!!注意!!!
自己写的xxx.gradle文件,需要放到项目的根目录。然后,需要在app项目中的build.gradle文件使用指令:apply from: "…/copyFileBat.gradle"进行引入,然后愉快地编写task即可。
这里要注意,我们是执行完 assembleRelease 才去进行复制上传的。通过百度网上的代码,可谓是五花八门,最后经过不断尝试,终于跑通了,下面放出代码,目的只有一个,就是要后人,少走弯路!
/*** 在打完正式包后执行(assembleRelease)*/
project.tasks.whenTaskAdded { Task task ->if (task.name == 'assembleRelease') {task.doLast {//todo 操作}}
}
我们的复制,上传,就是在todo作用域里面进行编码的。
而复制,就是传入两个文件全路径,进行api调用即可。例如:
a.jpg,b.jpg
Files.copy(new File(a.jpg).toPath(),new File(b.jpg).toPath())
即可实现复制!
而项目中,一般mapping路径如下图:
好了,源文件路径有了,复制指令有了,自己创建一个目录玩去吧。–就是一些File的api操作而已,不过记得,复制之前,要把父目录创建好,才不会报io错误。
下面放出所有核心代码:
copyFileBat.gradle文件:
import java.nio.file.Files
import java.text.SimpleDateFormatext {//copyFiledef CopyFilePath = "${rootProject.rootDir}/copytask/"//mapping文件原始目录MappingFileOrgPath = "${project.buildDir}/outputs/mapping/release/mapping.txt"//mapping复制文件目标目录def mappingRadonFile = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + "mapping.txt"MappingFileRootPath = CopyFilePath + "/mapping/"MappingPathName = MappingFileRootPath + mappingRadonFile//源apk文件ApkSourcePath = "${project.buildDir}/outputs/apk/release/"//复制apk目标文件夹ApkTargetPath = CopyFilePath + "/release/"//!!!!!!!!!!bugly 混淆上传相关,需要按照项目配置!!!!!!!!!!!!!!//jdk8运行环境
// JDK8Path = "D:\\software\\java\\jdk1.8\\bin\\java.exe"JDK8Path = "D:\\software\\Java\\jdk1.8.0_211\\bin\\java.exe"BuglySymJar = new File("${rootProject.rootDir}").parent + "/buglyqq-upload-symbol/buglyqq-upload-symbol.jar"BuglySymMappingPath = new File("${rootProject.rootDir}").parent + "/buglyqq-upload-symbol/mappingfile"BuglyPackageName = "com.example.androidjiaguchannel"BuglyKey = "d89bcdce-978b-4485-8ff4-a95b20372ed6"BuglyId = "877be21cd4"BuglyBat = new File("${rootProject.rootDir}").parent + "/buglyqq-upload-symbol/run.bat"
}/*** 查找apk文件* @param path* @param suffix* @return*/
private static File findApkFile(path, suffix) {def dir = new File(path)return dir.listFiles().find { it.isFile() && it =~ /.*${suffix}\.apk/ }
}/*** 查找txt文件* @param path* @param suffix* @return*/
private static File findTxtFile(path, suffix) {def dir = new File(path)return dir.listFiles().find { it.isFile() && it =~ /.*${suffix}\.txt/ }
}/*** 复制mapping文件* */
private static boolean copyMapping(MappingFileOrgPath, MappingFileRootPath, MappingPathName) {//复制mapping文件System.println("----------------assembleRelease finish----------------")try {System.println("----------------copy mapping file start---------------")def orgMappingFilePath = new File(MappingFileOrgPath)def targetMappingPath = new File(MappingFileRootPath)targetMappingPath.deleteDir()targetMappingPath.mkdirs()def targetMappingFilePath = new File(MappingPathName)Files.copy(orgMappingFilePath.toPath(), targetMappingFilePath.toPath())System.println("----------------copy mapping 目录---------------")System.println(targetMappingFilePath.toPath())} catch (Exception e) {e.printStackTrace()System.println("----------------copy mapping error,不再进行后续操作----------------")return false}return true
}/*** 复制apk文件* */
private static boolean copyReleaseApk(ApkSourcePath, ApkTargetPath) {//复制生成的apk release文件try {System.println("----------------copy release apk---------------")System.println("----------------copy apk file start---------------")def findSourceApk = findApkFile(ApkSourcePath, "-release")if (findSourceApk == null) {System.println("----------------copy apk can‘t find-------------")return}def sourceApkNameFullPath = findSourceApk.toPath().toString()def fileNameStartPos = sourceApkNameFullPath.lastIndexOf(File.separator)def sourceFileName = "app-release.apk"if (fileNameStartPos != -1) {sourceFileName = sourceApkNameFullPath.substring(fileNameStartPos + 1)}def orgApkFilePath = findSourceApkdef targetApkPath = new File(ApkTargetPath)targetApkPath.deleteDir()targetApkPath.mkdirs()def targetApkFilePath = new File(ApkTargetPath + sourceFileName)Files.copy(orgApkFilePath.toPath(), targetApkFilePath.toPath())System.println("----------------copy apk 目录---------------")System.println(targetApkFilePath.toPath())} catch (Exception e) {e.printStackTrace()System.println("----------------copy apk error,不再进行后续操作----------------")return false}return true
}/*** bugly mapping上传* */
private static void buglyMappingUpload(JDK8Path, BuglyBat, BuglySymMappingPath, MappingFileRootPath,BuglySymJar, BuglyPackageName, BuglyId, BuglyKey) {try {
//复制mapping文件到脚本目录,然后上传到buglydef jdk8File = new File(JDK8Path)if (!jdk8File.exists()) {System.println("----------------mapping上传要求的jdk8环境不存在---------------" + jdk8File.toPath())return}def outsidePath = BuglyBatdef uploadBatFile = new File(outsidePath)if (!uploadBatFile.exists()) {System.println("----------------mapping上传脚本不存在---------------")return}def outsideMappingPath = BuglySymMappingPath + "/"new File(outsideMappingPath).mkdirs()System.println("----------------mapping上传脚本开始---------------")def findSourceMappingFile = findTxtFile(MappingFileRootPath, "mapping")if (findSourceMappingFile == null) {System.println("----------mapping上传错误,源文件不存在----------")return}def orgFileName = "mapping.txt"def fileNameStartPos = findSourceMappingFile.toPath().toString().lastIndexOf(File.separator)if (fileNameStartPos != -1) {orgFileName = findSourceMappingFile.toPath().toString().substring(fileNameStartPos + 1)}//外部mapping文件def outSizeFilePath = outsideMappingPath + orgFileNamedef outsideFile = new File(outSizeFilePath)outsideFile.delete()Files.copy(findSourceMappingFile.toPath(), outsideFile.toPath())System.println("bugly文件路径: " + outsideFile.toPath())System.println("脚本路径:" + uploadBatFile.path)Process process = Runtime.getRuntime().exec("cmd /c start " + uploadBatFile.path +" ${JDK8Path} ${BuglySymJar} " +"${BuglyPackageName} ${BuglySymMappingPath} ${outSizeFilePath} ${BuglyId} ${BuglyKey}")process.waitFor() // 等待批处理文件执行完成int exitCode = process.exitValue() // 获取批处理文件的退出码System.out.println("批处理文件执行完成,退出码:" + exitCode)} catch (Exception e) {e.printStackTrace()System.println("mapping上传错误:\n" + e.printStackTrace())}
}/*** 在打完正式包后执行(assembleRelease)*/
project.tasks.whenTaskAdded { Task task ->if (task.name == 'assembleRelease') {task.doLast {//源mapping复制if (!copyMapping(MappingFileOrgPath, MappingFileRootPath, MappingPathName)) {return}//源apk复制if (!copyReleaseApk(ApkSourcePath, ApkTargetPath)) {return}//bugly mapping 上传buglyMappingUpload(JDK8Path, BuglyBat, BuglySymMappingPath, MappingFileRootPath,BuglySymJar, BuglyPackageName, BuglyId, BuglyKey)}}
}
bat脚本上传文件:
@echo off
CLS
chcp 65001
echo 参数jdk8路径:%1
echo 参数bugly混淆jar路径:%2
echo 参数bugly目标包名:%3
echo 参数bugly混淆文件父目录:%4
echo 参数bugly混淆文件全路径:%5
echo 参数bugly Id:%6
echo 参数bugly Key:%7
echo "------------------------java版本信息------------------------"
%1 -version
echo "------------------------begin------------------------"
echo "------------------------注意!注意!注意!mapping文件需要放在mappingfile目录下------------------------"
echo "------------------------版本号------------------------"
set /p codeInput="请输入app版本号(例如:1.0.0):"
echo 输入app版本号为:%codeInput%echo "------------------------开始上传------------------------"
%1 -jar %2 -appid %6 -appkey %7 -bundleid %3 -version %codeInput% -platform Android -inputSymbol %4 -inputMapping %5
pause
注意!!!
copyFileBat.gradle文件中:
//!!!!!!!!!!bugly 混淆上传相关,需要按照项目配置!!!!!!!!!!!!!!//jdk8运行环境
// JDK8Path = "D:\\software\\java\\jdk1.8\\bin\\java.exe"JDK8Path = "D:\\software\\Java\\jdk1.8.0_211\\bin\\java.exe"BuglySymJar = new File("${rootProject.rootDir}").parent + "/buglyqq-upload-symbol/buglyqq-upload-symbol.jar"BuglySymMappingPath = new File("${rootProject.rootDir}").parent + "/buglyqq-upload-symbol/mappingfile"BuglyPackageName = "com.example.androidjiaguchannel"BuglyKey = "d89bcdce-978b-4485-8ff4-a95b20372ed6"BuglyId = "877be21cd4"BuglyBat = new File("${rootProject.rootDir}").parent + "/buglyqq-upload-symbol/run.bat"
需要修改为你电脑,项目中的配置信息。上述涉及到的变量,都要重新定义,你的环境和我的不一样!
最后,附上相关工具类的网址:
bugly符号表:
https://bugly.qq.com/docs/release-notes/release-symboltool/?v=1.0.0
that’s all--------------------------------------------------------------------------------------