Author Topic: How to sign Android apps  (Read 16224 times)

Offline Dabz

  • Mr. Polyvector
  • ***
  • Posts: 104
    • View Profile
    • BambooCoder
How to sign Android apps
« on: 2011-Apr-24 »
Looks like Android is in, so, here's a quick rundown on how to sign apk files for Android in regards to GLBasic:-

1) First, build you app for Android

2) Create a folder called "keytools" and make a folder in that called "keys"  (I create the keytools folder in my root GLBasic project folder)

3) Navigate to your {App_Project_Root}/android/bin directory and copy the file glbasic-debug.apk to your keytools folder

The glbasic-debug.apk file has already been signed with a default key, which is no good when releasing stuff on the Android Market (It wont be accepted), so, we need to remove the debug key and add our own

4) To unsign it, we need to remove a few files from the apk package, the apk file is just a zip file really, so, you need to explore the package using a archive utility, I use 7-zip.

5) When exploring the package, there is a folder in the root of the package called 'META-INF', go into that folder and there should be three files... MANIFEST.MF, CERT.SF and CERT.RSA... Remove these three files and your package will be unsigned.

6) Open command prompt (cmd.exe)

7) Navigate to your keytools folder
8} Type the following (Make sure your Java paths are correct, if not, use whole paths to the Java tools used below):-
9)keytool -genkey -alias mykey.keystore -keyalg RSA -validity 20000 -keystore keys/mykey.keystore

Then following the questions...

10) Type the following: jarsigner -verbose -keystore keys/mykey.keystore -signedjar glbasic-signed.apk glbasic-debug.apk mykey.keystore

11) You apk file should now be correctly signed.

Dabz

Original instructions provided by therevills here: http://www.indiecodez.com/forum/index.php/topic,109.0.html, and I just updated them for GLBasic! :)
« Last Edit: 2011-Apr-24 by Dabz »

Offline trucidare

  • Administrator
  • Prof. Inline
  • *******
  • Posts: 1377
  • Bachelor of Fail
    • View Profile
Re: How to sign Android apps
« Reply #1 on: 2011-Apr-24 »
you can read the full instuctions on android sdk page.

anyone could write a simple tool for signing in windows?
MacBook Pro 2,2 GHz Core 2 Duo, 4 GB RAM, 160 GB HDD, 8600M GT
Core i3 - 3,07 GHz, 8 GB Ram, 2.5 TB HDD, Geforce GTX 260+ OC

Offline Dabz

  • Mr. Polyvector
  • ***
  • Posts: 104
    • View Profile
    • BambooCoder
Re: How to sign Android apps
« Reply #2 on: 2011-Apr-24 »
Well, they could, but you only need one key, it only takes 2 mins to sign an app using the command line too, so no need for additional tools imo!

Even better still you could get Ant to do the grunt when building, but, its horses for courses really.

Dabz

MrTAToad

  • Guest
Re: How to sign Android apps
« Reply #3 on: 2011-Apr-25 »
Easiest way would be do put that in a batch file

MrTAToad

  • Guest
Re: How to sign Android apps
« Reply #4 on: 2011-Apr-30 »
One thing you did forget to mention is that they signing process will require the following information (hopefully only requested once) :

Password
Full Name
Name of your organisational unit
Name of your organisation
Name of City/Locality
State/Province
Two letter country code

Another thing that should be done (you get warned about when publishing an application on the Market), is to zipalign the APK file (see http://developer.android.com/guide/developing/tools/zipalign.html)

Hope you dont mind if I re-word your post and put it in my book...
« Last Edit: 2011-May-02 by MrTAToad »

Offline spicypixel

  • Prof. Inline
  • *****
  • Posts: 646
  • Pixel Artist
    • View Profile
    • SpicyPixel.net
Re: How to sign Android apps
« Reply #5 on: 2011-May-24 »
Thanks for the post Dabz and the Link MrTAToad
http://www.spicypixel.net | http://www.facebook.com/SpicyPixel.NET

Comps Owned - ZX.81, ZX.48K, ZX.128K+2, Vic20, C64, Atari-ST, A500.600.1200, PC, Apple Mini-Mac.

MrTAToad

  • Guest
Re: How to sign Android apps
« Reply #6 on: 2011-Jul-07 »
This batch file should work :

Code: GLBasic [Select]
copy %1\glbasic-debug.apk glbasic-debug.zip
"C:\Program Files (x86)\7-Zip\7z.exe" d glbasic-debug.zip META-INF\manifest.mf META-INF\CERT.SF META-INF\CERT.RSA
copy glbasic-debug.zip glbasic-debug.apk
keytool -genkey -alias mykey.keystore -keyalg RSA -validity 20000 -keystore keys/mykey.keystore
jarsigner -verbose -keystore keys/mykey.keystore -signedjar glbasic-signed.apk glbasic-debug.apk mykey.keystore
zipalign -v 4 glbasic-signed.apk glbasic-release.apk

You need 7Zip to be installed, of course...
« Last Edit: 2011-Jul-07 by MrTAToad »

Offline Kitty Hello

  • code monkey
  • Administrator
  • Prof. Inline
  • *******
  • Posts: 10651
  • here on my island the sea says 'hello'
    • View Profile
    • http://www.glbasic.com
Re: How to sign Android apps
« Reply #7 on: 2011-Jul-07 »
wait - is that a debug version? I mean - could we get speed improvements if the build was triggered for a release?
Check the file compiler/platform/Andoird/bin/glb_build.bat

MrTAToad

  • Guest
Re: How to sign Android apps
« Reply #8 on: 2011-Jul-07 »
It'll be worth a try!  Does the main_rules.xml file also need to be modified ?  At the moment there doesn't seem to any speed increase :(

This is the complete release output :

Quote
_______________________________________
*** Configuration: ANDROID ***
precompiling:
GPC - GLBasic Precompiler V.7.917 SN:47ccc3f7 - 3D, NET
Wordcount:3370 commands
compile+link:
BUILDSRIPT
Buildfile: C:\Users\Nicholas\Documents\GLBasic\Utilities\GLBasicCommandSet\distribute\Android\build.xml
    [setup] Android SDK Tools Revision 10
    [setup] Project Target: Android 2.2
    [setup] API level: 8
    [setup]
    [setup] ------------------
    [setup] Resolving library dependencies:
    [setup] No library dependencies.
    [setup]
    [setup] ------------------
    [setup]
    [setup]
    [setup] Importing rules file: tools\ant\main_rules.xml

-set-release-mode:
     [echo] *************************************************
     [echo] ****  Android Manifest has debuggable=true   ****
     [echo] **** Doing DEBUG packaging with RELEASE keys ****
     [echo] *************************************************

-release-obfuscation-check:

-pre-build:

-dirs:
     [echo] Creating output directories if needed...

-aidl:
     [echo] Compiling aidl files into Java classes...

-renderscript:
     [echo] Compiling RenderScript files into Java classes and RenderScript bytecode...

-resource-src:
     [echo] Generating R.java / Manifest.java from the resources...

-pre-compile:

compile:
    [javac] Q:\Compiler\platform\android\android-sdk-windows\tools\ant\main_rules.xml:384: warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds
    [javac] Compiling 2 source files to C:\Users\Nicholas\Documents\GLBasic\Utilities\GLBasicCommandSet\distribute\Android\bin\classes

-post-compile:

-obfuscate:

-dex:
     [echo] Converting compiled files and external libraries into C:\Users\Nicholas\Documents\GLBasic\Utilities\GLBasicCommandSet\distribute\Android\bin\classes.dex...
    [apply] ***** GF: SKIP check the parameter *****

-package-resources:
     [echo] Packaging resources
     [aapt] Creating full resource package...
 [aapt.exe]     (skipping index file 'C:\Users\Nicholas\Documents\GLBasic\Utilities\GLBasicCommandSet\distribute\Android\assets\Media\Thumbs.db')
 [aapt.exe] Warning: AndroidManifest.xml already defines debuggable (in http://schemas.android.com/apk/res/android); using existing value in manifest.

-package-release:
[apkbuilder] Creating glbasic-unsigned.apk for release...

-release-prompt-for-password:

-release-nosign:
     [echo] No key.store and key.alias properties found in build.properties.
     [echo] Please sign C:\Users\Nicholas\Documents\GLBasic\Utilities\GLBasicCommandSet\distribute\Android\bin\glbasic-unsigned.apk manually
     [echo] and run zipalign from the Android SDK tools.

release:

BUILD SUCCESSFUL
Total time: 3 seconds
     [echo] Creating output directories if needed...
     [echo] Compiling aidl files into Java classes...
     [echo] Compiling RenderScript files into Java classes and RenderScript bytecode...
     [echo] Generating R.java / Manifest.java from the resources...
    [javac] Q:\Compiler\platform\android\android-sdk-windows\tools\ant\main_rules.xml:384: warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds
     [echo] Converting compiled files and external libraries into C:\Users\Nicholas\Documents\GLBasic\Utilities\GLBasicCommandSet\distribute\Android\bin\classes.dex...
     [echo] Packaging resources
     [echo] Running zip align on final apk...
     [echo] Debug Package: C:\Users\Nicholas\Documents\GLBasic\Utilities\GLBasicCommandSet\distribute\Android\bin\glbasic-debug.apk
     [echo] Installing C:\Users\Nicholas\Documents\GLBasic\Utilities\GLBasicCommandSet\distribute\Android\bin\glbasic-debug.apk onto default emulator or device...

BUILD FAILED
Q:\Compiler\platform\android\android-sdk-windows\tools\ant\main_rules.xml:639: The following error occurred while executing this line:
Q:\Compiler\platform\android\android-sdk-windows\tools\ant\main_rules.xml:271: exec returned: 1

Total time: 4 seconds
Android=C:\Users\Nicholas\Documents\GLBasic\Utilities\GLBasicCommandSet\distribute\Android
success
_______________________________________
*** Finished ***
Elapsed: 16.8 sec. Time: 15:34
Build: 1 succeeded.
« Last Edit: 2011-Jul-07 by MrTAToad »

Offline Minion

  • Mr. Polyvector
  • ***
  • Posts: 228
    • View Profile
Re: How to sign Android apps
« Reply #9 on: 2011-Oct-12 »
Im having a few issues with signing my android app ;( Not sure if Ive got this all right but here goes. I think ive fianlly got me a dev key. So then I drag my distribute folder onto the glb_code_sign.bat, at which point it asks me for keystore. I assume its the key ive just created so I drag that over to the window, hit enter, and then this happens ;( Any advice/help ?


Code: GLBasic [Select]
CODE SIGN - Drag/Drop distribute/Android folder on this
"C:\Users\Windows User\Documents\GLBasic\ARC_Inv\distribute"
Enter Passphrase for keystore:
jarsigner error: java.lang.RuntimeException: keystore load: C:\Program (The syst
em cannot find the file specified)
Unable to open 'bin\glbasic-signed.apk' as zip archive
Press any key to continue . . .
 

Offline ampos

  • Prof. Inline
  • *****
  • Posts: 1592
    • View Profile
    • AMpostata Website
Re: How to sign Android apps
« Reply #10 on: 2011-Oct-12 »
I use this file.bat to sign my android things. I drop the unsigned over it, then a shell opens that ask me about my key. I type it (invisible keytipping!) and it creates 2 files, signed and release.

Quote
@ECHO OFF
ECHO CODE SIGN - Drag/Drop distribute/Android folder on this
echo -----------------------------------------

set GLB_PROJ_PATH=%1
ECHO %GLB_PROJ_PATH%

echo -----------------------------------------

set proy=D:\Programas\GLBasic\proyectos\
echo %proy%
echo %proy%glbasic-signed.apk

echo -----------------------------------------


set ANDROIDSDK=D:\Programas\GLBasic\Compiler\platform\android\android-sdk-windows
echo %androidsdk%

echo -----------------------------------------

set JAVA_HOME="C:\Program Files\Java\jdk1.6.0_25"

set KEYSTORE=d:\android_developer.keystore

echo -----------------------------------------

%java_home%\bin\jarsigner.exe -verbose -keystore "%KEYSTORE%" -signedjar %proy%glbasic-signed.apk %glb_proj_path% alias_name

echo -----------------------------------------

"%ANDROIDSDK%\tools\zipalign.exe" -v 4 %proy%glbasic-signed.apk %proy%glbasic-release.apk

//PAUSE

check my web and/or my blog :D
http://diniplay.blogspot.com (devblog)
http://www.ampostata.org
http://ampostata.blogspot.com
I own PC-Win, MacBook 13", iPhone 3G/3GS/4G and iPAC-WinCE

Offline Minion

  • Mr. Polyvector
  • ***
  • Posts: 228
    • View Profile
Re: How to sign Android apps
« Reply #11 on: 2011-Oct-12 »
I assume that when it asks for passphrase for the keystore, thats the password you used to setup the key ?

I tried a modified version of your bat file and it still doesnt work ;(

Code: GLBasic [Select]
CODE SIGN - Drag/Drop distribute/Android folder on this
-----------------------------------------
"C:\Users\Windows User\Documents\GLBasic\ARC_Inv\distribute\Android\bin\glbasic-
unsigned.apk"

-----------------------------------------
C:\Users\Windows User\Documents\GLBasic\
C:\Users\Windows User\Documents\GLBasic\glbasic-signed.apk
-----------------------------------------
C:\Program Files\GLBasic\Compiler\platform\android\android-sdk-windows\
-----------------------------------------
-----------------------------------------
Enter Passphrase for keystore:
jarsigner: Certificate chain not found for: C:\Users\Windows User\Documents\GLBa
sic\ARC_Inv\distribute\Android\bin\glbasic-unsigned.apk.  C:\Users\Windows User\
Documents\GLBasic\ARC_Inv\distribute\Android\bin\glbasic-unsigned.apk must refer
ence a valid KeyStore key entry containing a private key and corresponding publi
c key certificate chain.
-----------------------------------------
Zip alignment utility
Copyright (C) 2009 The Android Open Source Project

Usage: zipalign [-f] [-v] <align> infile.zip outfile.zip
       zipalign -c [-v] <align> infile.zip

  <align>: alignment in bytes, e.g. '4' provides 32-bit alignment
  -c: check alignment only (does not modify file)
  -f: overwrite existing outfile.zip
  -v: verbose output
Press any key to continue . . .

 

MrTAToad

  • Guest
Re: How to sign Android apps
« Reply #12 on: 2011-Oct-12 »
Might need to use quotes around the % parameter in the batch file as you have spaces in the path name.

Offline AlienMenace

  • Mr. Polyvector
  • ***
  • Posts: 149
    • View Profile
Re: How to sign Android apps
« Reply #13 on: 2011-Nov-25 »
Quote
Password
Full Name
Name of your organisational unit
Name of your organisation
Name of City/Locality
State/Province
Two letter country code

What are you suppose to use for the password? Does this password have something to do with the Keytool utility in the SDK?

Thanks.
Apps published: 3

Offline Minion

  • Mr. Polyvector
  • ***
  • Posts: 228
    • View Profile
Re: How to sign Android apps
« Reply #14 on: 2011-Nov-25 »
Password can be anything you want....just remeber it because you`ll need it when signing your app.