-
Star
(118)
You must be signed in to star a gist -
Fork
(41)
You must be signed in to fork a gist
-
-
Save bizz84/605e2ca2088cb4acb7a076ca993f41cd to your computer and use it in GitHub Desktop.
#!/bin/bash | |
# Update Gradle, Java and other Android project settings in a Flutter project | |
# Works with both .gradle and .gradle.kts build files | |
# See: https://gradle.org/releases/ | |
# See: https://developer.android.com/build/releases/gradle-plugin#compatibility | |
DESIRED_GRADLE_VERSION="8.11.1" | |
# 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" | |
# Google Play Stores requires a minimum target SDK version | |
DESIRED_TARGET_SDK="35" | |
# The minimum Android SDK version | |
DESIRED_MIN_SDK_VERSION="24" | |
# This should be compatible with the DESIRED_GRADLE_VERSION set above | |
# See: https://developer.android.com/build/releases/gradle-plugin#updating-gradle | |
DESIRED_ANDROID_APPLICATION_VERSION="8.9.1" | |
# 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 | |
# Detect if using Groovy or Kotlin build gradle file | |
if [ -f "app/build.gradle.kts" ]; then | |
BUILD_GRADLE="app/build.gradle.kts" | |
else | |
BUILD_GRADLE="app/build.gradle" | |
fi | |
# Update Java version (if specified) | |
if [ -n "$DESIRED_JAVA_VERSION" ]; then | |
sed -i '' "s/JavaVersion.VERSION_[0-9_]*/JavaVersion.VERSION_${DESIRED_JAVA_VERSION}/" "$BUILD_GRADLE" | |
fi | |
# Update NDK version (if specified) | |
if [ -n "$DESIRED_NDK_VERSION" ]; then | |
sed -i '' "s/ndkVersion = .*/ndkVersion = \"${DESIRED_NDK_VERSION}\"/" "$BUILD_GRADLE" | |
fi | |
# Update minSdk version (if specified) | |
if [ -n "$DESIRED_MIN_SDK_VERSION" ]; then | |
sed -i '' "s/minSdk = .*/minSdk = ${DESIRED_MIN_SDK_VERSION}/" "$BUILD_GRADLE" | |
fi | |
# Update targetSdk version (if specified) | |
if [ -n "$DESIRED_TARGET_SDK" ]; then | |
sed -i '' "s/targetSdk = .*/targetSdk = ${DESIRED_TARGET_SDK}/" "$BUILD_GRADLE" | |
fi | |
# Update com.android.application version in settings.gradle (if specified) | |
if [ -n "$DESIRED_ANDROID_APPLICATION_VERSION" ]; then | |
# Detect if using Groovy or Kotlin build gradle file | |
if [ -f "settings.gradle.kts" ]; then | |
sed -i '' "s/id(\"com.android.application\") version \".*\" apply false/id(\"com.android.application\") version \"${DESIRED_ANDROID_APPLICATION_VERSION}\" apply false/" settings.gradle.kts | |
else | |
sed -i '' "s/id \"com.android.application\" version \".*\" apply false/id \"com.android.application\" version \"${DESIRED_ANDROID_APPLICATION_VERSION}\" apply false/" settings.gradle | |
fi | |
fi | |
echo "Android project updated. Run 'git diff android' to see the changes." |
Thank you for sharing this script!
thanks for sharing
Thank
On Windows I need to remove all '' after -i
if [ -n "$DESIRED_GRADLE_VERSION" ]; then
sed -i "s/gradle-.*-all.zip/gradle-${DESIRED_GRADLE_VERSION}-all.zip/" gradle/wrapper/gradle-wrapper.properties
fi
I've just updated the script to support both .gradle
and .gradle.kts
files.
Hello everyone any one have facing this type of issues in windows .
sed: can't read s/gradle-.-all.zip/gradle-8.11.1-all.zip/: No such file or directory
sed: can't read s/JavaVersion.VERSION_[0-9_]/JavaVersion.VERSION_17/: No such file or directory
sed: can't read s/ndkVersion = ./ndkVersion = "27.0.12077973"/: No such file or directory
sed: can't read s/minSdk = ./minSdk = 24/: No such file or directory
sed: can't read s/targetSdk = ./targetSdk = 35/: No such file or directory
sed: can't read s/id "com.android.application" version "." apply false/id "com.android.application" version "8.9.1" apply false/: No such file or directory
Android project updated. Run 'git diff android' to see the changes
I noticed a potential issue with this script.
if [ -n "$DESIRED_GRADLE_VERSION" ]; then
sed -i '' "s/gradle-.*-all.zip/gradle-${DESIRED_GRADLE_VERSION}-all.zip/" gradle/wrapper/gradle-wrapper.properties
fi
Notice this replacement expression is expecting *-all.zip
to be located in the distributionUrl property in the gradle-wrapper.properties file. This might work fine because flutter create
defaults to using the *-all.zip
gradle distribution. However, if you follow the recommended approach for updating the version of the gradle wrapper with:
./gradlew wrapper --gradle-version=<updated_gradle_version>
Then you might find that your distributionUrl no longer contains *-all.zip
but instead has *-bin.zip
.
*-all.zip
is useful if you require all the source code and documentation in your project but its heavy. Usually (when building a flutter app) you don't need all the extra weight that comes with the all distribution, especially when you configure your build on a CI/CD server that needs to download gradle to build. You will probably benefit from using the *-bin.zip
instead, which has a smaller footprint.
Anyway, I would recommend this script be modifed to use ./gradlew wrapper --gradle-version="${DESIRED_GRADLE_VERSION}"
instead of the existing sed command. The advantage of doing so is that gradlew will also update the gradlew
and gradlew.bat
files (which are .gitignore'd) to make sure they are current for the specified version of gradle.
Anyway, thank you for your courses and content Andrea.
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