BACK

Feb 16, 2025

0 m
0 words

Script to Update the Android Project Settings

Tired of dealing with Gradle and other build errors on Android? 🤮

Me too! So, I built a script to fix it all at once:

  • Gradle version

  • Java version

  • NDK version

  • Min SDK

  • Target SDK

The result? Faster updates and fewer headaches.

@echo off
setlocal enabledelayedexpansion

REM ----------------------------------------
REM Configuration - update these as needed
set DESIRED_GRADLE_VERSION=8.11.1
set DESIRED_JAVA_VERSION=17
set DESIRED_NDK_VERSION=27.0.12077973
set DESIRED_TARGET_SDK=35
set DESIRED_MIN_SDK_VERSION=24
set DESIRED_ANDROID_APPLICATION_VERSION=8.9.1
REM ----------------------------------------

REM Check if pubspec.yaml exists in current directory (Flutter project check)
if not exist "pubspec.yaml" (
    echo This is not a Flutter project (pubspec.yaml missing).
    exit /b 1
)

REM Check if android folder exists
if not exist android (
    echo Android directory not found.
    exit /b 1
)

cd android

REM Backup important files
echo Backing up important files...
copy gradle\wrapper\gradle-wrapper.properties gradle\wrapper\gradle-wrapper.properties.bak >nul
if exist app\build.gradle (
    copy app\build.gradle app\build.gradle.bak >nul
)
if exist app\build.gradle.kts (
    copy app\build.gradle.kts app\build.gradle.kts.bak >nul
)
if exist settings.gradle (
    copy settings.gradle settings.gradle.bak >nul
)
if exist settings.gradle.kts (
    copy settings.gradle.kts settings.gradle.kts.bak >nul
)

REM Update Gradle version using Gradle wrapper command
echo Updating Gradle wrapper version to %DESIRED_GRADLE_VERSION%...
call gradlew wrapper --gradle-version=%DESIRED_GRADLE_VERSION% --distribution-type=all

REM Determine build file type (Groovy or Kotlin)
set BUILD_GRADLE_FILE=
if exist app\build.gradle.kts (
    set BUILD_GRADLE_FILE=app\build.gradle.kts
) else if exist app\build.gradle (
    set BUILD_GRADLE_FILE=app\build.gradle
) else (
    echo No app build.gradle(.kts) file found.
    exit /b 1
)

REM Function to replace line in a file (Windows supports PowerShell, use that for safer regex replace)
REM Update Java version, NDK version, minSdk and targetSdk in build.gradle(.kts)
echo Updating project settings in %BUILD_GRADLE_FILE%...

powershell -Command ^
"(gc %BUILD_GRADLE_FILE%) -replace 'JavaVersion\.VERSION_[0-9_]+', 'JavaVersion.VERSION_%DESIRED_JAVA_VERSION%' | ^
Set-Content %BUILD_GRADLE_FILE%"

powershell -Command ^
"(gc %BUILD_GRADLE_FILE%) -replace 'ndkVersion\s*=\s*\"[^\"]+\"', 'ndkVersion = \"%DESIRED_NDK_VERSION%\"' | ^
Set-Content %BUILD_GRADLE_FILE%"

powershell -Command ^
"(gc %BUILD_GRADLE_FILE%) -replace 'minSdk\s*=\s*[0-9]+', 'minSdk = %DESIRED_MIN_SDK_VERSION%' | ^
Set-Content %BUILD_GRADLE_FILE%"

powershell -Command ^
"(gc %BUILD_GRADLE_FILE%) -replace 'targetSdk\s*=\s*[0-9]+', 'targetSdk = %DESIRED_TARGET_SDK%' | ^
Set-Content %BUILD_GRADLE_FILE%"

REM Update Android Gradle Plugin version in settings.gradle or settings.gradle.kts
if exist settings.gradle.kts (
    powershell -Command ^
    "(gc settings.gradle.kts) -replace 'id\\(\"com.android.application\"\\) version \\\"[\\d\\.]+\\\" apply false',^
    'id(\"com.android.application\") version \"%DESIRED_ANDROID_APPLICATION_VERSION%\" apply false' | Set-Content settings.gradle.kts"
) else if exist settings.gradle (
    powershell -Command ^
    "(gc settings.gradle) -replace 'id \"com.android.application\" version \"[\\d\\.]+\" apply false',^
    'id \"com.android.application\" version \"%DESIRED_ANDROID_APPLICATION_VERSION%\" apply false' | Set-Content settings.gradle"
)

echo Android project updated successfully.

echo Remember to run:
echo  flutter clean
echo  flutter pub get
echo To ensure project builds correctly after updates.

endlocal


To use the Windows batch script I provided for updating Gradle, Java, and Android project settings in your Flutter project, follow these steps:

  1. Save the Script: Copy the entire script into a text file and save it with a .bat extension, for example, update_flutter_android.bat.

  2. Place Script in Project Root: Ensure you place this batch script in the root folder of your Flutter project — the same directory containing the pubspec.yaml file.

  3. Open Command Prompt or PowerShell:

    • Press Win + R, type cmd or powershell, and press Enter to open a terminal.

    • For administrative permissions (recommended for updates), right-click Command Prompt or PowerShell and choose "Run as administrator".

  4. Run the Script:

    • Navigate to your Flutter project root in the terminal, e.g., cd C:\path\to\your\flutter_project.

    • Execute the script by typing its filename, e.g., update_flutter_android.bat, and press Enter.

  5. Script Actions:

    • It verifies that it is running inside a Flutter project by checking for pubspec.yaml.

    • It checks for the presence of the android directory.

    • Backups of critical files like gradle-wrapper.properties, build.gradle, settings.gradle are created.

    • Gradle wrapper is updated safely via the Gradle command.

    • The build files (build.gradle or build.gradle.kts) are updated with the desired Java version, NDK version, minSdk, and targetSdk.

    • The Android Gradle Plugin version is updated in settings.gradle or settings.gradle.kts.

    • On completion, it advises running flutter clean and flutter pub get to finalize changes.

Design & Code by Suman · Powered by Framer

© 2025 Core

Kathmandu, Nepal

20

°C

Design & Code by Suman · Powered by Framer

© 2025 Core

Kathmandu, Nepal

20

°C

Create a free website with Framer, the website builder loved by startups, designers and agencies.