Introduction to Phonegap · 5. Connect phone to computer and run! cordova run android The app will...

Preview:

Citation preview

Introduction to

Phonegap

Liyana Lazim [http://liyanawritescode.wordpress.com]

What is phonegap?

Cross-Platform or Call it Hybrid

Pros and Cons:

Pros:

One Language All Platform

Save Development Cost by A LOT

Free

Cons:

Certain native features might not be supported

Slower in performance

Don’t use it for games, unless it is really simple. Due to performance, other platform like Flash and Unity3D are more recommended.

Web Developers can now do mobile apps

Liyana Lazim [http://liyanawritescode.wordpress.com]

Phonegap features:

HTML5

CSS3

Javascript, Jquery

PHP (use ajax)

Bootstrap

Plugins (Cordova add-ons)

Etcs.

Liyana Lazim [http://liyanawritescode.wordpress.com]

Requirements:

Desktop / Laptop

Internet

Android Phone with minimum Android 5

Install Phonegap Desktop, Phonegap Mobile Application, Node.js, Git

Download SDK Manager:

Liyana Lazim [http://liyanawritescode.wordpress.com]

..Download SDK Manager

Link : https://developer.android.com/studio/index.html

Note : Only download the SDK manager, not the Android Studio bundle

Liyana Lazim [http://liyanawritescode.wordpress.com]

..Install SDK Manager and Open SDK

manager SDK manager can be found at your Android folder

Liyana Lazim [http://liyanawritescode.wordpress.com]

..Install Packages

Liyana Lazim [http://liyanawritescode.wordpress.com]

Creating Phonegap

Project

Liyana Lazim [http://liyanawritescode.wordpress.com]

1. Install Phonegap Desktop and Phonegap

Mobile App

Phonegap Desktop download link : http://phonegap.com/getstarted/

Phonegap Mobile App can be downloaded at Google Playstore and Apple Store

Liyana Lazim [http://liyanawritescode.wordpress.com]

2. Open Phonegap Desktop and Create

New Project

Liyana Lazim [http://liyanawritescode.wordpress.com]

Editing the HTML file

Liyana Lazim [http://liyanawritescode.wordpress.com]

1. Click on the local path link.

Liyana Lazim [http://liyanawritescode.wordpress.com]

2. You’ll be directed to your project

directory.

Liyana Lazim [http://liyanawritescode.wordpress.com]

3. Go to ‘www’ > ‘index.html’

Liyana Lazim [http://liyanawritescode.wordpress.com]

4. Edit your files as you like!

You can use :

Jquery Mobile

Bootstrap

Any HTML5 and CSS codes

Javascript & JQuery

Liyana Lazim [http://liyanawritescode.wordpress.com]

Debugging with

Phonegap Desktop and

Phonegap Mobile App

Liyana Lazim [http://liyanawritescode.wordpress.com]

1. Run your project and you’ll see a server

number

Liyana Lazim [http://liyanawritescode.wordpress.com]

2. On your phone, go to Phonegap App and

enter the server number.

** Make sure that your laptop is connected to the same wifi as your phone.

Click Connect and you’ll see your first phonegap app!

Liyana Lazim [http://liyanawritescode.wordpress.com]

Debugging with your

Android Phone

(For Android project

only)

Liyana Lazim [http://liyanawritescode.wordpress.com]

1. Download Node.js

Download Link : https://nodejs.org/download/

Liyana Lazim [http://liyanawritescode.wordpress.com]

2. Open the node.js command prompt and key in: npm install -g cordova

Liyana Lazim [http://liyanawritescode.wordpress.com]

3. In Node.js command prompt, go to

your directory

Liyana Lazim [http://liyanawritescode.wordpress.com]

cd [file path]

Liyana Lazim [http://liyanawritescode.wordpress.com]

4. Add platform:cordova platform add android

Liyana Lazim [http://liyanawritescode.wordpress.com]

5. Connect phone to computer and run!

cordova run android

The app will automatically open on your phone.

**Note: Make sure you activate your developer mode.

Activating developer mode:

1. Go to settings on your phone

2. Find ‘About device’

3. Click ‘Build Number’ multiple times and you will activate the developer

mode.

(If your phone is not Samsung or the above steps does not work, google: ‘How to

activate developer mode for [phone model]’)

Liyana Lazim [http://liyanawritescode.wordpress.com]

Adding Plugin

Liyana Lazim [http://liyanawritescode.wordpress.com]

1. Download Git

Here is the link : https://git-scm.com/downloads

Once you have downloaded Git, find the folder inside C:.

Go inside the bin folder and copy the file location.

Liyana Lazim [http://liyanawritescode.wordpress.com]

..cont

Paste the file location inside the system variable path

(Windows : Control Panel > System and Security > System > Advanced system

settings > Environment Variable>System Variable>Path>Edit)

Liyana Lazim [http://liyanawritescode.wordpress.com]

..cont

The function of Git is for plugins that start from https://github....

You can find plugins at Cordova’s website.

Here is an example of a plugin : https://www.npmjs.com/package/cordova-

plugin-x-socialsharing

Liyana Lazim [http://liyanawritescode.wordpress.com]

2. Find a plugin and find the installation

code snippet

Liyana Lazim [http://liyanawritescode.wordpress.com]

3. In Node.js command prompt, go to

your directory

Liyana Lazim [http://liyanawritescode.wordpress.com]

cd [file path]

Liyana Lazim [http://liyanawritescode.wordpress.com]

4. Add the plugin using nodejs cmd

Liyana Lazim [http://liyanawritescode.wordpress.com]

5. In your html file, call the cordova.js

file

Liyana Lazim [http://liyanawritescode.wordpress.com]

6. Implement the plugin.

You can use the example given in the plugin homepage (where you got the

plugin code snippet)

Liyana Lazim [http://liyanawritescode.wordpress.com]

7. Run your project!

Liyana Lazim [http://liyanawritescode.wordpress.com]

Publishing App

Liyana Lazim [http://liyanawritescode.wordpress.com]

1. In nodeJS command prompt, go to your apk

directory(yourprojectfile > platforms > android > build > outputs > apk)

Liyana Lazim [http://liyanawritescode.wordpress.com]

Liyana Lazim [http://liyanawritescode.wordpress.com]

2. Release the app

Syntax :

cordova build --release android

Generated: Unsigned APK

You can find the unsigned APK at yourprojectfile > platforms > android > build

> outputs > apk

Liyana Lazim [http://liyanawritescode.wordpress.com]

Liyana Lazim [http://liyanawritescode.wordpress.com]

Liyana Lazim [http://liyanawritescode.wordpress.com]

3. Generate keystore

Syntax:

Example:

Keystore generated : myTutorial.keystore

Note: Place the keystore inside yourprojectfile > platforms > android > build > outputs > apk

keytool -genkey -v -keystore <keystoreName>.keystore -alias <KeystoreAliasName> -keyalg <Key algorithm> -keysize <Key size> -validity <KeyValidity in Days>

keytool -genkey -v -keystore myTutorial.keystore -alias myTutorialAlias -keyalg RSA -keysize 2048 -validity 10000

Liyana Lazim [http://liyanawritescode.wordpress.com]

Liyana Lazim [http://liyanawritescode.wordpress.com]

Liyana Lazim [http://liyanawritescode.wordpress.com]

4. Run the jarsigner tool

Syntax :

Example:

Generated: Signed APK

Note : Please make sure <Unsigned APK file> is the same as the one in your

apk file

jarsigner -verbose -sigalg SHA1withRSA -digestalg SHA1 -keystore <keystorename <Unsigned APK file>

<Keystore Alias name>

jarsigner -verbose -sigalg SHA1withRSA -digestalg SHA1 -keystore myTutorial.keystore android-release-unsigned.apk myTutorialAlias

Liyana Lazim [http://liyanawritescode.wordpress.com]

Liyana Lazim [http://liyanawritescode.wordpress.com]

5. Optimize the signed APK using

zipalign

Syntax:

Example:

Generated: Renamed signed APK

zipalign -v 4 Example-release-unsigned.apk Example.apk

zipalign -v 4 android-release-unsigned.apk myFinishedApp.apk

Liyana Lazim [http://liyanawritescode.wordpress.com]

Liyana Lazim [http://liyanawritescode.wordpress.com]

Liyana Lazim [http://liyanawritescode.wordpress.com]

Note

If there is an error regarding zipalign does not exist, try searching

‘zipalign.exe’ from your android SDK directory.

Copy them and paste at your apk directory

yourprojectfile > platforms > android > build > outputs > apk

Liyana Lazim [http://liyanawritescode.wordpress.com]

Liyana Lazim [http://liyanawritescode.wordpress.com]

Liyana Lazim [http://liyanawritescode.wordpress.com]

..Note

If there is an error regarding "Jarsigner is not recognized internal external

command“

Make sure Java JDK is set on the PATH. In my case put

"D:\Java\jdk1.6.0_34\bin" into System Variable PATH.

Liyana Lazim [http://liyanawritescode.wordpress.com]

Copy jdk bin path

Liyana Lazim [http://liyanawritescode.wordpress.com]

Right Click on ‘My Computer’ and Click

‘Properties’

Liyana Lazim [http://liyanawritescode.wordpress.com]

Click ‘Advanced System Settings’

Liyana Lazim [http://liyanawritescode.wordpress.com]

Click ‘Environment Variables’

Liyana Lazim [http://liyanawritescode.wordpress.com]

Paste jdk bin path then click ok

Liyana Lazim [http://liyanawritescode.wordpress.com]

Recommended