Unity BuglySDK符号表接入

什么是符号表?

符号表是内存地址与函数名、文件名、行号的映射表。符号表元素如下所示:

为什么要配置符号表?

为了能快速并准确地定位用户APP发生Crash的代码位置,Bugly使用符号表对APP发生Crash的程序堆栈进行解析还原

第一步:

接入bugly sdk,具体参照bugly官网
符号表位置

image.png

第二步:

因为符号表需要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
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 193,968评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,682评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,254评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,074评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 60,964评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,055评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,484评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,170评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,433评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,512评论 2 308
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,296评论 1 325
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,184评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,545评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,880评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,150评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,437评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,630评论 2 335

推荐阅读更多精彩内容