ProGuard正在摆脱一些必需的代码?

时间:2015-11-23 21:42:22

标签: proguard android-proguard

您好我需要一些帮助来编写我的proguard-rules.pro文件。我对此知之甚少,到目前为止,在互联网的帮助下写了以下内容。

# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in C:\Users\Suresh Babu\AppData\Local\Android\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:
#
#}
-dontpreverify
-repackageclasses ''
-allowaccessmodification
-optimizations !code/simplification/arithmetic
-keepattributes *Annotation*
-dontwarn android.support.**
-dontwarn okio.**
-dontshrink
-dontoptimize

-keep public class * extends android.app.Activity
-keep public class * extends android.app.Application
-keep public class * extends android.app.Service
-keep public class * extends android.content.BroadcastReceiver
-keep public class * extends android.content.ContentProvider
-keep public abstract class *
-keep public interface *
-keep public abstract class edu.cias.rit.rocreadar.AbstractArchitectCamActivity
-keep public class edu.cias.rit.rocreadar.MainActivity

-keep public class * extends android.annotation.SuppressLint
-keep public class * extends android.content.ComponentCallbacks2
-keep public class * extends android.content.pm.ApplicationInfo
-keep public class * extends android.location.Location
-keep public class * extends android.location.LocationListener
-keep public class * extends android.media.AudioManager
-keep public class * extends android.opengl.GLES20
-keep public class * extends android.os.Build
-keep public class * extends android.os.Bundle
-keep public class * extends android.support.v7.app.AppCompatActivity
-keep public class * extends android.util.Log
-keep public class * extends android.webkit.WebView
-keep public class * extends android.widget.Toast

-keep public class * extends com.wikitude.architect.ArchitectView
-keep public class * extends com.wikitude.architect.ArchitectView.ArchitectUrlListener
-keep public class * extends com.wikitude.architect.ArchitectView.SensorAccuracyChangeListener
-keep public class * extends com.wikitude.architect.StartupConfiguration
-keep public class * extends com.wikitude.architect.StartupConfiguration.CameraPosition

-keep public class * extends org.json.JSONArray
-keep public class * extends org.json.JSONObject

-keep public class * extends java.io.IOException
-keep public class * extends java.util.HashMap


-keep public class * extends android.view.View {
    public <init>(android.content.Context);
    public <init>(android.content.Context, android.util.AttributeSet);
    public <init>(android.content.Context, android.util.AttributeSet, int);
    public void set*(...);
}

-keepclasseswithmembers class * {
    public <init>(android.content.Context, android.util.AttributeSet);
}

-keepclasseswithmembers class * {
    public <init>(android.content.Context, android.util.AttributeSet, int);
}

-keepclassmembers class * extends android.content.Context {
   public void *(android.view.View);
   public void *(android.view.MenuItem);
}

-keepclassmembers class * implements android.os.Parcelable {
    static ** CREATOR;
}

-keepclassmembers class **.R$* {
    public static <fields>;
}

-keepclassmembers class * {
    @android.webkit.JavascriptInterface <methods>;
}

-keepclasseswithmembers class * {
    native <methods>;
}

-keepclasseswithmembers class * {
    *** *Callback(...);
}

-keep class edu.cias.rit.AbstractArchitectCamActivity {
    void onCreate(***);
}

以下是堆栈跟踪。当我启动我的应用程序时,它崩溃了。它应该打开一个闪屏和主要活动等。

11-23 16:32:06.244     792-1266/? I/ActivityManager﹕ START u0 {act=android.intent.action.DELETE dat=package:edu.cias.rit.rocreadar flg=0x10800000 cmp=com.android.packageinstaller/.UninstallerActivity (has extras)} from uid 10022 on display 0
11-23 16:32:07.296      792-805/? I/ActivityManager﹕ Force stopping edu.cias.rit.rocreadar appid=10227 user=-1: uninstall pkg
11-23 16:32:07.646      792-823/? W/PackageManager﹕ Couldn't remove dex file for package:  at location /data/app/edu.cias.rit.rocreadar-1/base.apk, retcode=-1
11-23 16:32:07.748      792-823/? I/ActivityManager﹕ Force stopping edu.cias.rit.rocreadar appid=10227 user=0: pkg removed
11-23 16:32:07.866   3838-31907/? D/VoicemailCleanupService﹕ Cleaning up data for package: edu.cias.rit.rocreadar
11-23 16:32:08.018   1985-31925/? D/PackageBroadcastService﹕ Received broadcast action=android.intent.action.PACKAGE_REMOVED and uri=edu.cias.rit.rocreadar
11-23 16:32:08.018   1985-31925/? D/AccountUtils﹕ Clearing selected account for edu.cias.rit.rocreadar
11-23 16:32:08.029   1985-31925/? I/LocationSettingsChecker﹕ Removing dialog suppression flag for package edu.cias.rit.rocreadar
11-23 16:32:08.066   1985-31929/? D/gH_MetricsDatabase﹕ 0 metrics were deleted when clearing package edu.cias.rit.rocreadar.
11-23 16:32:08.073    1985-2711/? I/Icing﹕ doRemovePackageData edu.cias.rit.rocreadar
11-23 16:32:08.114   1667-31938/? I/UpdateIcingCorporaServi﹕ Updating corpora: APPS=edu.cias.rit.rocreadar, CONTACTS=MAYBE
11-23 16:32:19.291      792-823/? I/PackageManager.DexOptimizer﹕ Running dexopt (dex2oat) on: /data/app/vmdl413714101.tmp/base.apk pkg=edu.cias.rit.rocreadar isa=arm vmSafeMode=false debuggable=false oatDir = /data/app/vmdl413714101.tmp/oat
11-23 16:32:23.933      792-823/? W/PackageManager﹕ Unknown permission android.permission.ACCESS_GPS in package edu.cias.rit.rocreadar
11-23 16:32:24.070      792-823/? V/BackupManagerService﹕ restoreAtInstall pkg=edu.cias.rit.rocreadar token=e restoreSet=0
11-23 16:32:24.331   1985-32123/? D/PackageBroadcastService﹕ Received broadcast action=android.intent.action.PACKAGE_ADDED and uri=edu.cias.rit.rocreadar
11-23 16:32:24.349   1985-32126/? D/h﹕ Processing package: edu.cias.rit.rocreadar
11-23 16:32:24.380   1985-32126/? D/GassUtils﹕ Found app info for package edu.cias.rit.rocreadar:4. Hash: fb4fd99240fc2e2facc7748090f26288d88d4ddb3bd70b25bcf2d6cfc040f92a
11-23 16:32:24.380   1985-32126/? D/h﹕ Found info for package edu.cias.rit.rocreadar in db.
11-23 16:32:24.457   1667-32132/? I/UpdateIcingCorporaServi﹕ Updating corpora: APPS=edu.cias.rit.rocreadar, CONTACTS=MAYBE
11-23 16:32:26.100     792-1699/? I/ActivityManager﹕ START u0 {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 pkg=edu.cias.rit.rocreadar cmp=edu.cias.rit.rocreadar/.SplashScreen} from uid 10063 on display 0
11-23 16:32:26.153    792-13663/? I/ActivityManager﹕ Start proc 32141:edu.cias.rit.rocreadar/u0a228 for activity edu.cias.rit.rocreadar/.SplashScreen
11-23 16:32:26.479      792-810/? I/ActivityManager﹕ Displayed edu.cias.rit.rocreadar/.SplashScreen: +343ms
11-23 16:32:28.282    792-24283/? I/ActivityManager﹕ START u0 {cmp=edu.cias.rit.rocreadar/.MainActivity} from uid 10228 on display 0
11-23 16:32:28.486  32141-32141/? A/art﹕ art/runtime/java_vm_ext.cc:410]   native: #05 pc 0025237b  /data/app/edu.cias.rit.rocreadar-1/lib/arm/libarchitect.so (Java_com_wikitude_architect_Gameplay_createNative+74)
11-23 16:32:28.486  32141-32141/? A/art﹕ art/runtime/java_vm_ext.cc:410]   native: #06 pc 002c742f  /data/app/edu.cias.rit.rocreadar-1/oat/arm/base.odex (Java_com_wikitude_architect_Gameplay_createNative__+74)
11-23 16:32:28.487  32141-32141/? A/art﹕ art/runtime/java_vm_ext.cc:410]   at edu.cias.rit.rocreadar.AbstractArchitectCamActivity.onCreate(unavailable:-1)
11-23 16:32:28.487  32141-32141/? A/art﹕ art/runtime/java_vm_ext.cc:410]   at edu.cias.rit.rocreadar.MainActivity.onCreate(unavailable:-1)
11-23 16:32:28.614  32141-32141/? A/art﹕ art/runtime/runtime.cc:366]   native: #08 pc 0025237b  /data/app/edu.cias.rit.rocreadar-1/lib/arm/libarchitect.so (Java_com_wikitude_architect_Gameplay_createNative+74)
11-23 16:32:28.614  32141-32141/? A/art﹕ art/runtime/runtime.cc:366]   native: #09 pc 002c742f  /data/app/edu.cias.rit.rocreadar-1/oat/arm/base.odex (???)
11-23 16:32:28.614  32141-32141/? A/art﹕ art/runtime/runtime.cc:366]   at edu.cias.rit.rocreadar.AbstractArchitectCamActivity.onCreate(unavailable:-1)
11-23 16:32:28.614  32141-32141/? A/art﹕ art/runtime/runtime.cc:366]   at edu.cias.rit.rocreadar.MainActivity.onCreate(unavailable:-1)
11-23 16:32:28.614  32141-32141/? A/art﹕ art/runtime/runtime.cc:366]   at void edu.cias.rit.rocreadar.AbstractArchitectCamActivity.onCreate(android.os.Bundle) ((null):-1)
11-23 16:32:28.614  32141-32141/? A/art﹕ art/runtime/runtime.cc:366]   at void edu.cias.rit.rocreadar.MainActivity.onCreate(android.os.Bundle) ((null):-1)
11-23 16:32:28.615  32141-32141/? A/art﹕ art/runtime/runtime.cc:366]   native: #10 pc 0025237b  /data/app/edu.cias.rit.rocreadar-1/lib/arm/libarchitect.so (Java_com_wikitude_architect_Gameplay_createNative+74)
11-23 16:32:28.615  32141-32141/? A/art﹕ art/runtime/runtime.cc:366]   native: #11 pc 002c742f  /data/app/edu.cias.rit.rocreadar-1/oat/arm/base.odex (Java_com_wikitude_architect_Gameplay_createNative__+74)
11-23 16:32:28.615  32141-32141/? A/art﹕ art/runtime/runtime.cc:366]   at edu.cias.rit.rocreadar.AbstractArchitectCamActivity.onCreate(unavailable:-1)
11-23 16:32:28.615  32141-32141/? A/art﹕ art/runtime/runtime.cc:366]   at edu.cias.rit.rocreadar.MainActivity.onCreate(unavailable:-1)
11-23 16:32:28.718      194-194/? A/DEBUG﹕ pid: 32141, tid: 32141, name: s.rit.rocreadar  >>> edu.cias.rit.rocreadar <<<
11-23 16:32:28.746      194-194/? A/DEBUG﹕ #10 pc 0025237b  /data/app/edu.cias.rit.rocreadar-1/lib/arm/libarchitect.so (Java_com_wikitude_architect_Gameplay_createNative+74)
11-23 16:32:28.746      194-194/? A/DEBUG﹕ #11 pc 0070642f  /data/app/edu.cias.rit.rocreadar-1/oat/arm/base.odex (offset 0x43f000)
11-23 16:32:29.034    792-32172/? W/ActivityManager﹕ Force finishing activity edu.cias.rit.rocreadar/.MainActivity
11-23 16:32:29.046      792-954/? I/WindowState﹕ WIN DEATH: Window{a8674fb u0 edu.cias.rit.rocreadar/edu.cias.rit.rocreadar.SplashScreen}
11-23 16:32:29.075     792-1700/? I/ActivityManager﹕ Process edu.cias.rit.rocreadar (pid 32141) has died

1 个答案:

答案 0 :(得分:0)

使用outputs文件夹中的usage.txt文件和proguard-rules.pro文件中的-keep规则。这解决了我的问题。请遵循此github文档中的以下说明:https://github.com/futurice/android-best-practices

在此处添加部分文档,以防文档过时:

Proguard配置

ProGuard通常用于Android项目,以缩小和混淆打包的代码。

您是否使用ProGuard取决于您的项目配置。通常,您可以在构建版本apk时将gradle配置为使用ProGuard。

buildTypes {
    debug {
        minifyEnabled false
    }
    release {
        signingConfig signingConfigs.release
        minifyEnabled true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}

为了确定必须保留哪些代码以及可以丢弃或混淆哪些代码,您必须为代码指定一个或多个入口点。这些入口点通常是包含主要方法,applet,midlet,活动等的类.Android框架使用默认配置,可以从SDK_HOME / tools / proguard / proguard-android.txt中找到。使用上述配置,my-project / app / proguard-rules.pro中定义的自定义项目特定ProGuard规则将附加到默认配置。

与ProGuard相关的常见问题是,即使构建命令(即assembleRelease)成功而没有警告,应用程序也会在启动时遇到ClassNotFoundException或NoSuchFieldException或类似问题。这意味着两件事中的一件:

ProGuard删除了类,枚举,方法,字段或注释,考虑到它不是必需的。 ProGuard对类,枚举或字段名称进行了模糊处理(重命名),但它的原始名称间接使用,即通过Java反射。 检查app / build / outputs / proguard / release / usage.txt以查看相关对象是否已被删除。检查app / build / outputs / proguard / release / mapping.txt以查看相关对象是否已被混淆。

为了防止ProGuard剥离所需的类或类成员,请为ProGuard配置添加保留选项:

-keep class com.futurice.project.MyClass {*; } 要防止ProGuard混淆类或类成员,请添加一个keepnames:

-keepnames class com.futurice.project.MyClass {*; } 有关示例,请查看此模板的ProGuard配置。阅读Proguard的更多信息。

在项目的早期,进行发布构建以检查ProGuard规则是否正确地保留了重要的内容。此外,无论何时包含新库,都要进行发布构建并在设备上测试apk。不要等到你的应用程序最终版本为“1.0”才能进行发布版本,你可能会遇到几个令人不快的意外,并且很短的时间来修复它们。

提示。保存您发布给用户的每个版本的mapping.txt文件。通过为每个发布版本保留mapping.txt文件的副本,可以确保在用户遇到错误并提交混淆的堆栈跟踪时可以调试问题。

DexGuard。如果您需要用于优化和特别模糊发布代码的硬核工具,请考虑DexGuard,这是由构建ProGuard的同一团队制作的商业软件。它还可以轻松拆分Dex文件以解决65k方法限制。