从应用程序到库模块继承依赖项版本

时间:2018-10-29 08:26:23

标签: android android-library

我创建了一个使用内部一些标准依赖项的库:

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.google.android.gms:play-services-drive:16.0.0'
}

我想知道是否有一种方法可以从应用程序继承依赖版本,例如:

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:$INHERITED_FROM_APP'
implementation 'com.google.android.gms:play-services-drive:$INHERITED_FROM_APP'
}

2 个答案:

答案 0 :(得分:0)

在您的项目级别build.gradle中,定义全局变量

buildscript {    
    ext.global_minSdkVersion = 16
    ext.global_targetSdkVersion = 28
    ext.global_buildToolsVersion = '28.0.1'
    ext.global_supportLibVersion = '28.0.0'
    ext.global_googleLibVersion = '16.0.0'
}

转到应用程序级别build.gradle,并使用全局变量

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:$global_supportLibVersion'
    implementation 'com.google.android.gms:play-services-drive:$global_googleLibVersion'
}

答案 1 :(得分:0)

使用依赖项定义dependencies.gradle文件的便捷解决方案: dependencies.gradle个文件:

ext.versions = [
 'supportLib' : 28.0.0,
]

ext.deps = [
  'appcompat': "com.android.support:appcompat-v7:${versions.supportLib}",
]

build.gradle文件应用程序级别:

buildscript {
  apply from: rootProject.file('dependencies.gradle')
  ....
}

build.gradle文件应用模块级别:

implementation deps.appcompat