使用applicationIdSuffix来佩戴应用程序和自定义构建类型

时间:2014-08-05 14:16:15

标签: android gradle android-gradle wear-os

我有一个应用,我想添加Android Wear应用扩展程序。主应用程序有三种构建类型(调试,测试和发布)。 Beta版本具有applicationIdSuffix,允许我在同一设备上并行安装Play-store版本和当前开发版本。这一切都很好,直到我添加了磨损应用程序。

主app 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 个答案:

答案 0 :(得分:18)

按照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')

答案 1 :(得分:2)

你不能做你想做的事;模块的构建变体不会在构建时传播到依赖模块的构建。这会在https://code.google.com/p/android/issues/detail?id=52962

中进行跟踪

http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Library-Publication中所述,有一个设施可以使一个模块依赖于另一个模块的特定变体,但我不认为这种机制可以扩展到磨损的差别包装应用

答案 2 :(得分: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
    }
}

穿应用的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),因为磨损应用只构建一次,因此第二种构建类型失败
  2. gradle check由于原因1
  3. 而失败
  4. 仍然使用构建类型release构建了Wear应用程序,但是包名称只是根据主应用程序的应用程序ID后缀进行了更改

答案 3 :(得分:0)

别担心,你可以做你想做的事。我刚刚为我工作的企业应用程序做过。

关键是不要使用wearApp项目(':wear'),因为只有当你的磨损应用程序中的applicationId与主应用程序相同时才能使用。让我们面对现实,在现实生活中,这种情况经常发生吗?如果它发生了,你可能没有尽可能地使用Gradle。

您想要在google wear docs中按照手动打包的说明进行操作 https://developer.android.com/training/wearables/apps/packaging.html#PackageManually

不幸的是,这将要求您使用与当时正在制作的特定构建变体相同的applicationId构建您的磨损应用程序,但它确实允许您在具有多个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

相关问题