Android Local Maven (Android Library Part 2)

Tram Ho

Hi guys. In the previous article, I introduced Android Library and how to publish to remote (specifically, here is Jitpack.io )

Continuation of this Library related series.

Today I present a situation. When you want to change (add, update, fix, …) something on the Library. After changing the source, in order you have to update its version -> make a release on git -> send them to JitPack -> Expect them to be error free. Next is the main Project, you update the version of the Library on the main Project and then check if it runs fine. These steps will continue to repeat if the source you code in your Lib fails.

The above situation causes loss of time and waiting. To solve the above problem, I found that there is a solution that is Local Maven Repository.

Of course your Lib usually contains the Sample App to use all the features of the Library, so you can check if it works without the whole process you listed above, but sometimes is not enough and you need to check on the project that you really want to deploy the Library on.

The benefits that Local Maven Repository can see are:

  1. Gives you an option if you just want to save the Android Library or module locally as a maven repository on your computer. From there you can import the dependency directly into your Project as if it had been published.
  2. Provide the right solution to save time, avoid unnecessary waiting, as well as create initiative for Developer in the development process.

Now let’s go through the steps to implement.

  1. The first is to set up the 2 projects that we have set up earlier: CalculatedApp (Main Project) and CalculatedLib (Lib)
  2. Library
    1. In the build.gradle file, add the plugin

    image

    1. Define artifactId and groupId

    image

    1. Add Config
      1. If your Lib is single module, either there are many modules but the modules have no dependencies on each other.

      image

      1. If your Project has many modules that are independent libraries and one module is a collection of those libs.

      image

      1. Tip: If your project contains many modules. To configure them, we have to create for each Lib a config like above. To make it simpler we can create a publish_local.gradle file. In this file, I install it in a common config for the Libs.

      image

      image

      In each lib I just need to set-up groupId and corresponding artifactId

      image

      Especially add the link to the total config file

      image

      1. Publish To Maven Local

      All that is needed now is run task: publishToMavenLocal

      image

      Or run the Terminal command:

      ./gradlew clean ./gradlew build ./gradlew publishToMavenLocal

      image

      Specifically here are 2 files:

      1. The .pom file contains the information of the Lib (in XML format)
      2. The .aar file is the built-in Lib file
      3. Finally, Add dependency and config to the main Project using your library

      image

      • Enable mavenLocal() repository to the Build Systems . file
      • Notice that add mavenLocal() to the top of the list. It should be noted that having mavenLocal at the top of the list will help you to always select the libraries available in the ~/ .m2/repository/ directory first.

      Next, Add the corresponding dependency to the main Project and use the functions Library in the main source code.

      image

      Yes. That’s my little share related to Library, publish their remote vs local.

      You can refer to my source code below.

      Hope my article somewhere will help you in my Android GST community. See you in the next post.

      Source code :

      Thư viện

      Main Project

Share the news now

Source : Viblo