https://blog.csdn.net/shaoyezhangliwei/article/details/100516775
這個錯誤的原因就是build.gradle的配置都統一調用自定義的gradle文件
這個我們就要說一下自定義gradle文件了。
我們在項目開發中為了避免項目和引用的多個module使用的 sdk版本不一致,為了統一版本號,我們一般會建一個公用的gradle文件。
在項目主目錄下定義一個xxx.gradle的文件
我們這里定義了一個 dependencies.gradle的文件,內容為
-
ext.change = [
-
-
code : 100,
-
name : '1.1.0',
-
-
]
-
-
ext.versions = [
-
-
minSdk : 15,
-
targetSdk : 26,
-
compileSdk : 26,
-
-
buildTools : '26.0.2',
-
-
applicationId : "com.today.step",
-
-
androidGradlePlugin : '3.2.1',
-
-
supportLibs : '26.1.0',
-
]
-
-
ext.gradlePlugins = [
-
android : "com.android.tools.build:gradle:$versions.androidGradlePlugin",
-
]
-
-
ext.libraries = [
-
supportAppCompat : "com.android.support:appcompat-v7:$versions.supportLibs",
-
supportRecyclerView : "com.android.support:recyclerview-v7:$versions.supportLibs",
-
-
]
-
然后我們在項目的根目錄下 build.gradle配置如下:
-
-
-
buildscript {
-
-
apply from: 'dependencies.gradle'
-
-
repositories {
-
jcenter()
-
mavenCentral()
-
google()
-
}
-
dependencies {
-
classpath gradlePlugins.android
-
}
-
}
-
-
allprojects {
-
repositories {
-
jcenter()
-
google()
-
}
-
}
然后在APP及module中的 build.gradle文件中就可以直接這樣定義了
-
apply plugin: 'com.android.application'
-
-
android {
-
compileSdkVersion versions.compileSdk
-
buildToolsVersion '28.0.3'
-
-
defaultConfig {
-
applicationId versions.applicationId
-
minSdkVersion versions.minSdk
-
targetSdkVersion versions.targetSdk
-
versionCode change.code
-
versionName change.name
-
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
-
}
-
buildTypes {
-
debug{
-
}
-
release {
-
minifyEnabled false
-
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
-
}
-
}
-
}
-
-
dependencies {
-
implementation fileTree(include: ['*.jar'], dir: 'libs')
-
implementation libraries.supportAppCompat
-
implementation project(':lib-todaystepcounter')
-
}
再說回我們的這個錯誤,就是因為這個找不到versions這樣的屬性,也就是沒有定義,可能是我們直接從三方的代碼拷貝過來,也可能直接導入了一些三方的module ,但是咱們的主項目里面沒有這樣定義就會報這樣的錯誤,按照上面的定義一下就可以了。
歡迎各位小伙伴加入我的qq群:開發一群:454430053 開發二群:537532956 這里已經有很多小伙伴在等你了,快來加入我們吧!