Created
December 7, 2016 03:32
-
-
Save muyiou/e65d176c51438708340883b2ff92bdfa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
UseAge: | |
1:create providedJar dir at subProject | |
2:use:providAAR 'xxxx' in subproject | |
Code in root build.gradle: | |
buildscript { | |
repositories { | |
mavenLocal() | |
jcenter() | |
} | |
dependencies { | |
classpath 'com.android.tools.build:gradle:2.2.0' | |
classpath 'de.undercouch:gradle-download-task:3.1.2' | |
classpath 'org.apache.ivy:ivy:2.3.0' | |
} | |
} | |
apply plugin:ProvidedAAR | |
apply plugin: 'de.undercouch.download' | |
class ProvidedAAR implements Plugin<Project>{ | |
@Override | |
void apply(Project project) { | |
project.subprojects { | |
configurations { | |
providAAR | |
} | |
} | |
project.subprojects { | |
afterEvaluate { subProject -> | |
subProject.configurations.getByName('providAAR').getDependencies().each { | |
String depString = it.group + ":" + it.name + ":" + it.version | |
println("start convert aar to jar of " + depString) | |
String mavenLocal = null | |
Set<String> repoUrlList = new HashSet<>() | |
project.getRepositories().each { | |
if (it instanceof org.gradle.api.internal.artifacts.repositories.DefaultMavenLocalArtifactRepository) { | |
mavenLocal = it.getUrl().toString().replace("file:", "") | |
} else { | |
repoUrlList.add(it.getUrl()) | |
} | |
} | |
repoUrlList.add(project.getRepositories().jcenter().getUrl()) | |
subProject.dependencies.add('provided', project.fileTree(dir: subProject.getProjectDir().toString() + '/providedJar', include: ['*.jar'])) | |
//去providedJar目录查找对应的jar | |
if (new File(subProject.getProjectDir().toString() + "/providedJar/" + depString.replace(":", "_").replace(".", "_") + ".jar").exists()) { | |
return | |
} | |
//去gradleCache中查找 | |
if (getFromGradleCache(depString, subProject.getProjectDir().toString()) != null) { | |
return | |
} | |
//从repoUrlList中下载 | |
if (getFromNet(depString, repoUrlList, subProject.getProjectDir().toString()) != null) { | |
return | |
} | |
//去mavenLocal中查找 | |
if (getFromMavenCache(depString, gradleCacheDir, subProject.getProjectDir().toString()) != null) { | |
return | |
} | |
} | |
} | |
} | |
} | |
/** | |
* | |
* @param depString | |
* @param gradleFile | |
* @param jarDest | |
* @return | |
*/ | |
def File getFromNet(String depString, Set<String> repoList, String jarDest) { | |
String aarPath = "/tmp/netAAR" | |
String[] depItem = depString.split(":") | |
String depPath = depItem[0].replace(".", "/") + "/" + depItem[1].replace(".", "/") + "/" + depItem[2] | |
repoList.each { | |
try { | |
def urlLister = new org.apache.ivy.util.url.ApacheURLLister() | |
def files = urlLister.listFiles(new URL(it.toString() + depPath)) | |
download { | |
src files | |
dest aarPath | |
} | |
for (File file : new File(aarPath).listFiles()) { | |
if (file.toString().endsWith(".aar")) { | |
String jarFile = getJarFileFromAAR(depString, file, jarDest) | |
Runtime.getRuntime().exec((String[]) ["sh", "-c", "rm -rf " + aarPath].toArray()) | |
return new File(jarFile) | |
} | |
} | |
} catch (Exception e) { | |
} | |
} | |
return null | |
} | |
/** | |
* 这里暂时写一个空的实现 | |
* @param depString | |
* @param gradleFile | |
* @param jarDest | |
* @return | |
*/ | |
def File getFromMavenCache(String depString, String gradleFile, String jarDest) { | |
return null | |
} | |
def getJarFileFromAAR(String depString, File aarFile, String jarDest) { | |
println("getJarFileFromAAR " + aarFile.toString()) | |
ZipFile zipFile = new ZipFile(aarFile); | |
try { | |
Enumeration<? extends ZipEntry> entries = zipFile.getEntries(); | |
while (entries.hasMoreElements()) { | |
ZipEntry entry = entries.nextElement(); | |
File entryDestination = new File("/tmp/tmpAAR", entry.getName()); | |
if (entry.isDirectory()) { | |
entryDestination.mkdirs(); | |
} else { | |
entryDestination.getParentFile().mkdirs(); | |
InputStream is = zipFile.getInputStream(entry); | |
OutputStream out = new FileOutputStream(entryDestination); | |
IOUtils.copy(is, out); | |
IOUtils.closeQuietly(is); | |
out.close(); | |
} | |
} | |
String jarFile = jarDest + "/providedJar/" + depString.replace(":", "_").replace(".", "_") + ".jar" | |
Runtime.getRuntime().exec((String[]) ["sh", "-c", "cp /tmp/tmpAAR/classes.jar " + jarFile].toArray()) | |
Runtime.getRuntime().exec((String[]) ["sh", "-c", "rm -rf /tmp/tmpAAR/ "].toArray()) | |
return jarFile | |
} finally { | |
zipFile.close(); | |
} | |
return null | |
} | |
def File getFromGradleCache(String depString, String jarDest) { | |
File DEFAULT_GRADLE_USER_HOME = new File(SystemProperties.getInstance().getUserHome() + "/.gradle"); | |
String gradleUserHome = System.getProperty("gradle.user.home"); | |
if (gradleUserHome == null) { | |
gradleUserHome = System.getenv("GRADLE_USER_HOME"); | |
if (gradleUserHome == null) { | |
gradleUserHome = DEFAULT_GRADLE_USER_HOME.getAbsolutePath(); | |
} | |
} | |
String gradleCacheDir = gradleUserHome + "/caches/modules-2/files-2.1/" | |
File file = new File(gradleCacheDir + depString.replace(":", "/")) | |
if (file.exists()) { | |
for (File tmpFile : file.listFiles()) { | |
for (File itemFile : tmpFile.listFiles()) { | |
if (itemFile.toString().endsWith("aar")) { | |
String depFileString = getJarFileFromAAR(depString, itemFile, jarDest) | |
return new File(depFileString) | |
} | |
} | |
} | |
} | |
return null | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment