Multidex Android Library模块

3
我在gradle工程中有一个库模块,其中包含连接的Android测试,但是测试APK引用了太多的方法,需要进行multidex处理或启用ProGuard。 是否有一种方式只为连接的Android测试应用程序启用multidex或ProGuard? 直接在库上启用ProGuard没有意义,但如果有一种方式只为androidTest配置启用它,那将非常好。
我们已经为应用程序模块启用了ProGuard,因此它可以依赖于这个库模块,并成功地构建应用程序的APK。
寻找此问题的解决方案很困难,因为我只能找到有关使用Multidex支持库的信息。我知道如何为典型应用程序启用它。

据我所知,Proguard仅用于代码混淆。这里与Proguard没有任何关系。 - Reaz Murshed
@ReazMurshed ProGuard可以混淆代码,还可以删除未使用的方法和类。 https://developer.android.com/studio/build/shrink-code.html - E.M.
1个回答

2
有没有一种方法只针对连接的Android测试应用程序启用multidex或ProGuard?
是的,您可以使用专用测试构建类型仅为测试启用ProGuard。默认值为debug。 在下面的示例中,专用测试构建类型命名为minifiedTest
android {

    defaultConfig {

        /* Your configs here. */

        // Specify the name of the dedicated test build type.
        testBuildType 'minifiedTest'
    }

    buildTypes {
        debug {
            // Your debug configurations.
        }

        release {
            // Your release configurations.
        }

        minifiedTest {
            // Use this to get the initial configurations from another build type.
            // Some of them will be overridden from the configurations specified in this build type.
            // You can avoid to use this or you can get them from your release build type for example.
            initWith(debug)
            // Enable proguard.
            minifyEnabled true
            // Specify the proguard file.
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

感谢您的建议。不幸的是,如果该库依赖其他库项目,这种方法会导致新版本的gradle(4.1+)出现一些问题。我的解决方案是为库模块启用multidex。 - E.M.
@E.M. 您是如何为库模块启用multidex的?当我在一个应用了'com.android.library'插件的模块中包含它时,我得到一个gradle错误,指出“找不到方法multidexEnabled()”。 - curioustechizen
1
@E.M. 没关系 - 这是一个打字错误。应该是 multiDexEnabled true 而不是 multidexEnabled。我从已经有这个错误的地方复制粘贴了。如果我因为打字错误而自己给自己惹麻烦的次数能换成一美元就好了 :P - curioustechizen

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接