Android Deploy Script, in Delphi

Samuel R. O.
3 min read21 hours ago

--

Those who work in RAD Studio IDE (in particular — Delphi), know very well how easy it is to deploy apps on the devices connected to the machine. It’s not even necessary to explain it. It simply requires selecting the device (or emulator) and pressing “F9”.

Setting up everything

However, I would like to share an alternative that allows a few customizations along the way. We first need two things installed, assuming our development environment is Windows:

  1. Android Studio (even if you do not use an emulator).
  2. Microsoft Net Framework v4.5.

With these two installed, make sure to add these folders to the System environment variables. First, in Path, add these directories:

  1. C:\Users\<your_user>\AppData\Local\Android\Sdk\emulator
  2. C:\Users\<your_user>\AppData\Local\Android\Sdk\platform-tools
  3. C:\Windows\Microsoft.NET\Framework\v4.0.30319

This will ensure that MSBuild, adb, and other tools are accessible via the terminal. Secondly, add these three variables to your user or system:

  1. BDS, with value “C:\Program Files (x86)\Embarcadero\Studio\23.0
  2. FrameworkDir, with value “C:\Windows\Microsoft.NET\Framework\v4.0.30319”.
  3. FrameworkVersion, with value “v4.5”.
Should be this way

Last but not least, let’s also assume the location of the dproj file is two folders upwards of the location where our scripts for release and debug will be. That is:

Only to understand the steps we will be adding to the scripts later. As you observed, my application name is “AppPisces”, thus the package name for it is “com.embarcadero.AppPisces”.

The script

In a very simple and straightforward way, the script to deploy an APK in the emulator is shown bellow. Each step is self-explained in the code.

Only if you are not willing to use an emulator, you should change the steps where the emulator’s name is retrieved and replace it with your device ip, nothing else. A brief explanation for the last command states that the reason why the activity is named in this way is due to how Delphi manages the framework that makes a bridge with the JNIs behind the scenes.

Deploying

We could then use this script after building events from the IDE, replacing the use of native deployment. In this way, we only need to specify the path where our script is and place it under the commands section of the target we want to deploy. You can access it under Project > Options > Building > Build Events.

Then, pressing Ctrl + F9 will cause the IDE to compile the project, build the app, and deploy it on the device immediately after.

Why this way?

I find this to be a cooler way for several reasons, especially after a few nightmares programming with the RAD Studio IDE targeting Android.

  1. It prevents the IDE from pushing several trash files to your device, leaving only the app to be served.
  2. You can customize other steps along the deployment process.
  3. You can also deploy it to your physical device, via Wi-fi.

The biggest trade-off is we cannot debug via the IDE, as we are not using a PAServer. We have to rely on other techniques like the adb logcat. This requires us to have sharpened debugging skills.

--

--