I've just noticed in a library module, that compileSdkVersion and buildToolsVersion had recently been deprecated. These are the two configurations, which it complains about (strikethrough text). This currently only affects com.android.library, not com.android.application:
plugins {
id "com.android.library"
}
android {
defaultConfig {
compileSdkVersion 31
buildToolsVersion "31.0.0"
}
}
When clicking through, on may notice that com.android.build.gradle.AndroidConfig had been deprecated in favor of BaseExtension (whatever that may mean for a matching build.gradle):
Apparently compileSdkVersion is now called compileSdk (this part seems to work) ...and buildToolsVersion has to be defined as a property. But when building (it's not that I've not tried), property buildToolsVerson = "31.0.0" is unknown:
android {
defaultConfig {
compileSdk 31
buildToolsVersion = "31.0.0"
}
}
How to make it build without deprecation warnings?
If you are using a kts gradle file instead (build.gradle.kts)
plugins {
id("com.android.library")
}
android {
defaultConfig {
// compileSdkVersion 31
compileSdk = 31
}
}
It builds alike this (the library module needs to be changed):
plugins {
id "com.android.application"
}
android {
defaultConfig {
compileSdkVersion 31
}
}
plugins {
id "com.android.library"
}
android {
defaultConfig {
// compileSdkVersion 31
compileSdk 31
}
}
Related
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 {
...
What is wrong here?
Though it works fine for libraries in the same project:
Also everything is fine for the next lines in defaultConfig block
minSdkVersion project.ext.minSdkVersion
targetSdkVersion project.ext.targetSdkVersion
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
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.
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
...
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 {
...
I'm getting this error in React Native.
This question says to put
ext {
compileSdkVersion = 26
}
in the 'top level file'. What is the 'top level file' for this in React Native?
module/build.gradle already has
android {
compileSdkVersion rootProject.ext.compileSdkVersion...
React Native android has two build.gradle files
// android/build.gradle
ext {
buildToolsVersion = "26.0.3"
minSdkVersion = 16
compileSdkVersion = 26
targetSdkVersion = 26
supportLibVersion = "26.1.0"
}
//android/app/build.gradle
android {
compileSdkVersion rootProject.ext.compileSdkVersion
}
In an open source library of mine, I use something like following:
android {
compileSdkVersion setup.compileSdk
buildToolsVersion setup.buildTools
defaultConfig {
minSdkVersion setup.minSdk
targetSdkVersion setup.targetSdk
}
}
I don't want to force everyone to define those constants, but I want to use them and I use the same constants in all my projects and libraries. So I want to be able to use one code that works for me and for everyone else not defining those variables. I'm looking for something like following:
android {
// pseudo code line
if setup is defined
{
compileSdkVersion setup.compileSdk
buildToolsVersion setup.buildTools
defaultConfig {
minSdkVersion setup.minSdk
targetSdkVersion setup.targetSdk
}
}
// default setup, if the user did not define global constants
else
{
compileSdkVersion 24
buildToolsVersion "24.0.2"
defaultConfig {
minSdkVersion 16
targetSdkVersion 24
}
}
}
Is something like that possible? Any alternative suggestions?
I imagine that your setup variable is stored in project extension
project.ext.setup = setup
This way, it can be accessible from your project and all your subprojects
You can test the existence of setup like this
if (project.hasProperty('setup'))
The idea is to create a default setup var if no one is provided
if (!project.hasProperty('setup')){
project.ext.setup = new Setup()
project.setup.compileSdk = 24
project.setup.buildTools = "24.0.2"
project.setup.minSdk = 16
project.setup.targetSdk = 24
}
android {
compileSdkVersion project.setup.compileSdk
buildToolsVersion project.setup.buildTools
defaultConfig {
minSdkVersion project.setup.minSdk
targetSdkVersion project.setup.targetSdk
}
}
Sure, I've tried this and it built successfully.
android{
...
if (someCondition){
compileSdkVersion 23
buildToolsVersion "23.0.1"
...
}else {
compileSdkVersion 24
buildToolsVersion "24.0.3"
...
}
}
Rather than duplicating the android configuration block in each of the sub projects:
android {
compileSdkVersion 19
buildToolsVersion "19.0.0"
defaultConfig {
minSdkVersion 9
targetSdkVersion 14
}
}
I would much rather put this in the top-level/root gradle build file like:
subprojects{
android {
compileSdkVersion 19
buildToolsVersion "19.0.0"
defaultConfig {
minSdkVersion 9
targetSdkVersion 14
}
}
}
However, this doesn't work. :(
Error:
"..Could not find method android() for arguments..."
The solution to this turned out to be:
subprojects{
afterEvaluate {
android {
compileSdkVersion 19
buildToolsVersion "19.0.0"
defaultConfig {
minSdkVersion 9
targetSdkVersion 14
}
}
}
}
As far as I know, this is because to process/use the android{...} at evaluation time means it must be present (ie. explicitly written or included as part of an 'apply plugin') as soon as it hits the subprojects in the root build file. And, more accurately, it means that the top-level project must have it defined (which it likely doesn't because it might not be an 'android' or 'android-library' build itself). However, if we push it off to after the evaluation then it can use what is available within each subproject directly.
This question + solution also assume that all subprojects are some form of android project (in my case true, but not necessarily for others).
The safer answer would be to use:
subprojects{
afterEvaluate {
if(it.hasProperty('android')){
android {
compileSdkVersion 19
buildToolsVersion "19.0.0"
defaultConfig {
minSdkVersion 9
targetSdkVersion 14
}
}
}
}
}
Looking for some smart way of doing this only once e.g.
allprojects {
android {
buildToolsVersion '18.0.1'
compileSdkVersion 18
defaultConfig {
targetSdkVersion 18
}
}
}
Otherwise I need to up this thing in every subproject (e.g. in referenced library like volley or viewpagerindicator) every time there's a new SDK update available.
Typically the root project wouldn't contain code and hence you would use subprojects. Also you'll have to apply the Android plugin before the android {} block. Other than that, I'd expect this to work.
I answered in another question:
I found a solution here.
Will copy it here just in case:
In you root build.gradle add
ext {
compileSdkVersion = 20
buildToolsVersion = "20.0.0"
}
subprojects { subproject ->
afterEvaluate{
if((subproject.plugins.hasPlugin('android') || subproject.plugins.hasPlugin('android-library'))) {
android {
compileSdkVersion rootProject.ext.compileSdkVersion
buildToolsVersion rootProject.ext.buildToolsVersion
}
}
}
}
This will apply compileSdkVersion and buildToolsVersion to any android modules you have.
And in your main project's build.gradle change dependencies to this:
compileSdkVersion rootProject.ext.compileSdkVersion
buildToolsVersion rootProject.ext.buildToolsVersion
Basically you are defining them once and could use from anywhere.
Cheers.