Android Studio中的Gradle Error:找不到ID为'com.android.library'的插件

jac*_*des 7 android gradle android-studio build.gradle

当我尝试在Android Studio中构建Android库项目时,我收到以下Gradle错误:

Gradle sync failed: Plugin with id 'com.android.library' not found.
Run Code Online (Sandbox Code Playgroud)

我对Gradle很新,这对我来说很困惑.为什么会这样?

build.gradle文件如下:

apply plugin: 'com.android.library'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        minSdkVersion 8
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    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.1.0'
}
Run Code Online (Sandbox Code Playgroud)

Gab*_*tti 14

您的问题是您使用的顶级文件无法使用此类插件.

在AS中你有这样的结构:

Root/
 + lib
    | build.gradle
 | build.gradle
 | settings.gradle 
Run Code Online (Sandbox Code Playgroud)

您的顶级文件中,您可以使用:

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    repositories {
        jcenter()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:1.3.1'
    }
}

allprojects {
    repositories {
        jcenter()
    }
}
Run Code Online (Sandbox Code Playgroud)

在你lib/build.gradle可以使用张贴在问题的代码:

apply plugin: 'com.android.library'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        minSdkVersion 8
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    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.1.0'
}
Run Code Online (Sandbox Code Playgroud)

最后在你的 settings.gradle

include ':lib'
Run Code Online (Sandbox Code Playgroud)

你也可以参考这个问题.