jackofblades jackofblades - 4 months ago 72
Android Question

Gradle Error in Android Studio: Plugin with id 'com.android.library' not found

When I am trying to Build an Android Library Project in Android Studio, I am getting the following Gradle error:

Gradle sync failed: Plugin with id 'com.android.library' not found.


I am pretty new to Gradle and this is very confusing for me. Why is this happening?

The build.gradle file is as follows:

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'
}

Answer

Your issue is that you are using the top-level file where you can't use this kind of plugin.

In AS you have a structure like this:

Root/
 + lib
    | build.gradle
 | build.gradle
 | settings.gradle 

In your top-level file you can use:

// 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()
    }
}

In your lib/build.gradle you can use the code posted in the question:

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'
}

Finally in your settings.gradle

include ':lib'

Also you can refer this question.

Comments