【问题标题】:Why is release build throwing error, but not debug when buildTypes are identical? (Android gradle build types)为什么发布构建抛出错误,但当 buildTypes 相同时不调试? (Android gradle 构建类型)
【发布时间】:2019-03-20 15:37:45
【问题描述】:

我试图弄清楚为什么我无法使用 Android Studio 正确安装发布版本。这是我的 buildTypes 块:

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

这些是我的产品口味:

flavorDimensions "version"
productFlavors {
    free {
        applicationId "com.example.app.free"
        dimension "version"
        signingConfig signingConfigs.config
    }
    paid {
        applicationId "com.example.app.paid"
        dimension "version"
        signingConfig signingConfigs.config
    }
}

当我安装调试付费或免费版本时,一切都很好。一点都没有错。当我尝试安装发行版(付费或免费)时,我得到:

'Execution failed for task ':app:transformDexArchiveWithDexMergerForFreeRelease'. com.android.build.api.transform.TransformException: java.lang.RuntimeException: java.lang.RuntimeException: com.android.builder.dexing.DexArchiveMergerException: Error while merging dex archives: ... Learn how to resolve the issue at https://developer.android.com/studio/build/dependencies#duplicate_classes. Program type already present: com.google.android.youtube.player.YouTubeApiServiceUtil'

我的问题是:为什么重复类异常仅在发布期间发生,而我在发布和调试 buildTypes 之间没有定义任何不同?

编辑我已经通过删除 gradle 中的 YouTubePlayer 库依赖项解决了这个问题(b/c 显然我对 google YT 服务的实现导致创建了一个内部库,因此取决于进口的一个是多余的?)。这仍然使我的问题有效。为什么在没有任何不同声明的情况下调试有效,但发布却无效?

【问题讨论】:

  • 我也面临同样的问题,有什么好运的解决方案吗?
  • 我想这与 gradle 中的依赖关系以及不同版本生成类文件的方式有关,请参阅@StenSoft 的答案。这很可能是调试分配了不同的类名,但发布不是。

标签: android android-gradle-plugin build.gradle android-build-flavors android-build-type


【解决方案1】:

当我在其assemble 上添加另一个(间接)依赖项时,我在与另一个模块链接时遇到了同样的问题(它是 Protobuf 模块,需要在应用程序模块生成其 JSON 之前从 .proto 文件生成 Java 文件模型)。 assembleRelease 似乎以某种方式优化代码,它可能会根据调用它的位置生成稍微不同的 class 文件,然后 Dex 合并无法决定使用哪个 class 文件。 assembleDebug 始终生成可以合并的相同(未优化)代码。

(对于任何与 Protobuf 有相同问题的人,解决方案是依赖 :protobuf:GenerateProto 而不是 :protobuf:assemble。)

【讨论】:

    猜你喜欢
    • 2017-01-30
    • 2015-03-07
    • 1970-01-01
    • 1970-01-01
    • 2020-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多