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

1,493 Comments on “Add Google Play public key to a self generated p12

  1. I just wanted to construct a quick comment to be able to say thanks to you for those marvelous strategies you are giving on this site. My rather long internet look up has at the end been paid with awesome tips to exchange with my two friends. I ‘d tell you that we readers are unquestionably endowed to dwell in a useful site with very many marvellous individuals with helpful pointers. I feel extremely fortunate to have come across your website page and look forward to so many more brilliant minutes reading here. Thanks a lot once again for a lot of things.

  2. I in addition to my pals have already been studying the nice secrets and techniques located on the website and then all of the sudden developed a horrible feeling I had not thanked the web blog owner for those strategies. All of the young boys were definitely happy to read through all of them and now have without a doubt been tapping into them. Appreciation for indeed being simply accommodating and for settling on variety of smart themes millions of individuals are really needing to learn about. Our sincere regret for not expressing gratitude to sooner.

  3. I wish to voice my love for your generosity for those who absolutely need help on in this concept. Your special commitment to passing the message all over appeared to be quite effective and have specifically allowed girls much like me to get to their aims. Your entire valuable guideline implies a lot a person like me and much more to my colleagues. Best wishes; from all of us.

  4. I needed to draft you one little word to finally thank you very much yet again for the splendid solutions you’ve featured here. It is certainly wonderfully generous with people like you to deliver publicly all a few people would’ve sold for an e book to make some profit for their own end, and in particular now that you could possibly have done it in case you desired. The advice additionally acted as a fantastic way to comprehend most people have the same interest really like my own to know a good deal more around this condition. I know there are many more enjoyable opportunities ahead for people who looked at your site.

Leave a Reply

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