基于构建类型和口味组合的Android库依赖导入

3

我有一些外部库,它们的版本不同以匹配所有我的构建变体。在这个例子中,我有4个不同版本的同一个库。我无法弄清楚如何在我的build.gradle文件中实现这一点。我有两种不同的flavor,与release和debug组合使用。在下面的示例中,被注释的代码可以工作,而其他代码是我想要让其工作的代码。

  android {
      buildTypes {
          release {
              minifyEnabled true
          }
          debug {
              minifyEnabled false
          }
      }

      flavorDimensions "base"
      productFlavors {
          flavor1 {
              dimension "base"
              applicationIdSuffix ".flavor1"

          }
          flavor2 {
              dimension "base"
              applicationIdSuffix ".flavor2"
          }
      }
  }

  dependencies {
      // this works but cannot specify the build type
      flavor1Implementation files('libs/mylib-release.aar')

      // this combination of buildType+Flavor is not working
      // flavor1ReleaseImplementation files('libs/mylib-flavor1-release.aar')
      // flavor1DebugImplementation   files('libs/mylib-flavor1-debug.aar')

      // this works but cannot specify the build type
      flavor2Implementation files('libs/mylib-flavor2-release.aar')

      // flavor2ReleaseImplementation files('libs/mylib-flavor2-release.aar')
      // flavor2DebugImplementation   files('libs/mylib-flavor2-debug.aar')
  }
2个回答

3
将以下代码添加到您的build.gradle文件中,以初始化依赖项配置的占位符。
// Initializes a placeholder for these dependency configurations
configurations {
    flavor1DebugImplementation {}
    flavor1ReleaseImplementation {}
    flavor2DebugImplementation {}
    flavor2ReleaseImplementation {}
}

在build.gradle中实现完整解决方案的示例

android {
    buildTypes {
        release {
            minifyEnabled true
        }
        debug {
            minifyEnabled false
        }
    }

    flavorDimensions "base"
    productFlavors {
        flavor1 {
            dimension "base"
            applicationIdSuffix ".flavor1"

        }
        flavor2 {
            dimension "base"
            applicationIdSuffix ".flavor2"
        }
    }
}


// Initializes a placeholder for these dependency configurations
configurations {
    flavor1DebugImplementation {}
    flavor1ReleaseImplementation {}
    flavor2DebugImplementation {}
    flavor2ReleaseImplementation {}
}


dependencies {
    //flavor1Implementation files('libs/mylib-release.aar')

    flavor1ReleaseImplementation files('libs/mylib-flavor1-release.aar')
    flavor1DebugImplementation   files('libs/mylib-flavor1-debug.aar')

    //flavor2Implementation files('libs/mylib-flavor2-release.aar')
    
    flavor2ReleaseImplementation files('libs/mylib-flavor2-release.aar')
    flavor2DebugImplementation   files('libs/mylib-flavor2-debug.aar')
}

0
这种方法适用于多模块项目。
创建一个构建 gradle 文件(例如 flavor_config.gradle),并在其中定义您的配置,就像这样。
android {
    flavorDimensions 'resource_type'
    productFlavors {
        create("flavor1") {
            dimension 'resource_type'
            versionName "$app_version_name - flavor1"
        }
        create("flavor2") {
            dimension 'resource_type'
            versionName "$app_version_name - flavor2"
        }
    }
}

并且将此Gradle文件应用于您想要的每个模块,例如应用程序模块或功能模块,如下所示:

apply from: rootProject.file("flavor_config.gradle")

同步项目后,您可以像这样访问每个风味的具体实现。
flavor1Implementation("flavor1Library")
flavor2Implementation("flavor2Library")

请不要将相同的答案添加到多个问题。请回答最好的一个,并将其他问题标记为重复,一旦您获得足够的声望。如果不是重复的问题,请[编辑]答案并根据问题进行调整。 - double-beep

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接