If you are an Intermediate or advanced android developer, you are now familiar with modules. The number modules publicly available, is increasing each day exponentially. If you are planning to launch your own project, you might need to manage those modules in better and sustainable way.
How private maven repository helps to grow fast?
Well, of course we have option for public maven support by JitPack. Means if you want to make your library visible to public, and you don’t have to hide anything, than JitPack might be your best choice.
However, if the number of projects you work on, is 10/15+, and inside there’s lots of modules related with authentication, then definitely, you should consider setting up a private maven repository.
First you need a dedicated server of your own. Or you can purchase any VPS from reputable companies. In this tutorial we will discuss about Artifactory. However, there’s other alternatives that you might want to check out (1) Nexus, (2) Archiva.
Artifactory Installation
We will set up the whole system in three small steps:
(1) Install Java: In this step we will install the Java Runtime
(2) Install Artifactory: We will download and install artifactroy
(3) Configure Artifactory – We will configure artifactory from web browser and set up necessary groups and permissions.
(4) Set up a test library project for android – We’ll add a repository to our server, than we will set up a new project with new module. And push this module to the server.
(1) Install Java
SSH your VPS. And get the root privileges. Then perform the following commands:
# add-apt-repository ppa:webupd8team/java
# apt-get update
# apt-get install oracle-java8-installer
This will install java8. After installation you can check java: java –version. Not if this method doesn’t work, you can install the default jre provided in the apt list. In my case I’ve found an working artifactory installation with default-jre.
(2) Install Artifactory
First add the jfrog source in the list:
echo "deb https://jfrog.bintray.com/artifactory-debs {distribution} {components}" | sudo tee -a /etc/apt/sources.list
Note: If you are unsure, components should be “main.” To determine your distribution, run lsb_release -c
For example:
echo "deb https://jfrog.bintray.com/artifactory-debs xenial main" | sudo tee -a /etc/apt/sources.list
Next add the necessary keys
curl https://bintray.com/user/downloadSubjectPublicKey?username=jfrog | sudo apt-key add -
Now do an update and install the artifactory community edition
sudo apt-get update sudo apt-get install jfrog-artifactory-oss
The installer will show some important start/stop related info at end. In my case (February 2019), starting and checking the service status is:
systemctl start artifactory.service systemctl status artifactory.service
Start the artifactory
(3) Configure Artifactory
Open your browser and navigate to http://IPADDRESS:8081/artifactory/
Replace IPADRESS with the server ip address. You will then see the login screen. If its the first time you will need to set a new password for admin account.
(a) Next we will add a group. This group will connect users and permissions. Goto Admin>Groups. Click the new button to create a new permission. Lets name it ‘Contributors’. And lets describe it as ‘A group for library contributors’. We can skip the users part right now. Save it.
(b) Next we will create a permission. Browse to Admin>Security>Permissions. And click the New button at upper right. Lets name it ‘Contribute to Anything’ and check both ‘Any Local Repository’ and ‘Any Remote Repository’ box at the bottom. This will gain an access to any repository in the system to the contributors. If you don’t want to give access to all repository, leave those check boxes and select the repositories below to give access to. Click the ‘Groups’ tab now. Now doube click the ‘contributors’ group, and check all the privileges you want to give. Lets check the ‘Delete/Overwrite’, ‘Deploy/Cache’, ‘Annotate’, ‘Read’ leaving the ‘Manage’ checkbox. Click save.
(c) Now create an user account. In the create section you can assign the user to a group. Remember the permission we set up for the group? Assigning this user to the contributors group will gain them relevant access. Click save, to save the user.
(4) Set up a test library for android
(a) Before logging out, lets create a new repository. Browse to Admin>Repositories>Local. Click the +New button to create a new repository. Lets name is ‘libs-release-local’. Click save.
(b) Log out and log into the new user account we have created. After login click on the username at top bottom. Clicking on it will require your password to unlock. Then in Authentication Settings copy and store the Encrypted Password somewhere. We will need it.
(c) Now in android studio, create a new project, with any name. Now add new android library module via File->New->New Module->Android Library. Lets make the package name as ‘my.group.package’.
(d) Now, Add this to root level build.gradle
buildscript {
dependencies {
// Add this line
classpath "org.jfrog.buildinfo:build-info-extractor-gradle:4.9.3"
}
}
Always manually check for the latest version of ‘org.jfrog.buildinfo:buidl-info-extractor-gradle’ and update if required. Recently, I fell into a problem, and it was too complicated that, I had a hard time finding the root cause. Later on after 2/3 weeks just updating the library to its latest form solved the case. Android studio nor IntelliJ Community didn’t notify me about the updates in the library.
(e) Add this at the very first line of library’s build.gradle
apply plugin: 'com.jfrog.artifactory' apply plugin: 'maven-publish'
(f) Add these at the end of the same file
def libraryGroupId = 'my.group.package'
def libraryArtifactId = 'thelibrary'
def libraryVersion = '1.0.0'
This will result the form of as my.group.package:thelibrary:1.0.0
(g)At these at the end of the same working file
publishing {
publications {
aar(MavenPublication) {
groupId libraryGroupId
version libraryVersion
artifactId libraryArtifactId
artifact("$buildDir/outputs/aar/${artifactId}-release.aar")
}
}
}
def artFile = file('artifactory.properties')
def Properties versionProps = new Properties()
versionProps.load(new FileInputStream(artFile))
def artifactory_username = versionProps['artifactory_username'].toString()
def artifactory_password = versionProps['artifactory_password'].toString()
artifactory {
contextUrl = 'http://IPADDRESS:8081/artifactory'
publish {
repository {
repoKey = 'libs-release-local'
username = artifactory_username
password = artifactory_password
}
defaults {
publications('aar')
publishArtifacts = true
properties = ['qa.level': 'basic', 'q.os': 'android', 'dev.team': 'core']
publishPom = true
}
}
}
(h) Now add a file named ‘artifactory.properties’ in the library directory. And paste the created user credentials here:
artifactory_username=User-Name
artifactory_password=Encrypted-Password
(i) Upload the library: User terminal to run the command
gradlew.bat assembleRelease artifactoryPublish
Use the library
Add this to app level build.gradle in the project that you want to use this library:
allprojects {
repositories {
jcenter()
// Add these lines
maven {
url "http://IPADDRESS:8081/artifactory/libs-release-local"
credentials {
username = "${artifactory_username}"
password = "${artifactory_password}"
}
}
}
}
Add the dependancy
dependencies {
compile ''my.group.package:thelibrary:1.0.0"
}
And We’re done!
To edit the JVM Parameters to control the amount of RAM that artifactory uses, refer to the following URL.
In Summery open the $ARTIFACTORY_HOME/bin/artifactory.default and edit the jvm parameters.
Reference:
How to setup a Private Maven Repository for in-house Android libraries distribution