build-aar-windows.bat 3.63 KB
@echo off
setlocal EnableExtensions EnableDelayedExpansion

rem Windows one-click build for native-fast-printer AAR
rem It calls android-src/build-aar.sh via bash (Git Bash or bash in PATH).
rem
rem 目标:双击不闪退;失败时把原因与日志直接打印到窗口里。

rem Better Chinese output on most Windows terminals
chcp 65001 >nul 2>nul
title native-fast-printer AAR build

set "RC=0"
call :main
set "RC=!ERRORLEVEL!"

echo.
if not "!RC!"=="0" (
  echo [FAILED] exitCode=!RC!
) else (
  echo [OK] exitCode=0
)
echo.
echo Press any key to close...
pause >nul
exit /b !RC!

:main
set "SCRIPT_DIR=%~dp0"
echo [INFO] scriptDir=!SCRIPT_DIR!
pushd "!SCRIPT_DIR!"
if errorlevel 1 (
  echo [ERROR] Cannot cd to script directory.
  exit /b 1
)

echo [INFO] cwd=!CD!

rem 1) locate bash
set "BASH_EXE="
if exist "C:\Program Files\Git\bin\bash.exe" (
  set "BASH_EXE=C:\Program Files\Git\bin\bash.exe"
  goto bash_ok
)
bash --version >nul 2>nul
if errorlevel 1 goto bash_missing
set "BASH_EXE=bash"
:bash_ok
echo [INFO] bash=!BASH_EXE!
goto bash_done

:bash_missing
echo [ERROR] bash not found.
echo         Please install Git for Windows (Git Bash) or add bash to PATH.
echo         Expected: "C:\Program Files\Git\bin\bash.exe"
popd
exit /b 1

:bash_done

rem 2) choose ANDROID_JAR (try API 34 down to 21 under SDK root)
if not defined ANDROID_JAR call :pick_android_jar
echo [INFO] ANDROID_JAR(final)=!ANDROID_JAR!
if not defined ANDROID_JAR goto android_jar_missing
goto android_jar_ok

:android_jar_missing
echo [ERROR] Android jar not found under SDK roots.
echo         Install an Android platform in SDK Manager (API 34 or lower),
echo         or set ANDROID_JAR to an existing ...\platforms\android-XX\android.jar
echo         Checked roots:
echo           ANDROID_HOME=!ANDROID_HOME!
echo           ANDROID_SDK_ROOT=!ANDROID_SDK_ROOT!
echo           LOCALAPPDATA=!LOCALAPPDATA!\Android\Sdk
echo           USERPROFILE=!USERPROFILE!\AppData\Local\Android\Sdk
popd
exit /b 1

:android_jar_ok
if not exist "!ANDROID_JAR!" (
  echo [ERROR] Android jar path is invalid:
  echo         !ANDROID_JAR!
  popd
  exit /b 1
)
echo [INFO] ANDROID_JAR=!ANDROID_JAR!

rem 3) build via bash (log便于排查)
set "LOG_FILE=!SCRIPT_DIR!build-aar.log"
echo [INFO] Log file: !LOG_FILE!
del /f /q "!LOG_FILE!" >nul 2>nul

echo [INFO] Running build-aar.sh ...
if /I "%BASH_EXE%"=="bash" (
  bash build-aar.sh > "!LOG_FILE!" 2>&1
) else (
  "!BASH_EXE!" build-aar.sh > "!LOG_FILE!" 2>&1
)

set "RC=!ERRORLEVEL!"
if not "!RC!"=="0" (
  echo [ERROR] build-aar.sh failed. exitCode=!RC!
  echo ---------------- build-aar.log tail ----------------
  if exist "!LOG_FILE!" (
    type "!LOG_FILE!"
  ) else (
    echo (log file missing)
  )
  echo ------------------------------------------------------
  popd
  exit /b !RC!
) else (
  echo [OK] Built AAR successfully.
  echo ---------------- build-aar.log ----------------
  if exist "!LOG_FILE!" type "!LOG_FILE!"
  echo ------------------------------------------------
  popd
  exit /b 0
)

rem ---------- subroutines (must be after main exit) ----------

:pick_android_jar
set "ANDROID_JAR="
rem try multiple sdk roots in order
for %%R in ("!ANDROID_HOME!" "!ANDROID_SDK_ROOT!" "!LOCALAPPDATA!\Android\Sdk" "!USERPROFILE!\AppData\Local\Android\Sdk") do (
  call :try_sdk_root "%%~R"
  if not "!ANDROID_JAR!"=="" exit /b 0
)
exit /b 1

:try_sdk_root
set "SDK_ROOT=%~1"
if "!SDK_ROOT!"=="" exit /b 0
echo [INFO] scanning sdkRoot=!SDK_ROOT!
for /L %%V in (34,-1,21) do (
  if exist "!SDK_ROOT!\platforms\android-%%V\android.jar" (
    set "ANDROID_JAR=!SDK_ROOT!\platforms\android-%%V\android.jar"
    echo [INFO] found android.jar: !ANDROID_JAR!
    exit /b 0
  )
)
exit /b 0