constants. gradle
project.ext {
minSdkVersion = 19
compileSdkVersion = 28
targetSdkVersion = 28
buildToolsVersion = '28.0.3'
supportLibraryVersion = '28.0.0'
}
应用程序的build. gradle
apply plugin: 'com.android.application'
apply from: '../constants.gradle'
android {
compileSdkVersion project.ext.compileSdkVersion
buildToolsVersion project.ext.buildToolsVersion
defaultConfig {
...
这里怎么了?
尽管它适用于同一项目中的库:
defaultConfig
块中的下一行也一切正常
minSdkVersion project.ext.minSdkVersion
targetSdkVersion project.ext.targetSdkVersion
Android Studio 3.2,classpath'com.android.tools.build: gradle:3.2.0'
,分布Url=https\://services.gradle.org/distributions/gradle-4.6-all.zip
以前的Gradle或Studio似乎没有显示这样的警告
这只是一个警告,应该会起作用。
因为当你在android
范围内使用project
时,Gradle会尝试找到project
的调用位置。
您有两个选项来修复此警告。
获取android
范围之外的常量。
def compileSdkVersion = project.ext.compileSdkVersion
def buildToolsVersion = project.ext.buildToolsVersion
android {
compileSdkVersion compileSdkVersion
buildToolsVersion buildToolsVersion
...
或者更新您的constants. gradle
:
ext {
buildVersions = [
minSdkVersion : 19,
compileSdkVersion : 28,
targetSdkVersion : 28,
buildToolsVersion : '28.0.3',
supportLibraryVersion : '28.0.0',
]
}
并在您的build. gradle
中使用它,例如:
apply plugin: 'com.android.application'
apply from: '../constants.gradle'
android {
compileSdkVersion buildVersions.compileSdkVersion
buildToolsVersion buildVersions.buildToolsVersion
...
在我的例子中,相同的变量命名(compileSdkVersion compileSdkVersion)不正确,这是一个工作代码示例:
def compileSdkVer = 30
def buildToolsVer = '29.0.3'
android {
compileSdkVersion compileSdkVer
buildToolsVersion buildToolsVer
defaultConfig {
...