Expert Delphi
上QQ阅读APP看书,第一时间看更新

Deploying to Android

Deploying apps from Delphi to Android devices is simpler then deploying to iOS. You only need to have an Android device and a USB cable to connect your device to a Windows machine where you have Delphi installed.

Before deploying to an Android device, you need to find out if your device is supported by the version of Delphi you are using. For details, consult http://docwiki.embarcadero.com/RADStudio/en/Android_Devices_Supported_for_Application_Development for the list of currently supported Android versions.

The first step is to enable USB debugging on the device. This option can be set in Developer options on your device. This option is not easy to find. It is, in fact, hidden. On your Android device, go to the Settings | About menu. Tap the Build number seven times to make Settings | Developer options available. Then, check the USB Debugging option. Connect your Android device with a USB cable to a computer where your PC is running. If you are using VMware make sure that your Android device is connected to the virtual machine where you have Delphi and not to your Mac. The first time you connect your Android device, you may see a message displayed on the device to Allow USB debugging from the computer, identified by a string of hexadecimal numbers that represents the computer's RSA key fingerprint. Check the Always allow from this computer option and click on OK.

More info on enabling USB debugging on Android devices is available at http://docwiki.embarcadero.com/RADStudio/en/Enabling_USB_Debugging_on_an_Android_Device.

The next step is to install the USB driver for your Android device on the machine where Delphi is installed. The test device that I'm using is Google Nexus 7.

Probably the easiest option to download all the necessary bits for Android development is to download and install Android Studio for free from Google. It will download all necessary Java SDKs and tools that are needed in order to build an Android apk executable file. For reference, I'm using Android Studio version 2.2.2. On the splash, select the Android SDK option. In the dialog, select the SDK Tools tab and check the Google USB Driver option. Click OK and the driver will be downloaded and installed, as shown in the following screenshot:

Android SDK Tools with the "Google USB Driver" option checked

In order to make sure that the device driver has been installed successfully, go to the Device Manager window in Control Panel and you should find your device listed. If you right-click on the device, there is an option to Update Device Driver. I could find my Nexus 7 listed there and the device driver was up to date. Refer to the following screenshot:

Nexus 7 in Device Manager

Depending on your device, the steps may be different. You can find detailed information on how to install the USB driver for your Android device at http://docwiki.embarcadero.com/RADStudio/en/Installing_the_USB_Driver_for_Your_Android_Device.

Now, back to Delphi IDE. Make sure that our DelphiHelloWorld project is still open in the IDE. In the Project Manager, expand Target Platforms and click on Android. The first time you click on the Target node, you will see the message Android SDK tools are required. Do you want to download and install Android SDK tools automatically?. Click on Yes. The process to download and install SDK tools will start and you will see in the Command Prompt window the progress of the operation. After a moment, you should see your device listed under Tools in the Project Manager.

Double-click on the Android node to select the Android target. It should display in bold font. Now this is the current target, so when you click on the Run green arrow, the IDE will build our Delphi Hello World app using the Delphi Android compiler and the executable apk Android file will be created, deployed to the device, and run in just one operation.

Click on Run. The project will be compiled, deployed to your Android device, and run:

Delphi "Hello World" app running on Android

You can find more information about preparing your IDE for running apps on Android devices on DocWiki at http://docwiki.embarcadero.com/RADStudio/en/Android_Mobile_Application_Development.