如何在使用 minifyenabled true 构建签名的 apk 时限制应用程序崩溃,意味着应用程序?

how to restrict app to crash when build signed apk with minifyenabled true, means applied progurad?

在我的应用程序中,我通过 minifyEnabled true 启用了 progurad 和我没有添加的 progurad 规则,我构建了已签名的 apk 然后它在启动时崩溃了我以为那,我必须添加 classes ,它不必被缩小。然后我添加了像 -keep public class * extends android.app.Activity 这样的 progurd 规则,然后它也不起作用,我不知道为什么,因为它依赖于其他文件,如视图和 R 文件。所以我的疑问是,当我启用 progurad 时,我必须在 progurad 规则中添加我所有的 classes 和库,否则它会缩小其他 classes 并按原样制作 work.So 如何添加所有 classes 而不会在应用程序崩溃时构建签名 apk 添加混淆器规则后它也会使应用程序崩溃 在此处添加项目特定的混淆器规则.

> -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 版本构建 测试应用程序,是否有任何东西可以检查是否直接在 apk 中签名,或者我必须 添加任何映射文件 。我如何测试直接发布的 apk 或者如果 apk 上传到 playstore 那么它将基于映射文件工作

在我的 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'
        }
    }

编辑: 其他解决方案:

这就是 documentation 关于 Proguard 的简单说明。

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

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

我已经阅读了一篇不错的 post,因此您也应该阅读 here

如果您想知道它的实际使用情况,请this answer may be helpful to you and if you want suggestion about when to use it then see this回答。

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

所以我的解决方案是 释放 apk 上传到 playstore 并将 build\outputs\mapping\release\mapping.txt 文件下的映射文件上传到 playstore 所以从下载应用程序playstore 然后它可能会工作,在映射文件中所有映射 类 将是 there.so 那个时候它不会 crash.uploading 映射文件到 playstore https://support.google.com/googleplay/android-developer/answer/6295281?hl=en 我没试过,但是如果有人尝试,请告诉我编辑答案