This tutorial tries to show how to create a wrapper library with Android Studio.
Prepare the project
Creating a new project
By default a module named "app" is created. We don't need this so we can delete it.
Create Module for Core and B4AShared jar libraries
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.
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:
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:
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:
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!
Prepare the project
Creating a new project
- Enter an application name (We use "B4AWrapperTutorial" for this tutorial)
- select a domain name
- Click Next
- Select "Phone and Tablet" and a minimum SDK API level your library should have.
- Click Next
- Select "Add no Activity"
- Click Finish
By default a module named "app" is created. We don't need this so we can delete it.
- Select File->Project Structure...
- Select "app" in the modules list
- Click the "-" button at the top of the list.
- Confirm dialog
- Click Ok
Create Module for Core and B4AShared jar libraries
- Select File->New->New module...
- Select "Import .JAR/.AAR package"
- Select Core.jar from your B4A installation folder (C:\Program Files (x86)\Anywhere Software\Basic4android\Libraries\Core.jar)
- Subproject name should be left as "Core"
- Click Finish
- Select File->New->New module...
- Select "Import .JAR/.AAR package"
- Select B4AShared.jar from your B4A installation folder (C:\Program Files (x86)\Anywhere Software\Basic4android\Libraries\B4AShared.jar)
- Subproject name should be left as "B4AShared"
- Click Finish
- Select File->New->New module...
- Select "Android Library"
- Name your library (We use "MaterialTapTarget" for this tutorial)
- I always use the same upper/lower case name for the module name. So again use "MaterialTapTarget"
- Click Finish
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.
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: