具有 3 个风格、3 个 buildTypes 和 2 个 applicationIdSuffixes 的 Android Wear 项目

Posted

技术标签:

【中文标题】具有 3 个风格、3 个 buildTypes 和 2 个 applicationIdSuffixes 的 Android Wear 项目【英文标题】:Android wear project with 3 flavors, 3 buildTypes and 2 applicationIdSuffixes 【发布时间】:2016-02-11 09:15:46 【问题描述】:

在尝试将 wearApp 风格和 buildTypes 与 applicationIdSuffixes 结合后构建项目时,我收到以下错误消息:

Error:Execution failed for task ':app:handleFirstCustomerTestMicroApk'.
> The main and the micro apps do not have the same package name.

来自我的 app/build.gradle:

buildTypes 
    debug 
        applicationIdSuffix '.debug'
        debuggable true
        embedMicroApp = true
    
    customerTest 
        applicationIdSuffix '.customertest'
        debuggable true
        embedMicroApp = true
    
    release 
        proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        minifyEnabled true
        embedMicroApp = true
    


productFlavors 
    first 
        applicationId 'com.my.app.first'
    
    second 
        applicationId 'com.my.app.second'
    
    third 
        applicationId 'com.my.app.third'
    


dependencies 
    firstWearApp project(path: ':wear', configuration: 'firstDebug')
    firstWearApp project(path: ':wear', configuration: 'firstCustomerTest')
    firstWearApp project(path: ':wear', configuration: 'firstRelease')

    secondWearApp project(path: ':wear', configuration: 'secondDebug')
    secondWearApp project(path: ':wear', configuration: 'secondCustomerTest')
    secondWearApp project(path: ':wear', configuration: 'secondRelease')

    thirdWearApp project(path: ':wear', configuration: 'thirdDebug')
    thirdWearApp project(path: ':wear', configuration: 'thirdCustomerTest')
    thirdWearApp project(path: ':wear', configuration: 'thirdRelease')

来自我的 wear/build.gradle:

buildTypes 
    debug 
        applicationIdSuffix '.debug'
        minifyEnabled false
    
    customerTest 
        applicationIdSuffix '.customertest'
        minifyEnabled false
    
    release 
        minifyEnabled true
        proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
    

productFlavors 
    first 
        applicationId 'com.my.app.first'
    
    second 
        applicationId 'com.my.app.second'
    
    third 
        applicationId 'com.my.app.third'
    


android 
    publishNonDefault true

我从这些知道<buildType>WearApp 是可能的,但我真正需要的是<flavor><BuildType>WearApp(现在似乎不可能):

Android wear application packaging fails with flavours Wear App and with custom build type with applicationIdSuffix https://code.google.com/p/android/issues/detail?id=74658

如果我删除了 applicationIdSuffixes,则保留所有上述 9 个 wearApp 依赖项的工作,但是无论我在 Android Studio 中选择什么 buildType,它仍然会为每个 buildType 构建一个 wear apk - 我真的需要 applicationIdSuffixes。

有人有解决方法吗?到今天为止,每次我需要更改 buildType 和/或风味时,我都会手动添加和删除 wearApp 依赖项,从长远来看,这并不是我喜欢的解决方案。

编辑:一开始我没有注意到这一点,但由于某种原因,变体 firstDebug、secondDebug 和 thirdDebug 与 build.gradle 中的所有 9 个 wearApp 依赖项构建得很好。但是,对于 firstCustomerTest、firstRelease、secondCustomerTest、secondRelease、thirdCustomerTest 和 thirdRelease,错误消息仍然相同。所有变体每次都编译 9 个 wearApp,将其减少到 1 个会很巧妙。

【问题讨论】:

与您的问题无关:在 buildTypes-customerTest 中定义“initwith debug”,因此您的自定义构建类型将自动使用“调试”中的所有设置。现在你只需要定义'applicationIdSuffix':与'debug'相比的唯一不同请参阅dev.android.com以获取参考:developer.android.com/studio/build/… ...我知道它在你的情况下并不是非常有用,但我仍然会想指出这种可能性。 【参考方案1】:

根据This Post

试试这个

configurations 
    firstDebugWearApp
    firstCustomerTestWearApp
    firstReleaseWearApp
    secondDebugWearApp
 ...//  And all the others

  dependencies 
        firstDebugWearApp project(path: ':wear', configuration: 'firstDebug')
        firstCustomerTestWearApp project(path: ':wear', configuration: 'firstCustomerTest')
        firstReleaseWearApp project(path: ':wear', configuration: 'firstRelease')

        secondDebugWearApp project(path: ':wear', configuration: 'secondDebug')
        secondCustomerTestWearApp project(path: ':wear', configuration: 'secondCustomerTest')
        secondReleaseWearApp project(path: ':wear', configuration: 'secondRelease')

        thirdDebugWearApp project(path: ':wear', configuration: 'thirdDebug')
        thirdCustomerTestWearApp project(path: ':wear', configuration: 'thirdCustomerTest')
        thirdReleaseWearApp project(path: ':wear', configuration: 'thirdRelease')
    

【讨论】:

谢谢,“配置”是缺少的链接!它仍然在每次构建时构建所有这些,但重要的是它正在工作。

以上是关于具有 3 个风格、3 个 buildTypes 和 2 个 applicationIdSuffixes 的 Android Wear 项目的主要内容,如果未能解决你的问题,请参考以下文章

Gradle 总是从最后一种风格中的 buildType 中获取值

定义特定风味的buildconfigfield和buildType

Android开发:《Gradle Recipes for Android》阅读笔记(翻译)3.1——使用Build Types

Gradle:如何为需要相同 Activity 但具有不同意图过滤器的不同 buildType 合并 Android 清单文件

软件工程——团队作业3

具有 3 个标题和 colspan 的 Html 表