Friday, July 18, 2014

Mobile Development with Cordova (Phonegap): Chapters 5 and 6

Chapters 5 and 6: Running it, then Compiling APKs and IPAs


For the moment, let's forego developing the app and skip straight to running it and then compiling it. After all, what good is developing the app if you can't even run it for testing and hand it out to beta testers?


Plug in Your Hardware, Set up Emulators

iOS: Plug in that iPhone 4S and iPad you're using to test, and open the Xcode project file. In the upper-left you'll see a list of emulators and devices. When it's time to run your app, it's as simple as picking from this menu.

iOS: Look over the list of emulators, and  see if there are any missing that you would like. You may need to use Xcode/Preferences/Components to add some emulators, e.g. for testing iOS 7.0 for those folks who couldn't or didn't upgrade to 7.1

Android: Plug in your devices, then run adb devices from the command line. You should see your device listed, if USB Debugging is enabled on the device. Realistically, this almost never works and you'll spend time on Stackoverflow trying to figure out why. :)

Android: Run android from the command line to run the Android SDK manager. Install the necessary platform SDKs (e.g. Android 2.2 if you'll want to test there), then create emulators called Android Virtual Devices (ADVs).



Running it on Android


This should be as easy as:
    cordova prepare android
    cordova run android

The prepare step copies your www  into the target platform folder, where it will be bundled and run. Don't skip this step, or you'll find yourself running older code.

If you have a hardware device attached, it should be used automatically: the app will be injected onto it, and it should even launch automagically. Super convenient.

If you have no hardware attached, the platform will start up an emulator for you if you defined one. (this is mentioned above and also in Chapter 1, Android Virtual Devices or ADVs) If you already have an emulator running, that one will be reused.

To specify a device, give a --target parameter to the cordova run command, like this:
    cordova run android --target=ABC123456

Tip: To see what devices you have attached, run:
    adb devices
 If your device isn't listed, check out Google because that's a very well known and widespread phenomenon. Usually you just forgot to enable USB debugging, but some hardware just isn't gonna work. See chapter 2 where we set up your build environment and test it, for some tips.

In reality, I've had show-stopping problems with running the app on Android:

- The emulators take several minutes to load, if they happen at all. When they do, the app may or may not be there. As such, I've given up on the emulators.

- Some hardware devices are never detected via adb devices and therefore cordova run android doesn't detect them. In particular was my LG Spectrum, which would about 1 time in 50 be detected by adb devices but then disappear again. Eventually I went to Fry's and hit up Groupon, and got a Blu brand phone for $60 and a Nexus tablet for $100, and both of them worked fine. Moral of the story: the problem may be the device.

In the worst of times, my process for Android devolved back to old-school techniques: I compile the APK, then email it or upload it, so I can download it on my phone and install it. This is less than ideal, but if adb doesn't see it, not a lot I can do.


Running it on iOS

In Xcode with the project open, look in the upper-left. You'll see a list of devices, some of them your attached devices and some of them emulators. Select the one you like, then go to Project/Run.

Simple and slick.

Tip: You can terminate the app with Project/Stop.

Tip: You need a Provisioning Profile to distribute your app to other devices, but if you're using emulators and cable-attached devices, you can skip it for now.

Building an APK for Distribution (Android)

To compile an APK for Android:
    cordova build android --release

The resulting APK for Android will be in platforms/android/ant-build

You can submit this to the app store, hand it to your friends, etc. You can ship it on an SD card, email it, whatever.

Tip: Some versions of Android refuse to install an APK unless it comes from the authorized Store. If someone's trying to beta test and hits upon this, visit the Settings panel and see about enabling non-Store applications.


Tip: I prefer to always use the --release flag. It prompts me for the password (once for the keystore, then again for the specific key) but that's okay. The resulting APK will be useful to anybody who tries it. I know that if I get distracted I may generate a non-signed APK that only works for me, then hand that out to beta testers whose phones will refuse to install it.

Building an IPA for Distribution (iOS)

Load up the Xcode project. Hit up Project / Run which tries to run your app. It will run it on whichever device is displayed in the upper-left, be it an emulator or a real device.

Also in the Project menu is the Archive option. If it's greyed out, select a hardware device in the upper-left for running your app. You must have a hardware device attached and selected, for the Archive option to become enabled. Select the Archive option.

You'll be asked to choose a provisioning profile. Select whichever one you generated for Ad Hoc uses. (See Appendix D where I describe generating provisioning profiles)

Finally, the IPA file will be created for you, and is ready to hand out to your beta testers.


No comments:

Post a Comment

Note: Only a member of this blog may post a comment.