1

How can I change the minimum SDK in an Android (Studio) project with recent (2020+) versions of Android Studio and Gradle?

During the creation of a new Android Studio project the wizard ask for the minimum SDK required.

Android Studio project creation wizard

Since the wizard generates a lot of boilerplate files and code, I assume that the boilerplate code is tailored to the minimum SDK chosen. My first objective is to generate a modern, lean, forward-compatible (Kotlin) app, so I chose API 31 (most recent non-beta on 29 Dec 2021). However, once the app (which is simple) is working, I would like to lower the minimum SDK to include as many devices as possible (without adding legacy dependencies, code, etc.). Is this a correct way to think about the relation between the choice of minimum SDK and the boilerplate code?

There are existing questions on older (2013) versions of Android Studio (and Gradle), e.g. here, but these do not work in modern versions of Android Studio and Gradle (I have only one build.gradle file and it does not mention any SDK, adding this gives errors).

EDIT: see below an image of the folder tree, as suggested.

folder tree

Bastiaan Quast
  • 2,802
  • 1
  • 24
  • 50
  • You can change it in the `build.grade(app)`. It is still the right answer. Maybe post your folder tree to see if there is any problem? – Ricky Mo Dec 29 '21 at 09:17
  • 1
    If you choose "Android" in your project folder tree in android studio, the lowest part would be "Gradle Scripts", expand it should contain at least two `build.gradle`. One is `build.gradle (Project: YourAppName)`, another is `build.gradle (Module: YourAppName.app)` , the latter is which to change. If you look directly in File Explorer, the first one locates in `/YourProjectFolder/build.gradle` while the second one locates in `/YourProjectFolder/app/build.gradle` – Ricky Mo Dec 29 '21 at 09:31
  • Thank you, I only have one `build.gradle` see the project here: https://github.com/bquast/DiceRoller/blob/main/build.gradle. This is generated from Android Studio 2020.03 (Arch Linux) with minimum API 31. Note that this project runs fine (virtual device or Pixel 5 via adb) – Bastiaan Quast Dec 29 '21 at 09:38
  • 1
    [https://github.com/bquast/DiceRoller/blob/main/app/build.gradle](https://github.com/bquast/DiceRoller/blob/main/app/build.gradle) This is the right `build.gradle`. There are `build.gradle` in every module. You really have more than one `build.gradle`. It's real. – Ricky Mo Dec 29 '21 at 09:42
  • Thank you, that worked. From what I saw elsewhere there were two `build.gradle` files (shown?) in the root directory, with one having the `Module` label. I added a screenshot of the folder tree as you suggested. – Bastiaan Quast Dec 29 '21 at 09:58
  • Could you comment on if my intuition on the boilerplate generated is correct? – Bastiaan Quast Dec 29 '21 at 09:59
  • 1
    See the top left hand corner in your android studio file tree, there is a drop down box where you have selected "Project File", click it and change it to "Android" – Ricky Mo Dec 29 '21 at 10:01

2 Answers2

0

I believe all you need to do is set minSdkVersion like provided by this answer. The problems you may encounter are going to be massively different based on what you're going to be doing, but mostly it should be OK. Of course, more you lower minSdkVersion, the more problems you will encounter, but it should be mostly ok to at least version 23.

However, minSdkVersion is usually chosen at the beginning. You should probably set it to 21 (which covers 98% of devices) and start from there (industry standard is currently at 23, which covers 94.1% of devices).

You should not be afraid of the app not being forward compatible because changes are usually quite small and there are ways to support different versions with ease and minSdkVersion doesn't even affect forward compatibility. Also, supporting multiple versions does not make the final size of the app any larger. Code that is not called for a specific version gets deleted at build time, so you don't have to fear having the app not lean because of lower SDK support. There is some build time performance penalty, but for a simple app, this is not noticeable. And in case there's a blocker with SDK version being too low, it's easier to raise it than lower it.

Bottom line here I'd say is, that it is easy to set a low minSdkVersion from the beginning. The amount of possible issues you'll encounter when lowering that version are probably not worth it and are also harder to fix than supporting a low SDK version from the beginning. And it all massively depends on the actual code.

Primož Ivančič
  • 1,984
  • 1
  • 17
  • 29
0

Based on the comments by Ricky Mo.

The problem is that the default is the "Project View", which contains a build.gradle file that that defines the Kotlin version and the Android Studio Gradle plugin version.

The build.gradle file that defines the minSDK is found in the app folder (screenshot).

app build.gradle file

Alternatively you can switch from the "Project View" to Android. From the dropdown menu that open when you click "Project" (screenshot, highlighted).

View dropdown menu

Bastiaan Quast
  • 2,802
  • 1
  • 24
  • 50