Skip to content

Instantly share code, notes, and snippets.

@hypernova7
Created October 12, 2021 11:28
Show Gist options
  • Save hypernova7/8c38104b9308dcd2af8a9cf5d0a1445a to your computer and use it in GitHub Desktop.
Save hypernova7/8c38104b9308dcd2af8a9cf5d0a1445a to your computer and use it in GitHub Desktop.
Build and sign Flutter app with Github Actions
// android/app/build.gradle
def localProperties = new Properties()
def localPropertiesFile = rootProject.file('local.properties')
if (localPropertiesFile.exists()) {
localPropertiesFile.withReader('UTF-8') { reader ->
localProperties.load(reader)
}
}
def flutterRoot = localProperties.getProperty('flutter.sdk')
if (flutterRoot == null) {
throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.")
}
def flutterVersionCode = localProperties.getProperty('flutter.versionCode')
if (flutterVersionCode == null) {
flutterVersionCode = '1'
}
def flutterVersionName = localProperties.getProperty('flutter.versionName')
if (flutterVersionName == null) {
flutterVersionName = '1.0'
}
def keystoreProperties = new Properties()
def keystorePropertiesFile = rootProject.file('key.properties')
if (keystorePropertiesFile.exists()) {
keystoreProperties.load(new FileInputStream(keystorePropertiesFile))
}
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
android {
compileSdkVersion 30
// compileOptions {
// sourceCompatibility JavaVersion.VERSION_1_8
// targetCompatibility JavaVersion.VERSION_1_8
// }
// kotlinOptions {
// jvmTarget = '1.8'
// }
sourceSets {
main.java.srcDirs += 'src/main/kotlin'
}
defaultConfig {
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
applicationId "com.myflutter.app"
minSdkVersion 21
targetSdkVersion 30
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
}
lintOptions {
checkReleaseBuilds false
}
signingConfigs {
if (System.getenv("ANDROID_KEYSTORE_PATH")) {
release {
storeFile file(System.getenv("ANDROID_KEYSTORE_PATH"))
keyAlias System.getenv("ANDROID_KEYSTORE_ALIAS")
keyPassword System.getenv("ANDROID_KEYSTORE_PRIVATE_KEY_PASSWORD")
storePassword System.getenv("ANDROID_KEYSTORE_PASSWORD")
}
} else {
release {
keyAlias keystoreProperties['keyAlias']
keyPassword keystoreProperties['keyPassword']
storeFile keystoreProperties['storeFile'] ? file(keystoreProperties['storeFile']) : null
storePassword keystoreProperties['storePassword']
}
}
}
buildTypes {
release {
signingConfig signingConfigs.release
minifyEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
debug {
signingConfig signingConfigs.debug
shrinkResources false
}
}
}
flutter {
source '../..'
}
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
}
name: Flutter CI
# This workflow is triggered on pushes to the repository.
on:
push:
tags:
- v[0-9]+.[0-9]+.[0-9]+
# on: push # Default will running for every branch.
jobs:
build:
# This job will run on ubuntu virtual machine
runs-on: ubuntu-latest
env:
DNAME: ${{ secrets.DNAME }} # example: "CN=com.myflutter.app, OU=MyFlutter, O=App, L=United States, ST=New York, C=US"
ANDROID_KEYSTORE_PATH: ${{ secrets.ANDROID_KEYSTORE_PATH }} # root directory of jks file
ANDROID_KEYSTORE_ALIAS: ${{ secrets.ANDROID_KEYTORE_ALIAS }}
ANDROID_KEYSTORE_PRIVATE_KEY_PASSWORD: ${{ secrets.ANDROID_KEYSTORE_PRIVATE_KEY_PASSWORD }}
ANDROID_KEYSTORE_PASSWORD: ${{ secrets.ANDROID_KEYSTORE_PASSWORD }}
steps:
# Setup Java environment in order to build the Android app.
- uses: actions/checkout@v1
- uses: actions/setup-java@v1
with:
java-version: '12.x'
# Setup the flutter environment.
- uses: subosito/flutter-action@v1
with:
channel: 'stable' # 'dev', 'alpha', default to: 'stable'
# flutter-version: '1.12.x' # you can also specify exact version of flutter
- name: Build and sign Flutter app
run: |
# Generate signature
keytool -genkey -noprompt \
-alias $ANDROID_KEYSTORE_ALIAS \
-dname $DNAME \
-keystore $ANDROID_KEYSTORE_PATH \
-storepass $ANDROID_KEYSTORE_PRIVATE_KEY_PASSWORD \
-keypass $ANDROID_KEYSTORE_PASSWORD \
-keyalg RSA \
-keysize 2048 \
-validity 365 # days
# Get flutter dependencies.
flutter pub get
# Check for any formatting issues in the code.
flutter format --set-exit-if-changed .
# Statically analyze the Dart code for any errors.
flutter analyze .
# Run widget tests for our flutter project.
flutter test
# Build apk.
flutter build apk
# Upload generated apk to the artifacts.
- uses: actions/upload-artifact@v1
with:
name: release-apk
path: build/app/outputs/apk/release/app-release.apk
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment