使用ProGuard

时间:2015-09-24 21:01:17

标签: android debugging apk proguard

将已编译的APK上传到Google Developer Play Console后,您可以在应用崩溃时看到日志消息。此外,在调试模式下测试应用程序时,如果应用程序崩溃,您可以看到日志消息。

有没有办法在将应用程序作为已编译的APK进行测试时查看日志消息?请注意我使用的是ProGuard。

我的意思是,当在设备上安装已编译的APK时,能够以某种方式查看来自应用程序崩溃的日志消息。如果问题仍然不明确,请在评论中告诉我。

2 个答案:

答案 0 :(得分:0)

这是一些代码示例,当您的应用程序使用proguard加密时,它具有清晰的堆栈:

    private static void trace(String txt) {
    if (ENABLE_TRACE) {
        Trace.trace(txt);
        Trace.trace("\n");
        System.out.println(txt);
    }
}

public static String retrieveProguardStackInternal(String stack, File proguardFile,String sdkPath,String javaPath) {
    String ret = stack;
    trace("trying to uncrypt stack");
    trace("==== raw stack ====");
    trace(stack);
    if ((stack == null) || (stack.length() == 0) || (proguardFile == null) || (!proguardFile.exists()) || (sdkPath == null) || (javaPath == null)) {
        if ((stack == null) || (stack.length() == 0)) {
            trace("empty stack => abort");
        }
        if (!proguardFile.exists()) {
            trace("proguard file does not exists "+proguardFile.getAbsolutePath());
        }
        if (sdkPath == null) {
            trace("sdk path not defined");
        }
        if (javaPath == null) {
            trace("java path not defined");
        }
        return ret;
    }       
    trace("sdk : "+sdkPath);
    trace("jdk : "+javaPath);
    StringBuilder sb = new StringBuilder();
    sb.append(sdkPath);
    sb.append(File.separatorChar);
    sb.append("tools");
    sb.append(File.separatorChar);
    sb.append("proguard");
    sb.append(File.separatorChar);
    sb.append("lib");
    sb.append(File.separatorChar);
    sb.append("retrace.jar");
    String retraceJarPath = sb.toString();
    File retraceJar = new File(retraceJarPath);
    if (!retraceJar.exists()) {
        trace("proguard lib not found at "+retraceJarPath);
        return ret;
    }   
    sb = new StringBuilder();
    sb.append(javaPath);
    sb.append(File.separatorChar);
    sb.append("java");
    String javaexePath = sb.toString();
    sb = new StringBuilder();
    try {
        // using the Runtime exec method:
        Process p = Runtime.getRuntime().exec(new String[] {javaexePath,"-jar",retraceJarPath,proguardFile.getAbsolutePath()});
        OutputStream out = p.getOutputStream();
        out.write(stack.getBytes());
        out.close();
        BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
        BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
        // read the output from the command
        String line = null;
        String lineSeparator = System.getProperty("line.separator");
        while ((line = stdInput.readLine()) != null) {
            sb.append(line);
            sb.append(lineSeparator);
        }
        // read any errors from the attempted command
        StringBuilder error = new StringBuilder();
        while ((line = stdError.readLine()) != null) {
            error.append(line);
        }
        trace("error trace :"+error);
        p.waitFor(); 
    } catch (Throwable e) {
        trace("exception happened - here's what I know: ");
        trace(e.toString());
    }
    ret = sb.toString();
    trace("=== final stack ===");
    trace(ret);
    trace("===================");
    return ret;
}

您需要在编译期间提供原始堆栈+ proguard生成的映射文件

答案 1 :(得分:0)

只需将debuggable true放入我的应用的build.gradle,我就可以在手机连接到Android Studio时查看已编译的APK应用的堆栈跟踪(不包含行号)。

我的build.gradle文件在添加以下内容后看起来有点像这样:

...

buildTypes {
    release {
        debuggable true // This allows stack traces to appear in LogCat
        minifyEnabled true // This obfuscates the code
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}

...

此外,this answer解释了如何使用ProGuard在堆栈跟踪中显示行号。