Tuesday, November 15, 2022

Android aar deployment in Maven - 2022

Introduction

If you are working on android library project, you might be wondering how to publish it on Maven like this. Earlier it was done using Android studio plugin maven, but with gradle v 7.0+ it does not work. Now we have to use maven-publish. This post gives you more insights of this procedure.

Generally, there are two types of repositories: local and remote.

A local repository is the repository Maven creates on the computer it is building on. It is usually located under the $HOME/.m2/repository directory.

Remote repository is located on maven server. When any user wants to use our library, they will enter groupId and version of library they want to use.
We will create and deploy a new android aar artifact on maven.
The process can be summarized as
1. Create Account and repository on Nexus sonatype
2. Configure gradle to create, sign and upload aar file to sonatype.
3. Let sonatype verify the artifacts as per maven requirement (Close operation)
4. Release artifacts to maven.

Let's go through the steps one by one.

1. Create account on sonatype at https://issues.sonatype.org/secure/Dashboard.jspa. Register new project by creating new jira ticket. It will create new repository in sonatype
Create → Create Issue → Community Support - Open Source Project Repository Hosting → New Project → with groupid io.bitbucket.swapnilcpublic e.g. OSSRH-85813

2. You will be asked to prove that you own the domain mentioned in Jira ticket. (e.g. https://bitbucket.org/swapnilcpublic). You will be asked to place a file or create git repo under the domain to prove that it really belongs to you. Since I do not own a domain name, I created empty bitbucket repo under bitbucket repo. Here ossrh-85813 is the JIRA ticket id. For more details follow how-to-set-txt-record and personal groupId. If required a static web site can be created using bitbucket.

3. Signing: One of the requirements for publishing your artifacts to the Central Repository, is that they have been signed with PGP. Here is how tosetup signing with gpg.
Create new key with details like

Name: SwapnilGpg
Email: email@id.com
Pass: password
After creation, see created keys with
Export secret keys using
We need short key. It will be referred from gradle script. It appears after `rsa4096/` in output. Find short KeyId using
Once the GPG keys are generated, publish these keys to an open key server. Run the following command to do so. YYYYYYYY is the short key generated using previous step (E72FECF1 in my case).

Verify these keys using

4. Update build.gradle in ProjectRoot/swapnilCalculator/build.gradle with following

5. Add details below in ProjectRoot/gradle.properties In ProjectRoot/swapnilCalculator/gradle.properties

6. Upload aar, jar and signatures using
./gradlew clean publishReleasePublicationToMavenRepository After a successful deployment to OSSRH your components are stored in a separate, temporary repository, that is private to your projects members. In order to get these components published you will have to 'close' & release' them. 'Close' operation checks whether all artifacts are as specified by Maven. 'Close' operation takes few minutes to finish. Once that is successful, proceed with 'release' operation. If there is error, please resolve them and re-upload the library.
After uploading & releasing all artifacts, it takes 4-10 hours for maven to show the library.

7. Find the published library using

  1. Sonatype staging repository
  2. Maven repository
  3. Maven repository
  4. Sonatype staging repository
  5. Sonatype nexus

8. References

  1. https://gist.github.com/lopspower/6f62fe1492726d848d6d
  2. https://central.sonatype.org/publish/
  3. https://central.sonatype.org/publish/requirements/coordinates/
  4. https://central.sonatype.org/publish/publish-guide/
  5. https://shahsurajk.medium.com/technical-publishing-aars-to-maven-central-7e9c603f9ea1
  6. https://www.baeldung.com/maven-snapshot-release-repository
  7. https://docs.gradle.org/current/userguide/signing_plugin.html#sec:signatory_credentials
  8. https://docs.gradle.org/current/userguide/publishing_maven.html

Friday, February 5, 2021

Flutter: Making dashed line matching width of screen

 How can we make dashed line in flutter?

Dart does not have support for this yet. We need to make custom widget for showing dashed line. Code is given below.

class LinePainter extends CustomPainter {
@override
void paint(Canvas canvas, Size size) {
var max = size.width;
debugPrint("LinePainter max=$max");
var dashWidth = 5.0;
var dashSpace = 5.0;
double startX = 0;
final paint = Paint()..color = Colors.grey;
while (max >= 0) {
canvas.drawLine(Offset(startX, 0), Offset(startX + dashWidth, 0),
        paint..strokeWidth = 1);

final space = (dashSpace + dashWidth);
startX += space;
max -= space.toInt();
}
}

@override
bool shouldRepaint(CustomPainter oldDelegate) {
return false;
}
}
Above code will paint a line of width = size.width.

To use this painter as widget, we have to use 
CustomPaint(painter: LinePainter(),size:Size(400,1))
It will draw a dashed line with 400px width. But we want to draw line 
covering entire width of screen. For that we will use constraint in a container.
Container(constraints: BoxConstraints.tightForFinite(height: 1.0),
    child: CustomPaint(painter: LinePainter(),),)

It will draw a line as shown below

No need to use any external library for showing dashed line.


Android aar deployment in Maven - 2022

Introduction If you are working on android library project, you might be wondering how to publish it on Maven like this . Earl...