Multidex Android 库模块

Posted

技术标签:

【中文标题】Multidex Android 库模块【英文标题】:Multidex Android Library Module 【发布时间】:2018-03-25 04:07:21 【问题描述】:

我的 gradle 项目中有一个库模块,它连接了 android 测试,但测试 APK 引用了太多方法,需要多索引或启用 ProGuard。

有没有办法只为连接的 Android 测试应用程序启用 multidex 或 ProGuard?

直接在库上启用 ProGuard 没有任何意义,但如果有办法只为 androidTest 配置启用它,那会很好用。

我们确实为应用程序模块启用了 ProGuard,因此它可以安全地依赖此库模块并成功构建应用程序的 APK。

很难找到这个问题的解决方案,因为我只能找到有关使用 Multidex 支持库的信息。我了解如何为典型应用启用它。

【问题讨论】:

据我所知,Proguard 仅用于代码混淆。这里与 Proguard 的多索引无关。 @ReazMurshed ProGuard 可以混淆代码,但它也可以删除未使用的方法和类。 developer.android.com/studio/build/shrink-code.html 【参考方案1】:

有没有办法只为连接的 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.你是如何为 library 模块启用 multidex 的?当我将它包含在具有 apply plugin: 'com.android.library' 的模块中时,我收到一个 gradle 错误说 Could not find method multidexEnabled() @E.M.没关系 - 这是一个错字。它应该是 multiDexEnabled true - 而不是 multidexEnabled。我从已经有错字的地方复制粘贴。如果每次我因为打字错误而射中自己的脚时只有一美元:P

以上是关于Multidex Android 库模块的主要内容,如果未能解决你的问题,请参考以下文章

如何使用新的 Android Multidex 支持库启用多索引

如何使用新的 Android Multidex 支持库启用多索引

android 引入其他类库导致的各种坑

带有 multidex 的 Xamarin Android - 调试模式下的错误

Android 65536启用 multidex

分析google的multidex库