Generate APK

Create Android APK files for your Flutter app

📦 What is an APK?

APK (Android Package Kit) is the file format used to distribute and install Android apps. Flutter generates APK files that can be installed directly on Android devices or distributed through various channels.


# Generate release APK
flutter build apk --release

# Generate debug APK
flutter build apk --debug
                                    

Output:

✓ Built build/app/outputs/flutter-apk/app-release.apk

APK Build Types

🔧

Debug APK

Debug APKs are larger and include debugging information. They're used during development for testing on real devices without optimization or signing requirements.

flutter build apk --debug
🚀

Release APK

Release APKs are optimized, smaller, and require signing. They're production-ready builds for distribution to end users through various channels or app stores.

flutter build apk --release
📊

Profile APK

Profile APKs are used for performance testing and profiling. They include some debugging capabilities while maintaining near-release performance characteristics for accurate testing.

flutter build apk --profile
🎯

Split APK

Split APKs are separate files for different CPU architectures (arm64, armeabi, x86). This reduces download size as users only get the APK for their device.

flutter build apk --split-per-abi

🔹 Basic APK Generation

Simple steps to generate your first APK:

# Step 1: Clean previous builds
flutter clean

# Step 2: Get dependencies
flutter pub get

# Step 3: Build release APK
flutter build apk --release

# APK location
# build/app/outputs/flutter-apk/app-release.apk

Build Output:

Running Gradle task 'assembleRelease'...
✓ Built build/app/outputs/flutter-apk/app-release.apk (18.5MB)

🔹 APK Signing Configuration

Configure signing for release APKs in android/app/build.gradle:

android {
    ...
    signingConfigs {
        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
        }
    }
}

🔹 Create Keystore File

Generate a keystore for signing your APK:

# Generate keystore
keytool -genkey -v -keystore ~/my-release-key.jks \
-keyalg RSA -keysize 2048 -validity 10000 \
-alias my-key-alias

# You'll be prompted for:
# - Keystore password
# - Key password
# - Name, organization, etc.

⚠️ Important:

  • Keep your keystore file secure and backed up
  • Never commit keystore to version control
  • Store passwords securely
  • You cannot recover a lost keystore

🔹 Key Properties File

Create android/key.properties to store signing information:

storePassword=your_store_password
keyPassword=your_key_password
keyAlias=my-key-alias
storeFile=/path/to/my-release-key.jks

Add to .gitignore:

# Android signing
android/key.properties
*.jks
*.keystore

🔹 Split APK by Architecture

Generate separate APKs for different CPU architectures:

# Build split APKs
flutter build apk --split-per-abi --release

# Generates three APKs:
# app-armeabi-v7a-release.apk (32-bit ARM)
# app-arm64-v8a-release.apk (64-bit ARM)
# app-x86_64-release.apk (64-bit Intel)

Size Comparison:

Universal APK: ~25MB

Split APK (each): ~18MB

Savings: ~28% per device

🔹 APK Optimization

Reduce APK size and improve security:

# Obfuscate code (makes reverse engineering harder)
flutter build apk --obfuscate --split-debug-info=./debug-info

# Combine optimizations
flutter build apk --release \
  --split-per-abi \
  --obfuscate \
  --split-debug-info=./debug-info

# Analyze APK size
flutter build apk --analyze-size

🔹 Testing Your APK

Install and test the generated APK:

# Install APK on connected device
adb install build/app/outputs/flutter-apk/app-release.apk

# Install and launch
adb install -r app-release.apk
adb shell am start -n com.example.myapp/.MainActivity

# Uninstall
adb uninstall com.example.myapp

Testing Checklist:

  • Test on multiple Android versions
  • Verify all features work correctly
  • Check app permissions
  • Test offline functionality
  • Verify app signing

🧠 Test Your Knowledge

What command generates a release APK?