如何在 Android 测试中 运行 ProGuard?

How to run ProGuard on Android tests?

我们有一个带有一些库(和本机)依赖项的项目,如下所示:

Native SDK ← Library (Wrapper) ← Main Project

首先,这个结构不能改变,因为我们正在重复使用这些部分。我面临的问题是超过 65k 参考限制。当然,这有一个解决方法——启用 ProGuard。启用它后,项目将编译。

由于我们正在过渡到 Android 的默认测试框架,我们需要向测试配置添加更多依赖项,因此在依赖项中我们现在有:

compile 'com.google.android.gms:play-services-base:7.5.0'
compile 'com.google.android.gms:play-services-gcm:7.5.0'
compile 'com.google.android.gms:play-services-safetynet:7.5.0'
compile 'com.android.support:appcompat-v7:22.2.1'
compile 'com.android.support:recyclerview-v7:22.2.1'

compile files('libs/small-library1.jar')
compile files('libs/small-library2.jar')
compile files('libs/small-library3.jar')
compile files('libs/medium-library1.jar')
compile files('libs/medium-library2.jar')
compile files('libs/medium-library3.jar')
compile files('libs/huge-library1.jar')
compile files('libs/huge-library2.jar')

androidTestCompile 'com.android.support.test:runner:0.3'
androidTestCompile 'com.android.support.test:rules:0.3'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2'

我们使用的是 SDK (API) 22,所以几乎都是最新版本。问题是,我们的原生 SDK 在 protobuf 层有一堆代码,库包装器很大。对于所有其他 JAR,我们超过了 65k 的限制(但正如我所说,ProGuard 几乎没有解决这个问题)。 Multi-dex 是不可能的,因为它只适用于 Android 5.0+.

我们正在努力减少代码库,但即便如此,Android 测试仍因方法引用溢出问题(即未编译)而失败。

还有什么方法可以为测试启用混淆器吗?

一个选项是将使用 testBuildType 的测试版本更改为发布版本或启用 ProGuard 的其他版本变体。参见 Gradle User Guide. You can also try the solution here,但我自己还没有尝试过。