@echo off
COLOR 70
if (%1)==(0) goto skipme
if (%1) neq () goto adbi
echo -------------------------------------------------------------------------- >> log.txt
echo ^|%date% -- %time%^| >> log.txt
echo -------------------------------------------------------------------------- >> log.txt
Script 0 2>> log.txt
:skipme
mode con:cols=81 lines=40
echo Do you want to clean out all your current projects (y/n)?
set /P INPUT=Enter: %=%
if %INPUT% neq y goto skipme
echo Clearing Directories
rmdir /S /Q place-apk-here-for-modding
rmdir /S /Q place-apk-here-for-signing
rmdir /S /Q place-apk-here-to-batch-optimize
rmdir /S /Q place-ogg-here
rmdir /S /Q out
rmdir /S /Q other\temp
rmdir /S /Q %userprofile%\apktool
mkdir place-apk-here-for-modding
mkdir place-apk-here-for-signing
mkdir place-apk-here-to-batch-optimize
mkdir place-ogg-here
:skipme
set usrc=9
set heapy=64
java -version 
if errorlevel 1 goto errjava
adb version 
if errorlevel 1 goto erradb
cls
:restart
cls
echo  ------------------------------------------------------------------------------
echo    ^|  Compression-Level: %usrc%  ^|   Heap Size : %heapy%mb   ^|   Apk Manager : v4.7   ^|
echo  ------------------------------------------------------------------------------
echo  ----------------------------------
echo  Simple Tasks Such As Image Editing
echo  ----------------------------------
echo  0    Adb pull
echo  1    Extract apk
echo  2    Optimize images inside (Only if "Extract Apk" was selected)
echo  3    Zip apk 
echo  4    Sign apk (Dont do this if its a system apk)
echo  5    Zipalign apk (Do once apk is created/signed)
echo  6    Install apk (Dont do this if system apk, do adb push)
echo  7    Zip / Sign / Install apk (All in one step)
echo  8    Adb push (Only for system apk)
echo  -----------------------------------
echo  Advanced Tasks Such As Code Editing
echo  -----------------------------------
echo  9    Decompile apk
echo  10   Decompile apk (with dependencies) (For propietary rom apks)
echo  11   Compile apk
echo  12   Sign apk 
echo  13   Install apk
echo  14   Compile apk / Sign apk / Install apk (All in one step)
echo  -----------
echo  Other Stuff
echo  -----------
echo  15   Batch Optimize Apk (inside place-apk-here-to-batch-optimize only)
echo  16   Sign an apk(Batch support)(inside place-apk-here-for-signing folder only)
echo  17   Install apk(Batch support)(inside place-apk-here-for-signing folder only)
echo  18   Batch optimize ogg files (inside place-ogg-here only)
echo  19   Select compression level for apk's
echo  20   Set Max Memory Size (Only use if getting stuck at decompiling/compiling)
echo  21   Read Log
echo  22   Quit
echo  -------------------------------------------------------------------------------
SET /P menunr=Please make your decision:
IF %menunr%==0 (goto ap)
IF %menunr%==1 (goto ex)
IF %menunr%==2 (goto opt)
IF %menunr%==3 (goto zip)
IF %menunr%==4 (goto si)
IF %menunr%==5 (goto zipa)
IF %menunr%==6 (goto ins)
IF %menunr%==7 (goto alli)
IF %menunr%==8 (goto apu)
IF %menunr%==9 (goto de)
IF %menunr%==10 (goto ded)
IF %menunr%==11 (goto co)
IF %menunr%==12 (goto si)
IF %menunr%==13 (goto ins)
IF %menunr%==14 (goto all)
IF %menunr%==15 (goto bopt)
IF %menunr%==16 (goto asi)
IF %menunr%==17 (goto bins)
IF %menunr%==18 (goto ogg)
IF %menunr%==19 (goto usrcomp)
IF %menunr%==20 (goto heap)
IF %menunr%==21 (goto logr)
IF %menunr%==22 (goto quit)
:WHAT
echo You went crazy and entered something that wasnt part of the menu options
PAUSE
goto restart
:bins
echo Waiting for device
adb wait-for-device
echo Installing Apks
FOR %%F IN ("%~dp0place-apk-here-for-signing\*.apk") DO adb install -r "%%F"
goto restart
:heap
set /P INPUT=Enter max size for java heap space in megabytes (eg 512) : %=%
set heapy=%INPUT%
cls
goto restart
:usrcomp
set /P INPUT=Enter Compression Level (0-9) : %=%
set usrc=%INPUT%
cls
goto restart
:ogg
cd other
mkdir temp
echo Optimizing Ogg
FOR %%F IN ("../place-ogg-here/*.ogg") DO sox "../place-ogg-here/%%F" -C 0 "temp\%%F"
cd ..
MOVE other\temp\*  place-ogg-here
rmdir /S /Q other\temp
goto restart
:alli
cls
echo 1    System apk  (Retains signature)
echo 2    Regular apk (Removes signature for re-signing)
SET /P menunr=Please make your decision: 
IF %menunr%==1 (goto sys1)
IF %menunr%==2 (goto oa1)
:sys1
cd other
echo Zipping out folder into apk
7za a -tzip "../place-apk-here-for-modding/repackaged-unsigned.apk" "../out/*" -mx%usrc%
if errorlevel 1 (
echo "An Error Occured, Please Check The Log (option 21)"
PAUSE
)
cd ..
goto si1
:oa1
cd other
rmdir /S /Q "../out/META-INF"
7za a -tzip "../place-apk-here-for-modding/repackaged-unsigned.apk" "../out/*" -mx%usrc%
if errorlevel 1 (
echo "An Error Occured, Please Check The Log (option 21)"
PAUSE
)
cd ..
goto si1
:si1
cd other
echo Signing apk
java -Xmx%heapy%m -jar signapk.jar -w testkey.x509.pem testkey.pk8 ../place-apk-here-for-modding/repackaged-unsigned.apk ../place-apk-here-for-modding/repackaged-signed.apk
if errorlevel 1 (
echo "An Error Occured, Please Check The Log (option 21)"
PAUSE
)
DEL /Q "../place-apk-here-for-modding/repackaged-unsigned.apk"
cd ..
goto ins1
:ins1
echo Installing apk
adb install -r place-apk-here-for-modding/repackaged-signed.apk
if errorlevel 1 (
echo "An Error Occured, Please Check The Log (option 21)"
PAUSE
)
goto restart
:asi
cd other
DEL /Q "../place-apk-here-for-signing/signed.apk"
echo Signing apk
FOR %%F in (../place-apk-here-for-signing/*) DO call signer "%%F"
cd ..
goto restart
:bopt
set /P INPUT=Do you want to zipalign(z), optimize png(p) or both(zp)? : %=%
cd other
echo Optimizing apks
call apkopt_all %INPUT%
MOVE "optimized\*.apk" "../place-apk-here-to-batch-optimize"
rmdir /S /Q optimized
rmdir /S /Q original
cd ..
goto restart
:opt
mkdir temp
xcopy "%~dp0out\res\*.9.png" "%~dp0temp" /S /Y
cd other
echo Optimizing Png's
roptipng -o99 "../out/**/*.png"
cd ..
xcopy "%~dp0temp" "%~dp0out\res" /S /Y
rmdir temp /S /Q
goto restart
:ap
echo Where do you want adb to pull the apk from? 
echo Example of input : /system/app/launcher.apk
set /P INPUT=Type input: %=%
echo Pulling apk
adb pull %INPUT% "%~dp0place-apk-here-for-modding\something.apk"
if errorlevel 1 (
echo "An Error Occured, Please Check The Log (option 21)"
PAUSE
)
goto restart
:apu
echo Where do you want adb to push to and as what name 
echo Example of input : /system/app/launcher.apk
set /P INPUT=Type input: %=%
echo Waiting for device
adb wait-for-device
PAUSE
adb remount
echo Pushing apk
adb push "place-apk-here-for-modding\repackaged-unsigned.apk" %INPUT%
if errorlevel 1 (
echo "An Error Occured, Please Check The Log (option 21)"
PAUSE
)
goto restart
:zipa
echo Zipaligning Apk
zipalign -f 4 "%~dp0place-apk-here-for-modding\repackaged-signed.apk" "%~dp0place-apk-here-for-modding\repackaged-signed-aligned.apk"
if errorlevel 1 (
echo "An Error Occured, Please Check The Log (option 21)"
PAUSE
)
DEL /Q "%~dp0place-apk-here-for-modding\repackaged-signed.apk"
MOVE "%~dp0place-apk-here-for-modding\repackaged-signed-aligned.apk" "%~dp0place-apk-here-for-modding\repackaged-signed.apk"
zipalign -f 4 "%~dp0place-apk-here-for-modding\repackaged-unsigned.apk" "%~dp0place-apk-here-for-modding\repackaged-unsigned-aligned.apk"
DEL /Q "%~dp0place-apk-here-for-modding\repackaged-unsigned.apk"
MOVE "%~dp0place-apk-here-for-modding\repackaged-unsigned-aligned.apk" "%~dp0place-apk-here-for-modding\repackaged-unsigned.apk"
goto restart
:ex
cd other
DEL /Q "../place-apk-here-for-modding/repackaged.apk"
DEL /Q "../place-apk-here-for-modding/repackaged-signed.apk"
DEL /Q "../place-apk-here-for-modding/repackaged-unsigned.apk"
rmdir /S /Q "../out"
echo Extracting apk
7za x -o"../out" "../place-apk-here-for-modding/*.apk"
if errorlevel 1 (
echo "An Error Occured, Please Check The Log (option 21)"
PAUSE
)
cd ..
goto restart
:zip
cls
echo 1    System apk (Retains signature)
echo 2    Regular apk (Removes signature for re-signing)
SET /P menunr=Please make your decision: 
IF %menunr%==1 (goto sys)
IF %menunr%==2 (goto oa)
:sys
echo Zipping Apk
cd other
7za a -tzip "../place-apk-here-for-modding/repackaged-unsigned.apk" "../out/*" -mx%usrc%
if errorlevel 1 (
echo "An Error Occured, Please Check The Log (option 21)"
PAUSE
)

cd ..
goto restart
:oa
cd other
echo Zipping Apk
rmdir /S /Q "../out/META-INF"
7za a -tzip "../place-apk-here-for-modding/repackaged-unsigned.apk" "../out/*" -mx%usrc%
if errorlevel 1 (
echo "An Error Occured, Please Check The Log (option 21)"
PAUSE
)

cd ..
goto restart
:ded
cd other
DEL /Q "../place-apk-here-for-modding/repackaged.apk"
DEL /Q "../place-apk-here-for-modding/repackaged-signed.apk"
DEL /Q "../place-apk-here-for-modding/repackaged-unsigned.apk"
rmdir /S /Q "../out"
:temr
echo Drag the dependee apk in this window or type its path
echo Example to decompile Rosie.apk, drag com.htc.resources.apk in this window
set /P INPUT=Type input: %=%
java -jar apktool.jar if %INPUT%
if NOT EXIST %userprofile%\apktool\framework\2.apk (
echo.
echo "Sorry thats not the dependee apk, try again"
goto temr
)
echo Decompiling Apk
java -Xmx%heapy%m -jar apktool.jar d ../place-apk-here-for-modding/*.apk ../out
if errorlevel 1 (
echo "An Error Occured, Please Check The Log (option 21)"
PAUSE
)
cd ..
goto restart
:de
cd other
DEL /Q "../place-apk-here-for-modding/repackaged.apk"
DEL /Q "../place-apk-here-for-modding/repackaged-signed.apk"
DEL /Q "../place-apk-here-for-modding/repackaged-unsigned.apk"
rmdir /S /Q "../out"
echo Decompiling Apk
java -Xmx%heapy%m -jar apktool.jar d ../place-apk-here-for-modding/*.apk ../out
if errorlevel 1 (
echo "An Error Occured, Please Check The Log (option 21)"
PAUSE
)
cd ..
goto restart
:co
cd other
echo Building Apk
java -Xmx%heapy%m -jar apktool.jar b ../out "%~dp0place-apk-here-for-modding\repackaged-unsigned.apk"
if errorlevel 1 (
echo "An Error Occured, Please Check The Log (option 21)"
PAUSE
)
cd ..
goto restart
:si
cd other
echo Signing Apk
java -Xmx%heapy%m -jar signapk.jar -w testkey.x509.pem testkey.pk8 ../place-apk-here-for-modding/repackaged-unsigned.apk ../place-apk-here-for-modding/repackaged-signed.apk
if errorlevel 1 (
echo "An Error Occured, Please Check The Log (option 21)"
PAUSE
)

DEL /Q "../place-apk-here-for-modding/repackaged-unsigned.apk"
cd ..
goto restart
:ins
echo Waiting for device
adb wait-for-device
echo Installing Apk
adb install -r place-apk-here-for-modding/repackaged-signed.apk
if errorlevel 1 (
echo "An Error Occured, Please Check The Log (option 21)"
PAUSE
)

goto restart
:all
cd other
echo Building Apk
java -Xmx%heapy%m -jar apktool.jar b ../out "%~dp0place-apk-here-for-modding\repackaged-unsigned.apk"
if errorlevel 1 (
echo "An Error Occured, Please Check The Log (option 21)"
PAUSE
)

echo Signing Apk
java -Xmx%heapy%m -jar signapk.jar -w testkey.x509.pem testkey.pk8 ../place-apk-here-for-modding/repackaged-unsigned.apk ../place-apk-here-for-modding/repackaged-signed.apk
if errorlevel 1 (
echo "An Error Occured, Please Check The Log (option 21)"
PAUSE
)

DEL /Q "../place-apk-here-for-modding/repackaged-unsigned.apk"
echo Waiting for device
adb wait-for-device
echo Installing Apk
adb install -r ../place-apk-here-for-modding/repackaged-signed.apk
if errorlevel 1 (
echo "An Error Occured, Please Check The Log (option 21)"
PAUSE
)
cd ..
goto restart
:errjava
cls
echo Java was not found, you will not be able to sign apks or use apktool
PAUSE
goto restart
:erradb
cls
echo Adb was not found, you will not be able to manipulate the files on your phone
PAUSE
goto restart
:adbi
echo Waiting for device
adb wait-for-device
set count=0
:loop
if "%~n1"=="" goto :endloop
echo Installing %~n1
adb install -r %1
shift
set /a count+=1
goto :loop
:endloop
goto quit
:logr
cd other
Start "Read The Log - Main script is still running, close this to return" signer 1
goto restart
:quit
adb kill-server