【问题标题】:Gradle subproject external library dependency based on (main) project module flavours基于(主)项目模块风格的 Gradle 子项目外部库依赖
【发布时间】:2016-03-22 14:06:10
【问题描述】:

我有两个模块一个 app 和一个 library 和 我使用的两个外部 Maven 库。

外部库略有不同,是根据构建风格与 maven 分类器一起选择的。

(子项目/模块)和(主模块)都使用基于风格的相同外部库。

我的问题是我无法控制/选择子项目库时 编译。

模块应用:

apply plugin: 'android-sdk-manager'
apply plugin: 'com.android.application'

android {
productFlavors {
fOne {}
fTwo {}
}
}

dependencies {   
    compile 'com.android.support:appcompat-v7:22.2.1'
    compile 'com.android.support:gridlayout-v7:22.2.1'
    compile 'com.android.support:support-annotations:22.2.1'

// selecting the library based on the flavour
fOneCompile(group: 'com.xyz', name: 'SDK', version: '1.0', ext: 'aar')
fTwoCompile(group: 'com.xyz', name: 'SDK', version: '1.0', classifier: 'qa', ext: 'aar')

 //<< the library also needs the com.xyz.SDK
 fOneCompile project(path: ':library', configuration: "fOneCompile") 
 fTwoCompile project(path: ':library', configuration: "fTwoCompile")


}

模块库:

apply plugin: 'com.android.library'
android {
....
}

repositories{    
  jcenter()
  flatDir{
      dirs 'libs'
   }
}

configurations {
    fOne
    fTwo 
}

dependencies {
    ??? what goes here, flavours are not available ??? 
    //the library also needs the com.xyz.SDK
    fOneCompile(group: 'com.xyz', name: 'SDK', version: '1.0', ext: 'aar')
    fTwoCompile(group: 'com.xyz', name: 'SDK', version: '1.0', classifier: 'qa', ext: 'aar')
}

模块 library 无法编译,因为它找不到 SDK 和我 需要根据正在编译的风格包含一个。

【问题讨论】:

标签: android gradle


【解决方案1】:

使用这个配置:

defaultConfig {
       publishNonDefault true
}

configurations {
    fOneDebugCompile
    fOneReleaseCompile
    fTwoDebugCompile
    fTwoReleaseCompile
}

【讨论】:

    【解决方案2】:

    解决方案非常简单,因为存在重复的依赖项和 主项目选择合适的依赖项可以使用“提供” 解决依赖关系时在子模块中声明。

    在这种情况下,可以替换依赖项中的“编译”语句 模块库(子模块)中带有“提供”的部分:

    模块库:

    apply plugin: 'com.android.library'
    android {
    ....
    }
    
    repositories{    
      jcenter()
      flatDir{
          dirs 'libs'
       }
    }
    
    dependencies {
        //the library also needs the com.xyz.SDK
        provided(group: 'com.xyz', name: 'SDK', version: '1.0', ext: 'aar')
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多