为每个 android 风格使用不同的库模块

Ran*_*mar 9 android gradle android-gradle-plugin android-flavors

我想为每种风格使用不同的库模块。

例如:

  • 免费风味 -> 我需要使用免费库模块
  • 付费风味 -> 我需要使用付费库模块

我的口味

productFlavors {
    free{
     ....... 
     }
    paid{
     ....... 
     }
   }
Run Code Online (Sandbox Code Playgroud)

我试过的

freeImplementation  project(path:':freeLib', configuration: 'free')//for free

paidImplementation  project(path:':paidLib', configuration: 'paid')//for paid
Run Code Online (Sandbox Code Playgroud)

但是我得到了编译错误无法使用这个

注意:这不是一个重复的问题。我已经尝试过一些 StackOverflow 问题。它已经过时了(他们正在使用编译)

参考 -基于 Android Gradle 中多风格库的多风格应用

解决方案(来自 Gabriele Mariotti 评论)

 freeImplementation project(path:':freeLib')
 paidImplementation project(path:':paidLib')
Run Code Online (Sandbox Code Playgroud)

Gab*_*tti 16

如果您有一个包含多种产品口味的库,lib/build.gradle您可以在其中定义:

android {
    ...
    //flavorDimensions is mandatory with flavors.
    flavorDimensions "xxx"
    productFlavors {
        free{
            dimension "xxx"
        }
        paid{
            dimension "xxx"
        }
    }
    ...
}
Run Code Online (Sandbox Code Playgroud)

在您的app/build.gradle定义中:

android {
    ...

    flavorDimensions "xxx"
    productFlavors {
        free{
            dimension "xxx"

            // App and library's flavor have the same name.
            // MatchingFallbacks can be omitted
            matchingFallbacks = ["free"]
        }
        paid{
            dimension "xxx"

            matchingFallbacks = ["paid"]
        }
    }
    ...
}
dependencies {
    implementation project(':mylib')
}
Run Code Online (Sandbox Code Playgroud)

相反,如果你有单独的库,你可以简单地在你的app/build.gradle东西中使用:

dependencies {
    freeImplementation project(':freeLib')
    paidImplementation project(':paidLib')
}
Run Code Online (Sandbox Code Playgroud)

  • 是的,为我工作..感谢回复.. freeImplementation project(path:':freeLib')paidImplementation project(path:':paidLib') (2认同)
  • 刚刚在答案中添加了我的最后一条评论以支持这两种情况。 (2认同)

cao*_*eng 5

  1. 首先将以下 gradle 代码片段添加到您的app/build.gradle

    flavorDimensions "env"
    productFlavors {
        dev {
            dimension "env"
        }
        pre {
            dimension "env"
        }
        prod {
            dimension "env"
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)
  2. 其次,将以下 gradle 代码片段添加到您的module/build.gradle

    flavorDimensions "env"
    productFlavors {
        register("dev")
        register("pre")
        register("prod")
    }
    
    Run Code Online (Sandbox Code Playgroud)
  3. 同步你的项目,然后你可以发现productFlavors配置成功,如下图 在此输入图像描述