【问题标题】:how to restrict app to crash when build signed apk with minifyenabled true, means applied progurad?如何在使用 minifyenabled true 构建签名 apk 时限制应用程序崩溃,意味着应用程序?
【发布时间】:2016-11-18 15:10:40
【问题描述】:

在我的应用程序中,我启用了 progurad by minifyEnabled true 和我没有添加的 progurad 规则,我构建了已登录的 apk,然后它在启动时崩溃了,然后我才想到,我必须添加不需要缩小的类。然后我添加了诸如 -keep public class * extends android.app.Activity 之类的 progurd 规则,然后它也不起作用,我不知道为什么,因为它依赖于其他文件,如视图和 R 文件全部。所以我的疑问是,当我启用 progurad 时,我必须在 progurad 规则中添加我所有的类和库,否则它会缩小其他类并使其不起作用。所以 我怎样才能添加所有类而不会使应用程序崩溃在 apk 中构建签名 添加 proguard 规则后,应用程序也会崩溃在此处添加项目特定的 ProGuard 规则。

> -keep public class * extends android.app.Activity
> -keep public class * extends android.app.Application
> -keep public class * extends android.support.v7.app.AppCompatActivity
> -keep public class * extends android.content.BroadcastReceiver
> -keep public class * extends android.content.ContentProvider
> -keep public class * extends android.preference.Preference
> -keep public class android.os.AsyncTask
> -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); }
> -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); }
> # The official support library.
> -keep class android.support.v4.app.** { *; }
> -keep interface android.support.v4.app.** { *; }
> -keep class android.support.v7.app.** { *; }
> -keep interface android.support.v7.app.** { *; }
> -dontwarn android.support.**

如何使用 proguard release build 测试应用,是否有任何东西可以检查直接登录的 apk 或者我必须添加任何映射文件。我该如何测试直接发布 apk 或者如果该 apk 上传到 playstore 则它将基于映射文件工作

【问题讨论】:

标签: android mapping minify android-proguard


【解决方案1】:

我找到了一些解决方案,当我们直接安装 direct release apk with minified 然后代码将被缩小(混淆),所以应用程序不会知道这些代码是什么,所以它直接安装release apk会崩溃。

那么我的解决方案是在 将 apk 上传到 playstore 并将 build\outputs\mapping\release\mapping.txt 文件下的映射文件上传到 playstore 之后,从 playstore 下载应用程序那么它可能会起作用,在映射文件中,所有映射类都将在那里。所以它不会崩溃。将映射文件上传到 playstore https://support.google.com/googleplay/android-developer/answer/6295281?hl=en 我没有尝试过,但如果有人尝试然后让我知道编辑答案

【讨论】:

    【解决方案2】:

    这就是documentation 对 Proguard 的简单介绍。

    为了使您的 APK 文件尽可能小,您应该启用压缩功能以删除发布版本中未使用的代码和资源。本页介绍了如何执行此操作以及如何指定在构建期间保留或丢弃哪些代码和资源。

    ProGuard 提供代码收缩功能,它可以检测并删除打包应用程序中未使用的类、字段、方法和属性,包括包含的代码库中的那些(使其成为解决 64k 引用限制的宝贵工具)。 ProGuard 还优化了字节码,删除了未使用的代码指令,并用短名称混淆了剩余的类、字段和方法。

    我已经阅读了一篇关于它的好文章,所以你也应该从here阅读它

    如果您想知道如何实际使用它,那么this 答案可能对您有所帮助,如果您想了解何时使用它的建议,请参阅this 答案。

    【讨论】:

      【解决方案3】:

      在我的 proguard 文件中,所有内容都被注释掉了,它与 minifyEnabled true 一起工作得很好。你有没有尝试过这样的事情:

      proguard-rules.pro

      # Add project specific ProGuard rules here.
      # By default, the flags in this file are appended to flags specified
      # in C:\Users\<yourUser>\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:
      #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
      #   public *;
      #}
      

      我的 gradle 看起来像:

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

      编辑: 其他解决方案:https://stackoverflow.com/a/6492478

      【讨论】:

      • 您是否创建了任何已登录的 apk,例如发布版本,然后在该版本中它崩溃了
      • 发布构建后只需检查它是否工作(安装并检查)并检查调试 apk 和发布 apk 大小它会相差很大
      猜你喜欢
      • 2017-07-25
      • 1970-01-01
      • 1970-01-01
      • 2020-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多