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

时间:2023-01-03
本文介绍了基于 Android Gradle 中多风味库的多风味应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我的应用针对多个市场的应用内计费系统具有多种风格.

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

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

如果是这样,我如何在应用的各个风味中包含不同的风味?

我搜索了很多,但我找不到关于这个场景的任何信息.我发现的唯一接近的东西是

My app has several flavors for several markets in-app-billing systems.

I have a single library which shares the base code for all of my projects. So I decided to add those payment systems to this library as product flavors.

The question is can android library have product flavors?

If so, how can I include different flavors in respective flavor of the app?

I searched a lot, and I couldn't find anything about this scenario. The only close thing I found was this in http://tools.android.com/tech-docs/new-build-system/user-guide:

dependencies {
    flavor1Compile project(path: ':lib1', configuration: 'flavor1Release')
    flavor2Compile project(path: ':lib1', configuration: 'flavor2Release')
}

I changed configuration to different things but it did not work!

I'm using android studio 0.8.2.

解决方案

Finally I found out how to do this, I will explain it here for others facing same problem:

The key part is to set publishNonDefault to true in library build.gradle, Then you must define dependencies as suggested by user guide.

The whole project would be like this:

Library build.gradle:

apply plugin: 'com.android.library'

android {        
    ....
    publishNonDefault true
    productFlavors {
        market1 {}
        market2 {}
    }
}

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

Now you can select the app flavor and Build Variants panel and the library will be selected accordingly and all build and run will be done based on the selected flavor.

If you have multiple app module based on the library Android Studio will complain about Variant selection conflict, It's ok, just ignore it.

这篇关于基于 Android Gradle 中多风味库的多风味应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:Android 依赖在编译和运行时有不同的版本 下一篇:如何修复“意外元素<查询>在“清单"中找到错误?

相关文章

最新文章