Android Tutorial Developing library with Android Studio

I have moved all my projects from Eclipse to Android Studio, so decided to write how I have done for generating JAR and XML file for Basic4Android.

1. Create new project - File>New Project or if you have one go to Step2
2. File>New Module>Java Library
3. Copy .bat and doclet files to "libs" folder for example. .bat file is very simple with the following code:

B4X:
If Not Exist %1build\libs\ ( mkdir %1build\libs\ )
javadoc.exe -doclet BADoclet -docletpath %1libs\ -sourcepath %1src\main\java\ -classpath %1libs\* -b4atarget %1build\libs\YOUR_NAME.xml -subpackages YOUR_PACKAGE -exclude PACKAGE_TO_EXCLUDE or NONE

4. Now open "Run/Debug Configurations" and add Gradle, in tasks write JAR , then add your .bat file as external tool with the path to your project as parameter. Now you can generate JAR and XML files in build folder of your module by clicking "Run" button.
AS_B4A.PNG


Note: if you use JAVA8 as compiler, add to gradle the following lines:
B4X:
project(':YOUR_MODULE_NAME') {
    apply plugin: 'java'
    sourceCompatibility = 1.7
    targetCompatibility = 1.7
}
this makes JAR in compatibility with JAVA7.

Regards
John
 
Last edited:

corwin42

Expert
Licensed User
Longtime User
Sorry to push this thread to the top again.

@Erel:
SLC is not really usable with Android Studio projects. If you use something like the support libraries, SLC will not be able to resolve the dependencies to it.

@Johnmcenroy:
Thanks for the hint. Do you still use the same method or is there now a better solution?

I'm currently on a new attempt to switch to AndroidStudio because with the latest support libraries (extracted from the AARs) in Eclipse I have some strange problems like problems to cast Activity Objects to AppCompatActivity etc. So I decided to give Android Studio another chance. The learning curve in AndroidStudio is much higher than in Eclipse I think but I hope that I can switch now. I think Eclipse is dead for Android development now. Even for B4A library development if you want to use the Google repository or support repository.
 

warwound

Expert
Licensed User
Longtime User
I think Eclipse is dead for Android development now.

Too true.

I had to wrap the GoogleVR SDK into a b4a library recently and was gonna use Eclipse.
The b4a library depends on 4 .aar android libraries, if i'd used Eclipse then i'd have had to:
  • Unzip and convert each of the 4 .aar library files into separate android library projects in Eclipse.
  • Create a standard android application project in Eclipse and add the 4 android library projects as dependencies.
  • Finally start writing the wrapper code.

Then if/when the various 4 .aar libraries are updated by Google i'd have to manually update the corresponding Eclipse android library project.

The official GoogleVR demos are Android Studio projects.
So i decided to try Android Studio and am glad i did.
In Android Studio i:
  • Created a new project with a default android application module.
    This application module is my b4a library module.
  • Imported each of the 4 .aar libraries as new modules and added them as dependencies to my b4a library module.
  • Wrote my wrapper code.
  • Used SLC to compile the source to a b4a .jar and .xml files.

If/when the 4 .aar libraries are updated i'd just recompile my library project (after replacing old.aar file with new .aar file).
I used SLC to compile as i didn't have time to research how to get Android Studio to create the b4a .xml file.
Now i've found this thread i'll try compiling using Android Studio.
 
Last edited:

corwin42

Expert
Licensed User
Longtime User
Thanks for posting your experiences.

I'm now mostly done with AppCompat and DesignSupport Libraries. And I'm glad, too that I have done the migration. Updating to a new support library is now really easy. Just change the dependencies to the new version (change 25.0.1 to 25.0.2 or whatever the new version is), do a gradle sync and recompile everything -> done.

Creating the library XML is really easy now, too. I have modified the batch file from @Johnmcenroy slightly so it takes the classpath as a parameter. I can then pass $Classpath$ variable to it and don't have to worry about external jars etc.

Since AppCompat and DesignSupport include resources they are Android Libraries. The next versions will have a new structure. It will include the .xml as always. The jar will be a complete empty jar file and there will be an additional .aar with includes the library code and the resources.

The only thing I have to fix now to make it really nice is that at the end of the build I want to copy everything to one place (xml, .aar, empty .jar). I tried with a gradle copy task but I couldn't get it to work for now. I think I will need some more experience here with gradle.

About SLC:
How do you do it with SLC? In my tests I got many errors because SLC couldn't reference all the support library symbols and I can't pass any jars to it because there aren't any jars anymore.
 

warwound

Expert
Licensed User
Longtime User
The next versions will have a new structure. It will include the .xml as always. The jar will be a complete empty jar file and there will be an additional .aar with includes the library code and the resources.

I did ask Erel if a library .jar could be replaced by an .aar file in this thread, the answer is no, an empty/dummy .jar file is always required.

The same thread details a lack of support for 'jni' folders in .aar libraries, the b4a compiler does not copy jni folder resources to the compiled .apk file.
Something you'd have to watch for if your .aar contains .so files.

Perhaps another feature request could be for a b4a library .jar to be optional - it can be replaced with an .aar file.
With .aar jni folder support and no requirement to create a b4a .jar we could finally abandon the old .jar libraries entirely and use the newer .aar format.

About SLC:
How do you do it with SLC? In my tests I got many errors because SLC couldn't reference all the support library symbols and I can't pass any jars to it because there aren't any jars anymore.

In my java class i create the annotation:
B4X:
@BA.DependsOn(values={
        "common.aar",
        "commonwidget.aar",
        "videowidget.aar"
})
These three library files are located in my b4a additional libraries folder.
This enables using the library in b4a, to get SLC to compile it i also had to:
  • Create a folder named 'libs' in the root of my SLC source folder.
    My SLC source folder is named 'GoogleVR_B4A':
    GoogleVR_B4A\libs
    GoogleVR_B4A\src
  • Extract the classes.jar file from each of my .aar files to my libs folder.
    Rename each classes.jar - give them all unique filenames.
    My SLC source folder now looks like:
    GoogleVR_B4A\libs
    GoogleVR_B4A\libs\common.classes.jar
    GoogleVR_B4A\libs\commonwidget.classes.jar
    GoogleVR_B4A\libs\videowidget.classes.jar
    GoogleVR_B4A\src

SLC compiles perfectly with these extracted classes.jar files in the libs folder.

The GoogleVR SDK library files are not available via maven and must be manually downloaded.
You're using library files that are available via maven - and these library files are the ones you need to extract the classes.jar from.
That makes using SLC less attractive - your library updates no longer require a simple edit to the gradle file in android studio, instead you'd need to manually replace the old classes.jar files with new versions.

Compiling (to .jar or, ideally, .aar) and creating xml entirely within Android Studio sounds like a much better option.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
The b4a library depends on 4 .aar android libraries, if i'd used Eclipse then i'd have had to:
It is actually quite simple to do if you let B4A create the jar for you. Add references to the 4 aar libraries with #AdditionalJar and then take the generated stan.jar and add it to the Eclipse project.
 

syderbit

Member
Licensed User
Longtime User
hi @Johnmcenroy and @corwin42 . i confiuse generate JAR and XML from ADS, file bat not run correcly. help please, or u can upload small example from android studio.
thank u for u support and sorry my bad english. :(
 
Top