【问题标题】:Android wear application packaging fails with flavoursAndroid Wear 应用程序打包失败
【发布时间】:2014-12-09 19:42:15
【问题描述】:

我有一个包含穿戴应用程序的应用程序。在使用真实设备进行调试测试时一切正常。我还可以创建将磨损 apk 打包在其中的发布 apk。但前提是我的应用程序只有一种风格。

我想维护具有不同 applicationId 的应用程序的两个版本,但是尽管编译没有错误,但在这种情况下,两个发布 apk(每种风格一个)不包含相应的磨损 apk。

这是移动应用 build.gradle 的相关部分:

    productFlavors {
    Trial {
        applicationId "com.example.myapp.trial"
        versionName "3.0.1"
        versionCode 301
    }
    Full {
        applicationId "com.example.myapp"
        versionName "3.0.1"
        versionCode 301
    }
}

}

dependencies {
    compile 'com.google.android.gms:play-services:6.1.+@aar'
    wearApp project(':myWearApp')
}

这是对应的wear app build.gradle:

productFlavors {
    Trial {
        applicationId "com.example.myapp.trial"
        versionName "3.0.1"
        versionCode 301
    }
    Full {
        applicationId "com.example.myapp"
        versionName "3.0.1"
        versionCode 301
    }
}

}

dependencies {
    compile 'com.google.android.support:wearable:1.0.0'
    compile 'com.google.android.gms:play-services-wearable:6.1.71'
}

欢迎任何帮助。谢谢。

【问题讨论】:

    标签: android android-gradle-plugin wear-os


    【解决方案1】:

    感谢 Scott 给我的线索,这是完整的解决方案:

    1.) 味道必须小写

    2.) 依赖配置必须包含flavorRelease

    3.) 在 Wear app build gradle 中,在 android{} 下,我们必须包含 publishNonDefault true

    所以对于移动应用 build.gradle:

    android {
    
    ......
    
    productFlavors {
        trial {
            applicationId "com.sample.myapp.trial"
            versionName "3.0.1"
            versionCode 301
        }
        full {
            applicationId "com.sample.myapp"
            versionName "3.0.1"
            versionCode 301
        }
     }
    }
    
    dependencies {
        trialWearApp project(path: ':myWearApp', configuration: 'trialRelease')
        fullWearApp project(path: ':myWearApp', configuration: 'fullRelease')
    }
    

    对于穿戴应用程序 build.gradle:

    android {
    
      publishNonDefault true
    ......
    
    productFlavors {
        trial {
            applicationId "com.sample.myapp.trial"
            versionName "3.0.1"
            versionCode 301
        }
        full {
            applicationId "com.sample.myapp"
            versionName "3.0.1"
            versionCode 301
        }
     }
    }
    

    【讨论】:

    • 我之前根本没有编译过。当我这样做时,它现在可以编译,但是当我将发布 APK 安装到我的手机时,它不会将穿戴应用程序添加到手表。
    • 您必须使用相同的密钥库分别签署两个 apk。确保您在 build.gradle 中都有签名部分
    • 非常感谢您的回答。这是一个与手表相关的问题。它决定什么都不做。我重新启动了所有设备,它工作正常。 :)
    • @MiguelSesma 有没有办法同时签署两个 apk?来自我的 +1
    • 我们如何为移动设备和穿戴设备整合不同的 buildType?
    【解决方案2】:

    父应用的风格不会自动传播到 Wear 项目。您必须明确映射它。

    而不是这个:

    dependencies {
        wearApp project(':myWearApp')
    }
    

    这样做:

    在您的 Wear 应用中:

    android {
        publishNonDefault true
    }
    

    在您的父应用中:

    dependencies {
        TrialWearApp project(path: ':myWearApp', configuration: 'Trial')
        FullWearApp project(path: ':myWearApp', configuration: 'Full')
    }
    

    【讨论】:

    • 谢谢斯科特。 Gradle 无法识别 TrialWearApp 和 FullWearApp 这两个词,我应该在其他地方定义它们吗?
    • 好的,我发现为了使它起作用,风味必须以小写字母开头。一旦更改为:trialWearApp 和 fullWearApp,单词被识别,但现在我得到一个错误:模块版本 MyApp:myApp:未指定,配置“fullWearApp”声明对配置“完整”的依赖,该配置未在 MyApp 的模块描述符中声明: myWearApp:未指定
    【解决方案3】:

    我看到您找到了解决问题的方法,但这是我的版本,它结合了构建配置与风格和应用程序后缀,以防您将来需要它。对于那些最终通过谷歌搜索进入这篇文章的人来说,这也可能是相关信息。

    app/build.gradle:

    android {
        compileSdkVersion 23
        buildToolsVersion '23.0.3'
    
        signingConfigs {
            debug { ... }
            release { ... }
        }
    
        defaultConfig {
            applicationId "com.sample.myapp"
            minSdkVersion 14
            targetSdkVersion 23
            versionName "3.0.1"
            versionCode 301
        }
    
        buildTypes {
            debug {
                applicationIdSuffix ".debug"
                embedMicroApp = true
                minifyEnabled false
                debuggable true
            }
            release {
                embedMicroApp = true
                minifyEnabled true
                zipAlignEnabled true
                proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
            }
    
        productFlavors {
            trial {
                applicationIdSuffix ".trial"
            }
            full {
                applicationIdSuffix ".pro"
            }
        }
    }
    
    configurations {
        trialDebugWearApp
        fullDebugWearApp
        trialReleaseWearApp
        fullReleaseWearApp
    }
    
    dependencies {
        ...
    
        trialDebugWearApp project(path: ':myWearApp', configuration: 'trialDebug')
        fullDebugWearApp project(path: ':myWearApp', configuration: 'fullDebug')
        trialReleaseWearApp project(path: ':myWearApp', configuration: 'trialRelease')
        fullReleaseWearApp project(path: ':myWearApp', configuration: 'fullRelease')
    }
    

    wear/build.gradle:

    android {
        compileSdkVersion 23
        buildToolsVersion '23.0.3'
    
        publishNonDefault true
    
        signingConfigs {
            debug { ... }
            release { ... }
        }
    
        defaultConfig {
            applicationId "com.sample.myapp"
            minSdkVersion 20
            targetSdkVersion 23
            versionName "3.0.1"
            versionCode 301
        }
    
        buildTypes {
            debug {
                applicationIdSuffix ".debug"
                minifyEnabled false
                debuggable true
            }
            release {
                minifyEnabled true
                proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
            }
    
        productFlavors {
            trial {
                applicationIdSuffix ".trial"
            }
            full {
                applicationIdSuffix ".pro"
            }
        }
    
        dependencies {
            ...
        }
    }
    

    【讨论】:

    • 你能解释一下吗:embedMicroApp = true,好吗?
    • 如果您将此设置为 true,您的 Android Wear 应用程序(在依赖项中定义)将包含在给定构建类型或风格的构建中。如果您希望始终嵌入您的 Wear 应用,请在 defaultConfig 中设置 embedMicroApp = true 并将其从构建类型和风格中删除。
    • 实际上你错过了包括:publishNonDefault true磨损模块gradle文件。你能包括那个吗?没有它,它会抱怨,我花了大约 1 个小时试图弄清楚这一点。您的答案想法应该是被认为更完整的构建类型和风格
    • 很抱歉你在这上面浪费了时间,我的回答只是一个精简版,专注于真正的问题。但我现在已对其进行了更新,使其更加完整,以便更易于复制粘贴。
    【解决方案4】:

    我将在 @tormod 的回答中添加更多内容,因为他省略了一些关键点,包括 publishNonDefault true


    以下是一些用于打包磨损模块的示例 Gradle 文件带有风味和构建类型。

    模组手机build.gradle

    apply plugin: 'com.android.application'
    
    android {
        compileSdkVersion 23
        buildToolsVersion "23.0.3"
    
        defaultConfig {
            applicationId "com.example.app"
            minSdkVersion 15
            targetSdkVersion 23
            versionCode 85
            versionName "2.5.2"
        }
        buildTypes {
            debug {
                applicationIdSuffix ".debug"
                embedMicroApp = true
                minifyEnabled false
            }
            release {
                embedMicroApp = true
                shrinkResources true
                minifyEnabled true
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
                zipAlignEnabled true
            }
        }
        productFlavors {
            free{
                applicationId "com.example.app"
            }
            pro{
                applicationId "com.example.app.pro"
            }
        }
    }
    
    configurations {
        freeDebugWearApp
        proDebugWearApp
        freeReleaseWearApp
        proReleaseWearApp
    }
    
    dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])
        testCompile 'junit:junit:4.12'
        compile 'com.android.support:appcompat-v7:23.4.0'
    
        freeDebugWearApp project(path: ':wear', configuration: 'freeDebug')
        proDebugWearApp project(path: ':wear', configuration: 'proDebug')
    
        freeReleaseWearApp project(path: ':wear', configuration: 'freeRelease')
        proReleaseWearApp project(path: ':wear', configuration: 'proRelease')
    }
    

    模组磨损build.gradle

    apply plugin: 'com.android.application'
    
    android {
        compileSdkVersion 23
        buildToolsVersion "23.0.3"
        publishNonDefault true
    
        defaultConfig {
            applicationId "com.example.app"
            minSdkVersion 20
            targetSdkVersion 23
            versionCode 85
            versionName "2.5.2"
        }
        buildTypes {
            debug {
                applicationIdSuffix ".debug"
                minifyEnabled false
            }
            release {
                shrinkResources true
                minifyEnabled true
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
                zipAlignEnabled true
            }
        }
        productFlavors {
            free {
                applicationId "com.example.app"
            }
            pro {
                applicationId "com.example.app.pro"
            }
        }
    }
    
    dependencies {
    
        ...
    
    }
    

    【讨论】:

    • 无需复制我的答案并将其变为您自己的答案,只需添加一行甚至与问题无关的代码即可。我已按您的要求更新了答案,因此请删除此答案。
    • @Tormod Dude 如果你看到我不会复制粘贴。修复你的头脑并将你的问题转移到别处。你的回答不会产生你描述的结果,你没有修复它,所以我不得不为新用户回答。忍受它。
    • @Tormod Plus 你不应该对更完整的答案投反对票。您对不正确的答案投反对票。我建议您再次阅读 SO 规则和常见问题解答。这不是关于声誉,而是关于互相帮助。但我看到你对自己的行为有不同的看法
    • 我为投反对票道歉,这是我的错。但这不是关于声誉,而是关于保持对问题的答案尽可能少和准确,以免让读者感到困惑(当然,只要没有什么新东西出现在桌面上)。你给了我 11 分钟的时间来回应你的评论,所以你不能说我没有修复它。我尽快(后天)做了。但再次抱歉,我的反应不佳。
    • @Tormod 没问题。我心里也不是很清楚。我们俩都吸取了教训。感谢您的回答。它帮助了我。继续加油!
    猜你喜欢
    • 2014-08-20
    • 2018-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多