天天看點

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);
}      

以後的版本更新也是同樣操作,更改版本号上傳即可,稽核會在幾小時後通過。