DEV STUFF

Damien Pegoraro

Add Google Play public key to a self generated p12

FlashDevelop

Ok here’s the point:

Flash Develop is now really a great tool for producing air mobile apps. With a few batch files, the workflow is quite easy to catch and producing your first ipa or apk has never been so cool. (No need for XCode!)

For Android, FD generates a certificate (.P12 ) that is used to build the final apk. The setup for this certificate takes place in Run.bat


:: Android packaging
set AND_CERT_NAME="MyApp"
set AND_CERT_PASS=fd
set AND_CERT_FILE=cert\MyApp.p12
set AND_ICONS=icons/android
set AND_SIGNING_OPTIONS=-storetype pkcs12 -keystore "%AND_CERT_FILE%" -storepass %AND_CERT_PASS%

When you create an app on Google Play Dev Console, a Google API key is generated. It’s something like that:

MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAlHEYTHISISMYPUBLICKEY/jIUMdq8Zv6u7oHOK33bPp8C6GxMdMRIatvyePX1V**********CXEH340EZHD1GRCGZX9d2LrrUM6oVmuSU3dKdgRgQxKGtm2qrT3s63n************************trif7sPutdOftxrZUl87AAesz1XIqM1aXcZmcXLBN72a*********************POveqTThbtaR8N41U+QS+1U8JEwIQFwavGM1QIDAQAB

To use this key, you must include it to your p12 but FlashDevelop doesn’t provide any tool to do this at the moment. Hopefully, some good guy named nukeme2 posted a detailed howto.

The main idea is to install OpenSSL, unpack the generated p12, clean the file, add the key, repack the file.

That’s a bit boring to do, so here’s a little tool I wrote in batch (and that was really painful). It’s made to work with FlashDevelop. Just unzip and paste it at the same level as your Run.bat file.

As I’m not really used to batch programming, the code may be a little clumsy, but here it is:

A little config first, edit these two lines to match your settings:

@echo off
set PAUSE_ERRORS=1

::install openssl somewhere
set OPEN_SSL_PATH=C:\OpenSSL-Win32

:: grab your key on google developer console
set AND_PUBLIC_KEY=YOUR_API_KEY

All other data comes from FD SetupApplication.bat


call bat\SetupSDK.bat
call bat\SetupApplication.bat

echo Extracting key...

set OPENSSL_CONF=%OPEN_SSL_PATH%\bin\openssl.cfg
%OPEN_SSL_PATH%\bin\openssl pkcs12 -in %AND_CERT_FILE% -out %OPEN_SSL_PATH%\bin\tmp.pem -password pass:%AND_CERT_PASS% -nodes

echo Cleaning file...

if exist %OPEN_SSL_PATH%\bin\out.pem (del %OPEN_SSL_PATH%\bin\out.pem)

for /f "tokens=*" %%a in (%OPEN_SSL_PATH%\bin\tmp.pem) do call :cleanfile %%a

echo Writing key...

echo -----BEGIN RSA PUBLIC KEY----->> %OPEN_SSL_PATH%\bin\out.pem

echo.%AND_PUBLIC_KEY%>KEY_LENGTH
for %%a in (KEY_LENGTH) do set /a KEY_LENGTH=%%~za -2
set /a KEY_LINES=%KEY_LENGTH%/64
::ceil num lines if necessary
set /a TMP_CHARS=%KEY_LINES%*64
if %KEY_LENGTH% GTR %TMP_CHARS% (set /a KEY_LINES=%KEY_LINES%+1)
set /a goal=%KEY_LINES%-1
for /l %%v in (0, 1,%goal%) do call :pickline %%v

echo -----END RSA PUBLIC KEY----->> %OPEN_SSL_PATH%\bin\out.pem

echo Converting back to P12...

rename %AND_CERT_FILE% oldandroid.p12
%OPEN_SSL_PATH%\bin\openssl pkcs12 -export -in %OPEN_SSL_PATH%\bin\out.pem -inkey %OPEN_SSL_PATH%\bin\out.pem -out %AND_CERT_FILE% -password pass:%AND_CERT_PASS%

echo Cleaning files...

if exist %OPEN_SSL_PATH%\bin\out.pem (del %OPEN_SSL_PATH%\bin\out.pem)
if exist %OPEN_SSL_PATH%\bin\tmp.pem (del %OPEN_SSL_PATH%\bin\tmp.pem)
if exist KEY_LENGTH (del KEY_LENGTH)

echo Done !

pause
goto :eof

:pickline
setlocal enableDelayedExpansion

set line=%*

set /a ll=%line%*64
set KL=!AND_PUBLIC_KEY:~%ll%,64!
echo %KL%>> %OPEN_SSL_PATH%\bin\out.pem

endlocal
goto :eof

:cleanfile

set current=%*
set KEEP_IT=Y

SET current|FINDSTR /b "current="|FINDSTR /i Attributes >nul
IF not ERRORLEVEL 1 (set KEEP_IT=N)

SET current|FINDSTR /b "current="|FINDSTR /i localKeyID >nul
IF not ERRORLEVEL 1 (set KEEP_IT=N)

SET current|FINDSTR /b "current="|FINDSTR /i subject= >nul
IF not ERRORLEVEL 1 (set KEEP_IT=N)

SET current|FINDSTR /b "current="|FINDSTR /i issuer= >nul
IF not ERRORLEVEL 1 (set KEEP_IT=N)

IF %KEEP_IT%==Y (echo %current%>> %OPEN_SSL_PATH%\bin\out.pem)

goto :eof
:eof

The old p12 file is moved to oldandroid.p12, just rebuild your apk as your normally would. This should be okay, but let know if not!

Thanks for not reading and scrolling directly here.

The file:

IncludeGoogleKey

4 Comments on “Add Google Play public key to a self generated p12

  1. Thanks mate! Well explained and worked flawlessly. Cudos!

  2. I see you don’t monetize your page, don’t waste your traffic,
    you can earn additional bucks every month because you’ve got
    hi quality content. If you want to know how to make extra bucks, search for: Boorfe’s tips best adsense alternative

Leave a Reply

Your email address will not be published. Required fields are marked *