Skip to content

Commit

Permalink
Extract AarPath class to check if a path is exploded from a specified…
Browse files Browse the repository at this point in the history
… aar
  • Loading branch information
galenlin committed Mar 19, 2017
1 parent 1f0daeb commit c1895b3
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,19 @@ class AndroidPlugin extends BasePlugin {
return
}

project.tasks.withType(PrepareLibraryTask.class).findAll {
def name = TaskUtils.getAarExplodedDir(it).parentFile.name
return (rootSmall.hostStubProjects.find { it.name == name } != null)
}.each {
project.tasks.withType(PrepareLibraryTask.class).each {
it.doLast { PrepareLibraryTask aar ->
File manifest = new File(TaskUtils.getAarExplodedDir(aar), 'AndroidManifest.xml')
File aarDir = TaskUtils.getAarExplodedDir(aar)
if (aarDir == null) {
return
}

def aarName = aarDir.parentFile.name
if (rootSmall.hostStubProjects.find { it.name == aarName } != null) {
return
}

File manifest = new File(aarDir, 'AndroidManifest.xml')
def s = ''
boolean enteredProvider = false
boolean removed = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import groovy.io.FileType
import net.wequick.gradle.aapt.Aapt
import net.wequick.gradle.aapt.SymbolParser
import net.wequick.gradle.transform.StripAarTransform
import net.wequick.gradle.util.AarPath
import net.wequick.gradle.util.ClassFileUtils
import net.wequick.gradle.util.JNIUtils
import net.wequick.gradle.util.Log
Expand Down Expand Up @@ -99,7 +100,7 @@ class AppPlugin extends BundlePlugin {
mDependentLibProjects.add(it.dependencyProject)
} else {
mCompiledProjects.add(it.dependencyProject)
mUserLibAars.add(group: it.group, name: it.name, version: it.version)
collectAarsOfLibrary(it.dependencyProject, mUserLibAars)
}
}

Expand Down Expand Up @@ -154,7 +155,7 @@ class AppPlugin extends BundlePlugin {

mLibraryJars.addAll(libDependentJars)

// Collect stub and small jars
// Collect stub and small jars in host
Set<Project> sharedProjects = []
sharedProjects.addAll(rootSmall.hostStubProjects)
if (rootSmall.smallProject != null) {
Expand All @@ -169,6 +170,13 @@ class AppPlugin extends BundlePlugin {
}
}

rootSmall.hostProject.tasks.withType(TransformTask.class).each {
if ((it.variantName == 'release' || it.variantName.contains("Release"))
&& it.transform.name == 'dex') {
mLibraryJars.addAll(it.streamInputs.findAll { it.name.endsWith('.jar') })
}
}

return mLibraryJars
}

Expand Down Expand Up @@ -984,16 +992,12 @@ class AppPlugin extends BundlePlugin {
def hookMergeJniLibs(TransformTask t) {
stripAarFiles(t, { splitPaths ->
t.streamInputs.each {
def version = it.parentFile
def name = version.parentFile
def group = name.parentFile
def root = group.parentFile
if (root.name != 'exploded-aar') return

def aar = [group: group.name, name: name.name, version: version.name]
if (mUserLibAars.contains(aar)) {
// keep the user libraries
return
AarPath aarPath = new AarPath(it)
for (aar in mUserLibAars) {
if (aarPath.explodedFromAar(aar)) {
// keep the user libraries
return
}
}

splitPaths.add(it)
Expand Down Expand Up @@ -1101,23 +1105,25 @@ class AppPlugin extends BundlePlugin {
// Collect aar(s) in lib.*
mTransitiveDependentLibProjects.each { lib ->
// lib.* dependencies
collectAarsOfProject(lib, smallLibAars)

// lib.* self
smallLibAars.add(group: lib.group, name: lib.name, version: lib.version)
// lib.* self for android plugin 2.3.0+
File dir = lib.projectDir
smallLibAars.add(group: dir.parentFile.name, name: dir.name, version: lib.version)
collectAarsOfProject(lib, true, smallLibAars)
}

// Collect aar(s) in host
collectAarsOfProject(rootSmall.hostProject, smallLibAars)
collectAarsOfProject(rootSmall.hostProject, false, smallLibAars)

small.splitAars = smallLibAars
small.retainedAars = mUserLibAars
}

protected def collectAarsOfProject(Project project, HashSet outAars) {
protected static def collectAarsOfLibrary(Project lib, HashSet outAars) {
// lib.* self
outAars.add(group: lib.group, name: lib.name, version: lib.version)
// lib.* self for android plugin 2.3.0+
File dir = lib.projectDir
outAars.add(group: dir.parentFile.name, name: dir.name, version: lib.version)
}

protected def collectAarsOfProject(Project project, boolean isLib, HashSet outAars) {
String dependenciesFileName = "$project.name-D.txt"

// Pure aars
Expand All @@ -1127,6 +1133,10 @@ class AppPlugin extends BundlePlugin {
// Jar-only aars
file = new File(rootSmall.preLinkJarDir, dependenciesFileName)
collectAars(file, project, outAars)

if (isLib) {
collectAarsOfLibrary(project, outAars)
}
}

private def hookProcessManifest(Task processManifest) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,8 @@ class RootPlugin extends BasePlugin {
// - copy dependencies jars
ext.explodeAarDirs.each {
// explodedDir: **/exploded-aar/$group/$artifact/$version
if (it == null) return

File version = it
File jarDir = new File(version, 'jars')
File jarFile = new File(jarDir, 'classes.jar')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import com.android.build.api.transform.TransformInput
import com.android.build.api.transform.TransformOutputProvider
import com.android.build.gradle.internal.pipeline.TransformManager
import net.wequick.gradle.AppExtension
import net.wequick.gradle.util.AarPath
import org.apache.commons.io.FileUtils
import org.gradle.api.Project
import org.gradle.api.Task
Expand Down Expand Up @@ -84,17 +85,9 @@ public class StripAarTransform extends Transform {
}

// Strip from build-cache for android plugin 2.3.0+
if (src.absolutePath.contains('build-cache')) {
File input = new File(src.parentFile.parentFile.parentFile, 'inputs')
def prop = new Properties()
prop.load(input.newDataInputStream())
def path = prop.getProperty("FILE_PATH")
temp = small.splitAars.find { Map<String, String> aar ->
def group = aar.group.replaceAll('\\.', File.separator)
def aarPath = "$group$File.separator$aar.name"
path.contains(aarPath)
}
if (temp != null) {
AarPath aarPath = new AarPath(src)
for (aar in small.splitAars) {
if (aarPath.explodedFromAar(aar)) {
return
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright 2015-present wequick.net
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License. You may obtain
* a copy of the License at
*
* http:https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
package net.wequick.gradle.util

public class AarPath {

private static final String CACHE_DIR = "build-cache"
private static final String CACHE_INPUTS_FILE = "inputs"
private static final String CACHE_FILE_PATH_KEY = "FILE_PATH"

private String mSrc
private boolean isCache

public AarPath(File path) {
mSrc = path.absolutePath
if (mSrc.contains(CACHE_DIR)) {
this.initWithCachePath(path)
}
}

private void initWithCachePath(File path) {
while (path.parentFile.name != CACHE_DIR) {
path = path.parentFile
}

File input = new File(path, CACHE_INPUTS_FILE)
if (!input.exists()) {
return
}

def prop = new Properties()
prop.load(input.newDataInputStream())
def src = prop.getProperty(CACHE_FILE_PATH_KEY)
if (src == null) {
return
}

mSrc = src
isCache = true
}

public boolean explodedFromAar(Map aar) {
// ~/.gradle/caches/modules-2/files-2.1/net.wequick.small/small/1.1.0/hash/*.aar
// ^^^^^^^^^^^^^^^^^ ^^^^^
def moduleAarDir = "$aar.group$File.separator$aar.name"
if (mSrc.contains(moduleAarDir)) {
return true
}

// [sdk]/extras/android/m2repository/com/android/support/support-core-ui/25.1.0/*.aar
// ^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^
def repoGroup = aar.group.replaceAll('\\.', File.separator)
def repoAarPath = "$repoGroup$File.separator$aar.name"
return mSrc.contains(repoAarPath)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public class TaskUtils {
return task.explodedDir
}

return task.outputs.getFiles().files[0]
def files = task.outputs.files.files
if (files != null) return files[0]
return null
}
}

0 comments on commit c1895b3

Please sign in to comment.