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

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 注意:标签 :label 不能写在 if/for的圆括号块里面,否则双击运行会立刻退出(窗口闪一下)。

set "SCRIPT_DIR=%~dp0"
pushd "%SCRIPT_DIR%" || (
  echo [ERROR] Cannot cd to script directory.
  pause
  exit /b 1
)

rem 1) locate bash
where bash >nul 2>nul
if errorlevel 1 (
  if exist "C:\Program Files\Git\bin\bash.exe" (
    set "BASH_EXE=C:\Program Files\Git\bin\bash.exe"
  ) else (
    echo [ERROR] bash not found.
    echo         Please install Git for Windows ^(Git Bash^) or add bash to PATH.
    popd
    pause
    exit /b 1
  )
) else (
  set "BASH_EXE=bash"
)

rem 2) choose ANDROID_JAR (try API 34 down to 21 under SDK root)
if "%ANDROID_JAR%"=="" call :pick_android_jar

if "%ANDROID_JAR%"=="" (
  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
  popd
  pause
  exit /b 1
)

if not exist "%ANDROID_JAR%" (
  echo [ERROR] Android jar path is invalid:
  echo         %ANDROID_JAR%
  popd
  pause
  exit /b 1
)

echo Using ANDROID_JAR:
echo %ANDROID_JAR%

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

echo Running: %BASH_EXE% 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 See log:
  echo   %LOG_FILE%
) else (
  echo [OK] Built AAR successfully.
  echo See log:
  echo   %LOG_FILE%
)

popd
pause
exit /b %RC%

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

:pick_android_jar
set "ANDROID_JAR="
set "SDK_ROOT_CAND=%ANDROID_HOME%"
if "%SDK_ROOT_CAND%"=="" set "SDK_ROOT_CAND=%ANDROID_SDK_ROOT%"
if "%SDK_ROOT_CAND%"=="" set "SDK_ROOT_CAND=%LOCALAPPDATA%\Android\Sdk"
if "%SDK_ROOT_CAND%"=="" set "SDK_ROOT_CAND=%USERPROFILE%\AppData\Local\Android\Sdk"
for /L %%V in (34,-1,21) do (
  if exist "%SDK_ROOT_CAND%\platforms\android-%%V\android.jar" (
    set "ANDROID_JAR=%SDK_ROOT_CAND%\platforms\android-%%V\android.jar"
    exit /b 0
  )
)
exit /b 1