Android wear application packaging fails with flav

2019-01-21 23:37发布

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.

4条回答
Summer. ? 凉城
2楼-- · 2019-01-21 23:57

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 {

    ...

}
查看更多
Bombasti
3楼-- · 2019-01-22 00:03

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')
}
查看更多
【Aperson】
4楼-- · 2019-01-22 00:15

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
    }
 }
}
查看更多
小情绪 Triste *
5楼-- · 2019-01-22 00:21

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 {
        ...
    }
}
查看更多
登录 后发表回答