*本篇文章已授權微信公眾號 guolin_blog (郭霖)獨家發布
*本篇文章視頻 慕課網之Android依賴管理與私服搭建
1.Android引入依賴的多種方式
2.多平臺私服搭建
3.創建和依賴自己的代碼倉庫
1、Android引入依賴的多種方式
引入方式一
AS切換project視圖>新建libs目錄(高版本AS默認存在libs目錄)>copy jar 包>add as library
下面以我開源的支付寶以及微信app支付項目為例引入相關的jar
引入方式二(module>aar)
import module>Project Structure>選擇對應的Module>module dependent
我們知道jar包是不能引入資源文件的,如果已module模式引入資源文件同樣可以使用。
Lib Module 生成aar
New Module >Android Library>實現包功能并生成aar文件
項目中引用aar文件
以上兩種引入jar依賴弊端就是jar包升級之后需要Copy替換換新的jar并修改對應的配置文件。
引入方式三(推薦)
從倉庫中下載引用
找到jar對應的build.gradle>compile(jcenter、mavencenter、mavenlocal)>通過Nexue搭建私服
https://bintray.com/bintray/jcenter
http://search.maven.org
http://www.sonatype.org
部分網站訪問需要科學上網,你懂的哈
Android Studio最新版默認使用的是jcenter,好處就是jar包升級自需要修改版本號,構建項目會自動從倉庫中下載(只會下載一次,下載以后會從本地引入)。
如何引入倉庫中的jar
下面以引入retrofit為例
或者直接在對應項目Module的build.gradle中dependencies節點添加
compile 'com.squareup.retrofit2:retrofit:2.1.0'
相互依賴的包也會下載下來,retrofit依賴okhttp依賴ikoi
2、多平臺Nexus私服搭建
http://books.sonatype.com/nexus-book/3.0/reference/index.html
Note: The GUI installer distribution development is suspended and is no longer an install option until further notice.
Nexus Repository Manager 3.1.0-04 開始并沒有提供GUI的安裝程序。建議JDK使用1.8版本。
安裝介紹資料
http://books.sonatype.com/nexus-book/3.0/reference/install.html#installation-archive
解壓下載的nexus
$ tar xvzf nexus-3.0.2-02-mac.tgz
$ tar xvzf nexus-3.0.2-02-unix.tar.gz
The bin folder contains the generic startup scripts for Unix-like platforms called nexus. The Windows platform equivalent is called nexus.exe. To start the repository manager from the bin folder on a Unix-like platform like Linux use
./nexus run
The equivalent invocation on Windows requires a / in front of the run and any other commands.
nexus.exe /run
簡單解釋就是Mac、Linux平臺使用./nexus run
Window平臺使用 nexus.exe /run
運行Nexus Repository Manager
Mac
Linux
進
bin
目錄,執行./nexus run
Nexus Repository Manager依賴的是Java開發環境,最新版JDK得1.8或者以上
在nexus的bin目錄修改nexus,設置
INSTALL4J_JAVA_HOME_OVERRIDE
關閉防火墻 默認端口8081
systemctl stop firewalld
參考質料:
http://books.sonatype.com/nexus-book/3.0/reference/install.html#installation-java
2.1、創建自己的倉庫
1、創建用戶
首先使用管理員密碼登陸到Nexus私服并添加用戶
使用新創建的用戶登錄Nexus并創建倉庫
2.2、上傳自己的Module到倉庫
第一 在項目級別的build.gradle中的allprojects下repositories節點添加
mavenLocal()
第二 在Lib Module級別的build.gradle中添加maven插件
apply plugin: 'maven'
第三 在Lib Module級別的build.gradle中android節點添加上傳行為
解析:
compile 'com.android.support:appcompat-v7:24.2.1'
pom.version = "24.2.1"
pom.artifactId = "appcompat-v7"
pom.groupId ="com.android.support"
//定義上傳行為
uploadArchives {
//倉庫基于maven
repositories.mavenDeployer {
//配置上傳的url
repository(url: "http://192.168.1.100:8081/repository/com.javen205/") {
authentication(userName: "javen", password: "javen205")
}
pom.version = "0.0.1"
pom.artifactId = "JPay"
pom.groupId = "com.javen205"
}
}
第四 使用Gradle插件上傳aar到Maven私服
點擊uploadArchives自動上傳
2.2、引用私服中的Module
在需要依賴Module的build.gradle中添加如下節點,其中URL就是上文中創建倉庫的url
allprojects {
repositories {
maven {
url "http://192.168.1.100:8081/repository/com.javen205/"
}
}
}
compile 'com.javen205:JPay:0.0.1'
查看 ** External Libraries**
推薦閱讀
Android Studio 上傳aar(Library)到JCenter
Android版-支付寶APP支付
Android版-微信APP支付
支付寶Wap支付你了解多少?
一張二維碼集成微信、支付寶支付
安利時間:
JPay是對微信App支付、支付寶App支付的二次封裝,對外提供一個相對簡單的接口以及支付結果的回調
極速開發微信公眾號是對微信公眾平臺接口的二次封裝。包括開發者模式、事件回調監聽、微信模板消息、微信客服消息、自定義菜單、微信支付、素材管理等
如遇到問題歡迎留言交流
記錄學習的點滴,以此勉勵不斷奮斗的自己?????? 如果對你有幫助記得點喜歡告訴我