1. 程式人生 > >關於自己寫的aar包釋出到maven過程中的一些問題解決

關於自己寫的aar包釋出到maven過程中的一些問題解決

很多開發者都想自己寫一套好用的框架,釋出出去給別人引用,自從android studio問世以來,不斷的改進,關於引用他人的成果非常方便簡單了,用過android studio的使用者都知道,在build.gradle中引用只需新增一句程式碼:
 compile 'com.android.support:appcompat-v7:24.1.1'
 是不是非常簡單?比以往的下載jar包,拷貝到專案是不是簡單多了?那麼,我寫好了一套非常好用的程式碼,我也想分享出去給別人用,讓別人也能在build.gradle按照上面的型別新增一句程式碼,就可以直接使用,我該怎麼做呢?具體怎麼做,可以參考這篇部落格,是他教會了我的。
[部落格](http://blog.csdn.net/lmj623565791/article/details/51148825)
根據這篇部落格,我就馬上跟著hongyang大神操作。
(1)註冊bintray.com賬號,獲取api key.(可參考上面的hongyang部落格)
 (2)引入bintray-release

在第2步的過程中,遇到了一些小麻煩,我先把對應程式碼貼下
1.在專案的build.gradle檔案中新增bintray-release的classpath
// 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'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
        //新增的classpath
        classpath 'com.novoda:bintray-release:0.3.4'
} } allprojects { repositories { jcenter() } } task clean(type: Delete) { delete rootProject.buildDir }
2.在要給他人引用的module下的build.gradle下新增如下程式碼:
apply plugin: 'com.android.library'
apply plugin: 'com.novoda.bintray-release'//新增
android {
    compileSdkVersion 24
    buildToolsVersion "24.0.1"
defaultConfig { minSdkVersion 15 targetSdkVersion 24 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:24.1.1' } //新增 publish { userOrg = 'huangxuanheng'//bintray.com使用者名稱 groupId = 'com.example.fragmentstack'//jcenter上的路徑 artifactId = 'fragmentstack'//專案名稱 publishVersion = '1.0.0'//版本號 desc = 'Oh hi, this is a nice description for a project, right?'//描述,不重要 website = 'https://github.com/huangxuanheng/ishowProject'//網站,不重要;儘量模擬github上的地址,例如我這樣的;當然你有地址最好了 }
好了,接下來就是執行下面程式碼:
gradlew clean build bintrayUpload -PbintrayUser=huangxuanheng -PbintrayKey=xxxxxxxxxxxxxxxxxx  -PdryRun=false

的時候,遇到了一些問題,問題如下:

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:lint'.
> Lint found errors in the project; aborting build.

Fix the issues identified by lint, or add the following to your build script to proceed with errors:
...
android {
    lintOptions {
        abortOnError false
    }
}
...

* Try:        
Run with --debug option to get more log output.

我一看,馬上copy這句話:

android {
    lintOptions {
        abortOnError false
    }
}

到我的module對應的build.gradle裡面,copy過來後的程式碼如下:

apply plugin: 'com.android.library'
apply plugin: 'com.novoda.bintray-release'//新增
android {
    compileSdkVersion 24
    buildToolsVersion "24.0.1"

    defaultConfig {
        minSdkVersion 15
        targetSdkVersion 24
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    //新增的那段報錯程式碼
    lintOptions {
        abortOnError false
        warning 'InvalidPackage'
    }

}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:24.1.1'
}
//新增
publish {
    userOrg = 'huangxuanheng'//bintray.com使用者名稱
    groupId = 'com.example.fragmentstack'//jcenter上的路徑
    artifactId = 'fragmentstack'//專案名稱
    publishVersion = '1.0.0'//版本號
    desc = 'Oh hi, this is a nice description for a project, right?'//描述,不重要
    website = 'https://github.com/hyman/basetools'//網站,不重要;儘量模擬github上的地址,例如我這樣的;當然你有地址最好了
}

繼續執行gradlew clean build bintrayUpload -PbintrayUser=huangxuanheng -PbintrayKey=xxxxxxxxxxxxxxxxxx -PdryRun=false
還是報那個錯誤,我非常的納悶,一步一步的檢查,認為自己已經新增進去了,可是無論我怎麼執行,總之還是報這個錯誤,我非常的鬱悶與無助,於是,我就開始了百度,經過了九九八十一難,終於找到了一篇比較類似我的文章,不記得那篇文章是哪個大神寫的了,但根據他寫的內容,關於該問題,我得到了解決。其實是我忽略了一點,我雖然添加了這段程式碼

 lintOptions {
        abortOnError false
        warning 'InvalidPackage'
    }

到要分享的module對應的build.gradle,但一個專案中還有其他的module,然而其他的module我並沒有新增,我以為其他不新增也不會有什麼影響,我錯了,問題就出在這裡。
於是,我在所有的module對應的build.gradle中,都添加了這幾行程式碼:

 lintOptions {
        abortOnError false
        warning 'InvalidPackage'
    }
然後同步專案後執行:
gradlew clean build bintrayUpload -PbintrayUser=huangxuanheng -PbintrayKey=xxxxxxxxxxxxxxxxxx  -PdryRun=false
發現上面的問題解決了,不再報那錯誤了,但還是出現了新的錯誤,錯誤內容大概如下:
Could not create package 'huangxuanheng/maven/fragmentstack': HTTP/1.1 404 Not Found [message:Repo 'maven' was not found]
這個錯誤的大概意思是,在我註冊的bintray.com賬號裡面的maven沒有找到
於是我又苦逼的找資料,我[在這](http://blog.csdn.net/small_lee/article/details/52328613)
找到了答案,原來是我註冊的bintray.com賬號裡面,沒有建有maven專案,於是就根據[文章](http://blog.csdn.net/small_lee/article/details/52328613)去建我的maven專案,建好了之後,在我的要分享的module對應build.gradle裡面,把artifactId修改為我在maven中建的專案,改完後的大致程式碼如下:
apply plugin: 'com.android.library'
apply plugin: 'com.novoda.bintray-release'//新增
android {
    compileSdkVersion 24
    buildToolsVersion "24.0.1"

    defaultConfig {
        minSdkVersion 15
        targetSdkVersion 24
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    lintOptions {
        abortOnError false
        warning 'InvalidPackage'
    }

}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:24.1.1'
}
//新增
publish {
    userOrg = 'huangxuanheng'//bintray.com使用者名稱
    groupId = 'com.example.fragmentstack'//jcenter上的路徑
    artifactId = 'ishow'//專案名稱
    publishVersion = '1.0.0'//版本號
    desc = 'Oh hi, this is a nice description for a project, right?'//描述,不重要
    website = 'https://github.com/huangxuanheng/ishowProject'//網站,不重要;儘量模擬github上的地址,例如我這樣的;當然你有地址最好了
}

改好後,繼續執行:gradlew clean build bintrayUpload -PbintrayUser=huangxuanheng -PbintrayKey=xxxxxxxxxxxxxxxxxx -PdryRun=false
驚奇的發現,竟然BUILD SUCCESS。於是我馬上去我的bintray.com賬號裡面看,果然成功釋出到了maven專案裡面
我的這些問題的解決,離不開大神們的貢獻,在此,非常感謝一直默默貢獻的大神們,謝謝你們