【问题标题】:Android ACRA and ProGuardAndroid ACRA 和 ProGuard
【发布时间】:2014-11-04 17:36:39
【问题描述】:
在 ACRA 网站上有一个关于在 Eclipse 中使用 ACRA 和 ProGuard 的指南:https://github.com/ACRA/acra/wiki/ProGuard
就我而言,我使用的是 Android Studio,但我不是 Java 专家,但是我需要将 ACRA 支持添加到我的应用程序中。我刚刚编辑了我的项目,但我不明白如何在 Android Studio 中设置 ProGuard 以与 ACRA 一起使用,因此在我的发布应用程序中 ACRA 不起作用。
你能解释一下将 Eclipse 步骤“转换”成 Android Studio 步骤吗?
【问题讨论】:
标签:
java
android
eclipse
android-studio
proguard
【解决方案1】:
您的 Android Studio 项目应该有一个 proguard-rules.txt 文件。您需要在此文件中添加一些指令来告诉 ProGuard 要保留什么,利用 ACRA 指令中的 ProGuard 指令。
大多数情况下,您会将 ACRA 的 proguard-project.txt 的内容复制到您项目的 proguard-rules.txt 文件中。
您可能需要添加一些指令,ACRA 指令预计会在 Eclipse 的 ProGuard 配置基础文件 proguard-android-optimize.txt 中,其中该文件与 Android Studio 的文件 android-studio\sdk\tools\proguard\proguard-android-optimize.txt 不同
来自proguard-android-optimize.txt 的示例指令:
-keepclasseswithmembernames class * {
native <methods>;
}
这告诉 ProGuard 保留每个具有本机方法的类。
【解决方案2】:
您是否有现有的 eclipse 项目并希望将其导入 Android Studio?
如果不是这里是解决方案:
您的 build.gradle(位于“app”文件夹中)文件应如下所示:
apply plugin: 'com.android.application'
android {
compileSdkVersion 20
buildToolsVersion '20.0.0'
defaultConfig {
applicationId "com.yourapp"
minSdkVersion 14
targetSdkVersion 20
versionCode 1
versionName "1.0"
}
buildTypes {
release {
runProguard true//make this true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile project(':library')
compile 'ch.acra:acra:4.5.0'//add this line here
}
还有你的 proguard-rules.pro:
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in C:\Android\AndroidSDKBundle2014-03-21x64\sdk/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the proguardFiles
# directive in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html
# Add any project specific keep options here:
# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
#class:
-keepclassmembers class fqcn.of.javascript.interface.for.webview {
public *;
}
#ACRA specifics
# Restore some Source file names and restore approximate line numbers in the stack traces,
# otherwise the stack traces are pretty useless
-keepattributes SourceFile,LineNumberTable
# ACRA needs "annotations" so add this...
# Note: This may already be defined in the default "proguard-android-optimize.txt"
# file in the SDK. If it is, then you don't need to duplicate it. See your
# "project.properties" file to get the path to the default "proguard-android-optimize.txt".
-keepattributes *Annotation*
# keep this class so that logging will show 'ACRA' and not a obfuscated name like 'a'.
# Note: if you are removing log messages elsewhere in this file then this isn't necessary
-keep class org.acra.ACRA {
*;
}
# keep this around for some enums that ACRA needs
-keep class org.acra.ReportingInteractionMode {
*;
}
-keepnames class org.acra.sender.HttpSender$** {
*;
}
-keepnames class org.acra.ReportField {
*;
}
# keep this otherwise it is removed by ProGuard
-keep public class org.acra.ErrorReporter {
public void addCustomData(java.lang.String,java.lang.String);
public void putCustomData(java.lang.String,java.lang.String);
public void removeCustomData(java.lang.String);
}
# keep this otherwise it is removed by ProGuard
-keep public class org.acra.ErrorReporter {
public void handleSilentException(java.lang.Throwable);
}
其余的就像日食。