Android Tutorial Create a wrapper library with Android Studio

Discussion in 'Libraries developers questions' started by corwin42, Aug 16, 2017.

  1. corwin42

    corwin42 Expert Licensed User

    This tutorial tries to show how to create a wrapper library with Android Studio.

    Prepare the project

    Creating a new project
    1. Enter an application name (We use "B4AWrapperTutorial" for this tutorial)
    2. select a domain name
    3. Click Next
    4. Select "Phone and Tablet" and a minimum SDK API level your library should have.
    5. Click Next
    6. Select "Add no Activity"
    7. Click Finish
    Delete the default "app" module

    By default a module named "app" is created. We don't need this so we can delete it.

    1. Select File->Project Structure...
    2. Select "app" in the modules list
    3. Click the "-" button at the top of the list.
    4. Confirm dialog
    5. Click Ok
    This only deletes the module app in AS. No files are deleted. To delete the files switch the main View from "Android" to "Project". You will still see the "app" folder. Right click on it and select "Delete...". Confirm the dialog.

    Create Module for Core and B4AShared jar libraries
    1. Select File->New->New module...
    2. Select "Import .JAR/.AAR package"
    3. Select Core.jar from your B4A installation folder (C:\Program Files (x86)\Anywhere Software\Basic4android\Libraries\Core.jar)
    4. Subproject name should be left as "Core"
    5. Click Finish
    6. Select File->New->New module...
    7. Select "Import .JAR/.AAR package"
    8. Select B4AShared.jar from your B4A installation folder (C:\Program Files (x86)\Anywhere Software\Basic4android\Libraries\B4AShared.jar)
    9. Subproject name should be left as "B4AShared"
    10. Click Finish
    Create the main wrapper module
    1. Select File->New->New module...
    2. Select "Android Library"
    3. Name your library (We use "MaterialTapTarget" for this tutorial)
    4. I always use the same upper/lower case name for the module name. So again use "MaterialTapTarget"
    5. Click Finish
    This will create our wrapper library module.

    Create/Modify build.gradle for our library

    In this tutorial I will create a wrapper for the library MaterialTapTargetPrompt. The library requires android-support-annotations:26.0.0 which are provided by Google via maven repository. We have to add this to our Project build.gradle file (B4AWrapperTutorial).

    Depending on where the library to be wrapped is hosted you may need to add other repositories like MavenCentral or jitpack. This is normally explained in the documentation of the library you want to wrap.

    allprojects {
        repositories {
            maven { url 
    "" }
            //maven { url '' }
    The MODULE build.gradle file needs lots of changes so I normally copy it from one of my other wrappers and modify it. Here is a full example for our needs:

    apply plugin: ''

    android {
        enforceUniquePackageName = 
        android.packageBuildConfig = 

        defaultConfig {

        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), ''

        compileOptions {
            sourceCompatibility JavaVersion.VERSION_1_7
            targetCompatibility JavaVersion.VERSION_1_7

        task copyB4AFiles(type: Copy, dependsOn: "assembleRelease") {
            from 'src/MaterialTapTarget.jar'
            from 'build/outputs/aar/MaterialTapTarget-release.aar'
            into 'build/B4Alibs'
            rename { String fileName ->
                fileName.replace('-release', '')

    dependencies {
        compile fileTree(include: ['*.jar'], dir: 'libs')
        compile ''
        compile project(':Core')
        compile project(':B4AShared')
    • android.packageBuildConfig = false: Normally AS creates a BuildConfig class by default for each project. This is not needed for B4A and can cause conflicts if you create more libraries.
    • task coopyB4AFiles: This should be modified to the correct library file names. The task copies a dummy XXX.jar file and the library .aar file to the destination folder.
    • In the dependencies part you reference the Core and B4AShared projects. Additionally the repository of the wrapped library is referenced here. In this case the tap-target-prompt library.
    Delete files from testing framework

    The default project creates some code for the testing framework we don't need, so we delete these files/folders. Select the "Project" view and open the src tree. There are three subfolders. Main is our main source tree. Test and androidTest folders can be deleted. Right click them and select Delete.

    Create a dummy .jar file

    B4A always expects a .jar file as a library. With our AS project we can only create .aar files. So we have to include a dummy .jar file in our release. This is just an empty .zip file renamed properly. An example is attached to this post. I place this file under <Project Dir>\src\

    Create your wrapper code classes

    Create your wrapper classes like in Eclipse. I won't explain this here. You should be familiar how to create a wrapper class. You only need to create a proper DependsOn annotation in your class:

    @BA.DependsOn(values = {"MaterialTapTarget.aar""material-tap-target-prompt-2.0.1.aar"})
    The source for the MaterialTapTargetWrapper is attached.

    Create B4AtoXML.bat Batch script

    To create the .xml file for our library we use a Batch script that calls the doclet:

    If Not Exist %2\build\B4Alibs\ ( mkdir %2\build\B4Alibs\ )
    1\bin\javadoc.exe -doclet BADoclet -docletpath C:\B4X\B4A\Doclet -sourcepath %2\src\main\java %3 -classpath %4 -b4atarget %5
    Change the path to the B4A doclet to your needs. I placed it under C:\B4X\B4A\Doclet.
    The B4AtoXML.bat file is attached to this post and you should place it in the <Project Dir>\src\ folder. Remember to change the correct doclet path. Change the filename extension to .bat!

    Attached Files:

    Last edited: Sep 5, 2017
  2. corwin42

    corwin42 Expert Licensed User

    Create the projects Run/Build configuration and Build the library

    To create the project we need to call the copyB4AFiles task we added to the build.gradle file. To make this easy, we create a Run configuration.

    Select the Spinner in the toolbar (it may show the "app" build configuration) and select "Edit preferences..."

    You can at first delete the "app" configuration with the "-" button in the toolbar.

    Then create a new configuration with the following sequence:
    1. Press the green "+" button.
    2. Select "Gradle" as configuration type
    3. Enter "MaterialTapTarget library" as Configuration name
    4. Check the "Share" checkbox
    5. Select "MaterialTapTarget" as Gradle project
    6. Enter "copyB4AFiles" as Task
    7. Press the green "+" button in the bottom of the window to add an external tool and select "Run External Tool" as the type.
    8. Again press the green "+" button.
    9. Enter the following information in the dialog:
    Program: $ProjectFileDir$\MaterialTapTarget\src\B4AtoXML.bat
    Parameters: "$JDKPath$" "$ProjectFileDir$\MaterialTapTarget" de.amberhome.materialtaptarget "$Classpath$" "$ProjectFileDir$\MaterialTapTarget\build\B4Alibs\MaterialTapTarget.xml"
    Working Directory: $ProjectFileDir$\MaterialTapTarget\src

    Confirm all dialogs with OK.

    Be sure to set the package name (here de.amberhome.materialtaptarget) to the correct package name of your project. Otherwise the xml file will not be created correctly.

    Create the library

    After you have created the build/run configuration it schould be selected directly and you can create your library with the Play button:

    The library wrapper should now be created. The copyB4AFiles task should place everything we need in the following directory: <Project Dir>\build\B4Alibs
    There you can find MaterialTapTarget.arr (our library), MaterialTapTarget.jar (dummy jar file) and the MaterialTapTarget.xml (B4A library xml file). Copy these three files to your additional libraries folder.
    You can mark all three files in the "Project" browser, press CTRL-C to copy the files to the clipboard and paste them to your additional libraries folder.

    This is my whole work cycle, when I make changes to a library.
    1. Make changes in the code
    2. press "Play"
    3. Mark all three files in Project tree and press CTRL-C
    4. Change into a windows file explorer which points to the additional libraries folder and press CTRL-V
    5. Select Reload in the libraries tab in B4A to refresh the library and test the changes.

    The complete project structure should look like this:
    Last edited: Sep 5, 2017
  3. corwin42

    corwin42 Expert Licensed User

    Download the original libraries .aar files

    We are not finished yet. We still need the .aar files for the original library. To get them is sometimes a bit tricky because Android Studio downloads and extracts the original library automatically if you put it in the dependencies of your project.

    For B4A we need the .aar files and we have to put them into the additional libraries folder.

    For the MaterialTapTarget library we go to the original github project: MaterialTapTargetPrompt

    On the github page click on the "Download" button:

    Click on Files:

    Click on the 2.0.1 folder:

    Download the material-tap-target-prompt-2.0.1.aar file.

    This file needs to be placed to the additional libraries folder, too.

    Now it should be possible to use the library in B4A:
    Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    End Sub

    Sub Activity_Resume

    End Sub

    Sub Activity_Pause (UserClosed As Boolean)

    End Sub

    Sub ShowTapTarget
    Dim TapTarget As MaterialTapTargetBuilder
    "This is a test for the MaterialTapTarget library")
    End Sub
    Last edited: Sep 5, 2017
  4. azzam223

    azzam223 Active Member Licensed User

    thanks you corwin to this tutorial Could please record this in video So Can any one like me see it
  5. DonManfred

    DonManfred Expert Licensed User

    You are able to read or not. Why not following the tutorial by yourself?
    It is not as hard.
    I followed it yesterday and i did not found it to be complicated!?
  6. corwin42

    corwin42 Expert Licensed User

    Thanks. I thought many times about video tutorials for B4A but I think it is not worth the afford.
    Hopefully I will continue the tutorial tomorrow.
    Peter Simpson likes this.
  7. DonManfred

    DonManfred Expert Licensed User

    I hardly can´t wait for it :)

    Once i have followed all steps to work with AS i´ll try to build the next library wrap directly with AS.

    Surely i´ll post some questions...

    Like this for ex.

    I did a wrap for the Philips HUE SDK which is based on 2 3rd party jars from Philips.
    Can i include the jars inside the AS-Project to deliver it "at the end" as a AAR containing the additional jars?

    Based on what i read in post #1 i can imagine it is possible with the build-script of AS but i´m not sure if and how to do ;-)
  8. corwin42

    corwin42 Expert Licensed User

    No, I don't think it is possible to put other JARs in a single AAR.
  9. Erel

    Erel Administrator Staff Member Licensed User

    It is possible from B4A side. Any jar found in the AAR package will be extracted.
  10. corwin42

    corwin42 Expert Licensed User

    Is it possible to include aar files, too?
  11. Erel

    Erel Administrator Staff Member Licensed User

    AAR files inside an AAR file? No.
  12. corwin42

    corwin42 Expert Licensed User

    Sorry for the big delay. I was too busy the last two weeks.

    Ok, I have finished the tutorial and added all missing files now.

    Be aware that I have changed the Module name from TapTargetWrapper to MaterialTapTarget. So if you had started before you need to refactor and rename the module. Or you just start again.

    Please tell me if the tutorial works for you or if there is something not working.
    moster67, stevel05 and DonManfred like this.
  13. corwin42

    corwin42 Expert Licensed User

    One more thing. If you have a question about the tutorial please ask in this thread.

    If you have problems to create your own wrapper in AndroidStudio please ask your questions in a new thread.
  14. DonManfred

    DonManfred Expert Licensed User

    Can you upload a sample B4AtoXML.bat? Or did i missed it?
  15. corwin42

    corwin42 Expert Licensed User

    It is in the first post. It is not allowed to attach .bat files in the forum so I uploaded it as .txt. Just change the suffix.
    DonManfred likes this.
  16. barx

    barx Well-Known Member Licensed User

    This looks sooooo good. thank you so much @corwin42 for your hard work. i will be giving it a test run very soon.
  17. westingenieria

    westingenieria Active Member Licensed User

    Parsing code. 0.05
    Compiling code. 0.24
    Compiling layouts code. 0.03
    Generating R file. 0.07
    Compiling generated Java code. Error
    javac 1.7.0_80
    src\b4a\example\ error: package does not exist
    public = null;

    hello everyone, i have a error when I try create example of FirstLibrary with function very simple(sum two numbers), but with android studio
    as in this example, but in b4a trown me this error.

    this is my class library

    public class MiPrimeraLibreriaAS {

    public int add(int x, int y) {
    return x + y;}

    any idea, why this error ??

    thanks in advance
  18. DonManfred

    DonManfred Expert Licensed User

    BUT you are asking this in the wrong thread! You should create a NEW Thread for any question you have. In this case in the developer questionsforum.

    But not in a thread about a totally different thing.
  19. westingenieria

    westingenieria Active Member Licensed User

    I am not sure DonManfred, because i wrote ""but with android studio
    as in this example""

    i am not understanding you response, because my question is complete relations with this thread
  20. Erel

    Erel Administrator Staff Member Licensed User

    It doesn't matter whether your question is related to the thread or not. You should always start a new thread when you post a question.
    Peter Simpson likes this.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice