Recent

Author Topic: Tutorial - Android & Lazarus for novices like me  (Read 59209 times)

kitsaros

  • New Member
  • *
  • Posts: 27
Tutorial - Android & Lazarus for novices like me
« on: October 21, 2013, 10:02:40 pm »
I have made a quick tutorial on how to create android apps with Lazarus.
In this tutorial i am using JDK 1.6u45 + SDK 22.2.1 + ndk-r9 + Laz4Android1.1-41139-FPC2.7.1

1)Create on C:\ a folder called "Android"

2)Install JDK 1.6u45 (i have installed jdk-6u45-windows-x64.exe)
http://www.oracle.com/technetwork/java/javasebusiness/downloads/java-archive-downloads-javase6-419409.html#jdk-6u45-oth-JPR
It will be installed in to C:\Program Files\Java\.

3) Installing SDK ...
Go to http://developer.android.com/sdk/index.html
DOWNLOAD FOR OTHER PLATFORMS > SDK Tools Only
installer_r22.2.1-windows.exe (Recommended)
Install sdk (installer_r22.2.1-windows) into C:\Android\android-sdk
Copy at "C:\Android\android-sdk\tools" the file "apkbuilder.bat" (can be found here
https://github.com/ACSOP/android_sdk/raw/master/apkbuilder/etc/apkbuilder.bat )

Now update the SDK [WIN START > SDK Manager ]
add android 4.0 (API 14)
add android 2.2 (API 8]
Install packages.

4) Installing NDK ...
http://developer.android.com/tools/sdk/ndk/index.html
Download only the file android-ndk-r9-windows-x86.zip
Unzip android-ndk-r9-windows-x86.zip into C:\Android\android-ndk-r9

5) Installing Laz4Android1.1-41139-FPC2.7.1.7z ...
Download it from https://skydrive.live.com/?cid=89ae6b50650182c6&id=89AE6B50650182C6!149
Unzip Laz4Android1.1-41139-FPC2.7.1.7z into C:\Android\laz4android
Copy
C:\Android\android-ndk-r9\toolchains\arm-linux-androideabi-4.6\prebuilt\windows\bin\arm-linux-androideabi-*.exe
to
C:\Android\laz4android\fpc\2.7.1\bin\i386-win32\*

Edit the file C:\Android\laz4android\build.bat like this :
SET FPC_BIN_PATH=C:\Android\laz4android
Run build.bat (doubleclick) ->  It will compile & bulid lazarus.

6) Rebuilding Laz4Android IDE ...
Run C:\Android\laz4android\lazarus.exe
Select "Start IDE"
select "Package" "Install/Uninstall Packages"
select customdrawn 0.0 > install selection and hit save and rebuild IDE , continue

7) Compiling the demo "androidlcl"
Copy C:\Android\laz4android\examples\androidlcl at D:\Projects\android\androidlcl
Run "C:\Android\laz4android\lazarus.exe"
File > open "D:\Projects\android\androidlcl\androidlcltest.lpr"
Project > project options

Compiler options >  select "Release TAndroid"

Paths > 

Libraries -Ll :
C:\Android\android-ndk-r9\platforms\android-8\arch-arm\usr\lib;C:\Android\android-ndk-r9\toolchains\arm-linux-androideabi-4.6\prebuilt\windows\lib\gcc\arm-linux-androideabi\4.6

Target file name :
android\libs\armeabi\liblclapp.so

Code generation
  Target OS -T android
  Target CPU -P arm
  Target processor default

Linking :
  Enable strip symbols (-Xs)

Other : -dANDROID -Xd -CpARMV6 -FLlibdl.so

Ide macro values :
macro name : LCLWidgetType
macro value : customdrawn

Now
Run > Compile this will generate this library :
D:\Projects\android\androidlcl\android\libs\armeabi\liblclapp.so [5.073 kb]

8] Deployment ...
a) Edit with a text editor
D:\Projects\android\androidlcl\android\generate_debug_key.bat
Modify the first 6 lines like this :
Code: [Select]
REM Adjust these paths to yours
SET PATH=C:\Android\android-sdk\tools;C:\Android\android-sdk\build-tools\17.0.0;C:\Android\android-sdk\platform-tools\;C:\Progra~1\Java\jdk1.6.0_45\bin
SET APP_NAME=lcltest
SET ANDROID_HOME=C:\Android\android-sdk
SET APK_SDK_PLATFORM=C:\Android\android-sdk\platforms\android-8
SET APK_PROJECT_PATH=D:\Projects\android\androidlcl\android
Run "generate_debug_key.bat"
Use as password : "senhas"
This will produce the file :
D:\Projects\android\androidlcl\android\bin\LCLDebugKey.keystore

b) Edit with a text editor
D:\Projects\android\androidlcl\android\build_debug_apk.bat
Modify the first 6 lines like this :
Code: [Select]
REM Adjust these paths to yours
SET PATH=C:\Android\android-sdk\tools;C:\Android\android-sdk\build-tools\17.0.0;C:\Android\android-sdk\platform-tools\;C:\Progra~1\Java\jdk1.6.0_45\bin
SET APP_NAME=lcltest
SET ANDROID_HOME=C:\Android\android-sdk
SET APK_SDK_PLATFORM=C:\Android\android-sdk\platforms\android-8
SET APK_PROJECT_PATH=D:\Projects\android\androidlcl\android
run "build_debug_apk.bat" answer at the questions and give the password "senhas" when asked
This will generate
D:\Projects\android\androidlcl\android\bin\lcltest.apk [1.549 kb]

c) Create an Android Virtual Device
START >  AVD Manager
New > AVD name > give a name
Device 5.1 WVGA
Target Android 4 API 14
Hit OK
Once created select the virtual device and hit "start" > "Launch" wait a couple of minutes.
 

d) Edit with a text editor
D:\Projects\android\androidlcl\android\adb_install.bat
Modify  like this : 
Code: [Select]
REM Adjust these paths to yours
SET PATH=C:\Android\android-sdk\tools;C:\Android\android-sdk\build-tools\17.0.0;C:\Android\android-sdk\platform-tools\;C:\Progra~1\Java\jdk1.6.0_45\bin

adb uninstall com.pascal.lcltest
adb install bin\lcltest.apk

pause
Run "adb_install.bat" . This will install the "LCL Test" into your Virtual device

e) Debugging
Edit with a text editor the file 
D:\Projects\android\androidlcl\android\adb_install.bat
like this :
Code: [Select]
REM Adjust these paths to yours
SET PATH=C:\Android\android-sdk\tools;C:\Android\android-sdk\build-tools\17.0.0;C:\Android\android-sdk\platform-tools\;C:\Progra~1\Java\jdk1.6.0_45\bin

adb logcat > std.txt

pause
By running this you will get a debug file here :
D:\Projects\android\androidlcl\android\std.txt

Enjoy !
« Last Edit: October 21, 2013, 10:16:01 pm by kitsaros »

jwdietrich

  • Hero Member
  • *****
  • Posts: 1036
    • formatio reticularis
Re: Tutorial - Android & Lazarus for novices like me
« Reply #1 on: October 21, 2013, 10:27:31 pm »
Thank you! Would you mind to write a tutorial like this in the Lazarus wiki?
function GetRandomNumber: integer; // xkcd.com
begin
  GetRandomNumber := 4; // chosen by fair dice roll. Guaranteed to be random.
end;

http://www.formatio-reticularis.de

Lazarus 2.0.6 | FPC 3.0.4 | PPC, Intel, ARM | macOS, Windows, Linux

Daren453

  • Newbie
  • Posts: 3
Re: Tutorial - Android & Lazarus for novices like me
« Reply #2 on: November 04, 2013, 01:55:48 am »
I have tried three other installation tutorials and this one was the first that worked for me. Thanks!

sys

  • Newbie
  • Posts: 4
Re: Tutorial - Android & Lazarus for novices like me
« Reply #3 on: November 26, 2013, 07:24:49 pm »
Followed steps (updated paths, build-tools\19.0.0 and ..android-ndk-r9b..)

No problems, but running "lcl test" crashes (Unfortuntely, LCL Test has stopped)

E/AndroidRuntime(  631): FATAL EXCEPTION: main
E/AndroidRuntime(  631): java.lang.UnsatisfiedLinkError: LCLOnCreate

Did something wrong?

kitsaros

  • New Member
  • *
  • Posts: 27
Re: Tutorial - Android & Lazarus for novices like me
« Reply #4 on: December 03, 2013, 06:00:15 pm »
@sys pay attention at the 8] step. check the paths !
@all : i do not have the time to post it to the wiki but i wish someone does this work. I do not request anything in exchange i just wanted to help.

johnjces

  • New member
  • *
  • Posts: 6
Re: Tutorial - Android & Lazarus for novices like me
« Reply #5 on: February 21, 2014, 04:10:13 am »
I am not new to Delphi or Lazarus, but can anyone advise if these instructions work for the SourceForge project laz2android at

http://sourceforge.net/projects/laz4android/ and...

has anyone had any luck with laztoapk, also located on SourceForge at

http://sourceforge.net/projects/laztoapk/

I have Googled and searched and have just now come across this setup tutorial and I have been beating my head against a rock for days trying to get things to work. Plus, there is no forum or wiki on the above SourceForge sites.

One last question, in my attempts to compile up an Android app, such as the lcl android example, I have had better luck with 'Quick Compile'. Compile, Build etc always error. So, if las4android gets setup correctly, what build/compile option is used?

Thanks.

John

johnjces

  • New member
  • *
  • Posts: 6
Re: Tutorial - Android & Lazarus for novices like me
« Reply #6 on: February 21, 2014, 07:00:45 am »
I am not a newbie to Delphi or Lazarus but a newbie to Android programming. I guess I have been to used to a 'set and forget about it' type of compiler. In any event, I followed these instructions above explicitly, checked my paths and etc.

I get the following errors;

C:\Android\laz4android\fpc\2.7.1\bin\i386-win32\arm-linux-androideabi-ld.bfd.exe: cannot find crtbegin_so.o
C:\Android\laz4android\fpc\2.7.1\bin\i386-win32\arm-linux-androideabi-ld.bfd.exe: cannot find -ljnigraphics
C:\Android\laz4android\fpc\2.7.1\bin\i386-win32\arm-linux-androideabi-ld.bfd.exe: cannot find -llog
C:\Android\laz4android\fpc\2.7.1\bin\i386-win32\arm-linux-androideabi-ld.bfd.exe: cannot find -lc
C:\Android\laz4android\fpc\2.7.1\bin\i386-win32\arm-linux-androideabi-ld.bfd.exe: cannot find -lc

What in the world are these files that the linker cannot find? Look more like command line arguments, but...

Also, I did not see an option under linking that would allow the strip symbols, (-Xs). Could that be my problem?

Thanks for any help or pointers!

John

DelphiFreak

  • Full Member
  • ***
  • Posts: 246
    • Fresh sound.
Re: Tutorial - Android & Lazarus for novices like me
« Reply #7 on: February 21, 2014, 07:09:54 am »
Hi,

I am the author of the tutorial and the developer of laztoapk and truetom is the author of laz4android.

As the name of my setup says, it's "alpha" and "experimental".
To setup the whole toolchain is very tricky and depends on so many tools&frameworks. (jdk,sdk,ndk,lazarus,fpc,customdrawn components).

I am working hard on a new version of the tutorial and a new setup, but my sparetime is very limited.

The version 41139 from May 2013 is here and creates working apps.
http://sdrv.ms/12cHbIZ

The version 43585 from http://sourceforge.net/projects/laz4android/ does also create executables, but they crash as soon as you click onto the app.

Me and truetom have no idea why. I have filled a bug in the fpc bugtracker about the problem.

So I have to go back to laz4android 41139 at the moment, release a new setup and update the tutorial.
I am not sure if this is all possibile this weekend. Please be patient.


Linux Mint 19.1, Lazarus 2.0, Windows 7&10, Delphi 7, Delphi 10.3 Rio

DelphiFreak

  • Full Member
  • ***
  • Posts: 246
    • Fresh sound.
Re: Tutorial - Android & Lazarus for novices like me
« Reply #8 on: February 21, 2014, 11:08:29 pm »
Ok, new setup and new tutorial is now available here http://sourceforge.net/projects/laztoapk/files/?source=navbar

Have fun.
Linux Mint 19.1, Lazarus 2.0, Windows 7&10, Delphi 7, Delphi 10.3 Rio

johnjces

  • New member
  • *
  • Posts: 6
Re: Tutorial - Android & Lazarus for novices like me
« Reply #9 on: February 22, 2014, 03:03:26 am »
@ DelphiFreak. Thanks so much! I do realize all is Alpha at best and thank-you so much for your help, making this stuff available on Sourceforge as well. I will check it out and please know your work and time is appreciated.

John

johnjces

  • New member
  • *
  • Posts: 6
Re: Tutorial - Android & Lazarus for novices like me
« Reply #10 on: February 22, 2014, 05:36:29 pm »
Good day!

Since I really would like to help with this project, I thought I'd give my results on the latest laz4android and laztoapk. I am not certain exactly where to report joy and no-joy, so if there is a better place, please let me know.

I have a Lenovo T61 laptop with a Core 2 Duo, 4 gb of ram and 32 bit Windows 7 Ultimate. I followed the instructions to the letter and this time I even downloaded the exact JDK mentioned near the top of the tutorial from Oracle. I had to create an account to get an archived JDK.

I created an Android directory off of my root, C:\android and made certain that all files and all directories had full permissions and and everything had read/write permissions to ensure no issues with UAC, etc. Installed everything into this directory and followed the guide, double checking my work/entries.

Following the tutorial I created the empty project1 directory as directed in laztoapk and did the build and install project, and was pleased that a lazarus project was created in this directory. Continuing and after creating the key and entering the password, everything worked to that point and I was reminded to enter the password in subsequent screens as it tries to build an APK.

Moving on and entering the password, entering other stuff, i.e., name, city and etc., it came time to enter the password again or hit enter to use the same password. Beyond this point it errors every time not being able to find project1-realigned.apk. I do get a zero byte project1-unsigned.apk. It simply cannot find files.

I moved things around to my C:\users\john directory thinking I had some permissions issues. I will explore this a bit further.

In trying to laz4android again, I get the get the exact same errors when trying to compile the project1 example or the lcl example as posted above. It cannot find files that appear to be command line arguments.

@Delphifreak, what OS are you using in which things work? Anyway, just a report hoping it is helpful to you.

DelphiFreak

  • Full Member
  • ***
  • Posts: 246
    • Fresh sound.
Re: Tutorial - Android & Lazarus for novices like me
« Reply #11 on: February 22, 2014, 06:26:41 pm »
Hello  johnjces,

thank you for your feedback. I use Windows XP SP3.

Go to the folder project1\android

You will find the batch file "build_debug_apk.bat".
Open it with a text editor and add the word "pause" at the end. Save the batch file and run it.
Then provide me please the screen output. I will try to help you.
Linux Mint 19.1, Lazarus 2.0, Windows 7&10, Delphi 7, Delphi 10.3 Rio

johnjces

  • New member
  • *
  • Posts: 6
Re: Tutorial - Android & Lazarus for novices like me
« Reply #12 on: February 23, 2014, 05:14:17 am »
Thanks DelphiFreak!

I have uninstalled everything and reinstalled everything, (getting good at it), and now was able to create a good apk! Not certain what my issue is/was. But now I am having difficulty with my Android emulator. It starts and just keeps 'twinkling' ANDROID and it never starts as it did before.

So, I need to reinstall the AVD and see what happens. I'll report later. But I wish I knew what my problem was and how doing another install solved it.

john

johnjces

  • New member
  • *
  • Posts: 6
Re: Tutorial - Android & Lazarus for novices like me
« Reply #13 on: February 23, 2014, 05:36:33 pm »
Got all things working and I am impressed.  ::)

I still cannot compile the LCL example under the laz4android\examples directory, however.  %)

Now to learn nuances of Android programming and see if Lazarus and FPC is viable.

Obviously, Lazarus cannot create an Android project and a new project needs to be hand written or started with laztoapk. Is this correct?

Thanks!
« Last Edit: February 23, 2014, 05:38:05 pm by johnjces »

Mazarus

  • Newbie
  • Posts: 3
Re: Tutorial - Android & Lazarus for novices like me
« Reply #14 on: February 23, 2014, 09:17:34 pm »
Hello, I'm new here. :)

Thanks to DelphiFreak, I was able to create an apk. The new tutorial and laztoapk helped so much!

Here are some things I discovered and I will remember for using laztoapk:

1. To install or uninstall an apk, the AVD device has to run.
2. I had the same problem like johnjces. For no obvious reason, my created AVD device freezed while starting.  It showed this "Android" starting sequence forever. I had to delete it and created a new device.
3. When I type in my password for creating the Android key, there is no visual hint that I'm typing, but the program is receiving the input. I don't need to type in any real personal information when the program asks for name, etc. ENTER is enough. After asking this personal information, I have to type in any letter (but not "n") and ENTER to continue.

I have a general Android question:
Where do I see the limitations of programming for Android? For example: I made a TCDButton green in Lazarus. It still was gray in the AVD device. TTimer works. Canvas.Pixels doesn't. TButton does. So I would really like to know what Components/Properties are supported by Android.
« Last Edit: February 23, 2014, 09:21:00 pm by Mazarus »