Using Java code directly in B4A

Inman

Well-Known Member
Licensed User
Longtime User
First of all, I have zero knowledge in Java. Years of experience in Visual Basic helped me develop couple of Android apps, without much sweating, using the amazing B4A. So what I am about to say might be completely wrong and foolish. Please bear with me.

Since 2010, Android's popularity is on the rise. This has led to certain service provider sites (advertisement networks, analytic providers etc..) release a dedicated Android SDK of their service, which can be integrated into our Android projects. They usually provide a JAR file and some sample code as well which can be used. For example, here is Google Analytics SDK for Android:

Google Analytics SDK for Android - Google Analytics for Mobile - Google Code

In the above page, they are providing the Analytics SDK JAR file as well as the sample code that can be used to access it. Unfortunately, since the above code is in Java, we cannot use it directly in B4A. Normally we need a B4A wrapper library for these SDKs but we cannot expect the Java developers here to come up with libraries for each and every SDKs available. Of course we have the Reflection library but I am not sure if it can be used to execute such blocks of code.

Now if my understanding is correct, B4A actually converts the code to Java first and then compiles the APK file. If that is the case, I was wondering if it will be possible for B4A to take the Java code like the sample code on Analytics SDK page, and send it directly to compiler.

One of the first doubts will be where to put this Java code in our project in such a way that the B4A compiler, which expects B4A code, will not come up with syntax errors. Currently we have 3 types of modules - Activity, Service and Code. I was wondering if we could have a 4th module called Java module where we can put the Java code directly, may be inside Subs or Functions which we can call from B4A.

I know this isn't as easy as I make it out to be. In fact this may be a completely stupid idea. Still I am curious about the possibilities.
 

agraham

Expert
Licensed User
Longtime User
This requires far too much explanation for a complete answer to even attempt trying but here a some bullet points.

1) Even if you could compile the sample Java into your apk code you still have to call it from B4A code and the sample code as provided usually would not run under B4A without modification so you need to know some Java and Android to do that.

2) If you know enough to modify the sample Java code you probably know enough to write a library wrapper.

3) You can probably (I haven't tried so I can't be definite but I see no problem) include any arbitrary jar in the apk and use the Reflection library to use it. However owing to the way reflection works it produce complex and clumsy B4A code, and if you knew enough Java and Android to work out how to use reflection on the jar you know enough to write a wrapper library.

Bottom line - to use a third party jar you need to know enough Java to understand how to access it in which case you may as well make a library.
 
Upvote 0

Inman

Well-Known Member
Licensed User
Longtime User
Thanks for the reply, agraham.

While it is true that you need a certain level of knowledge to use most JAR files, I have seen documentation of certain SDKs which basically asks the user to just "copy paste this code and you are done". I was hoping we could include such code in B4A. But yes, it may work with "copy-paste" on Java but on B4A it probably needs modifications to incorporate it.

And as per your suggestion I will try to see if Reflection library is able to work with arbitrary JAR files. That will be big help.
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
And as per your suggestion I will try to see if Reflection library is able to work with arbitrary JAR files. That will be big help.
I really wouldn't bother. I wrote the Reflection library so I know how to use it with arbitrary jars and it really would be easier and quicker to throw a library together.
 
Upvote 0

ashchopra

Member
Licensed User
Longtime User
This Should Be on the Wish List

Hi InMan,

What you say makes a lot of sense. There is a whole lot of java code available on the internet that is free for re-use.

As B4Android basically converts all B4Android code to java, there should be a way that you could use the java functions/methods/events in the B4Android code making a inline mention to the compiler that what follows is java code and not B4Android Code.

This will save a lot of time in writing wrapper libraries and really mushroom the development in B4Android.

Why dont we put this up to Erel ??:sign0142:
 
Upvote 0

Groofertje

New Member
Licensed User
Longtime User
Translate Java module directly by B4Android

I completeli agree whit Ashchopra

As B4Android basically converts all B4Android code to java, there should be a way that you could use the java functions/methods/classses in B4Android,
(eventual with a externe module). I find it a little "stupid" to make a library of every module written in Java :p because most of them wil be used once.

What i mean is that the included code is a class written in java. I want simply make a [class]module in B4Android of it. so i have completely rewritte it in B4Android. and after that B4Android retranslate it to java :icon_clap: If i had something in B4Android to translate this code directly into a B4Android classe i saved a lot of my time.

Attention the code is only a demo what i mean and is not compleed :)


@ShortName("PersianCalender")
public class PersianDate extends AbstractDate {

private static final String[] monthName = { "", "فروردین",
"اردیبهشت", "خرداد", "تیر", "مرداد", "شهریور", "مهر", "آبان",
"آذر", "دی", "بهمن", "اسفند" };

public String[] getMonthsList() {
return monthName;
}

private int year;

private int month;

private int day;

public PersianDate(int year, int month, int day) {
setYear(year);
// Initialize day, so that we get no exceptions when setting month
this.day = 1;
setMonth(month);
setDayOfMonth(day);
}

public int getDayOfMonth() {
return day;
}

public int getMonth() {
return month;
}

public String getMonthName() {
return monthName[month];
}

public int getWeekOfYear() {
throw new RuntimeException("not implemented yet!");
}

public int getYear() {
return year;
}

public void rollDay(int amount, boolean up) {
throw new RuntimeException("not implemented yet!");
}

public void rollMonth(int amount, boolean up) {
throw new RuntimeException("not implemented yet!");
}

public void rollYear(int amount, boolean up) {
throw new RuntimeException("not implemented yet!");
}

public void setDayOfMonth(int day) {
if (day < 1)
throw new DayOutOfRangeException("day " + day + " is out of range!");

if (month <= 6 && day > 31)
throw new DayOutOfRangeException("day " + day + " is out of range!");

if (month > 6 && month <= 12 && day > 30)
throw new DayOutOfRangeException("day " + day + " is out of range!");

if (month == 12 && day > 29 && (!isLeapYear()))
throw new DayOutOfRangeException("day " + day + " is out of range!");

this.day = day;
}

public void setMonth(int month) {
if (month < 1 || month > 12)
throw new MonthOutOfRangeException("month " + month
+ " is out of range!");

// Set the day again, so that exceptions are thrown if the
// day is out of range
setDayOfMonth(day);

this.month = month;
}

public void setYear(int year) {
if (year == 0)
throw new YearOutOfRangeException("Year 0 is invalid!");

this.year = year;
}

public String getEvent() {
throw new RuntimeException("not implemented yet!");
}

public int getDayOfWeek() {
throw new RuntimeException("not implemented yet!");
}

public int getDayOfYear() {
throw new RuntimeException("not implemented yet!");
}

public int getWeekOfMonth() {
throw new RuntimeException("not implemented yet!");
}

public boolean isLeapYear() {
int mod = getYear() % 33;
if (mod == 1 || mod == 5 || mod == 9 || mod == 13 || mod == 17
|| mod == 22 || mod == 26 || mod == 30)
return true;
return false;
}

public boolean equals(PersianDate persianDate) {
if (this.getDayOfMonth() == persianDate.getDayOfMonth()
&& this.getMonth() == persianDate.getMonth()
&& this.getYear() == persianDate.getYear())
return true;
return false;
}
}
 
Upvote 0

NeoTechni

Well-Known Member
Licensed User
Longtime User
erel should be able to easily allow unconverted code blocks

ie: we use comments to start/end blocks of code that are left as-is instead of converted from basic to java.

'JAVASTART
pfdfjfdj
'JAVAEND
 
Upvote 0
Top