Saturday, November 2, 2013

Android apps start slowly? Here is the trick

When you run an Android app, it will be a slow startup because of the Android technology choices. The local emulator is an emulator, which means slow. If you attach a phone for development, the app still needs to be packaged, the old app needs to be uninstalled, the new version needs to be installed etc. All this takes time.

Fortunately, there is a solution: You can make your app compile for both Android and Windows, and debug most functionality as a Windows app. These guidelines can help you:

  1. Put all Android-specific code into units that have a name that starts with "Android". For instance, if you want to send an SMS, put the code for that into an AndroidSms.pas unit.
  2. Use {$IFDEF ANDROID} for code that you want to execute only when deployed on Android.
  3. Exclude Android* units from the dpr file
Here is an example of a uses-clause that compiles with both Android and Windows:

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
{$IFDEF ANDROID}
  AndroidSms,
{$ENDIF}
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.StdCtrls;

Here is an example of some code that does one thing on Android, and another thing when compiling to Windows:

procedure TForm2.SendSMS (target,messagestr:string);
begin
  {$IFDEF ANDROID}
  AndroidSms.SendSms(target,messagestr);
  {$ELSE}
  Memo1.Lines.Add (target+': '+messagestr);
  {$ENDIF}
end;

As you can see in this last example, the Windows version will visualize to the programmer, that the app was meant to send an SMS at this point, instead of actually doing it. This speeds up testing a lot.

1 comment:

  1. Waw, Great site. I have read your article. really your have shared your natural Idea. I like your article very much. I think it is the best Webdesign site. Everyone can understand everything easily.
    App Programmierung

    ReplyDelete