It’s not uncommon to see github importing someone else’s library with a similar statement:
repositories {
jcenter()
}
dependencies {
compile 'com. A unique: bottomsheet - core: 1.5.0'
compile 'com. A unique: bottomsheet - Commons: 1.5.0' // optional
}
Copy the code
I have to say, it’s really easy to use. So if you have a good library, how do you share it with others, or put it in Bintray for your own use?
### Release the first Android library (still a little exciting to think about)
Register at bintray.com and get apI-key:
Special thanks must be given to the author :Ashraff Hathibelagal
Code.tutsplus.com/zh-hans/tut… I basically follow his tutorial. Also refer to XRecyclerView gradle, also thank you.
The article is good, but there are pits…. So I reorganized myself
There are two main steps:
1. Write a library as usual
2, publish to Bintray
There are two Gradles that need to be modified.
Project build.gradle
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com. Android. Tools. Build: gradle: 2.0.0'
In order to interact with Bintray in Android Studio, you should add Bintray to the Dependencies of your project's build.gradle file. Classpath 'com. Jfrog. Bintray. Gradle: gradle bintray - plugin: 1.2' because you want the library to the Maven repository, you should also like this to add Maven plug-ins. Classpath "com. Making. Dcendents: android - maven - gradle - plugin: 1.3" * /
classpath 'com. Jfrog. Bintray. Gradle: gradle bintray - plugin: 1.2'
classpath "Com. Making. Dcendents: android - maven - gradle - plugin: 1.3"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
Copy the code
Module build.gradle
apply plugin: 'com.android.library'
/* Step 2: Apply plug-ins Open your library module's build.gradle file and add the following code to apply the plug-ins we added in the previous step. * /
apply plugin: 'com.jfrog.bintray'
apply plugin: 'com.github.dcendents.android-maven'
/* Step 3: Specify POM details when uploading the library, the Bintray plugin looks for POM files. Even if the Maven plugin generated it for you, you should specify the groupId tag and version tag values yourself. To do this, use the group and version variables in the Gradle file. * /
version = "1.0.0"
group = "com.example"
def siteUrl = 'https://github.com/heinika/MyHelloWorldLibrary'
def gitUrl = 'https://github.com/heinika/MyHelloWorldLibrary.git'
android {
compileSdkVersion 23
buildToolsVersion "23.0.3"
defaultConfig {
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs'.include: ['*.jar'])
testCompile 'junit: junit: 4.12'
compile 'com. Android. Support: appcompat - v7:23.3.0'
}
To comply with the Maven standard, your library should also have a JAR file containing the library's source files. To generate the JAR file, you need to create a new JAR task, generateSourcesJar, and specify the location of the source file using the FROM function. * /
task generateSourcesJar(type: Jar) {
from android.sourceSets.main.java.srcDirs
classifier 'sources'
}
We also recommend that you have a JAR file containing Javadocs in your library. Because you don't currently have any Javadocs, you need to create a new Javadoc task, generateJavadocs, to generate them. Use the source variable to specify the location of the source file. You should also update the classpath variable so that the task can find the classes that belong to the Android SDK. You can do this by adding the return value of the Android.getBootCLASspath method to it. * /
task generateJavadocs(type: Javadoc) {
source = android.sourceSets.main.java.srcDirs
classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
}
/* Next, to generate the JAR from Javadocs, create the JAR task, generateJavadocsJar, and pass generateJavadocs destinationDir property to its FROM function. To ensure that the generateJavadocsJar task starts only after the generateJavadocs task completes, add the following code snippet that uses the dependsOn method to determine the order of the tasks: your new task should look like this :*/
task generateJavadocsJar(type: Jar, dependsOn: generateJavadocs) {
from generateJavadocs.destinationDir
classifier 'javadoc'
}
/* * step 6: * To import the source and Javadoc JAR files into the artifacts list, you should add the names of their tasks to the Configuration, * called archives, The Artifacts list will be uploaded to the Maven repository. Use the following snippet to do this: */
artifacts {
archives generateJavadocsJar
archives generateSourcesJar
}
Now it's time to run the task we created in the previous steps. Open the Gradle Projects window and search for a task named Install. * /
To configure the Bintray plugin, you should use the Bintray closure in the Gradle file. First, authenticate using the user and key variables that correspond to your Bintray username and API key. In Bintray, your library will be placed in the Bintray Package. You should use the intuitively named repo, name, Licenses, and vcsUrl parameters in the PKG closure to provide detailed information, and if the package does not exist, it will be created automatically for you. When you upload files to Bintray, they are associated with a version in the Bintray package. Therefore, the PKG must contain a Version closure whose name attribute is set to a unique name. Alternatively, you can use the desc, release, and vcsTag parameters to provide a description, release date, and Git tag. Finally, to specify which files should be uploaded, set the value of the configuration parameter to Archives. bintray { user = 'test-user' key = '01234567890abcdef01234567890abcdef' pkg { repo = 'maven' name = '. Com. Making. Hathibelagal mylittlelibrary version {name = '- tuts 1.0.1 desc =' My test upload 'released = new Date () VcsTag = '1.0.1} licenses = [' Apache 2.0] vcsUrl =' https://github.com/hathibelagal/LibraryTutorial.git 'websiteUrl = 'https://github.com/hathibelagal/LibraryTutorial' } configurations = ['archives'] } */
Properties properties = new Properties()
properties.load(project.rootProject.file('local.properties').newDataInputStream())
bintray {
user = properties.getProperty("BINTRAY_USER")
key = properties.getProperty("BINTRAY_KEY")
configurations = ['archives']
pkg {
repo = "maven"
name = "MyHelloWorldLibrary" // The name of the project to publish to JCenter
version {
name = '- tuts 1.0.1'
desc = 'My test upload'
vcsTag = '1.0.0'
}
websiteUrl = siteUrl
vcsUrl = gitUrl
licenses = ["Apache - 2.0"]
publish = true}}/* Step 9: Upload a file using Bintray. Open the Gradle Projects window again and search for the bintrayUpload task. Double-click on it to start uploading files. * /
/* Once the task is complete, you can open your browser to access your Bintray package details page. You will see a notification that you have four unpublished files. To Publish these files, click the Publish link. * /
Your library is now available as a Bintray package. As long as you share the URL of your Maven repository, plus the Group ID, artifact ID, and version number, any developer can access your repository. For example, in order to use the library we just created, the developer must introduce the following code snippet: repositories { maven { url 'https://dl.bintray.com/eruzza/maven' } } dependencies { compile 'com. Making. Hathibelagal. Librarytutorial: mylittlelibrary: @ 1.0.1 aar'} note that before add libraries to compile dependence, Developers must explicitly import your repository in the list of Repositories. * /
/ * * Bintray user manual: https://bintray.com/docs/usermanual/ * /
Copy the code
Add the library to JCenter
By default, Android Studio searches for libraries in a repository called JCenter. If you introduce your own repositories into the JCenter repository, the developer does not have to add anything to his repositories.
To add your library to JCenter, open a browser and visit your Bintray package details page. Click the Add to JCenter button
Then you come to a page that asks you to fill in some information. You can use the Comments section to mention any details about the library. Click the Send button to start the Bintray review process. In a day or two, the Bintray people will link your library to the JCenter repository so you can see a link to JCenter on your package details page. Any developer can now use your repository without changing your list.
use
When you use the library in the future, just use the following sentence. Isn’t that cool? Hahaha.
compile 'com. Example: myfristlibrary: 1.0.0'
Copy the code
The address of the project: https://github.com/heinika/MyHelloWorldLibrary the original address: http://heinika.github.io/2016/04/13/%E5%8F%91%E5%B8%83%E7%AC%AC%E4%B8%80%E4%B8%AAAndroid%E5%BA%93(%E6%83%B3%E6%83%B3%E8% BF%98%E6%98%AF%E6%9C%89%E7%82%B9%E5%B0%8F%E6%BF%80%E5%8A%A8)/