Could not get unknown property 'versions' for object of type com.android.build.gradle.AppExtension

https://blog.csdn.net/shaoyezhangliwei/article/details/100516775

这个错误的原因就是build.gradle的配置都统一调用自定义的gradle文件

这个我们就要说一下自定义gradle文件了。

我们在项目开发中为了避免项目和引用的多个module使用的 sdk版本不一致,为了统一版本号,我们一般会建一个公用的gradle文件。

在项目主目录下定义一个xxx.gradle的文件 

我们这里定义了一个 dependencies.gradle的文件,内容为

  1.  
    ext.change = [
  2.  
     
  3.  
    code : 100,
  4.  
    name : '1.1.0',
  5.  
     
  6.  
    ]
  7.  
     
  8.  
    ext.versions = [
  9.  
     
  10.  
    minSdk : 15,
  11.  
    targetSdk : 26,
  12.  
    compileSdk : 26,
  13.  
     
  14.  
    buildTools : '26.0.2',
  15.  
     
  16.  
    applicationId : "com.today.step",
  17.  
     
  18.  
    androidGradlePlugin : '3.2.1',
  19.  
     
  20.  
    supportLibs : '26.1.0',
  21.  
    ]
  22.  
     
  23.  
    ext.gradlePlugins = [
  24.  
    android : "com.android.tools.build:gradle:$versions.androidGradlePlugin",
  25.  
    ]
  26.  
     
  27.  
    ext.libraries = [
  28.  
    supportAppCompat : "com.android.support:appcompat-v7:$versions.supportLibs",
  29.  
    supportRecyclerView : "com.android.support:recyclerview-v7:$versions.supportLibs",
  30.  
     
  31.  
    ]
  32.  
     

然后我们在项目的根目录下 build.gradle配置如下:

  1.  
    // Top-level build file where you can add configuration options common to all sub-projects/modules.
  2.  
     
  3.  
    buildscript {
  4.  
     
  5.  
    apply from: 'dependencies.gradle'
  6.  
     
  7.  
    repositories {
  8.  
    jcenter()
  9.  
    mavenCentral()
  10.  
    google()
  11.  
    }
  12.  
    dependencies {
  13.  
    classpath gradlePlugins.android
  14.  
    }
  15.  
    }
  16.  
     
  17.  
    allprojects {
  18.  
    repositories {
  19.  
    jcenter()
  20.  
    google()
  21.  
    }
  22.  
    }

然后在APP及module中的 build.gradle文件中就可以直接这样定义了

  1.  
    apply plugin: 'com.android.application'
  2.  
     
  3.  
    android {
  4.  
    compileSdkVersion versions.compileSdk
  5.  
    buildToolsVersion '28.0.3'
  6.  
     
  7.  
    defaultConfig {
  8.  
    applicationId versions.applicationId
  9.  
    minSdkVersion versions.minSdk
  10.  
    targetSdkVersion versions.targetSdk
  11.  
    versionCode change.code
  12.  
    versionName change.name
  13.  
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
  14.  
    }
  15.  
    buildTypes {
  16.  
    debug{
  17.  
    }
  18.  
    release {
  19.  
    minifyEnabled false
  20.  
    proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
  21.  
    }
  22.  
    }
  23.  
    }
  24.  
     
  25.  
    dependencies {
  26.  
    implementation fileTree(include: ['*.jar'], dir: 'libs')
  27.  
    implementation libraries.supportAppCompat
  28.  
    implementation project(':lib-todaystepcounter')
  29.  
    }

再说回我们的这个错误,就是因为这个找不到versions这样的属性,也就是没有定义,可能是我们直接从三方的代码拷贝过来,也可能直接导入了一些三方的module ,但是咱们的主项目里面没有这样定义就会报这样的错误,按照上面的定义一下就可以了。

欢迎各位小伙伴加入我的qq群:开发一群:454430053 开发二群:537532956   这里已经有很多小伙伴在等你了,快来加入我们吧!

原文地址:https://www.cnblogs.com/sundaysandroid/p/13577736.html