在 Android Studio 中使用 ProGuard 时无法构建应用

App cannot build when using ProGuard in Android Studio

我正在使用最新版本的 Android Studio,并且我在 [=12] 中 混淆了 我的项目 ProGuard =].

Build.gradle的内容例如:

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

但是在启动构建项目时,出现了这样的错误 see error

警告图片:see warning

我该如何解决这个问题?

简答

如果您完全确定您的应用在其他构建类型(调试)中按预期工作,则通过将以下内容添加到您的 proguard-rules.pro 文件来抑制警告

-dontwarn com.github.siyamed.shapeimageview.**


长答案

警告说找不到引用的 class org.kxml2.io.KXmlParserKXmlParser 仅用于 SvgToPath.java,如下图所示:

import org.kxml2.io.KXmlParser;
...
public class SvgToPath {
    ...
    private static PathInfo parse(InputStream in, boolean ignoreDefs, float dpi) {
            try {
                XmlPullParser xr = new KXmlParser();
                ...
            } catch (Exception e) {
                Log.w(TAG, "Parse error: " + e);
                throw new RuntimeException(e);
            }
    }
    ...
}

我认为这会导致失败的两个潜在原因之一:

  1. 您使用的库不包含 org.kxml2.io.KXmlParser,因此您必须自己包含它
  2. Proguard 通过混淆代码破坏了 com.github.siyamed.shapeimagevieworg.kxml2.io.KXmlParser

如果您认为 Proguard 破坏了您的构建,请考虑以下内容

At the end of the day the libraries that you and most people use are nearly always open source. Obfuscating open source code provides no benefit as any potential attacker could just look up the source code online.

您可以通过将以下内容添加到您的 proguard-rules.pro 文件来保留 classes(阻止它们被 Proguard 混淆):

-keep class com.github.siyamed.shapeimageview.** { *; }

-keep interface com.github.siyamed.shapeimageview.** { *; }

-keep class org.kxml2.io.KXmlParser.** { *; }

-keep interface org.kxml2.io.KXmlParser.** { *; }