Android Tutorial Create a wrapper library with Android Studio

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.

B4X:
allprojects {
    repositories {
        jcenter()
        maven { url "https://maven.google.com" }
        //mavenCentral()
        //maven { url 'https://jitpack.io' }
    }
}
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:

B4X:
apply plugin: 'com.android.library'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"
    enforceUniquePackageName = false
    android.packageBuildConfig = false

    defaultConfig {
        minSdkVersion 14
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"

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

    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 'uk.co.samuelwall:material-tap-target-prompt:2.0.1'
    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:

B4X:
@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:

B4X:
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!
 

Attachments

Last edited:

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..."
upload_2017-9-5_10-47-24.png


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:
upload_2017-9-5_10-57-56.png

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:
upload_2017-9-5_11-40-37.png


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:
upload_2017-9-5_12-20-50.png
 
Last edited:

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:
upload_2017-9-5_11-49-46.png


Click on Files:
upload_2017-9-5_11-50-36.png



Click on the 2.0.1 folder:
upload_2017-9-5_11-51-15.png



Download the material-tap-target-prompt-2.0.1.aar file.
upload_2017-9-5_11-52-0.png


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

Now it should be possible to use the library in B4A:
B4X:
Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("layout1")
    CallSubDelayed(Me, "ShowTapTarget")
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub ShowTapTarget
    Dim TapTarget As MaterialTapTargetBuilder
    TapTarget.Initialize("TapTarget")
    TapTarget.PrimaryText("This is a test for the MaterialTapTarget library")
    TapTarget.TargetView(Button1)
    TapTarget.Show
End Sub
 
Last edited:

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
 

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.
 

DonManfred

Expert
Licensed User
Hopefully I will continue the tutorial tomorrow.
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 ;-)
 

corwin42

Expert
Licensed User
It is possible from B4A side. Any jar found in the AAR package will be extracted.
Is it possible to include aar files, too?
 

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.
 

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.
 

corwin42

Expert
Licensed User
Can you upload a sample B4AtoXML.bat? Or did i missed it?
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.
 

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.
 

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\main.java:286: error: package my.name.package does not exist
public my.name.package_my_name_class = 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

@BA.Version(1.0f)
@BA.ShortName("FirstLib")
@BA.DependsOn(values={"miprimeralibreriaas.aar"})
public class MiPrimeraLibreriaAS {

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


any idea, why this error ??

thanks in advance
 

westingenieria

Active Member
Licensed User
No
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.

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
 
Top