Simple Windows batch script to sign unsigned apk for us newbs

mistermentality

Active Member
Licensed User
Longtime User
Simply put this script into a folder with your unsigned apk and release key file and double click the icon to sign your unsigned android apk file with your own release key.

It just saves having to keep typing commands into a dos prompt if you use a different key to the debug or B4A generated one and hopefully it will be of use to someone else too.

All it does is
1. Ask you for the name of the unsigned apk (eg temp.ap_)
2. Sign the apk file with the release key
3. Verify the file is signed

Simply save the attached txt file with a .bat extension or copy the following code into notepad and save as signapk.bat
B4X:
@echo off
Title Sign an unsigned apk
set OLDDIR=%CD%


cls
echo.
echo You will need to copy your keystore file
echo eg. my-release-key.keystore
echo into the same folder as this script.
echo.
echo Please place your unsigned app in the same 
echo directory as this program, when you have
echo done this please type in its name below:
echo.
echo.
set /p apk= What is the filename (including the .apk at the end)?  
echo Your app is named %apk% 
echo.
echo.
echo while creating your Private Key, what did you put as your ALIASNAME?
set /p alias= Aliasname 
echo. 
echo Is the Java JDK in your path?
echo if you do not know what this means, please type "N"
CHOICE /C:YN /M "Java JDK in path?"
If errorlevel 2 goto nojdk
If errorlevel 1 goto jdk

:nojdk
echo.
echo.
echo It is recommended that you put the jdk in your path
echo To see how please visit this web page:
echo http://www.oracle.com/technetwork/java/javase/documentation/install-windows-152927.html
echo.
echo.
pause
cls
echo.
echo.
:jdk
echo ok, you will need to type in your password when prompted

jarsigner.exe -verbose -keystore  my-release-key.keystore %apk% %alias%
echo.
echo. 
echo Checking it is signed correctly
jarsigner -verify %apk%
echo.
echo If you got an error you probably
echo compiled your apk with a key
echo (probably the debug key).
echo Try recompiling it without signing
echo and then copy and paste the
echo resulting temp.ap_ file here.
echo.
echo.
Pause

Dave
 

Attachments

  • SignApk.txt
    1.5 KB · Views: 639

mistermentality

Active Member
Licensed User
Longtime User
I have amended the script to also zipalign the finished apk. It now produces a renamed and zip aligned apk that is market ready.

I have attached a zip file that contains the amended .bat file and also the zipalign exe file for those who might not have it so all that's needed to produce market ready apk's from B4A's unsigned temp.ap_ apk is (assuming you have the contents of the zip in a folder) copy your temp.ap_ file to that folder and double click the .bat file to run it.

If you just want the amended .bat code it is
B4X:
@echo off
Title Sign an unsigned apk
set OLDDIR=%CD%


cls
echo.
echo You will need to copy your keystore file
echo eg. my-release-key.keystore
echo into the same folder as this script.
echo.
echo Please place your unsigned app in the same 
echo directory as this program, when you have
echo done this please type in its name below:
echo.
echo.
set /p apk= What is the filename (including the .apk at the end)?  
echo Your app is named %apk% 
echo.
echo.
echo while creating your Private Key, what did you put as your ALIASNAME?
set /p alias= Aliasname 
echo. 
echo Is the Java JDK in your path?
echo if you do not know what this means, please type "N"
CHOICE /C:YN /M "Java JDK in path?"
If errorlevel 2 goto nojdk
If errorlevel 1 goto jdk

:nojdk
echo.
echo.
echo It is recommended that you put the jdk in your path
echo To see how please visit this web page:
echo http://www.oracle.com/technetwork/java/javase/documentation/install-windows-152927.html
echo.
echo.
pause
cls
echo.
echo.
:jdk
echo ok, you will need to type in your password when prompted

jarsigner.exe -verbose -keystore  my-release-key.keystore %apk% %alias%
echo.
echo. 
echo Checking it is signed correctly
jarsigner -verify %apk%
echo.
echo If you got an error you probably
echo compiled your apk with a key
echo (probably the debug key).
echo Try recompiling it without signing
echo and then copy and paste the
echo resulting temp.ap_ file here.
echo.
echo.
echo Now we just need to zipalign the apk
echo. 
echo.
echo.
set /p final= What do you want the finished apk to be called?:     
echo %final% it is
echo. 
pause
zipalign -v 4 %apk% %final%
echo.
echo.
pause
cls
Pause

This script file will....
1. Ask you for the unsigned apk's name (eg temp.ap_)
2. Sign the apk with your release key
3. Ask you what the finished apk should be called
4. Zipalign the signed apk and give it the name you chose

Dave
 

Attachments

  • SignApk.zip
    174.5 KB · Views: 536
Upvote 0

rtesluk

Member
Licensed User
Longtime User
Dos choice command

'Choice' is not recognized as an internal or external command, operable program or batch file in XP Pro.

Ray
 
Upvote 0

mistermentality

Active Member
Licensed User
Longtime User
'Choice' is not recognized as an internal or external command, operable program or batch file in XP Pro.

Ray

I checked at Batch files - The CHOICE command and apparently it is in some versions of Windows including my own (Windows 7) but was taken out of XP for some reason. However if you replace the batch file text with this....

B4X:
@echo off
Title Sign an unsigned apk
set OLDDIR=%CD%


cls
echo.
echo You will need to copy your keystore file
echo eg. my-release-key.keystore
echo into the same folder as this script.
echo.
echo Please place your unsigned app in the same 
echo directory as this program, when you have
echo done this please type in its name below:
echo.
echo.
set apk=temp.ap_  
echo Your app is named %apk% 
echo.
echo.
echo while creating your Private Key, what did you put as your alias name?
set alias=YOUR_ALIAS_NAME 
echo. 
echo ok, you will need to type in your password when prompted

jarsigner.exe -verbose -keystore  YOUR_KEYSTORE_FILE_NAME.keystore %apk% %alias%
echo.
echo. 
echo Checking it is signed correctly
jarsigner -verify %apk%
echo.
echo If you got an error you probably
echo compiled your apk with a key
echo (probably the debug key).
echo Try recompiling it without signing
echo and then copy and paste the
echo resulting temp.ap_ file here.
echo.
echo.
echo Now we just need to zipalign the apk
echo. 
echo.
echo.
set /p final= What do you want the finished apk to be called?:     
echo %final% it is
echo. 
pause
zipalign -v 4 %apk% %final%
echo.
echo.
pause
cls
Pause

Change the two names in it that are in capitals so they reflect your own details and then whenever you want to sign an unsigned app just put the temp file in the same folder and run the dos script.

It will ask you for your key password and the signed apks name (what you want it to be called) and do it for you.

Dave
 
Upvote 0

melamoud

Active Member
Licensed User
Longtime User
creating an APK, still confused

Note that you can sign your file from within the IDE by choosing Tools - Private Sign Key.
Such a script is only needed if you want to sign the file manually.

Hi,

so with the new version if I create a keystore file, and upload it to the IDE, now how do I create an APK with it ?
if I compile using ALT-2 its without signing right ? (I tried and copied the temp.apk (after renaming it) to myphone and it failed to install (its working through the bridge but not install by copy the APK)

I know I can manually sign it, but reading this thread got me thinking that the IDE can do this for me,
am I wrong ?
do you have a setp by step instruction (could not find one) on how to use the IDE to create an APK ready for the market ?

thanks
 
Upvote 0

melamoud

Active Member
Licensed User
Longtime User
Signed apk

hi,

I read the totorial and tried to work according to it, but as I wrote in this post it did not work, let me be more specific, maybe I was not clear on what consfused me

1. I created a keystore and instruct the IDE to use it + verify the IDE configuration is to use the new key
2. if I complile using Alt-2 I have the temp apk file, I renamed it and copy it to my phone, I tried to install it and the phone said it is unsecsesfull (I guessed its the certificate)
3. if I compile using alt-2 its not signed right ? (thats what the menu say)
4. if I compile using alt3 or alt1 the app is moving ueing the bridget to the phone (a different phone) and the app is working, but then I do not have the temp apk file right ?

how do I get an apk file on my PC which is signed ?

and am I correct about the failure to install reason is because the app is not signed or signed with debug and not my keystore ?

thanks
 
Upvote 0

melamoud

Active Member
Licensed User
Longtime User
create a signed app

hi, thanks I tried it before and the temp.ap_ was not created for some reason,
I tried it again and it is working , I guess what confused me is that t\alt-1 and alt-3 are compile and run meaning you need a device oran emulator and all I wanted is an apk, and since I did not have a device connected it failed (but the apk was created)

anyway thanks,

maybe it wil be usefull to just add compile option (with signing)

thanks
 
Upvote 0
Top