adnroid 自动更换api接口地址,生成2个apk

@echo off
chcp 65001 >nul
setlocal EnableDelayedExpansion

echo =================================================================
echo   SMTTraceApp Dual Server APK Builder v2.0 (UTF-8 Fixed)
echo   Version: 10.6.0
echo   Target: Build APKs for different server configurations
echo   Fix: Resolved UTF-8 encoding issues in URL replacement
echo =================================================================
echo.

:: Set project paths
set PROJECT_DIR=H:\svn\U8ChongQunRepository\SMTTraceApp
set STRINGS_FILE=%PROJECT_DIR%\app\src\main\res\values\strings.xml
set OUTPUT_DIR=%PROJECT_DIR%\app\build\outputs\apk\debug
set TIMESTAMP=%date:~0,4%%date:~5,2%%date:~8,2%_%time:~0,2%%time:~3,2%%time:~6,2%
set TIMESTAMP=%TIMESTAMP: =0%

:: Change to project directory
echo [INFO] Changing to project directory: %PROJECT_DIR%
cd /d "%PROJECT_DIR%"
if errorlevel 1 (
    echo [ERROR] Cannot access project directory!
    pause
    exit /b 1
)

:: Check required files
echo [INFO] Checking required files...
if not exist "gradlew.bat" (
    echo [ERROR] gradlew.bat does not exist!
    pause
    exit /b 1
)

if not exist "%STRINGS_FILE%" (
    echo [ERROR] strings.xml file does not exist!
    pause
    exit /b 1
)

:: Create timestamped backup in project root directory (avoid Android resource scanner)
echo [INFO] Creating timestamped backup...
set BACKUP_FILE=%PROJECT_DIR%\strings.xml.backup_%TIMESTAMP%
copy "%STRINGS_FILE%" "%BACKUP_FILE%" >nul
if errorlevel 1 (
    echo [ERROR] Cannot create backup file!
    pause
    exit /b 1
)
echo [INFO] Backup created: .\strings.xml.backup_%TIMESTAMP%

:: Verify current configuration
echo [INFO] Verifying current configuration...
findstr "api_base_url.*110.110.110.110:8092" "%STRINGS_FILE%" >nul
if errorlevel 1 (
    echo [WARNING] Current configuration is not 110.110.110.110:8092
    echo [INFO] Current API configuration:
    findstr "api_base_url" "%STRINGS_FILE%"
)

:: Clean previous builds
echo [INFO] Cleaning previous builds...
call ".\gradlew.bat" clean --no-daemon --console=plain --quiet
if errorlevel 1 (
    echo [WARNING] Clean process has warnings, continuing...
)

:: ===================== Build first version (110.110.110.110) =====================
echo.
echo [STEP 1/5] Building 110.110.110.110 version...
echo [INFO] Ensuring configuration is set to 110.110.110.110:8092...

:: Ensure correct configuration using UTF-8 encoding
powershell -Command "$content = Get-Content '%STRINGS_FILE%' -Encoding UTF8; $content = $content -replace 'http://10\.74\.175\.210:8092', 'http://110.110.110.110:8092'; $content | Set-Content '%STRINGS_FILE%' -Encoding UTF8"
if errorlevel 1 (
    echo [ERROR] Failed to set 110.110.110.110 configuration!
    goto :restore_and_exit
)

:: Verify configuration change
findstr "api_base_url.*110.110.110.110:8092" "%STRINGS_FILE%" >nul
if errorlevel 1 (
    echo [ERROR] Configuration verification failed - 110.110.110.110 not found!
    goto :restore_and_exit
)
echo [SUCCESS] Configuration verified: 110.110.110.110:8092

echo [INFO] Building Debug APK for 110.110.110.110...
call ".\gradlew.bat" assembleDebug --no-daemon --console=plain
if errorlevel 1 (
    echo [ERROR] 110.110.110.110 version build failed!
    goto :restore_and_exit
)

:: Check if APK was generated
if not exist "%OUTPUT_DIR%\app-debug.apk" (
    echo [ERROR] APK file was not generated!
    goto :restore_and_exit
)

:: Copy and rename APK
echo [INFO] Creating: SMTTraceApp-110.110.110.110-8092-debug.apk
copy "%OUTPUT_DIR%\app-debug.apk" "%OUTPUT_DIR%\SMTTraceApp-110.110.110.110-8092-debug.apk" >nul
if errorlevel 1 (
    echo [ERROR] Cannot rename first APK!
    goto :restore_and_exit
)

echo [SUCCESS] 110.110.110.110 version build completed!

:: ===================== Modify config and build second version =====================
echo.
echo [STEP 2/5] Switching to 10.74.175.210 configuration...

:: Replace URL with proper UTF-8 encoding
powershell -Command "$content = Get-Content '%STRINGS_FILE%' -Encoding UTF8; $content = $content -replace 'http://120\.132\.9\.102:8092', 'http://10.74.175.210:8092'; $content | Set-Content '%STRINGS_FILE%' -Encoding UTF8"
if errorlevel 1 (
    echo [ERROR] Cannot modify configuration file!
    goto :restore_and_exit
)

:: Verify configuration change
findstr "api_base_url.*10.74.175.210:8092" "%STRINGS_FILE%" >nul
if errorlevel 1 (
    echo [ERROR] Configuration verification failed - 10.74.175.210 not found!
    goto :restore_and_exit
)
echo [SUCCESS] Configuration verified: 10.74.175.210:8092

echo [STEP 3/5] Building 10.74.175.210 version...
call ".\gradlew.bat" assembleDebug --no-daemon --console=plain
if errorlevel 1 (
    echo [ERROR] 10.74.175.210 version build failed!
    goto :restore_and_exit
)

:: Check if second APK was generated
if not exist "%OUTPUT_DIR%\app-debug.apk" (
    echo [ERROR] Second APK file was not generated!
    goto :restore_and_exit
)

:: Copy and rename second APK
echo [INFO] Creating: SMTTraceApp-10.74.175.210-8092-debug.apk
copy "%OUTPUT_DIR%\app-debug.apk" "%OUTPUT_DIR%\SMTTraceApp-10.74.175.210-8092-debug.apk" >nul
if errorlevel 1 (
    echo [ERROR] Cannot rename second APK!
    goto :restore_and_exit
)

echo [SUCCESS] 10.74.175.210 version build completed!

:: ===================== Restore configuration =====================
echo.
echo [STEP 4/5] Restoring original configuration...

:: Restore using UTF-8 encoding
powershell -Command "$content = Get-Content '%STRINGS_FILE%' -Encoding UTF8; $content = $content -replace 'http://10\.74\.175\.210:8092', 'http://110.110.110.110:8092'; $content | Set-Content '%STRINGS_FILE%' -Encoding UTF8"
if errorlevel 1 (
    echo [ERROR] Cannot restore configuration using PowerShell!
    echo [INFO] Attempting restore from backup...
    copy "%BACKUP_FILE%" "%STRINGS_FILE%" >nul
    if errorlevel 1 (
        echo [ERROR] Cannot restore from backup file!
        echo [CRITICAL] Please manually restore strings.xml!
        goto :restore_and_exit
    )
)

:: Verify restoration
findstr "api_base_url.*110.110.110.110:8092" "%STRINGS_FILE%" >nul
if errorlevel 1 (
    echo [ERROR] Configuration restoration verification failed!
    echo [INFO] Attempting restore from backup...
    copy "%BACKUP_FILE%" "%STRINGS_FILE%" >nul
    if errorlevel 1 (
        echo [CRITICAL] Restore failed! Please check strings.xml manually!
    )
) else (
    echo [SUCCESS] Configuration restored to: 110.110.110.110:8092
)

:: Cleanup backup file
if exist "%BACKUP_FILE%" (
    del "%BACKUP_FILE%" >nul 2>&1
    echo [INFO] Temporary backup cleaned up
)

:: ===================== Final verification and summary =====================
echo.
echo [STEP 5/5] Final verification and summary...
echo =================================================================
echo   BUILD COMPLETED SUCCESSFULLY!
echo =================================================================

:: Verify both APKs exist and show their sizes
set APK1_EXISTS=false
set APK2_EXISTS=false

if exist "%OUTPUT_DIR%\SMTTraceApp-110.110.110.110-8092-debug.apk" (
    set APK1_EXISTS=true
    for %%i in ("%OUTPUT_DIR%\SMTTraceApp-110.110.110.110-8092-debug.apk") do (
        echo [鉁揮 SMTTraceApp-110.110.110.110-8092-debug.apk (%%~zi bytes)
    )
) else (
    echo [鉁梋 SMTTraceApp-110.110.110.110-8092-debug.apk (NOT FOUND)
)

if exist "%OUTPUT_DIR%\SMTTraceApp-10.74.175.210-8092-debug.apk" (
    set APK2_EXISTS=true
    for %%i in ("%OUTPUT_DIR%\SMTTraceApp-10.74.175.210-8092-debug.apk") do (
        echo [鉁揮 SMTTraceApp-10.74.175.210-8092-debug.apk (%%~zi bytes)
    )
) else (
    echo [鉁梋 SMTTraceApp-10.74.175.210-8092-debug.apk (NOT FOUND)
)

echo.
echo Output directory: %OUTPUT_DIR%
echo Build timestamp: %TIMESTAMP%
echo UTF-8 encoding: PRESERVED
echo Configuration: RESTORED to 110.110.110.110:8092
echo =================================================================

:: Final status check
if "%APK1_EXISTS%"=="true" if "%APK2_EXISTS%"=="true" (
    echo [SUCCESS] Both APK files generated successfully!
    echo.
    echo [INFO] Open output directory? (Y/N)
    set /p open_folder="Enter your choice: "
    if /i "!open_folder!"=="Y" (
        explorer "%OUTPUT_DIR%"
    )
    echo.
    echo [COMPLETED] Dual-version APK build completed successfully!
    goto :end
) else (
    echo [WARNING] Some APK files may be missing! Check the build output above.
    pause
    exit /b 1
)

:restore_and_exit
echo.
echo [ERROR] Build process failed! Attempting to restore configuration...
if exist "%BACKUP_FILE%" (
    copy "%BACKUP_FILE%" "%STRINGS_FILE%" >nul
    del "%BACKUP_FILE%" >nul 2>&1
    echo [INFO] Configuration restored from backup
) else (
    echo [WARNING] No backup file found! Please check strings.xml manually
)
echo [FAILED] Build task failed! Check error messages above.
pause
exit /b 1

:end
pause

 

posted @ 2025-09-26 15:52  _York  阅读(7)  评论(0)    收藏  举报