Java Question [SOLVED] Handle Inline Java in a cross platform library

max123

Well-Known Member
Licensed User
Longtime User
Hi all,

after I developed some platform dependant libraries (B4A and B4J) now is time
to try develop a cross platform library that works on multiple platforms.

On this direction, I have developed two libraries, one for B4A and one for B4J, they do the same things
apart that do in a different way adapted to both platforms.

So I started a new B4XPages project, arranged both libraries on B4A and B4J and eventually B4i,
I used conditionals #If B4A #Else If B4J to adapt my code global variables and subs for both,
but now I encountered this problem.

How to handle inline java code for more than one platform ?

May is a stupid question, but it won't work for me, I tried this way:
B4X:
#If JAVA

#If B4A
  // Java code for android
#Else If B4J
  // Java code for desktop
#Else If B4i
  // Java code for iOS
#End If

#End If
But it don't work as expected.....
I even tried this way:
B4X:
#If B4A
    #If JAVA
          // Java code for android
    #End If
#Else If B4J
    #If JAVA
           // Java code for desktop
    #End If
#Else If B4i
    #If JAVA
          // Java code for iOS
    #End If
#End If
but even this don't worked.

Many thanks for help
 
Last edited:

max123

Well-Known Member
Licensed User
Longtime User
Thanks for reply @aeric .
Really I dont used #Else If B4i, just B4A and B4J. But both seem do not work in my project.

Can conditionals be nested one inside another one ?
 

max123

Well-Known Member
Licensed User
Longtime User
I'ts a bit difficult because it is inside a complex library. Eventually I will test with a blank project and post errors if any.
Now here is night and is time to sleep. Tomorrow I will test it. I started with assumption that conditionals cannot be nested one inside others. But may I'm wrong ?
 

max123

Well-Known Member
Licensed User
Longtime User
Because rarely I sleep the night.... like a wolf ?

Here the test code in a very small project.
B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
   
    Private NativeMe As JavaObject
End Sub

Public Sub Initialize
'    B4XPages.GetManager.LogEvents = True
End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
   
    NativeMe = Me
End Sub

'You can see the list of page related events in the B4XPagesManager object. The event name is B4XPage.

Private Sub Button1_Click
    xui.MsgboxAsync("Hello world!", "B4X")
End Sub

#If JAVA

#If B4A
  // Java code for android
#Else If B4J
  // Java code for desktop
#Else If B4i
  // OBJC code for iOS
#End If

#End If
and this is the compilation result.... as expected seem a problem with nested conditionals. Inside a #If JAVA, the compiler only expets java code.
B4J Versione: 10.00
Analisi del Codice. (0.02s)
Java Versione: 11
Building folders structure. (0.01s)
Esecuzione azione personalizzata. (0.05s)
Compilazione del codice. (0.04s)
Compilazione del codice di layouts (0.00s)
Organizzazione Librerie. (0.00s)
Compilazione del codice Java prodotto. Error
B4J line: 17
End Sub
src\b4j\example\b4xmainpage.java:74: error: illegal character: '#'
#If B4A
^
1 error
 
Last edited:

Daestrum

Expert
Licensed User
Longtime User
You might need to use
B4X:
#If B4A
#If Java
... java code...
#End If
#End If

#If B4J
#If Java
... java code ...
#End If
#End If
 

max123

Well-Known Member
Licensed User
Longtime User
Thanks @Daestrum . I will try it.
Tried this and compiled too, but I'm not sure it works, if I change names with strange names it compiles too, so probably is wrong:
B4X:
#If JAVA AND B4A

#Else If JAVA AND B4J

#Else If OBJC AND B4i

#End If
Even this seem to work now.....
Ahahah.... I just tried it the same before I've read your last message:
B4X:
#If B4A
    #If JAVA
    //... java code...
    #End If
#Else If B4J
    #If JAVA
    //... java code ...
    #End If
#End If
 
Last edited:

Daestrum

Expert
Licensed User
Longtime User
or to make it shorter(ish)
B4X:
#if B4A
' If this is green its B4A
#if java
public static void test1(){
    System.out.println("B4A and java");
}
#end if
#else if B4J
'If this is green its B4J
#if java
public static void test1(){
    System.out.println("B4J and java");
}
#End If
#End if
I tend to put a comment directly after the #If B4A/B4J so its easy to see which code block its using as the green comment is the live block.
 
Last edited:

max123

Well-Known Member
Licensed User
Longtime User
Thanks both @aeric and @Daestrum, this code works as expected on B4J. Tomorrow I will test on B4A.
B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
 
    Private NativeMe As JavaObject
End Sub

Public Sub Initialize
'    B4XPages.GetManager.LogEvents = True
End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
 
    NativeMe = Me
End Sub

'You can see the list of page related events in the B4XPagesManager object. The event name is B4XPage.

Private Sub Button1_Click
    NativeMe.RunMethod("test1", Null)
End Sub

#If B4A
    #If JAVA
    public static void test1(){
        System.out.println("B4A and java");
    }
    #End If
#Else If B4J
    #If JAVA
    public static void test1(){
        System.out.println("B4J and java");
    }
    #End If
#End if
 
Last edited:

aeric

Expert
Licensed User
Longtime User
The above conditional symbols works but I think there is no System.out.println() in Android.

I tested the following works on both B4A and B4J.
B4X:
Private Sub Button1_Click
    Dim value As Int = Sum(2, 4)
    Log(value)
End Sub

Sub Sum (n1 As Int, n2 As Int) As Int
    Dim jo As JavaObject = Me
    Return jo.RunMethod("sum", Array As Object(n1, n2))
End Sub

#If B4A
    #If JAVA
    // Java code for android
    public int sum(int a, int b) {
        return (a + b);
    }
    #End If
#Else If B4J
    #If JAVA
    // Java code for desktop
    public int sum(int a, int b) {
        return (a + b);
    }       
    #End If
#End If
 

max123

Well-Known Member
Licensed User
Longtime User
Thanks @aeric I had the same problem here, the @Daestrum code worked well on B4J, but no on B4A.
I'm here from 2 hours to know because it so just a simple code won't work and now I've read your message.

Mmmm so there is no System.out.println on Android java...
I missed this information from a very long time I develop on Android, 10 and more years...

Here some explanations even if not much related to this topic:

So finally I can put [SOLVED] in the thread title, I just have to add this at the end to ensure all works for all 3 platforms:
B4X:
#Else If B4i
   #If OBJC
        // Objective C code
   #End If
#End If
but because I don't use B4i I cannot try it.
May if someone that use it can test. Know if works as expected can be an useful
information for me and other users that read this post. At this point I assume it should work.
 
Last edited:

aeric

Expert
Licensed User
Longtime User
Thanks @aeric I had the same problem here, the @Daestrum code worked well on B4J, but no on B4A.
I'm here from 2 hours to know because it so just a simple code won't work and now I've read your message.

Mmmm so there is no System.out.println on Android java...
I missed this information from a very long time I develop on Android, 10 and more years...

Here some explanations even if not much related to this topic:
Yes, that's what happened this morning.
I also tried Log.d() but also not getting a success.
I gave up and just create a small test function.
 
Top