第一步:建立github項目
将項目Clone到本地,記錄本地路徑如:/Users/xxxx/Documents/Workspace/Java/xxxxx
第二部:将如下代碼複制到libaray項目中
在Library項目的build.gradle中增加如下代碼:
apply from: 'maven_build.gradle'
//對maven_build.gradle進行依賴
檔案名:maven_build.gradle
内容如下:
apply plugin: 'maven'
ext {
// 這個路徑是從Github上Clone的本地路徑
GITHUB_REPO_PATH = "/Users/xxxx/Documents/Workspace/Java/xxxxx"
// 在用到的代碼上依賴的代碼compile 'com.andrjhf.storage:encrypt:1.0.0'
PUBLISH_GROUP_ID = 'com.andrjhf.storage'
PUBLISH_ARTIFACT_ID = 'encrypt'
PUBLISH_VERSION = '1.0.0'
}
uploadArchives {
repositories.mavenDeployer {
def deployPath = file(project.GITHUB_REPO_PATH)
repository(url: "file://${deployPath.absolutePath}")
pom.project {
groupId project.PUBLISH_GROUP_ID
artifactId project.PUBLISH_ARTIFACT_ID
version project.PUBLISH_VERSION
}
}
}
// 源代碼一起打包(不需要打包源代碼的不要添加這幾行)
task androidSourcesJar(type: Jar) {
classifier = 'sources'
from android.sourceSets.main.java.sourceFiles
}
artifacts {
archives androidSourcesJar
}
第三步:編譯這個libaray
點選項目右側Gradle,找到對應的項目,找到upload下的uploadArchives輕按兩下運作
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiIhdjZ1YjM3MGZ2QGOvwFcvwVbvNmL1h2cuFWaq5yd3d3Lc9CX6MHc0RHaiojIsJye.jpg)
1533106214944.jpg
在對應的GITHUB_REPO_PATH = "/Users/xxxx/Documents/Workspace/Java/xxxxx"目錄下會産生檔案,上傳到剛才建立好的github上
第四步:在使用的項目中依賴這個Library
1. 在整個項目的build.gradle中增加如下代碼:
allprojects {
repositories {
maven { url "https://raw.githubusercontent.com/UserName/ProjectName/master" }
}
}
https://raw.githubusercontent.com/UserName/ProjectName/master注意這個連結一定是這個形式的,隻需要修改UserName和ProjectName就可以了
2. 在需要使用的地方做如下依賴就可以了
compile 'com.andrjhf.storage:encrypt:1.0.0'