jmpessoa / lazandroidmodulewizard

LAMW: Lazarus Android Module Wizard: Form Designer and Components development model!
247 stars 82 forks source link

[Feature] Support for latest stable NDK (revision 22) #386

Open WayneSherman opened 3 years ago

WayneSherman commented 3 years ago

Feature Request: LAMW support NDK r22

The latest stable NDK is r22b: https://developer.android.com/ndk/downloads https://github.com/android/ndk/wiki/Changelog-r22

I did some basic build and run tests with NDK r22 with the following environment:

My test programs compile and build without errors. They run correctly on a Pixel 4 w/ Android 11.

jmpessoa commented 3 years ago

Good NEWS!

What we need change? What about the missing "platforms" folder?

We have reference to it in project .lpi here:

Libraries Value="C:\android\ndkr18b\platforms\android-22\arch-arm\usr\lib\; C:\android\ndkr18b\toolchains\arm-linux-androideabi-4.9\prebuilt\windows-x86_64\lib\gcc\arm-linux-androideabi\4.9.x\"

Can we dropped this reference?

Thank you!

WayneSherman commented 3 years ago

We have reference to it in project .lpi here: Libraries Value="C:\android\ndkr18b\platforms\android-22\arch-arm\usr\lib; C:\android\ndkr18b\toolchains\arm-linux-androideabi-4.9\prebuilt\windows-x86_64\lib\gcc\arm-linux-androideabi\4.9.x" Can we dropped this reference?

For my tests that path is not required. The project compiles and builds ok. ("/home/user/android/sdk/ndk-bundle/platforms/android-21/arch-arm/usr/lib/" is not required in libraries path)

To test, before building a working project you can rename the ndk "platforms" folder to something else (e.g. "platforms-BAK").

jmpessoa commented 3 years ago

Nice!

But, so using NDK 22 we lost the (platform) API concept?

WayneSherman commented 3 years ago

At least for NDK r22, it appears you don't need to reference the platforms folder as a library. The changelog says:

https://github.com/android/ndk/wiki/Changelog-r22 "The deprecated /platforms and /sysroot directories have been removed. These directories were merged and relocated into the toolchain during r19. The location of these contents should not be relevant to anyone, including build systems, since the toolchain handles them implicitly."

WayneSherman commented 3 years ago

It also works with NDK r21. I renamed the directory "./sdk/ndk/21.4.7075529/platforms" to "./sdk/ndk/21.4.7075529/platforms-BAK" and did a clean build and it still works.

(I had to actually rename the platforms folder since the libraries path is re-created at some point when doing a build)

jmpessoa commented 3 years ago

Ok. I will try update LAMW!

Thank you!!

jmpessoa commented 3 years ago

@WayneSherman "For my tests that path is not required. The project compiles and builds ok."

Well, I am testing NDK 22 ... and here I got "Error: while linking"

image

WayneSherman commented 3 years ago

I am testing NDK 22 ... and here I got "Error: while linking"

On Linux, I have my environment setup using the How-To I posted in the forum: https://forum.lazarus.freepascal.org/index.php/topic,40750.0.html

The jnigraphics.so file is located here: /home/user/android/sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib/arm-linux-androideabi/29/ (but this is not in any of my paths, and not used during compiling or linking)

But, I also have that file located here: /home/user/android/fpc-lazarus/cross/lib/arm-android/ and here: /home/user/android/fpc-lazarus/cross/lib/aarch64-android/

So I think FPCUPdeluxe has done some work behind the scenes to make the libraries in the cross/lib folder available to the compiler. Although I can compile and build a project, are the FPCUPdeluxe cross lib libraries safe to use?

WayneSherman commented 3 years ago

So I think FPCUPdeluxe has done some work behind the scenes to make the libraries in the cross/lib folder available to the compiler.

from fpc.cfg

# begin fpcup do not remove arm-android
# Inserted by fpcup 4-6-21 21:42:06
# Cross compile settings dependent on both target OS and target CPU
#IFDEF FPC_CROSSCOMPILING
#IFDEF ANDROID
#IFDEF CPUARM
-CfVFPV3
-FD/home/user/android/fpc-lazarus/cross/bin/all-android/bin/
-XParm-linux-androideabi-
-Xd
-Fl/home/user/android/fpc-lazarus/cross/lib/arm-android/
-FLlibdl.so
#ENDIF CPUARM
#ENDIF ANDROID
#ENDIF FPC_CROSSCOMPILING
# end fpcup do not remove

# begin fpcup do not remove aarch64-android
# Inserted by fpcup 4-6-21 21:44:37
# Cross compile settings dependent on both target OS and target CPU
#IFDEF FPC_CROSSCOMPILING
#IFDEF ANDROID
#IFDEF CPUAARCH64
-FD/home/user/android/fpc-lazarus/cross/bin/all-android/bin/
-XPaarch64-linux-android-
-Xd
-Fl/home/user/android/fpc-lazarus/cross/lib/aarch64-android/
-FLlibdl.so
#ENDIF CPUAARCH64
#ENDIF ANDROID
#ENDIF FPC_CROSSCOMPILING
# end fpcup do not remove
LongDirtyAnimAlf commented 3 years ago

Correct observation. Due to the fact that the libs-location has been changed in R22, fpcupdeluxe cannot find them and will download them (the libs) from its own repo. That is why linking will succeed. Updates of fpcupdeluxe are pending for R22. But I am still investigating the claim that linking should succeed by magic of the tools of R22, but I doubt that this will be sufficient for FPC.

jmpessoa commented 3 years ago

I think that we got a canonical solution.... Now LAMW will handle [transparently] NDK >= 22!

Here is the project ".lpi" fragment for NDK = 22:

Libraries Value="C:\android\android-ndk-r22b\toolchains\llvm\prebuilt\windows-x86_64\sysroot\usr\lib\arm-linux-androideabi\22\;C:\android\android-ndk-r22b\toolchains\arm-linux-androideabi-4.9\prebuilt\windows-x86_64\lib\gcc\arm-linux-androideabi\4.9.x\"

And it works!!!

I will do an update as soon as possible!

I thank everyone!

LongDirtyAnimAlf commented 3 years ago

https://bugs.freepascal.org/view.php?id=38987

WayneSherman commented 3 years ago

Here is the project ".lpi" fragment for NDK = 22: Libraries Value="C:\android\android-ndk-r22b\toolchains\llvm\prebuilt\windows-x86_64\sysroot\usr\lib\arm-linux-androideabi\22;C:\android\android-ndk-r22b\toolchains\arm-linux-androideabi-4.9\prebuilt\windows-x86_64\lib\gcc\arm-linux-androideabi\4.9.x"

The path "C:\android\android-ndk-r22b\toolchains\llvm\prebuilt\windows-x8664\sysroot\usr\lib\arm-linux-androideabi\" contains a folder for each android API level with a matched set of libraries for each API level_. In "arm-linux-androideabi" I have folders named 16, 17, 18, 19, 21, 22, 23, 24, 26, 27, 28, 29, 30. For a correct build (compile/link), should we be using the libraries in the folder that matches the API level?

(there are also similar folder trees for aarch64-linux-android, x86_64-linux-android, and i686-linux-android)

WayneSherman commented 3 years ago

...Due to the fact that the libs-location has been changed in R22, fpcupdeluxe cannot find them...

For my Linux testing, fpcupdeluxe does not know the location of the android build libraries for any of the NDK releases. Although I download the android SDK and NDK before running fpcupdeluxe, I have not set any paths so fpcupdeluxe doesn't know where the libraries are located for any of the NDK versions.

Reference Android on Linux How-To: https://forum.lazarus.freepascal.org/index.php/topic,40750.0.html

LongDirtyAnimAlf commented 3 years ago

fpcupdeluxe does not know the location of the android build libraries for any of the NDK releases.

Please enlighten me ! What are the standard locations of NDK (and SDK) on Linux ? AFAIK, I have read through the Android docs to include these standard locations if available. Follow up. What are the preferred locations ? Follow up. Shall fpcupdeluxe follow the conventions as you describe in your forum post ?

All is possible ... ;-)

WayneSherman commented 3 years ago

What are the standard locations of NDK (and SDK) on Linux ?

Android Studio may have a standard location that it uses for the NDK and SDK, but I use the command line tools and the location of the SDK can be anywhere. (I have confirmed, that I am using the same default location that Android Studio uses)

I let the "sdkmanager" command line tool install the SDK and NDK into standard locations relative to itself, which for my testing get installed three directories up from sdkmanager in "../../../[sdk_files_and_folders]". For the NDK it looks like "../../../ndk-bundle" or "../../../ndk/[ndk-revision-number]". For example:

I create a folder structure and put sdkmanager into it like this

/some/path/to/the/files/my_sdk/
/some/path/to/the/files/my_sdk/cmdline-tools/tools/bin/sdkmanager (command line installer)

sdkmanager installs the sdk and ndk to these folders

/some/path/to/the/files/my_sdk/  (sdk files and folders)
/some/path/to/the/files/my_sdk/ndk-bundle/ (most recent ndk revision e.g. rev 22)
/some/path/to/the/files/my_sdk/ndk/  (specifically installed ndk revisions)
/some/path/to/the/files/my_sdk/ndk/21.4.7075529/  (ndk revision 21)

The environment variable ANDROID_SDK_ROOT can be used to locate the SDK: https://developer.android.com/studio/command-line/variables (e.g. export ANDROID_SDK_ROOT="/some/path/to/the/files/my_sdk")

But I removed the environment variable from my latest setup procedure since it didn't seem to be needed. If fpcupdeluxe is using the ANDROID_SDK_ROOT variable, or if it happens to look into the correct path I installed the SDK, I was not aware of it.

(On Linux, the current Android Studio sdk default location is: /home/[username]/android/sdk But I have seen posts using different case: "/home/[username]/Android/Sdk/" and "/home/[username]/Android/sdk/")

LongDirtyAnimAlf commented 3 years ago

https://github.com/LongDirtyAnimAlf/fpcupdeluxe/blob/master/fpcuputil.pas#L3200 https://github.com/LongDirtyAnimAlf/fpcupdeluxe/blob/master/fpcuputil.pas#L3228

This is the current code. Might need some changes to fit our needs !

jmpessoa commented 3 years ago

@WayneSherman "I have folders named 16, 17, 18, 19, 21, 22, 23, 24, 26, 27, 28, 29, 30. For a correct build (compile/link), should we be using the libraries in the folder that matches the API level?"

No! You can use any .... for my example this was just an ambiguous coincidence.... (sorry)

we can have something like this:

Libraries Value="C:\android\android-ndk-r22b\toolchains\llvm\prebuilt\windows-x86_64\sysroot\usr\lib\arm-linux-androideabi\28;C:\android\android-ndk-r22b\toolchains\arm-linux-androideabi-4.9\prebuilt\windows-x86_64\lib\gcc\arm-linux-androideabi\4.9.x"

I am doing some final tests and then I will update LAMW....

WayneSherman commented 3 years ago

@LongDirtyAnimAlf

This is the current code. Might need some changes to fit our needs !

How about look for the android SDK in this order?:

if ANDROID_SDK_ROOT is a valid directory, then use it

result := GetEnvironmentVariable('ANDROID_SDK_ROOT');
if DirectoryExists(result) then...

else look for the sdk in the standard locations starting with users home and then check system level location(s):

(Linux example)
$HOME/android/sdk  (**This directory, with lowercase android/sdk, is the default location currently used by Android Studio**)
$HOME/Android/sdk
$HOME/Android/Sdk
/usr/lib/android-sdk
jmpessoa commented 3 years ago

Done!!!

Now LAMW can handle [transparently] NDK >= 22!

I thank everyone!

LoganTennis commented 3 years ago

I have Trunk FPC, Trunk Lazarus, NDK 22, JDK 14, Latest FPCUpdate 64 bit and latest LAMW from github. All on 64 bit Windows 10 system. Trying to link to an Android Jelly Been device 4.2.2 API 17. Error message is Messages Verbose: Selected chip architecture: armeabi-v7a Verbose: Taking libraries from folder: ...\libs\armeabi-v7a Verbose: Found library: libcontrols.so Compile package FCL 1.0.1: Exit code 1, Errors: 1 Error: ppcarm.exe can't be executed, error message: Failed to execute ""ppcarm.exe" -Tandroid -MObjFPC -Scghi -O1 -g -gl -l -vewnhibq -Fue:\fpcupdeluxe\lazarus\packager\registration\ -FUe:\fpcupdeluxe\lazarus\packager\units\arm-android\ -g -gl -O1 fcllaz.pas Exception, Errors: 1 Fatal: [Exception] Failed: Cannot build project

I cannot get NDK 22 to work here

jmpessoa commented 3 years ago

Hi, LoganTennis!

maybe fpcupdeluxe still not solved some issue related to NDK 22....

What FPC cross-compiler you have installed for Android??

LoganTennis commented 3 years ago

Not sure, how to answer that question is that in LAMW settings for the project? I just ran the latest FPCdeluxe, selected Trunk of FPC and Lazarus and hoped it would sort all that out. Can you help me work out how to list all the settings required to give you an overview of the setup? I thought I had done that in my previous post.

jmpessoa commented 3 years ago

" I just ran the latest FPCdeluxe, selected Trunk of FPC and Lazarus ......"

And then? Have you pressed the button "Android LAMW" ?