Unity BuglySDK符号表接入
2022-01-14 本文已影响0人
忆中异
什么是符号表?
符号表是内存地址与函数名、文件名、行号的映射表。符号表元素如下所示:
为什么要配置符号表?
为了能快速并准确地定位用户APP发生Crash的代码位置,Bugly使用符号表对APP发生Crash的程序堆栈进行解析和还原。
第一步:
接入bugly sdk,具体参照bugly官网
符号表位置
第二步:
因为符号表需要so文件,在Editor文件夹下编写获取复制so文件脚本
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEditor.Build;
using UnityEditor.Callbacks;
using System.IO;
using UnityEngine;
using System;
public class XAndroidConfig : MonoBehaviour
{
[PostProcessBuild]
public static void OnPostprocessBuild(BuildTarget buildTarget, string path)
{
if (buildTarget == BuildTarget.Android)
{
string buglytoolPath = Path.Combine(System.Environment.CurrentDirectory, "Buglytools");
PostProcessAndroidBuild(buglytoolPath);
}
}
public static void PostProcessAndroidBuild(string pathToBuiltProject)
{
ScriptingImplementation backend =
PlayerSettings.GetScriptingBackend(UnityEditor.BuildTargetGroup
.Android); //as UnityEditor.ScriptingImplementation;
if (backend == UnityEditor.ScriptingImplementation.IL2CPP)
{
CopyAndroidIL2CPPSymbols(pathToBuiltProject, PlayerSettings.Android.targetArchitectures);
}
}
public static void CopyAndroidIL2CPPSymbols(string pathToBuiltProject, AndroidArchitecture targetDevice)
{
//string buildName = Path.GetFileNameWithoutExtension(pathToBuiltProject);
//FileInfo fileInfo = new FileInfo(pathToBuiltProject);
//string symbolsDir = fileInfo.Directory.Name;
//symbolsDir = symbolsDir + "/" + buildName + "_IL2CPPSymbols";
string symbolsDir = pathToBuiltProject;
CreateDir(symbolsDir);
CopyARMSymbols(symbolsDir);
CopyX86Symbols(symbolsDir);
CopyARM64ymbols(symbolsDir);
//switch (PlayerSettings.Android.targetArchitectures)
//{
// case AndroidArchitecture.All:
// {
// CopyARMSymbols(symbolsDir);
// CopyX86Symbols(symbolsDir);
// CopyARM64ymbols(symbolsDir);
// break;
// }
// case AndroidArchitecture.ARMv7:
// {
// CopyARMSymbols(symbolsDir);
// break;
// }
// case AndroidArchitecture.X86:
// {
// CopyX86Symbols(symbolsDir);
// break;
// }
// default:
// break;
//}
}
const string libpath = "/../Temp/StagingArea/symbols/";
const string libFilename = "libil2cpp.so.debug";
private static void CopyARMSymbols(string symbolsDir)
{
string sourcefileARM = Path.GetFullPath(Application.dataPath + libpath + "armeabi-v7a/" + libFilename);
CreateDir(symbolsDir + "/armeabi-v7a/");
try
{
File.Copy(sourcefileARM, symbolsDir + "/armeabi-v7a/libil2cpp.so.debug");
Debug.Log("sourcefileARM: " + sourcefileARM);
}
catch (Exception e)
{
Debug.LogError("CopyARMSymbolsError: " + e.Message);
}
}
private static void CopyX86Symbols(string symbolsDir)
{
string sourcefileX86 = Path.GetFullPath(Application.dataPath + libpath + "x86/" + libFilename);
try
{
File.Copy(sourcefileX86, symbolsDir + "/x86/libil2cpp.so.debug");
Debug.Log("sourcefileX86: " + sourcefileX86);
}
catch (Exception e)
{
Debug.LogError("CopyX86Symbols: " + e.Message);
}
}
private static void CopyARM64ymbols(string symbolsDir)
{
string sourcefileARM64 = Path.GetFullPath(Application.dataPath + libpath + "arm64-v8a/" + libFilename);
CreateDir(symbolsDir + "/arm64-v8a/");
try
{
File.Copy(sourcefileARM64, symbolsDir + "/arm64-v8a/libil2cpp.so.debug");
Debug.Log("sourcefileARM64: " + sourcefileARM64);
}
catch (Exception e)
{
Debug.LogError("CopyARM64ymbols: " + e.Message);
}
}
public static void CreateDir(string path)
{
if (Directory.Exists(path))
return;
Directory.CreateDirectory(path);
}
}
这样打包的时候就会将so文件复制出来 接下来写上传so文件脚本
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Threading;
using UnityEditor;
using UnityEngine;
using Debug = UnityEngine.Debug;
public class UpLoadBuglySo
{
private static string[] soFolders = { "arm64-v8a", "armeabi-v7a", "x86" };
//private static string soCmdTemplate =
// "java -jar buglySymbolAndroid.jar -i #SOPATH#/libil2cpp.so.debug -u -id #ID# -key #KEY# -package #PACKAGE# -version #VERSION#";
private static string soCmdTemplate =
"java -jar buglyqq-upload-symbol.jar -appid #ID# -appkey #KEY# -bundleid #PACKAGE# -version #VERSION# -platform Android -inputSymbol #SOPATH# ";
/// <summary>
/// 打完包后调用此方法 自动上传符号表文件
/// </summary>
[MenuItem("MHFrameWork/自动上传Android符号表")]
public static void UploadBuglyso()
{
DeleteSo();
CopyBuglySo();
if (EditCommand(out string arg))
{
Debug.Log("EditCommand: " + arg);
string output = "";
RunCmd(arg, BuglyToolPath());
//using (StringReader stringReader = new StringReader(output))
//{
// string tempOutput = string.Empty;
// string line;
// bool analyze = false;
// while ((line = stringReader.ReadLine()) != null)
// {
// if (analyze)
// {
// if (string.IsNullOrEmpty(line))
// {
// break;
// }
// line = line.Trim();
// tempOutput += line + "\n";
// }
// if (line.Contains("git log"))
// {
// analyze = true;
// }
// }
// output = tempOutput;
//}
//Debug.Log("output: " + output);
}
}
private static void CopyBuglySo()
{
FileTool.CopyFolder(DeBugSOPath(), BuglyToolPath());
FileTool.OpenFolder(BuglyToolPath());
}
private static void DeleteSo()
{
foreach (var folder in soFolders)
{
FileTool.DeleteFolder(BuglyToolPath() + "/" + folder);
}
}
private static bool EditCommand(out string arg)
{
bool exists = false;
StringBuilder sb = new StringBuilder();
exists = true;
sb.Append(soCmdTemplate);
sb.Replace("#SOPATH#", BuglyToolPath());
sb.Replace("#ID#", "ce631e5135"); //这里注意一下要改成自己的id
sb.Replace("#KEY#", "897f2327-6ec6-47c1-8b08-18c8eda04b28"); //自己的key
sb.Replace("#PACKAGE#", Application.identifier);
sb.Replace("#VERSION#", Application.version);
sb.Append(" exit").Replace("/",@"\");
arg = sb.ToString();
return exists;
}
public static string DeBugSOPath()
{
return Path.GetFullPath(Application.dataPath + "/../Temp/StagingArea/symbols");
}
public static string BuglyToolPath()
{
return Path.GetFullPath(Application.dataPath + "/../Buglytools");
}
public static void RunCmd(string arg, string workingDirectory, string exe = "cmd.exe")
{
//string cmd = "ping www.baidu.com";
//cmd = cmd.Trim().TrimEnd('&');// + "&exit";
//using (Process p = new Process())
//{
// p.StartInfo.FileName = "cmd.exe";
// p.StartInfo.UseShellExecute = false;
// p.StartInfo.RedirectStandardInput = true;
// p.StartInfo.RedirectStandardOutput = true;
// p.StartInfo.RedirectStandardError = true;
// p.StartInfo.CreateNoWindow = false;
// p.Start();
// p.StandardInput.WriteLine(cmd);
// p.StandardInput.AutoFlush = true;
// p.StartInfo.StandardOutputEncoding = Encoding.GetEncoding(936);
// p.StartInfo.StandardErrorEncoding = System.Text.Encoding.UTF8;
// //p.StartInfo.StandardErrorEncoding = Encoding.GetEncoding(936);
// StreamWriter streamWriter = p.StandardInput;
// streamWriter.AutoFlush = true;
// streamWriter.WriteLine(arg); //启动git
// streamWriter.Close();
// output = p.StandardOutput.ReadToEnd();
// p.WaitForExit();
// p.Close();
//}
ProcessStartInfo info = new ProcessStartInfo(exe);
info.Arguments = arg;
info.WorkingDirectory = workingDirectory;
info.CreateNoWindow = true;
info.ErrorDialog = true;
info.UseShellExecute = true;
if (info.UseShellExecute)
{
info.RedirectStandardOutput = false;
info.RedirectStandardError = false;
info.RedirectStandardInput = false;
}
else
{
info.RedirectStandardOutput = true;
info.RedirectStandardError = true;
info.RedirectStandardInput = true;
info.StandardOutputEncoding = System.Text.UTF8Encoding.UTF8;
info.StandardErrorEncoding = System.Text.UTF8Encoding.UTF8;
}
Debug.Log("RunCmd: " + info.Arguments);
Process process = Process.Start(info);
//process.StandardInput.WriteLine(arg);
//process.StandardInput.AutoFlush = true;
if (!info.UseShellExecute)
{
Debug.Log(process.StandardOutput);
Debug.Log(process.StandardError);
}
//process.BeginOutputReadLine();
//output = process.StandardOutput.ReadToEnd();
process.WaitForExit();
process.Close();
}
}
这是一个工具类 用于复制
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using Debug = UnityEngine.Debug;
public class FileTool
{
public static bool CheckFolder(string path)
{
if (Directory.Exists(path))
{
return true;
}
//UnityEditor.EditorUtility.DisplayDialog("Error", "Path does not exist \n\t" + path, "确认");
return false;
}
public static void OpenFolder(string path)
{
if (CheckFolder(path))
{
System.Diagnostics.Process.Start(path);
}
}
public static void CopyFolder(Dictionary<string, string> copyDic)
{
foreach (KeyValuePair<string, string> path in copyDic)
{
if (CheckFolder(path.Key))
{
CopyDir(path.Key, path.Value);
Debug.Log("Copy Success : \n\tFrom:" + path.Key + " \n\tTo:" + path.Value);
}
}
EditorUtility.ClearProgressBar();
}
public static void CopyFolder(string fromPath, string toPath)
{
CopyDir(fromPath, toPath);
Debug.Log("Copy Success : \n\tFrom:" + fromPath + " \n\tTo:" + toPath);
EditorUtility.ClearProgressBar();
}
public static void CreateFolder(string path)
{
if (Directory.Exists(path))
{
Directory.Delete(path, true);
}
Directory.CreateDirectory(path);
}
public static void DeleteFolder(string path)
{
if (Directory.Exists(path))
{
Directory.Delete(path, true);
}
}
private static void CopyDir(string origin, string target)
{
#if UNITY_IOS
if (!origin.EndsWith("/"))
{
origin += "/";
}
if (!target.EndsWith("/"))
{
target += "/";
}
#else
if (!origin.EndsWith("\\"))
{
origin += "\\";
}
if (!target.EndsWith("\\"))
{
target += "\\";
}
#endif
if (!Directory.Exists(target))
{
Directory.CreateDirectory(target);
}
DirectoryInfo info = new DirectoryInfo(origin);
FileInfo[] fileList = info.GetFiles();
DirectoryInfo[] dirList = info.GetDirectories();
float index = 0;
foreach (FileInfo fi in fileList)
{
if (fi.Extension == ".zip" || fi.Extension == ".meta" || fi.Extension == ".rar")
{
Debug.Log("dont copy :" + fi.FullName);
continue;
}
float progress = (index / (float)fileList.Length);
EditorUtility.DisplayProgressBar("Copy ", "Copying: " + Path.GetFileName(fi.FullName), progress);
File.Copy(fi.FullName, target + fi.Name, true);
index++;
}
foreach (DirectoryInfo di in dirList)
{
if (di.FullName.Contains(".svn"))
{
Debug.Log("Continue SVN " + di.FullName);
continue;
}
CopyDir(di.FullName, target + "\\" + di.Name);
}
}
}
第三步:
编译apk,这里要注意 要改成IL2CPP模式 不然复制不到so文件
image.png
第四步
第三步编辑完成后直接上传so文件
image.png