Sunday, February 15, 2015

Build and publish Android application with Phonegap






Build and publish android application with phonegap

In this post I'm going to describe how to build an android application with Phonegap, WebStorm IDE, and publish it in Google Play Store.  

I'm using Cordova version 3.5.0, WebStorm 9.0.3, Yosemite 10.10.1.

I would assume that you already have Node.js and Android Studio installed on your machine and that using 'SDK Manager' in Android Studio you already installed 'platform-tools' and you have at least 'api rev 19'+.
It is important that you should have phonegap and/or cordova installed globally - I'll be using cordova.
You should have the Google Play store developer's account.

Why WebStorm

WebStorm IDE is used only to speed the process up. Why is it helpful:
- it builds the PhoneGap/Cordova project from scratch
- it provides you with build in terminal
- it provides you with the ability to configure Node, Git, Mocha, Phonegap commands which you use most often in your project
- very useful version control functionalities
- and a lot more...

(I'm not selling the product I'm just a happy user)

Let's get started

1. Setup the project with WS using 'new project' option and choose 'PhoneGap/Cordova project'


2. Add android platform to the project (you can use a build in terminal for that)

cd /path/to/project/ && cordova platform add android
You should see that additional folder appeared in 'platforms' directory of your project.


3. Debugging your app (2 ways)

- Android Device - connect your device using usb (make sure you have all debugging options set on your phone/tablet and it's is unlocked)


cordova run android --debug
- Browser - use python in terminal to create a simple server and debug your app (e.g. Chrome)


cd /path/to/project/ && python -m SimpleHTTPServer

Now you should be able to open your project in the browser (most probably http://localhost:8000/www/).


4. As soon as you are ready to build the final product try using

cordova build --release android

After this step you should see your application (apk file) in 'platform/android/ant-build' directory. You may try to install your app by transferring the apk file to your device. If you come across errors try uninstalling previous installations of the app on the device. 

I'm sure that just like me at this point you would like to upload your application to Play Store. The only issue is that your application still isn't signed. You didn't generate the 'keystore' for you application yet. I think that a good idea is to create the keystore somewhere in the root of your project. To do that use the following commands:

keytool -genkey -v -keystore <keystoreName>.keystore -alias <Keystore AliasName> -keyalg <Key algorithm> -keysize <Key size> -validity <Key Validity in Days>

e.g.:


keytool -genkey -v -keystore NAME-mobileapps.keystore -alias NAMEmobileapps -keyalg RSA -keysize 2048 -validity 10000

Remember what password and alias you've used as you'll be asked for that later on.


5. now you have to make sure that your application is going to be signed when you build it

Create the 'ant.properties' file if it's not already available in 'platform/android/' directory. 


key.store=projectroot/filepath/key_name.keystore

key.alias=myApp_alias

Next, build your application and it should be properly signed.

6. Create the release version of your application
cordova build --release android
and upload it to your play store!


Zipalign

Before you prepare yourself for your application deployment make sure you have 'zipalign' in 'your_path_to/adt-bundle.../sdk/platform-tools/'. It's going to be needed to optimise the apk before the upload to production. You should follow the commands here.

"com.example" package issue

You might have some difficulties while uploading your final apk file even after signing the application. If you get 'com.example' it means that you have to go to config.xml and AndroidManifest.xml and change the package name. After you do that rebuild the project and upload the apk file again.

Updating your application

Before you upload your updated apk to the Google Play Store remember that you have to update the version of your code. This one can be found in AndroidManifest.xml inside Android platform folder - android:versionCode="10000"
To generate the updated version of this file go to config.xml of your project and simply change 'version="1.0.0"' to anything appropriate.

Conclusion

It may be easy to develop some applications with phonegap. However, we still have to find the optimal way to debug, build, and publish them. I hope I managed to shed some light on the topic.

Please let me know if I missed something or something is unclear in the description above.

References:

3. stackoverflow - link 1
4. stackoverflow - link 2
5. developer.android.com
6. ionic



1 comment:

  1. I find this article is very informative and helpful. Thank you for sharing!


    Mobile App Developer

    ReplyDelete