Skip to content

Instantly share code, notes, and snippets.

@bizz84
Created November 18, 2024 09:17
Show Gist options
  • Save bizz84/605e2ca2088cb4acb7a076ca993f41cd to your computer and use it in GitHub Desktop.
Save bizz84/605e2ca2088cb4acb7a076ca993f41cd to your computer and use it in GitHub Desktop.
Script to update Gradle, Java and other Android project settings in a Flutter project
#!/bin/bash
# Update Gradle, Java and other Android project settings in a Flutter project
# See: https://gradle.org/releases/
DESIRED_GRADLE_VERSION="8.9"
# Build errors often show the required Java version
DESIRED_JAVA_VERSION="17"
# See: https://developer.android.com/ndk/downloads
DESIRED_NDK_VERSION="27.0.12077973"
# The minimum Android SDK version
DESIRED_MIN_SDK_VERSION="24"
# Google Play Stores requires a minimum target SDK version
DESIRED_TARGET_SDK="34"
# This shouldn't be too old, otherwise it won't compile with the DESIRED_GRADLE_VERSION set above
DESIRED_ANDROID_APPLICATION_VERSION="8.7.2"
# Exit if this is not a Flutter project
if [ ! -f "pubspec.yaml" ]; then
echo "This is not a Flutter project"
exit 1
fi
# Exit if the Android directory does not exist
if [ ! -d "android" ]; then
echo "The Android directory does not exist"
exit 1
fi
# Navigate to the Android directory
cd android
# Update Gradle version (if specified)
if [ -n "$DESIRED_GRADLE_VERSION" ]; then
sed -i '' "s/gradle-.*-all.zip/gradle-${DESIRED_GRADLE_VERSION}-all.zip/" gradle/wrapper/gradle-wrapper.properties
fi
# Update Java version (if specified)
if [ -n "$DESIRED_JAVA_VERSION" ]; then
sed -i '' "s/JavaVersion.VERSION_[0-9_]*/JavaVersion.VERSION_${DESIRED_JAVA_VERSION}/" app/build.gradle
fi
# Update NDK version (if specified)
if [ -n "$DESIRED_NDK_VERSION" ]; then
sed -i '' "s/ndkVersion = .*/ndkVersion = \"${DESIRED_NDK_VERSION}\"/" app/build.gradle
fi
# Update minSdk version (if specified)
if [ -n "$DESIRED_MIN_SDK_VERSION" ]; then
sed -i '' "s/minSdk = .*/minSdk = ${DESIRED_MIN_SDK_VERSION}/" app/build.gradle
fi
# Update targetSdk version (if specified)
if [ -n "$DESIRED_TARGET_SDK" ]; then
sed -i '' "s/targetSdk = .*/targetSdk = ${DESIRED_TARGET_SDK}/" app/build.gradle
fi
# Update com.android.application version in settings.gradle (if specified)
if [ -n "$DESIRED_ANDROID_APPLICATION_VERSION" ]; then
sed -i '' "s/id \"com.android.application\" version \".*\" apply false/id \"com.android.application\" version \"${DESIRED_ANDROID_APPLICATION_VERSION}\" apply false/" settings.gradle
fi
echo "Android project updated. Run 'git diff' to see the changes or 'git reset --hard' to discard them."
@bizz84
Copy link
Author

bizz84 commented Nov 18, 2024

How to use:

  • Download the script and add it to a folder in your system PATH
  • Give it execution access (chmod +x update-android-project.sh)
  • Tweak the versions if needed
  • Run it from the root of your Flutter project

@vbuberen
Copy link

Looks good! But I would suggest to either change DESIRED_ANDROID_APPLICATION_VERSION name or add into a comment above information that this is Android Gradle Plugin (AGP) version. Also, instead of vague description for this variable This shouldn't be too old.. I would add a link to the official table of compatibility between Gradle version and Android Gradle Plugin versions: https://developer.android.com/build/releases/gradle-plugin

@alefwd
Copy link

alefwd commented Nov 19, 2024

about:

echo "Android project updated. Run 'git diff' to see the changes or 'git reset --hard' to discard them."

I would prefer 'git checkoout -- .' to clean up the temporary changes, instead of 'git reset --hard' which could cause problems by accident

@Tobias1967
Copy link

Hi Andrea!

Thank you for your helpful courses! I have an app almost finished, but I have massive problems with Gradle and have been trying to solve the problems for 4 weeks again and again via ChatGPT or Gemini. But it all goes in circles and I can't find a solution. No matter which solution I try, there are always problems with the apk. Even your script didn't help me.
In the Ios emulator I can run the app. That works. But it's all only finished when I can make the apk. Can you help me or is there anyone who could help me? Best regards Tobias

@SawyerPowell
Copy link

Thanks for sharing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment