此登錄庫釘選版本所收錄的最新上游作者日期。

gplay-gradle-build

已同步 v1.1.0 授權: MIT 來源: tamtom/gplay-cli-skills/skills/gplay-gradle-build 提交: 10301b2 收錄的最新變更:

Build, sign, and package Android apps with Gradle before uploading to Google Play. Use when asked to create an APK or AAB, configure signing, or set up build pipelines.

摘要

用途
它說明如何透過 Gradle 建置 Android 發行版本,涵蓋簽署、版本管理、程式碼最佳化及 Play Console 準備工作。
使用時機
當您需要設定可重複執行的 Android 發行建置、選擇 AAB 或 APK 輸出,或排查簽署與版本問題時使用。
輸出結果
完成設定的發行建置會產生 AAB 或 APK;啟用 R8 或 ProGuard 時,也會產生供當機符號還原使用的 mapping 檔案。
上游變更(1 筆,最新收錄 2026-02-05)
acb31df Initial commit: Google Play CLI Skills repository 新增一份指南,說明如何使用 Gradle 建置並簽署 Android APK 與 AAB、管理版本與 R8、設定建置變體和 CI/CD,以及排查 Google Play 建置問題。
npx skills add "lettucebo/Skills#v2.2.0@gplay-gradle-build" --full-depth

⚠ v2.2.0 發布後即可使用

Gradle Build for Google Play

Use this skill when you need to build an Android app before uploading to Play Store.

Build Types

App Bundle (AAB) - Recommended

App Bundles are required for new apps on Google Play:

./gradlew bundleRelease

Output: app/build/outputs/bundle/release/app-release.aab

APK - Legacy

For apps not using bundles:

./gradlew assembleRelease

Output: app/build/outputs/apk/release/app-release.apk

Signing Configuration

build.gradle (app level)

android {
    signingConfigs {
        release {
            storeFile file(System.getenv("KEYSTORE_FILE") ?: "release.keystore")
            storePassword System.getenv("KEYSTORE_PASSWORD")
            keyAlias System.getenv("KEY_ALIAS")
            keyPassword System.getenv("KEY_PASSWORD")
        }
    }

    buildTypes {
        release {
            signingConfig signingConfigs.release
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

Environment Variables

export KEYSTORE_FILE=/path/to/release.keystore
export KEYSTORE_PASSWORD=your_keystore_password
export KEY_ALIAS=your_key_alias
export KEY_PASSWORD=your_key_password

./gradlew bundleRelease

Version Management

gradle.properties

VERSION_NAME=1.2.3
VERSION_CODE=123

build.gradle

android {
    defaultConfig {
        versionCode project.property('VERSION_CODE').toInteger()
        versionName project.property('VERSION_NAME')
    }
}

Increment version

# Read current version
CURRENT_VERSION=$(grep VERSION_CODE gradle.properties | cut -d'=' -f2)
NEW_VERSION=$((CURRENT_VERSION + 1))

# Update gradle.properties
sed -i "" "s/VERSION_CODE=$CURRENT_VERSION/VERSION_CODE=$NEW_VERSION/" gradle.properties

# Build
./gradlew bundleRelease

ProGuard/R8 Configuration

Enable R8 (default in AGP 3.4+)

buildTypes {
    release {
        minifyEnabled true
        shrinkResources true
        proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
    }
}

Generate mapping file

Mapping file location: app/build/outputs/mapping/release/mapping.txt

Upload to Play Console for crash symbolication:

gplay deobfuscation upload \
  --package com.example.app \
  --edit $EDIT_ID \
  --apk-version 123 \
  --type proguard \
  --file app/build/outputs/mapping/release/mapping.txt

Build Variants

Product Flavors

android {
    flavorDimensions "version"
    productFlavors {
        free {
            dimension "version"
            applicationIdSuffix ".free"
        }
        paid {
            dimension "version"
        }
    }
}

Build specific flavor

./gradlew bundleFreeRelease
./gradlew bundlePaidRelease

CI/CD Build

GitHub Actions Example

- name: Build Release AAB
  run: |
    echo "${{ secrets.KEYSTORE_BASE64 }}" | base64 -d > release.keystore
    export KEYSTORE_FILE=release.keystore
    export KEYSTORE_PASSWORD="${{ secrets.KEYSTORE_PASSWORD }}"
    export KEY_ALIAS="${{ secrets.KEY_ALIAS }}"
    export KEY_PASSWORD="${{ secrets.KEY_PASSWORD }}"
    ./gradlew bundleRelease

- name: Upload to Play Store
  env:
    GPLAY_SERVICE_ACCOUNT: ${{ secrets.PLAY_SERVICE_ACCOUNT }}
  run: |
    gplay release \
      --package com.example.app \
      --track internal \
      --bundle app/build/outputs/bundle/release/app-release.aab

Common Build Tasks

Clean build

./gradlew clean bundleRelease

Build with specific SDK versions

./gradlew bundleRelease -PminSdkVersion=21 -PtargetSdkVersion=34

Build without tests

./gradlew bundleRelease -x test -x lint

Build multiple variants

./gradlew bundle  # Builds all variants

Troubleshooting

Build fails with signing errors

# Verify keystore
keytool -list -v -keystore release.keystore

Check AAB contents

bundletool build-apks \
  --bundle=app-release.aab \
  --output=output.apks \
  --mode=universal

unzip output.apks

Verify version codes

./gradlew tasks --all | grep Version

Best Practices

  1. Never commit keystores - Store in secure location
  2. Use environment variables for credentials
  3. Enable ProGuard/R8 for release builds
  4. Upload mapping files after each release
  5. Increment version code for every build
  6. Test release builds before uploading
  7. Use App Bundles (AAB) instead of APKs when possible

歷程

v1.1.0 baseline-verified 10301b2
v1.1.0 license-refresh 10301b2