【问题标题】:Android studio 1.5.1: Could not find property 'vectorDrawables'Android Studio 1.5.1:找不到属性“vectorDrawables”
【发布时间】:2016-02-29 06:34:37
【问题描述】:

我正在使用 Android Studio 1.5.1、Gradle 2.8 和我的项目 min sdk vserion:14, target sdk version: 23.

所以,当我通过 Google 文档将 vectorDrawables 添加到配置中时:Added VectorDrawable support library,我收到以下错误:

Error:(13, 0) Could not find property 'vectorDrawables' on ProductFlavor_Decorated{name=main, dimension=null, minSdkVersion=ApiVersionImpl{mApiLevel=14, mCodename='null'}, targetSdkVersion=ApiVersionImpl{mApiLevel=23, mCodename='null'}, renderscriptTargetApi=null, renderscriptSupportModeEnabled=null, renderscriptNdkModeEnabled=null, versionCode=25, versionName=1.0.25, applicationId=com.smsoft.alibaba, testApplicationId=null, testInstrumentationRunner=null, testInstrumentationRunnerArguments={}, testHandleProfiling=null, testFunctionalTest=null, signingConfig=null, resConfig=null, mBuildConfigFields={}, mResValues={}, mProguardFiles=[], mConsumerProguardFiles=[], mManifestPlaceholders={}}.

这是我的 build.gradle 文件:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        applicationId "com.smsoft.alibaba"
        minSdkVersion 14
        targetSdkVersion 23
        versionCode 25
        versionName "1.0.25"
        vectorDrawables.useSupportLibrary = true
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
     compile fileTree(dir: 'libs', include: ['*.jar'])
     testCompile 'junit:junit:4.12'
     compile 'com.android.support:appcompat-v7:23.2.0'
     compile 'com.android.support:design:23.2.0'
     compile 'com.android.support:support-v4:23.2.0'
     compile 'com.android.support:cardview-v7:23.2.0'
}

有人知道如何解决这个问题吗?

编辑

感谢@Gabriele Mariotti 提醒我对 gradle 和 gradle 插件的困惑。阅读添加的 Compact Vector Drawables 指令时,我感到很困惑。

【问题讨论】:

    标签: android android-studio gradle android-support-library android-vectordrawable


    【解决方案1】:

    如果您使用 v2.0 或更高版本的 Gradle 插件,您必须使用:

    android {
      defaultConfig {
        vectorDrawables.useSupportLibrary = true
      }
    }
    

    如果您使用 v1.5.0 或以下版本的 Gradle 插件,您需要将以下内容添加到您应用的build.gradle

    android {
      defaultConfig {
        // Stops the Gradle plugin’s automatic rasterization of vectors
        generatedDensities = []
      }
      // Flag to tell aapt to keep the attribute ids around
      aaptOptions {
        additionalParameters "--no-version-vectors"
      }
    }
    

    不要将 gradle 与 gradle 插件混淆。检查根文件夹(或模块内)中的build.gradle 以获取gradle 插件使用的版本(检查classpath 'com.android.tools.build:gradle:1.5.0' 行)

    【讨论】:

    • 嗨@Gabriele,我解决了我的问题,但我忘记将“vectorDrawables.useSupportLibrary = true”行添加到defaultConfig,添加此行后出现此错误,请查看上方
    • 你用的是插件2.0吗?
    • @mr.boyfox 不要将 gradle 与 gradle 插件混淆。 2.8 是 gradle 版本,不是插件。检查根文件夹中的 build.gradle。
    • 哦,对不起,你是对的,我使用 com.android.tools.build:gradle:1.5.0 构建插件
    • 我不明白。我使用 gradle 2+ 仍然有与 OP 相同的错误
    【解决方案2】:

    专门从com.android.tools.build:gradle:1.5.0升级。

    1. 编辑 /build.gradle 并设置:

      buildscript {
          ...
          dependencies {
              ...
              classpath 'com.android.tools.build:gradle:2.0.0'
              ...
          }
      }
      
    2. 编辑 /gradle/wrapper/gradle-wrapper.properties 并设置:

      distributionUrl=https\://services.gradle.org/distributions/gradle-2.10-all.zip
      
    3. 编辑您的模块的 build.gradle 并添加:

      android {
          ...
          defaultConfig {
              ...
              vectorDrawables.useSupportLibrary = true
          }
          ...
      }
      

    【讨论】:

    【解决方案3】:

    我在 v3.4.2 上使用 Gradle 插件时遇到了这个问题。它阻止了项目同步的完成。

    通过删除vectorDrawables 并将vectorDrawables.useSupportLibrary = true 添加到defaultConfig 解决了这个问题。

    android {
        compileSdkVersion 29
        buildToolsVersion "29.0.1"
        defaultConfig {
            applicationId "com.example.android.diceroller"
            minSdkVersion 19
            targetSdkVersion 29
            versionCode 1
            versionName "1.0"
            testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
            vectorDrawables.useSupportLibrary = true
        }
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
            }
        }vectorDrawables
    }
    

    android {
        compileSdkVersion 29
        buildToolsVersion "29.0.1"
        defaultConfig {
            applicationId "com.example.android.diceroller"
            minSdkVersion 19
            targetSdkVersion 29
            versionCode 1
            versionName "1.0"
            testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
            vectorDrawables.useSupportLibrary = true
        }
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
            }
        }
    }
    

    【讨论】:

      【解决方案4】:

      如果你有最新版本的useSupportLibary 使用

      vectorDrawables.useSupportLibary(true)
      In your defaultconfig 
      

      【讨论】:

        猜你喜欢
        • 2020-05-28
        • 1970-01-01
        • 2017-10-28
        • 1970-01-01
        • 2018-07-01
        • 2014-02-24
        • 2018-04-12
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多