具有3个口味、3个构建类型和2个应用程序ID后缀的Android Wear项目

30

当我尝试将wearApp flavors和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(目前似乎不可能):

如果我去掉applicationIdSuffixes ,则保留所有9个wearApp依赖项似乎可以工作,但无论我在Android Studio中选择哪种构建类型,它仍会构建一个wear apk - 而我确实需要applicationIdSuffixes

有没有解决此问题的方法? 我现在需要手动添加和删除wearApp依赖项,每次需要更改我的构建类型和/或味道,这并不是一种长期解决方案。

编辑:起初我没有注意到,但由于某种原因变体firstDebug、secondDebug和thirdDebug使用build.gradle中的所有9个wearApp依赖项构建成功。然而,对于firstCustomerTest、firstRelease、secondCustomerTest、secondRelease、thirdCustomerTest和thirdRelease,错误消息仍然相同。所有变体每次都编译9个wearApps,将其减少到1会很方便。


与您的问题无关:在buildTypes-customerTest中定义“initwith debug”,这样您的自定义构建类型将自动使用“debug”的所有设置。现在,您只需要定义'applicationIdSuffix':与'debug'相比唯一不同的是 有关详细信息,请参见dev.android.com:https://developer.android.com/studio/build/build-variants.html#build-types ...我知道它在您的情况下并不是非常有用,但我仍然想指出这种可能性。 - P Kuijpers
1个回答

8

根据这篇文章所述:

请尝试以下方法

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')
    }

谢谢,"配置项" 是缺失的关键!虽然每次构建仍会构建所有配置项,但重要的是它正在工作。 - Tormod

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