Android Question Compiling a native android application in more detail?

Caligari

Member
Hi all!
And hi Erel!

B4X - very cool project!
I am engaged in programming from time to time - as needed, for my own needs. Mobile development is a new challenge for me. B4X seems to be the right tool for this. Just started studying. And my first question: can you explain the process of compiling a native android application in more detail?
 

Albert Kallal

Active Member
Licensed User
At the end of the day? Well, one can say you don't have to worry!

But, under the hood? Ok, I'll bite!!

yes, the output tends to be JavaCode, and THEN some REAL magic starts to take place!

The folks at Google and Android spent some REAL serious money. They built a optimizer. (I think it came out around Android 4-6???

The result? Well, the optimizers are REALLY cool and really amazing. They work similar to how the .net compiler works. So in .net you can use C#, vb.net, or I think out of favor was F#. In all 3 cases, they compile down to a p-code system (called IL - or intermediate language). That is the code sent to the platform in question. Then there is the new magic thingy called a JIT. (a just in time compiler).

So for Android? The native language is java, and you can think of that output result VERY similar to the "IL" system in .net

Then toss is some REAL BIG bucks - and a lot of R&D? And a REALLY cool "JIT"?

Well, it then crunches down that code to native Android assembly - on the fly with that JIT. The advantage of this architecture is that you can thus have different CPU's and processors - you only have to re-write that JIT for the different processor.

And how good does it work? Hang on to your underpants!!!! - it works VERY good.

In fact in some technical papers from Google and Android? They state that in some cases you get as good or EVEN better in some cases then HAND coded c++! And the JIT can MAKE decisions!!!! (this is NOT possible with a native pre-compiled code system).

So while running your code? It can say, hay, that is a REALLY bad way to use some memory, and I think we better off to re-use some the CPU registers since that LAST loop and code did NOT run all that great. So it can tweak and change it mind as to how next loop and code executing will occur. If that loop becomes processing intensive? Well, then the JIT can have another go at the code - and re-compile again with HIGHER optimizations. (but why bother for some simple lines of code - just execute them and we are done!

Now for real high speed and some real heaving games for Android? Sure, the developers will write c++ code and use a native compiler.

However, with all these incredible optimizers and those boatloads of money spent on the optimizing system and that JIT?

all that $$$$ spent on this JIT?

You get native assembler CPU speed, and in some cases as noted, you get as good or even BETTER execution of code developed by a experienced c++ coder and pre-compiled code!!! And you GET this using B4A!!!!

On my dumpster fire el-cheap-o "test" mule Android phone? (a budget Moto g)?
(4 cores - 1.4 ghz).

Writing in nice clean simple Basic code - something I done since my Apple II?

Well, on Quroa there was this question: How many 7 digit prime numbers are there?
(basic 1 million to 10 million)

My answer:
586,081 primes exist

So, the number is quite large. There are 8,999,999 (about 9 million) 7 digit long numbers.

So, out of about 9 million existing 7 digit long numbers, 586,081 are primes.

So I needed a big fast computer platform to solve this. Used to have to pay for time on Control Data "Cyber" computer. That computer was in Calgary - funded by the government and some oil companies. It was rated at about 200 mflops.

But, in place of that big mainframe, to answer that question on Quroa about how many prime numbers?

Hum, what computer do I have sitting around? OHHH I know, I use my cheap low cost smartphone and B4A!

After all it only what - we only need to do close to 1 billion calculations - right? How hard is that?

Well, I wrote the code in B4A to find this answer!!! -

B4X:
Sub PrimeCounter
    
    Dim i As Int
    Dim k As Int
    Dim Upper As Int
    Dim bolPrime As Boolean
    Dim PrimeCount As Int = 0
    
    Dim MyTicks As Long
    
    MyTicks = DateTime.Now
    
    Dim lngStart As Int
    Dim lngEnd As Int
    
    lngStart = 1000001
    lngEnd   = 9999999
    
    For i = lngStart To lngEnd Step 2

        Upper = Sqrt(i)
        k = 3
        bolPrime = True
        Do While k  <= Upper
            If i Mod k = 0 Then
                bolPrime = False
                Exit
            End If
            k = k + 2
        Loop
        If bolPrime Then
            PrimeCount = PrimeCount + 1
        End If
    Next
    
    Dim TimeTicks As Long
    TimeTicks = DateTime.Now
    
    TimeTicks = TimeTicks - MyTicks
       
    Log("===============")
    Log("time = " & (TimeTicks / 10000))

    Log("Count of primes found = " & PrimeCount)
    
    EditText1.Text = "time = " & ( (DateTime.Now - MyTicks) / 1000) & _
    CRLF & "Count of primes found = " & PrimeCount

the answer? 586,081 primes exist in that range.

The time for that el-cheapo phone?

Well, first run I get this:

13.69 seconds.

If I run it again - then even more of that amazing R & D from google kicks in:

I get this:

8.812 seconds.

(now think about how this 2nd run was OH SO much faster? Those optimizers figured out HOW to do things even better!
So to answer your question?

yes, the software eventually gets crunched down to native arm CPU code. And it is STUPID fast - really!!!

Now of course just looping raw code? That's real fast. You will find that crunching views + display does chew up memory and resources in a "different way", and some of the UI update and rendering can slow things down. But raw code speed?

You get the same speed in B4A as just about any other code system - including that of hand writing some c++ routines that is compiled down to native assembly code all nice and "RAW" for the CPU to chew on!

Those with better phones and newer gen processors can give the above a try (run in release mode), but the speed of your code in terms of base execution speed of code? it is fast - very fast. Its pushing things out to the UI that will bite you, and that still takes care and considering on your part - and that considering exists for just about any all Android SDK's.

The word of the day here? Absolute beyond remarkable how fast this code runs.

to be fair, this code could be split in half, or even 4 loop ranges, and then all 4 cores could run that code. So, to be fair? With better threading support, one could chop this time down in half, or even 1/4th the time to run.

But right now? You can keep your pants on - but B4A is able to blow them right off with some of the speeds I seen it run things.


Regards,
Albert D. Kallal
Edmonton, Alberta Canada
 
Upvote 0

Caligari

Member
Thanks Albert, but this is not exactly what I wanted to hear.

The question was simple: what does B4A do when compiling the application: generates the bytecode on its own, or translates the B4A program into the Java program and then transfers it to the Java compiler javac. Magic JIT (with REAL BIG bucks and a lot of R&D) remains offscreen in both cases, doesn't it?

I was also interested in how the development of an application in B4A turns out to be much faster and easier than in other frameworks. After a little experimentation, a really very pleasant impression of B4X is formed. The mass of ready-made solutions and the responsiveness of the community only enhance this impression.
But I want to understand what the catch is?))
There must be downsides compared to other more established development tools. So far I see only one, concerning large development teams, associated with long-term maintenance of software written in a framework, the developer and owner of which is actually one person (my great personal admiration for Erel!).
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
what does B4A do when compiling the application:
You can find the generated Java source code under the project Objects/src/... folder tree. After that it is compiled using the Android SDK like any other Android app.

There must be downsides
As far as I can see the worst possible downside would be that Erel vanishes in a puff of smoke and even then we are left with a working product as it exists at that time. VB6 is still being used despite development stopping many years ago.

more established development tools.
B4A was one of the earliest. I helped Erel develop the first version in 2010!
 
Last edited:
Upvote 0

Albert Kallal

Active Member
Licensed User
The question was simple: what does B4A do when compiling the application: generates the bytecode on its own, or translates the B4A program into the Java program and then transfers it to the Java compiler javac. Magic JIT (with REAL BIG bucks and a lot of R&D) remains offscreen in both cases, doesn't it?

As noted, B4A eventually spits out that Java. (so no, it does not create compiled native code. But the official Android tools ALSO DO NOT!!!).
So it don't matter, since as I noted, code WILL eventually get compiled down to native Android assembler code. (kind of thought that was clear - but that's is what occurs if I was not clear. And this is WHAT the major Android developer platforms ALSO do - and so does B4A).

I was also interested in how the development of an application in B4A turns out to be much faster and easier than in other frameworks. After a little experimentation, a really very pleasant impression of B4X is formed. The mass of ready-made solutions and the responsiveness of the community only enhance this impression.
But I want to understand what the catch is?))

Well, actually, I do think the MAIN reason why B4A is oh so very easy to use?
Because you not exposed to the HTML like markup for forms layout, and that of having to write Java code in addition AFTER having laid out a form (view). So, you don't have to spend time dropping down into the html like markup (XMAL (zammel?)) when building forms, and you don't have to learn a language with what I consider a difficult syntax. So B4A eliminates these two difficult challenges. So layout is GUI, no XML markup, and no strange code syntax like Java.

As a result, the interaction between those view/forms and that Java code, and having to tweak, and write html like markup is all eliminated here.

It really is MUCH the same difference between using .net WinForms for desktop vs that of using WPF forms for the desktop.

There must be downsides compared to other more established development tools.

There are FEW compromises, and this is due to B4A using and leveraging the standard SDK and development stacks that make Android work.

When I was looking for something to use with Android? I looked at a number of systems. Include a few companies that started out building software for windows CE (and even the Palm platform). However, their "falling down" was those systems had their own custom system for building forms, and the code was NOT translated into native Android code. As a result, two things occurred:

First, code execution speed was VERY slow - it was interpreter type of deal - not good. Very limited code features.

2nd: Because these other canned systems did NOT use the standard Android libraries to create the final result (form views) then the look + feel, the swiping, scrolling, and things like say a time picker were thus NOT based on the standard Android widgets.

The end result was the resulting applications looked like crap, and did not really feel like a Android applications. (and in fact they were not!!!).

B4A's GREATEST strength is that the results not only run well, but they look, they feel like real Android applications (and the reason of course is because that's what in fact they are!!! - real Android applications!!!).

The only real downside I can think of? Well, there is "truck-loads" of libraries and code examples you can get/grab/see/use that are written for the standard Android dev tools.

To use those bits and parts in B4A? You have to up your skill level, and such code examples and libraries CAN be consumed, but it not all that easy to do so in B4A (well not for those that never learned the standard Android dev tools).

So it takes a good deal higher level of skill (a level of skill approaching that of having learned the Kotlen or whatever the latest dev tools that Google/android is pushing). So this is perhaps the downside, but then again, at the end of the day? Such Android code and libraries CAN be used in B4A in most cases - its just not all that easy for a new comer, and in fact quite hard for one with experience in B4A.

So consuming of 3rd party libraries and code written for Android is one downside or challenging in B4A.

However, as noted, B4A can in most cases consume such Android example code and libraries' - it just not all that easy to do so. But, at the end of the day? It is POSSIBLE in most cases in B4A to consume + use such example code - and once again that favors why B4A is so good.

In other words, once one gains the skill level to use + consume these libraries? Well, then you approaching the skill level to use those mainstream dev tools for Android - so that's not all that bad.

I don't think the larger team effort/issue is all that bad either. (I not attempted to use a B4A project with say git-hub, but I don't see why it would not work).
Larger teams don't necessary benefit by say using git-hub. They benefit due to how code is written (in more in-dependent code bits and parts - and that takes developer discipline - and is again possible for a B4A developer.

Regards,
Albert D. Kallal
Edmonton, Alberta Canada
 
Upvote 0
Top