18

I have an application that includes a wear app. All works fine on debug tested with a real device. I can alse create the release apk that packs the wear apk inside it. But only if there is only one flavour on my application.

I want to maintain two versions of the application with different applicationId, but although this compile without errors, in this case the two release apks (one of each flavour) don't cointain the corresponding wear apks.

This is the relevant part of the mobile 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.gms:play-services:6.1.+@aar'
    wearApp project(':myWearApp')
}

And this is the correspondig 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'
}

Any help will be welcomed. Thanks.

Miguel Sesma
  • 750
  • 5
  • 15

4 Answers4

31

Thanks to the clue Scott gave me this is the full solution:

1.) Flavors must be lowercase

2.) dependency configurations must include flavorRelease

3.) In Wear app buil gradle, under android{}, we must include publishNonDefault true

So for mobile app 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')
}

And for wear app 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
    }
 }
}
Miguel Sesma
  • 750
  • 5
  • 15
  • It wasn't compiling at all before for me. When I did this, it now compiles, but it doesn't add the wear app to the watch when I install the release APK to my phone. – tasomaniac Apr 09 '15 at 07:41
  • You must sign both apks separately with the same keystore. Ensure you have the signing sections in both build.gradle – Miguel Sesma Apr 10 '15 at 09:49
  • Thank you very much for your answer. It was a watch related problem. It decided not to do anything. I rebooted all the devices and it worked fine. :) – tasomaniac Apr 10 '15 at 09:56
  • @MiguelSesma is there a way to sign both apks at the same time? +1 from me – Blackbelt Apr 28 '15 at 10:48
  • How would we incorporate different buildTypes for mobile and wear? – arne.jans Aug 11 '15 at 16:26
  • How to combine 3 flavors with 3 buildconfigs in both the app and wear project? – Tormod Nov 10 '15 at 12:44
  • @MiguelSesma At the end what's the working solution for you? – moxi Jan 29 '16 at 00:23
  • @arne.jeans For different Build types use something like 'trialWearApp project(path: ':myWearApp', configuration: 'trialDebug') – Miguel Sesma Jan 30 '16 at 12:44
  • @TormodT could you elaborate your question? We are talking about packaging. SO the only important thing are flavours. In debug mode you install app and wear app separately – Miguel Sesma Jan 30 '16 at 12:48
  • @MiguelSesma: Why install them separately when they can be combined? Either way I need them combined in two out of three build types (and all flavours). More details on what I need here: http://stackoverflow.com/q/33630929/3328701 – Tormod Jan 31 '16 at 11:32
  • TIP: You can do the same with `buildTypes` e.g. `debugWearApp`/`releaseWearApp` – Diolor Mar 05 '16 at 18:41
  • Where are the configurations in your answer? I am still missing code – Jimmy Kane May 26 '16 at 12:29
6

The flavor of the parent app isn't propagated automatically to the Wear project. You have to map it explicitly.

Instead of this:

dependencies {
    wearApp project(':myWearApp')
}

Do this:

In your Wear app:

android {
    publishNonDefault true
}

In your parent app:

dependencies {
    TrialWearApp project(path: ':myWearApp', configuration: 'Trial')
    FullWearApp project(path: ':myWearApp', configuration: 'Full')
}
Scott Barta
  • 79,344
  • 24
  • 180
  • 163
  • Thanks Scott. The words TrialWearApp and FullWearApp aren't recognized by Gradle, Should I define them elsewhere? – Miguel Sesma Dec 09 '14 at 22:01
  • Ok, I have discobered that in order of this to work, flavours must start with lowercase letter. Once changed to: trialWearApp & fullWearApp the words are recognized , but now I get a Error:Module version MyApp:myApp:unspecified, configuration 'fullWearApp' declares a dependency on configuration 'full' which is not declared in the module descriptor for MyApp:myWearApp:unspecified – Miguel Sesma Dec 09 '14 at 22:35
1

I see that you found a solution to your problem, but here is my version that combines build configs with flavors and application suffixes in case you might need that in the future. Could also be relevant information for those who end up googling their way into this post.

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 {
        ...
    }
}
Tormod
  • 571
  • 5
  • 21
  • Can you explain: `embedMicroApp = true`, please? – GabrielOshiro May 19 '16 at 02:52
  • If you set this to true, your Android Wear App (defined in dependencies) will be included in the build for the given build type or flavor. If you want your Wear App to always be embedded, set embedMicroApp = true in the defaultConfig and remove it from the build types and flavors. – Tormod May 24 '16 at 06:34
  • Actually you missed to include: `publishNonDefault true` for the wear module gradle file. Can you include that? Without that it gradle will complain and I spend about 1 hour trying to figure that out. Your answer thought should be the one accepted as more complete with build types and flavours – Jimmy Kane May 26 '16 at 13:32
  • I'm sorry you wasted time on that, my answer was merely a stripped version to focus on the real problem. But I have updated it now to be more complete so that it's more copy paste friendly. – Tormod May 27 '16 at 16:51
0

I'll add a bit more to @tormod's answer as he omited some crucial points as to include the publishNonDefault true


Here are some example Gradle files for packaging a wear module with flavours and buildtypes.

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

Module Wear 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 {

    ...

}
Jimmy Kane
  • 16,223
  • 11
  • 86
  • 117
  • No need to copy my answer and make it your own just to add one line of code that doesn't even relate to the question. I've updated my answer as you requested, so please delete this answer. – Tormod Jun 16 '16 at 09:45
  • 1
    @Tormod Dude I do not copy paste if you see. Fix your head and move your problems elsewhere. You answer would not poduce the result you described, you did not fix it so I had to asnwer for new users. Live with that. – Jimmy Kane Jun 16 '16 at 11:07
  • 1
    @Tormod Plus you should not downvote answers that are more complete. You donwvote answers that are not correct. I suggest you read again the SO rules and FAQ. This is not about reputation but about helping each other. But I see you think different with your behaviour – Jimmy Kane Jun 16 '16 at 11:12
  • 1
    I apologize for the downvote, that was wrong of me. But it's not about reputation, it's about keeping answers to questions as few and precise as possible to keep from confusing readers (as long as nothing new is brought to the table of course). You gave me 11 minutes to react to your comment, so you can't say I didn't fix it. I did as soon as I could (the day after). But again, sorry for my poor reaction. – Tormod Jul 06 '16 at 17:55
  • 1
    @Tormod its ok man. I also was not at the most clear mind. Both of us lesson learned. Thanks for the answer. It helped me. Keep it up! – Jimmy Kane Jul 12 '16 at 08:17