Skip to content

Instantly share code, notes, and snippets.

@mrabro
Forked from Thong-Tran/README.md
Created September 27, 2022 07:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrabro/6c31a9005a0293b462b5ad3e2096528b to your computer and use it in GitHub Desktop.
Save mrabro/6c31a9005a0293b462b5ad3e2096528b to your computer and use it in GitHub Desktop.
Set up auto build and distribute flutter app on Appcenter

Auto deploying flutter app via Appcenter

Configure project

  • Active AndroidX if it isn’t already.
  • Create and checkout to new branch (appcenter currently only listen trigger from commit code on branch)
  • Setup android:
    • Comment those lines in file android/.gitignore
    # gradle-wrapper.jar
    /.gradle
    /captures/
    # /gradlew
    # /gradlew.bat
    /local.properties
    GeneratedPluginRegistrant.java
    
    • Create KeyStore in android/app
    • Add config to android/app/build.gradle
    def flutterVersionCode = localProperties.getProperty('flutter.versionCode')
    if (flutterVersionCode == null) {
        flutterVersionCode = '1'
    }
    
    // Get VersionCode from AppCenter if auto build
    def appCenterVersionCode = System.getenv('APPCENTER_BUILD_ID')
    if (appCenterVersionCode != null) {
        flutterVersionCode = appCenterVersionCode
    }
    • Modify android\gradlew to skip appcenter's gradle because we already call it in android/appcenter-post-clone.sh. Add this code below or custom gradlew by yourself
    #!/usr/bin/env bash
    
    ##############################################################################
    ##
    ##  Gradle start up script for UN*X
    ##
    ##############################################################################
    
    if [[ "$@" != *"-Ptarget"* ]]; then
      echo "Not found target in arguments when call gradlew, force skip gradle."
      exit 0
    fi
    • Save file android-appcenter-post-clone.sh to android/app/appcenter-post-clone.sh
  • Setup iOS:
  • Commit these config

AppCenter Setup

  • Create 2 app and change any other details as needed
  • Connect to app response

References

#!/usr/bin/env bash
# Place this script in project/android/app/appcenter-post-clone.sh
# Original file stored at
# https://github.com/microsoft/appcenter/blob/master/sample-build-scripts/flutter/android-build/appcenter-post-clone.sh
cd ..
# fail if any command fails
set -e
# debug log
set -x
cd ..
git clone -b stable https://github.com/flutter/flutter.git
export PATH=`pwd`/flutter/bin:$PATH
flutter channel stable
flutter doctor
echo "Installed flutter to `pwd`/flutter"
# build APK
flutter -v build apk --release --target-platform android-arm64 --target $APP_TARGET
# copy the APK where AppCenter will find it
mkdir -p android/app/build/outputs/apk/; mv build/app/outputs/apk/release/app-release.apk $_
# build bundle (AAB)
if [ "$BUILD_AAB" = "true" ] ; then
flutter -v build appbundle --target $APP_TARGET --target-platform android-arm,android-arm64,android-x64
# copy the AAB where AppCenter will find it
mkdir -p android/app/build/outputs/bundle/; mv build/app/outputs/bundle/release/app-release.aab $_
fi
#!/usr/bin/env bash
# Place this script in project/ios/appcenter-post-clone.sh
# Original file stored at
# https://github.com/microsoft/appcenter/blob/master/sample-build-scripts/flutter/ios-build/appcenter-post-clone.sh
# fail if any command fails
set -e
# debug log
set -x
cd ..
git clone -b stable https://github.com/flutter/flutter.git
export PATH=`pwd`/flutter/bin:$PATH
flutter channel stable
flutter doctor
echo "Installed flutter to `pwd`/flutter"
flutter -v pub get
function generate_new_config() {
new_str=""
while IFS=$'\n' read -r line; do
if [[ "$line" == *"FLUTTER_TARGET"* ]]; then
IFS='=' read -r key val <<< "$line"
new_str="$new_str"$'\n'"$key"="$APP_TARGET""$2"
else
if [[ "$line" == *"FLUTTER_FRAMEWORK_DIR"* ]]; then
IFS='=' read -r key val <<< "$line"
new_str="$new_str"$'\n'"$key"="`pwd`/flutter/bin/cache/artifacts/engine/ios-release""$2"
else
if [ "$new_str" == '' ] ; then
new_str="$line"
else
new_str="$new_str"$'\n'"$line"
fi
fi
fi
done < "$1"
echo "$new_str" > "$1"
}
eval generate_new_config ios/Flutter/flutter_export_environment.sh '\"'
eval generate_new_config ios/Flutter/Generated.xcconfig
# Install ios tools
flutter precache --ios --no-android --no-universal
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment