Wear App and with custom build type with applicationIdSuffix

后端 未结 4 1992
天命终不由人
天命终不由人 2020-12-15 04:53

I have an app where I\'d like to add an Android Wear app extension. The main app has three build types (debug, beta and release). Beta builds have an applicationIdSuff

4条回答
  •  Happy的楠姐
    2020-12-15 05:26

    UPDATE Now there is official support for build variants (see answer of Cyril Leroux). Hence this answer is deprecated.


    I found a very (very) ugly solution which has some drawbacks but works for now until there is support for build variants for wear apps.

    I set a global variable in the rootProject which contains the applicationIdSuffix of the currently built main app.

    Within build.gradle of the main app I added the following:

    // 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
        }
    }
    

    In the build.gradleof the wear app I added the following snipped:

    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
            }
        }
    }
    

    This has several drawbacks:

    1. You can't build multiple variants (i.e. gradle assembleBeta assembleRelease) because the wear app is only built once and hence the second build type fails
    2. gradle check fails because of reason 1
    3. The wear app is still built with build type release but the package name is just changed according to to application id suffix of the main app

提交回复
热议问题