天天看点

Android建立远程动态库,提交到Jcenter一、注册bintray帐号,获取userName、ApiKey:

用Android Studio的人都知道build中默认远程库地址就是JCenter,所以我们有时开发的开源项目也可提交至JCenter然后直接compile即可。Jcenter是Bintray旗下的库之一,所以先要提交到bintray,然后同步到jcenter。

一、注册bintray帐号,获取userName、ApiKey:

注册地址:https://bintray.com,已有帐号可忽略。 然后拿到帐号的ApiKey

Android建立远程动态库,提交到Jcenter一、注册bintray帐号,获取userName、ApiKey:

二、新建Module:

在项目中新建module:

Android建立远程动态库,提交到Jcenter一、注册bintray帐号,获取userName、ApiKey:

选择android library:

Android建立远程动态库,提交到Jcenter一、注册bintray帐号,获取userName、ApiKey:

我在module中新建了一个Util作为测试:

/**
 * Created by wangcong on 2017/3/8.
 * <p>
 */

public class LibUtil {
    public static void toast(Context context) {
        Toast.makeText(context, "LibDemo", Toast.LENGTH_SHORT).show();
    }
}      

三、配置local.properties:

在local.properties中配置user及apikey:

Android建立远程动态库,提交到Jcenter一、注册bintray帐号,获取userName、ApiKey:
bintray.user=YourUserName
bintray.apikey=YourApiKey      

四、配置build.gradle:

在project中添加bintray及github插件:

Android建立远程动态库,提交到Jcenter一、注册bintray帐号,获取userName、ApiKey:
dependencies {
    classpath 'com.android.tools.build:gradle:2.2.2'
    classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7.3'
    classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'
    // NOTE: Do not place your application dependencies here; they belong
    // in the individual module build.gradle files
}
      

在library中添加配置:

Android建立远程动态库,提交到Jcenter一、注册bintray帐号,获取userName、ApiKey:
apply plugin: 'com.android.library'

//配置插件
apply plugin: 'com.github.dcendents.android-maven'
apply plugin: 'com.jfrog.bintray'

version = "v1.0.1" //lib版本号

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.0"

    defaultConfig {
        minSdkVersion 15
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.0.1'
    testCompile 'junit:junit:4.12'
}

def siteUrl = 'https://github.com/Yuhoon'      // 项目主页
def gitUrl = 'https://github.com/Yuhoon'      // Git仓库的地址
group = "com.yuhoon.demo"    // GroupId ,一般为包名(必填)

install {
    repositories.mavenInstaller {
        // This generates POM.xml with proper parameters
        pom {
            project {
                packaging 'aar'
                // Add your description here
                name 'This is util'     //项目描述
                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 'yuhoon'
                        name 'wangcong'
                        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.srcDirs
    classpath += 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 = "lib"    //发布到JCenter上的库(必填)
        websiteUrl = siteUrl
        vcsUrl = gitUrl
        licenses = ["Apache-2.0"]
        publish = true
    }
}      

配置完成后,sync project下

五、本地库编译及上传:

打开Terminal,输入 gradlew install 进行安装编译:

Android建立远程动态库,提交到Jcenter一、注册bintray帐号,获取userName、ApiKey:
Android建立远程动态库,提交到Jcenter一、注册bintray帐号,获取userName、ApiKey:

如果build successful,更新编译成功可以上传了,上传命令为:gradlew bintaryUpload 一样如果successful上传成功了,就可在自己的maven中查看了。

六、提交到Jcenter:

上传成功后,打开bintray网站就可以在maven中看到自己多了个package

Android建立远程动态库,提交到Jcenter一、注册bintray帐号,获取userName、ApiKey:

选择项目,添加到Jcenter ,点击Add to jCenter,然后填写msg:

Android建立远程动态库,提交到Jcenter一、注册bintray帐号,获取userName、ApiKey:

审核一般当天就可以通过,审核通过会发送到绑定邮箱,小伙伴注意查收。审核通过就可以用自己的远端的库了。

七、远程库的使用:

审核通过后在build.gradle中添加:

compile 'com.yuhoon.demo:library:v1.0.1'      

组合为你之前申请的 groupId+项目名+版本号,同时你也 可以在你的bintray上查到你的库引用地址

Android建立远程动态库,提交到Jcenter一、注册bintray帐号,获取userName、ApiKey:

添加关联后就可以调用库中的方法了:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    LibUtil.toast(this);
}      

以后的版本更新也是同样操作,更改版本号上传即可,审核会在几小时后通过。