Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學

Ubuntu 全命令行構建 Android 編譯環境:JDK、SDK、NDK、Gradle 與 APK 下載實戰

編輯:Linux 系統常見問題解答

無桌面環境的 Ubuntu Server(或 CI/CD Runner)上構建 Android 專案,需完整配置 JDK、SDK 命令列工具、NDK、Gradle 並驗證編譯流程。本文提供從零開始的標準化部署腳本、環境變數管理、授權接受自動化與 Google Play APK 下載技巧。

一、系統前置

# Ubuntu 22.04 LTS
apt update && apt install -y \
  openjdk-17-jdk-headless \
  wget unzip git curl lib32z1 lib32ncurses6 lib32stdc++6 \
  python3 python3-pip \
  qemu-kvm libvirt-daemon-system bridge-utils  # 模擬器可選

# 驗證
java -version
# openjdk version "17.0.8" 2023-07-18

二、Android SDK 命令列工具部署

# 1. 建立目錄
export ANDROID_HOME=/opt/android-sdk
export CMDLINE_TOOLS=$ANDROID_HOME/cmdline-tools
mkdir -p $CMDLINE_TOOLS

# 2. 下載最新 commandlinetools(Linux)
cd /tmp
wget -q https://dl.google.com/android/repository/commandlinetools-linux-11076708_latest.zip
unzip -q commandlinetools-linux-11076708_latest.zip -d $CMDLINE_TOOLS
mv $CMDLINE_TOOLS/cmdline-tools $CMDLINE_TOOLS/latest

# 3. 環境變數持久化
cat > /etc/profile.d/android.sh <<'EOF'
export ANDROID_HOME=/opt/android-sdk
export CMDLINE_TOOLS=$ANDROID_HOME/cmdline-tools/latest
export PATH=$PATH:$CMDLINE_TOOLS/bin:$ANDROID_HOME/platform-tools:$ANDROID_HOME/emulator
EOF
source /etc/profile.d/android.sh

# 4. 授權自動接受(關鍵:無互動)
yes | $CMDLINE_TOOLS/bin/sdkmanager --licenses

# 5. 安裝核心套件
$CMDLINE_TOOLS/bin/sdkmanager --install \
  "platform-tools" \
  "platforms;android-34" \
  "build-tools;34.0.0" \
  "ndk;26.1.10909125" \
  "cmake;3.22.1" \
  "sources;android-34"

# 6. 驗證
sdkmanager --list_installed | head -20
adb version
# Android Debug Bridge version 1.0.41

三、Gradle 獨立安裝(不依賴 Wrapper)

cd /opt
wget -q https://services.gradle.org/distributions/gradle-8.5-bin.zip
unzip -q gradle-8.5-bin.zip
ln -sf gradle-8.5 gradle
cat > /etc/profile.d/gradle.sh <<'EOF'
export GRADLE_HOME=/opt/gradle
export PATH=$PATH:$GRADLE_HOME/bin
EOF
source /etc/profile.d/gradle.sh
gradle -v
# Gradle 8.5

四、專案編譯驗證

# 1. Clone 測試專案
git clone https://github.com/android/architecture-samples.git
cd architecture-samples/todoapp

# 2. 本地屬性(指定 SDK 路徑)
cat > local.properties <

五、Google Play APK 下載(無桌面環境)

官方無公開 API,第三方工具 googleplay-api(Python)可模擬登入下載。

# 1. 安裝
pip3 install googleplay-api

# 2. 配置憑證(需 Google 帳號、裝置 ID)
cat > ~/.config/googleplay.json <<'EOF'
{
  "email": "your@gmail.com",
  "password": "app-specific-password",  # 兩步驟驗證需應用程式密碼
  "device_id": "0123456789abcdef",      # 從已登入裝置獲取
  "gms_version": "23.45.67"
}
EOF

# 3. 搜尋並下載
cd /tmp
python3 -c "
from googleplay import GooglePlayAPI
api = GooglePlayAPI('en_US', 'TW')
api.login_from_config()
result = api.search('com.twitter.android', 1)
app = result[0]
docid = app['docid']
offer = api.purchase(docid)
delivery = api.delivery(docid, offer[0]['offerType'], offer[0]['docId'])
api.download(delivery, 'twitter.apk')
print('Downloaded:', delivery['file']['size'])
"

# 4. 驗證
aapt dump badging twitter.apk | grep package
# package: name='com.twitter.android' versionCode='10.2.3' versionName='10.2.3'

六、CI/CD 整合建議

階段工具/指令快取策略
依賴安裝sdkmanager --install ...快取 $ANDROID_HOME 整個目錄
Gradle 依賴./gradlew --offline assembleDebug快取 ~/.gradle/caches
編譯./gradlew assembleRelease輸出 APK 至 Artifacts
測試./gradlew test connectedAndroidTest模擬器快照快取
簽名apksigner sign --ks release.keystoreKeystore 存 Vault

七、常見坑點

  • ❌ sdkmanager --licenses 卡住 → yes | sdkmanager --licenses 或預先 mkdir -p $ANDROID_HOME/licenses && echo "24333f8a63b6825ea9c5514f83c2829b004d1fee" > $ANDROID_HOME/licenses/android-sdk-license
  • ❌ aapt 找不到 → 安裝 build-tools;34.0.0 並確保 $ANDROID_HOME/build-tools/34.0.0 在 PATH
  • ❌ NDK 版本不匹配 → local.properties 加入 ndk.dir=$ANDROID_HOME/ndk/26.1.10909125
  • ❌ 記憶體不足 → GRADLE_OPTS="-Xmx4g -XX:MaxMetaspaceSize=1g"

結語: 全命令行 Android 環境 = JDK + cmdline-tools + Gradle + 授權自動化。把上述步驟封裝成 Ansible Role / Dockerfile / GitHub Actions,即可在任何乾淨 Ubuntu 上 10 分鐘拉起可編譯、可測試、可打包的 Android CI/CD 基礎設施。

Copyright © Windows教程網 All Rights Reserved