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

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

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

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

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

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

【问题讨论】:

标签: android proguard android-library android-multidex


【解决方案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
猜你喜欢
  • 2015-02-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多