前言:
在開發中,經常會使用第三方庫,一般是通過maven或gradle的方式引用。很方便,整個引用過程也就幾行配置代碼(gradle只用一行)。
下面就是記錄將將自己的的庫開源出去的整個流程:
參照https://inthecheesefactory.com/blog/how-to-upload-library-to-jcenter-maven-central-as-dependency/en 【翻譯在此】
這里已經很詳細了,接下來是實際操作過程中應注意的地方:
1、在Mac終端中,gradle的相關命令應 ./gradlew xxx
這樣寫,以下都默認在Mac中
2、在運行./gradew install
時,會報
Caused by: java.lang.NoClassDefFoundError: org/gradle/api/publication/maven/internal/DefaultMavenFactory
當使用的Gradle版本是2.4以上,Android插件版本是1.3.0以上的時候就會出現這個問題,這時候我們需在project的 build.gradle
文件中的插件版本添加一行
classpath ‘com.github.dcendents:android-maven-gradle-plugin:1.3’
3、在運行./gradlew bintrayUpload
時,報
What went wrong:
Execution failed for task ':library:bintrayUpload'.
Could not create package 'sujian27@163.com/maven/simplify': HTTP/1.1 401 Unauthorized [message:This resource requires authentication]
這是local.properites
中的bintray.user
配置錯誤導致的。在注冊bintray時,直接用github的賬號注冊的,這時候bintray.user應為主頁顯示的那個名稱,而不是郵箱。
4、在運行./gradlew bintrayUpload
時,報
What went wrong:
Execution failed for task ':library:bintrayUpload'.
Could not upload to 'https://api.bintray.com/content/timesfire/maven/simplify/0.1.0/com/github/timesfire/library/0.1.0/library-0.1.0.pom': HTTP/1.1 400 Bad Request [message:Unable to upload files: Maven group, artifact or version defined in the pom file do not match the file path 'com/github/timesfire/library/0.1.0/library-0.1.0.pom']
這個時候我們的庫已經上傳到bintray上了,在對應的maven目錄下也有對應的tag生成,但是不能正常引用,這是因為需要上傳的庫工程對應的module名稱和我們配置的不一致,默認的庫module名稱是library,只需重命名為我們配置的名稱即可,我這里是重命名為simplify。讓后先將bintray上已經生成的TAG刪除,重新運行上傳命令即可。