Hot Articles Guide | Click the title to read
Welcome to join the Java and Android architecture community, receive learning videos
Awesome! 74 complete APP source codes!
Further Discussion on the Latest Android Architecture—Google’s Official Android Development New Architecture Guide
In our daily work, we often reference third-party open-source libraries, such as butterknife, Glide, RxJava, etc. The simplest and most convenient way is to reference them in gradle using compile, for example:
compile ‘io.reactivex:rxjava:1.1.3’
Have you ever thought about writing your own open-source library to provide a reference for others? This article introduces how to create a library for others to use.
First, you need to have a library
Creating a library is simple; there are plenty of tutorials online. To save space, this article will only provide a brief introduction. In Android Studio, first create a normal project, then right-click on the project and select -> New -> Module
In the pop-up window, select ‘Android Library’, and this will create the so-called aar package, which is similar to a traditional jar package.
Then give it a powerful name, and you’re done!(However, if you want your main project to use this lib, you also need to configure it in build.gradle; there are many articles online that cover this, so I won’t go into detail)
Next is Bintray and jCenter
What is Bintray? What is jCenter?
First, jCenter is a code repository, equivalent to a public storage control. Simply put, you package your aar file and some documentation files and upload them to the jCenter server, and others can download your uploaded packages from the jCenter server.
So what is Bintray? Bintray is the provider of jCenter, supporting the upload of libraries to multiple platforms. jCenter is just one of many platforms. To put it simply, jCenter is a warehouse located somewhere, and Bintray is the delivery truck; the library you write is the cargo.
Therefore, if we want to share our written library with others, we must upload it to jCenter through Bintray. First, we need to register an account on Bintray or log in directly using a GitHub account.
After logging in, on the left menu at the bottom of the page at https://bintray.com/profile/edit, there is an API Key to copy.
Add the following to the root gradle of your project:
Refer to
Add the following in the library’s gradle:
apply plugin: 'com.github.dcendents.android-maven'
apply plugin: 'com.jfrog.bintray'
version = "0.1.1" // Change to your version number
// Change to the homepage of your project
def siteUrl = 'https://github.com/h3clikejava/ExtendImageView'
def gitUrl = 'https://github.com/h3clikejava/ExtendImageView.git' // Change to the url of your Git repository
group = "h3c.extendimageview" // Maven Group ID for the artifact, usually fill in your unique package name
install {
repositories.mavenInstaller {
// This generates POM.xml with proper parameters
pom {project {
packaging 'aar'// Add your description here
name 'auto extend ImageView for Android.' // Project description
url siteUrl// Set your license
licenses {
license {
name 'The Apache Software License, Version 2.0'
url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
}
}
developers {
developer {
id 'H3c' // Fill in some basic information
name 'H3c'
email '[email protected]'}
}
scm {
connection gitUrl
developerConnection gitUrl
url siteUrl
}
}
}
}task sourcesJar(type: Jar) {from android.sourceSets.main.java.srcDirs
classifier = 'sources'}task javadoc(type: Javadoc) {source = android.sourceSets.main.java.srcDirsclasspath += project.files(android.getBootClasspath().join(File.pathSeparator))
}task javadocJar(type: Jar, dependsOn: javadoc) {
classifier = 'javadoc'from javadoc.destinationDir}artifacts {
archives javadocJar
archives sourcesJar
}
Properties properties = new Properties()
properties.load(project.rootProject.file('local.properties').newDataInputStream())
bintray {
user = properties.getProperty("bintray.user")
key = properties.getProperty("bintray.apikey")configurations = ['archives']
pkg {
repo = "maven"name = "extend-imageview" // Project name published to JCenterwebsiteUrl = siteUrl
vcsUrl = gitUrl
licenses = ["Apache-2.0"]
publish = true}
}
The code is a bit messy, I was feeling lazy. It’s actually quite simple; just copy the above segment and modify it to your parameters. For specifics, you can refer to here(https://github.com/h3clikejava/ExtendImageView/blob/master/library/build.gradle)
You also need to write your Bintray username and API key into the local.properties file of your project
// Sample values, for reference only
bintray.user=h3 // your bintray user name
bintray.apikey=c5434272d522d35d1a0123459981225564155753 // your bintray api key
OK, after completing the above configuration, you are ready to upload to Bintray. Execute it through gradle.
gradlew installgradlew bintrayUpload
Usually, everyone will fail; there are many reasons. Here it’s very frustrating not to mention! Because this article will later teach you to give up this lengthy and extremely prone to failure method.
If you compile successfully and upload to Bintray normally, then congratulations, you are really awesome, and the next steps will be very easy for you.
Seeing your version in Versions means success. Next, click Add to JCenter
Enter some descriptions and wait for approval. Once approved, it can be referenced in third-party libraries.
A Simpler Method: Novoda
After reading the above content, I believe you already know how to write a library and upload it to jCenter through Bintray. But don’t you think it’s really complicated… There are so many configurations in build.gradle that it’s almost impossible to write it with your eyes closed; you can only search online for copy-paste.
Next, I will introduce a simpler way using the Novoda library. It still uses Bintray and jCenter, but greatly simplifies the configuration method in build.gradle. First, add this line to the root gradle:
apply plugin: 'com.novoda.bintray-release'
// must be applied after your artifact generating plugin (eg.
java / com.android.library)
Note, it must be written below com.android.library. Next, in your lib gradle, write this:
publish {
userOrg = 'novoda'
groupId = 'com.novoda'
artifactId = 'bintray-release'
publishVersion = '0.3.4'
}
In the android{} section, you need to add:
lintOptions {
abortOnError false
}
At this point, run the following command in the AS terminal:
$ ./gradlew clean build bintrayUpload -PbintrayUser=BINTRAY_USERNAME -PbintrayKey=BINTRAY_KEY -PdryRun=false
Replace BINTRAY_USERNAME with your Bintray name and BINTRAY_KEY with your Bintray key.
If successful, congratulations; if failed… there are many reasons, but this article won’t mention them! Using this method is just so confident, those who like Bintray jCenter can go ahead and tinker with it.
jCenter is currently the most widely used third-party gradle repository, but its application, creation, compilation, and upload processes are really cumbersome, feeling like a product from the 1980s. You still need to upload, compile, and go through reviews, which is super easy to fail, right?
JitPack is also a code repository, equivalent to jCenter, but currently, it has fewer users than jCenter. However, more and more open-source projects are starting to use JitPack.
It’s really very, very simple. How simple? Let’s take a look.
For example, I wrote a personal Android base library for quick project building, located at: AFastProject (those who don’t reward me, remember to give me a star…), due to the reference of third-party libraries, both methods mentioned above for publishing the aar resulted in various issues, either unable to compile or package loss; I spent a long time tinkering. In the end, I solved it with JitPack in one line…
First, open the website http://jitpack.io
In the input box, enter your GitHub project address, and click Look up. It will automatically traverse your recent commits and released versions.
Click on the version you want to reference and choose Get it.
At the end, it will tell you how to reference it. Yes, you read that right, the client doesn’t need to change anything. Other projects can reference it directly!
Source: http://www.jianshu.com/p/31410d71eaba
Did you gain something after reading this article? Please share it with more people.
For more learning materials, click the “Read Original” below to get them.
Java and Android Architecture
Welcome to follow us, let’s discuss technology together. Scan and long press the QR code below to quickly follow us. Or search for the WeChat public account: JANiubility.
Public Account:JANiubility