穿戴应用和使用自定义构建类型以及applicationIdSuffix

23

我有一个应用程序,想添加Android Wear应用程序扩展。主应用程序有三种构建类型(debug、beta和release)。Beta版本具有一个applicationIdSuffix,可以让我在同一设备上并行安装play-store版本和当前开发版本。这一切都很好,直到我添加了wear应用。

主应用程序的build.gradle如下:

apply plugin: 'com.android.application'

android {
    ...
    defaultConfig {
        ...
        applicationId "com.example.mainApp"
        ...
    }
    buildTypes {
        debug {
            applicationIdSuffix '.debug'                
        }
        beta {
            applicationIdSuffix '.beta'
        }
        release {
        }
    }
}

dependencies {
    ...
    wearApp project(':wear')
}

Wear-App具有相同的构建类型和相同的applicationIdSuffix值。但是,当我构建beta应用程序(通过调用gradle assembleBeta)时,构建过程会构建:wear:assembleRelease而不是:wear:assembleBeta,这就是为什么我在构建过程中会得到以下错误消息:

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:handleBetaMicroApk'.
> The main and the micro apps do not have the same package name.

当使用构建类型 beta 打包主应用程序时,我该如何告诉构建过程构建正确的构建类型?

4个回答

19

根据Scott Barta发布的链接(http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Library-Publication),我得出如下结论:

在wear应用程序的build.gradle中添加publishNonDefault true(以发布所有变体):

android {
    publishNonDefault true
}

在主应用程序的 build.gradle 文件中,
替换

wearApp project(':wear')

通过

debugWearApp project(path:':wear', configuration: 'debug')
releaseWearApp project(path:':wear', configuration: 'release')

5
如何将这个与口味结合起来? - Tormod
这似乎无法与Android Gradle插件3.0.0一起使用。依赖机制已更改,而且变体也不太适用于此。 - pablisco

2

我尝试了您第二个链接中描述的机制,但它不适用于仓库应用程序。 - Tom
感谢分享跟踪此问题的链接。 - qix

1

别担心,你可以做你想做的事情。我刚刚在我工作的企业应用程序上完成了这个任务。

关键是不要使用wearApp project(':wear'),因为只有当你的wear应用程序与主应用程序具有相同的applicationId时才能起作用。而且让我们面对现实吧,在现实生活中这种情况有多少次发生?如果发生了,你可能没有充分利用Gradle。

你需要按照Google Wear文档中手动打包的说明进行操作 https://developer.android.com/training/wearables/apps/packaging.html#PackageManually

不幸的是,这将要求你使用与你正在制作的特定构建变体相同的applicationId来构建你的wear应用程序,但它确实允许你成功地将wear应用程序打包到具有多个applicationId的应用程序中。

另外,我做的一个小技巧是不把wear apk放在/res/raw中,而是放在/assets中,这样你就不必处理Andriod Studio压缩apk的问题。

希望这可以帮到你!我为了找到解决方案而疯狂了几天。而且唯一的教程是用法语写的,我不得不翻译网站才能阅读它!https://translate.google.com/translate?hl=en&sl=auto&tl=en&u=http%3A%2F%2Fblog.octo.com%2Fpackager-une-application-android-wear-dans-la-vraie-vie%2F


1

更新 现在官方已经支持构建变体(请查看Cyril Leroux的答案)。因此,本回答已过时。


我找到了一个非常(非常)丑陋的解决方案,它有一些缺点,但现在可以使用,直到支持构建变体的手表应用程序出现为止。
我在rootProject中设置了一个全局变量,其中包含当前构建的主应用程序的applicationIdSuffix。
在主应用程序的build.gradle中,我添加了以下内容:
// Set a global variable, depending on the currently built build-type.
// This allows us to set the applicationIdSuffix of the wear app depending on
// the build-type of the main app.
android.applicationVariants.all { variant ->
    def task = variant.checkManifest
    def suffix = variant.buildType.applicationIdSuffix
    task.doLast {
        rootProject.ext.currentApplicationIdSuffix = suffix
    }
}

wear应用的build.gradle中,我添加了以下代码片段:
android.applicationVariants.all { variant ->
    def task = variant.generateBuildConfig
    task.dependsOn(propagateApplicationIdSuffix)
}


task propagateApplicationIdSuffix << {
    project.android.buildTypes.all { type ->
        if (rootProject.hasProperty('currentApplicationIdSuffix')) {
            type.applicationIdSuffix = rootProject.ext.currentApplicationIdSuffix
        }
    }
}

这有几个缺点:
  1. 无法构建多个变体(即gradle assembleBeta assembleRelease),因为wear应用程序仅构建一次,因此第二个构建类型将失败。
  2. gradle check失败,原因是第1点。
  3. 尽管包名称根据主应用程序的应用ID后缀进行更改,但wear应用程序仍使用构建类型release构建。

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