【问题标题】:android import library dependency based on build type and flavor combinations基于构建类型和风味组合的android导入库依赖
【发布时间】:2021-08-18 07:06:08
【问题描述】:

我有一些不同版本的外部库来匹配我所有的构建变体。在这个例子中,我有同一个库的 4 个不同版本。我无法在我的 build.gradle 文件中弄清楚如何做到这一点。我有两种不同的风格,结合了发布和调试。在下面的示例中,注释掉的代码可以工作,而其他代码是我正在尝试的工作。

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

【问题讨论】:

    标签: android gradle build variant


    【解决方案1】:

    将以下代码添加到您的 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')
    }
    

    【讨论】:

      猜你喜欢
      • 2015-03-24
      • 1970-01-01
      • 2018-11-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多