Saman Sattari
Saman Sattari

Reputation: 3568

ext property inside top level build.gradle file

I'm developing an android application. I've an 'dependencies.gradle' file in the root project:

ext {
    // Android
    kotlinVersion = '1.2.51'
    gradleVersion = '3.1.3'
}

The problem is that I can use this properties in the App 'build.gradle' file but can't use inside Root 'build.gradle' file and it gives me this error:

Could not get unknown property 'kotlinVersion' for object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.

This is my 'Root Build Gradle':

apply from: 'dependencies.gradle'

buildscript {
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath "com.android.tools.build:gradle:3.1.3"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

And this is my 'App Build Gradle' :

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 27
    defaultConfig {
        applicationId "com.shimibox.client"
        minSdkVersion 18
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlinVersion"
    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation 'com.android.support.constraint:constraint-layout:1.1.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

Upvotes: 8

Views: 8408

Answers (1)

Saman Sattari
Saman Sattari

Reputation: 3568

I've found the problem. The ext must be inside the buildscript block. So I moved the apply from: 'dependencies.gradle' inside of that block. Now Root build.gradle file is:

buildscript {

    apply from: 'dependencies.gradle'

    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath "com.android.tools.build:gradle:3.1.3"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
    }
}

Upvotes: 12

Related Questions