16

constants.gradle

project.ext {
    minSdkVersion = 19
    compileSdkVersion = 28
    targetSdkVersion = 28
    buildToolsVersion = '28.0.3'
    supportLibraryVersion = '28.0.0'
}

build.gradle of the app

apply plugin: 'com.android.application'
apply from: '../constants.gradle'

android {

    compileSdkVersion project.ext.compileSdkVersion
    buildToolsVersion project.ext.buildToolsVersion

    defaultConfig {
    ...

enter image description here

What is wrong here?

Though it works fine for libraries in the same project:

enter image description here

Also everything is fine for the next lines in defaultConfig block

minSdkVersion project.ext.minSdkVersion
targetSdkVersion project.ext.targetSdkVersion

enter image description here

Android Studio 3.2, classpath 'com.android.tools.build:gradle:3.2.0', distributionUrl=https\://services.gradle.org/distributions/gradle-4.6-all.zip

Seems it didn't show such warnings with the previous Gradle or Studio

user924
  • 8,146
  • 7
  • 57
  • 139

2 Answers2

18

It's just a warning and It should work.

Because when you use project inside android scope, Gradle tries to find the invocation location of project.

List of project candidates reported by IDE

You have two options to fix this warning.

Get your constants outside of android scope.

def compileSdkVersion = project.ext.compileSdkVersion
def buildToolsVersion = project.ext.buildToolsVersion

android {

    compileSdkVersion compileSdkVersion
    buildToolsVersion buildToolsVersion
    ...

Or update your constants.gradle:

ext {
    buildVersions = [
      minSdkVersion : 19,
      compileSdkVersion : 28,
      targetSdkVersion : 28,
      buildToolsVersion : '28.0.3',
      supportLibraryVersion : '28.0.0',
    ]
}

and use it in your build.gradle like:

apply plugin: 'com.android.application'
apply from: '../constants.gradle'

android {

    compileSdkVersion buildVersions.compileSdkVersion
    buildToolsVersion buildVersions.buildToolsVersion
    ...
ParkerM
  • 302
  • 1
  • 4
  • 17
Saeed Masoumi
  • 8,746
  • 7
  • 57
  • 76
  • Your second solutions gives an error. The IDE shows that the syntax is invalid for buildVersions = [ ... ]. If you add commas between items then it doesn't show an error. Also if you wrap with curly braces it also doesn't show as a syntax error. Neither of those actually resolve though. – Matt Wolfe Aug 26 '19 at 22:05
  • 1
    If anyone else is looking for this, the correct syntax for Saeed Masoumi's second solution is: ext { buildVersion = [ minSdk: 19, compileSdk: 28, targetSdk: 28 ] } (I cannot comment, otherwise I'd have put that below their answer) – Adrien de Sentenac Sep 04 '19 at 19:54
0

The same variables naming (compileSdkVersion compileSdkVersion) was incorrect in my case, here's an example of working code:

def compileSdkVer = 30
def buildToolsVer = '29.0.3'

android {
    compileSdkVersion compileSdkVer
    buildToolsVersion buildToolsVer
defaultConfig {
...
kirchhoff
  • 21
  • 2