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   這里已經有很多小伙伴在等你了,快來加入我們吧!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM