基于Android Gradle多风味库的多风味应用

Ali*_*Ali 97 android gradle android-gradle-plugin

我的应用程序有几种风格适用于多个市场的应用程序计费系统.

我有一个库,它共享我所有项目的基本代码.所以我决定将这些支付系统作为产品口味添加到这个库中.

问题是android库可以有产品口味吗?

如果是这样,我如何在应用程序的各自风格中包含不同的风格?

我搜索了很多,我找不到任何关于这种情况的信息.我在http://tools.android.com/tech-docs/new-build-system/user-guide中找到的唯一近似内容是:

dependencies {
    flavor1Compile project(path: ':lib1', configuration: 'flavor1Release')
    flavor2Compile project(path: ':lib1', configuration: 'flavor2Release')
}
Run Code Online (Sandbox Code Playgroud)

我将配置更改为不同的东西,但它不起作用!

我正在使用android studio 0.8.2.

Ali*_*Ali 136

最后我发现了如何做到这一点,我将在这里解释面临同样问题的其他人:

关键部分是在库build.gradle中将publishNonDefault设置为true,然后您必须按照用户指南的建议定义依赖关系.

整个项目将是这样的:

库build.gradle:

apply plugin: 'com.android.library'

android {        
    ....
    publishNonDefault true
    productFlavors {
        market1 {}
        market2 {}
    }
}
Run Code Online (Sandbox Code Playgroud)

project build.gradle:

apply plugin: 'com.android.application'

android {
    ....
    productFlavors {
        market1 {}
        market2 {}
    }
}

dependencies {
    ....
    market1Compile project(path: ':lib', configuration: 'market1Release')
    market2Compile project(path: ':lib', configuration: 'market2Release')
}
Run Code Online (Sandbox Code Playgroud)

现在,您可以选择应用程序风格和"构建变体"面板,并相应地选择库,并根据所选的风格完成所有构建和运行.

如果您有多个基于库的应用程序模块,Android Studio会抱怨Variant选择冲突,没关系,只需忽略它.

在此输入图像描述

  • 运行的1.1.0,上述方案似乎还在努力,但是1)调试/发行版本的选择是失去了,我似乎保持有问题与AIDL中不能经常产生相应的代码库中找到.有什么想法吗? (2认同)

App*_*evo 35

阿里回答有一个问题.我们在构建变体中失去了一个非常重要的维度.如果我们想要所有选项(在我的示例中低于4(2 x 2)),我们只需要在主模块build.gradle文件中添加自定义配置,以便能够使用所有多风味的multi-buildType .我们还必须在库模块build.gradle文件中设置publishNonDefault true.Build Variants

示例解决方案

Lib build.gradle

android {

    publishNonDefault true

    buildTypes {
        release {
        }
        debug {
        }
    }
    productFlavors {
        free {
        }
        paid {
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

App build.gradle

android {

    buildTypes {
        debug {
        }
        release {
        }
    }
    productFlavors {
        free {
        }
        paid {
        }
    }
}

configurations {
    freeDebugCompile
    paidDebugCompile
    freeReleaseCompile
    paidReleaseCompile
}

dependencies {

    freeDebugCompile project(path: ':lib', configuration: 'freeDebug')
    paidDebugCompile project(path: ':lib', configuration: 'paidDebug')
    freeReleaseCompile project(path: ':lib', configuration: 'freeRelease')
    paidReleaseCompile project(path: ':lib', configuration: 'paidRelease')

}
Run Code Online (Sandbox Code Playgroud)


Har*_*789 17

Android插件3.0.0及更高版本的更新

根据官方Android文档 - 迁移本地模块的依赖关系配置,

使用变体感知依赖项解析,您不再需要为本地模块依赖项使用特定于变体的配置(例如freeDebugImplementation) - 插件会为您处理此问题

您应该按如下方式配置依赖项:

dependencies {
    // This is the old method and no longer works for local
    // library modules:
    // debugImplementation project(path: ':library', configuration: 'debug')
    // releaseImplementation project(path: ':library', configuration: 'release')

    // Instead, simply use the following to take advantage of
    // variant-aware dependency resolution. You can learn more about
    // the 'implementation' configuration in the section about
    // new dependency configurations.
    implementation project(':library')

    // You can, however, keep using variant-specific configurations when
    // targeting external dependencies. The following line adds 'app-magic'
    // as a dependency to only the "debug" version of your module.

    debugImplementation 'com.example.android:app-magic:12.3'
}
Run Code Online (Sandbox Code Playgroud)

所以在阿里的回答中,改变

dependencies {
    ....
    market1Compile project(path: ':lib', configuration: 'market1Release')
    market2Compile project(path: ':lib', configuration: 'market2Release')
}
Run Code Online (Sandbox Code Playgroud)

implementation project(':lib')
Run Code Online (Sandbox Code Playgroud)

插件将自动处理变体特定配置.希望有人帮助其他人将Android Studio插件升级到3.0.0及更高版本.


Jia*_*aGu 8

我的Android插件是3.4.0,我发现它现在不需要配置。你需要的是确保应用程序中的flavorDimensions和productFlavors在库中包含一个相同flavorDimensions和productFlavors的productFlavor。例如:

在 mylibrary 的 build.gradle 中

apply plugin: 'com.android.library'

android {        
    ....
    flavorDimensions "mylibFlavor"

    productFlavors {
        market1
        market2
    }
}
Run Code Online (Sandbox Code Playgroud)

应用程序的 build.gradle:

apply plugin: 'com.android.application'

android {
    ....
    flavorDimensions "mylibFlavor", "appFlavor"
    productFlavors {
        market1 {
            dimension "mylibFlavor"
        }
        market2 {
            dimension "mylibFlavor"
        }
        common1 {
            dimension "appFlavor"
        }
        common2 {
            dimension "appFlavor"
        }
    }
}

dependencies {
    ....
    implementation project(path: ':mylibrary')
}
Run Code Online (Sandbox Code Playgroud)

同步后,您可以在 Build Variants 窗口中切换所有选项: 在此处输入图片说明