Skip to content

Commit

Permalink
Support android plugin 2.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
galenlin committed Mar 19, 2017
1 parent 7a0e2a6 commit 9df12c5
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 32 deletions.
4 changes: 2 additions & 2 deletions Android/DevSample/buildSrc/provided.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ sourceSets {
dependencies {
if (gradle.startParameter.projectDir == project.projectDir) {
// gradlew -p buildSrc xx
provided 'com.android.tools.build:gradle:2.1.2'
provided 'com.android.tools.build:gradle:2.3.0'
} else {
compile 'com.android.tools.build:gradle:2.1.2'
compile 'com.android.tools.build:gradle:2.3.0'
}
compile 'org.fusesource.jansi:jansi:1.8'
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import com.android.build.gradle.api.BaseVariant
import com.android.build.gradle.internal.pipeline.TransformTask
import com.android.build.gradle.internal.transforms.ProGuardTransform
import com.android.build.gradle.internal.tasks.PrepareLibraryTask
import com.android.build.gradle.tasks.MergeManifests
import net.wequick.gradle.util.TaskUtils
import org.gradle.api.Project

class AndroidPlugin extends BasePlugin {
Expand Down Expand Up @@ -109,15 +111,18 @@ class AndroidPlugin extends BasePlugin {
* So we need to remove all the unimplemented content providers from `Stub`.
*/
protected void removeUnimplementedProviders() {
if (pluginType == PluginType.Library ||
pluginType == PluginType.Host) return // nothing to do with `lib.*` and host
if (pluginType == PluginType.Host) return // nothing to do with host
MergeManifests manifests = project.tasks.withType(MergeManifests.class)[0]
if (manifests.hasProperty('providers')) {
return
}

project.tasks.withType(PrepareLibraryTask.class).findAll {
def name = it.explodedDir.parentFile.name
def name = TaskUtils.getAarExplodedDir(it).parentFile.name
return (rootSmall.hostStubProjects.find { it.name == name } != null)
}.each {
it.doLast { PrepareLibraryTask aar ->
File manifest = new File(aar.explodedDir, 'AndroidManifest.xml')
File manifest = new File(TaskUtils.getAarExplodedDir(aar), 'AndroidManifest.xml')
def s = ''
boolean enteredProvider = false
boolean removed = false
Expand Down Expand Up @@ -199,7 +204,7 @@ class AndroidPlugin extends BasePlugin {
small.outputFile = variant.outputs[0].outputFile
small.explodeAarDirs = project.tasks
.withType(PrepareLibraryTask.class)
.collect { it.explodedDir }
.collect { TaskUtils.getAarExplodedDir(it) }

// Hook variant tasks
variant.assemble.doLast {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import com.android.build.gradle.tasks.ProcessTestManifest
import com.android.build.gradle.tasks.MergeManifests
import com.android.build.gradle.tasks.MergeSourceSetFolders
import com.android.build.gradle.tasks.ProcessAndroidResources
import com.android.builder.dependency.LibraryDependency

import com.android.sdklib.BuildToolInfo
import groovy.io.FileType
import net.wequick.gradle.aapt.Aapt
Expand All @@ -50,6 +50,7 @@ class AppPlugin extends BundlePlugin {

protected Set<Project> mDependentLibProjects
protected Set<Project> mTransitiveDependentLibProjects
protected Set<Project> mCompiledProjects
protected Set<Map> mUserLibAars
protected Set<File> mLibraryJars
protected File mMinifyJar
Expand Down Expand Up @@ -91,11 +92,13 @@ class AppPlugin extends BundlePlugin {
Set<DefaultProjectDependency> smallLibs = []
mUserLibAars = []
mDependentLibProjects = []
mCompiledProjects = []
allLibs.each {
if (rootSmall.isLibProject(it.dependencyProject)) {
smallLibs.add(it)
mDependentLibProjects.add(it.dependencyProject)
} else {
mCompiledProjects.add(it.dependencyProject)
mUserLibAars.add(group: it.group, name: it.name, version: it.version)
}
}
Expand Down Expand Up @@ -151,6 +154,21 @@ class AppPlugin extends BundlePlugin {

mLibraryJars.addAll(libDependentJars)

// Collect stub and small jars
Set<Project> sharedProjects = []
sharedProjects.addAll(rootSmall.hostStubProjects)
if (rootSmall.smallProject != null) {
sharedProjects.add(rootSmall.smallProject)
}
sharedProjects.each {
def jarTask = it.tasks.withType(TransformTask.class).find {
it.variantName == 'release' && it.transform.name == 'syncLibJars'
}
if (jarTask != null) {
mLibraryJars.addAll(jarTask.otherFileOutputs)
}
}

return mLibraryJars
}

Expand Down Expand Up @@ -182,25 +200,37 @@ class AppPlugin extends BundlePlugin {
project.tasks.withType(MergeManifests.class).each {
if (it.variantName.startsWith('release')) return

if (it.hasProperty('providers')) {
it.providers = []
return
}

hookProcessDebugManifest(it, it.libraries)
}

// processDebugAndroidTestManifest
project.tasks.withType(ProcessTestManifest.class).each {
if (it.variantName.startsWith('release')) return

if (it.hasProperty('providers')) {
it.providers = []
return
}

hookProcessDebugManifest(it, it.libraries)
}
}

protected void collectLibManifests(def lib, Set outFiles) {
outFiles.add(lib.getManifest())

if (lib instanceof LibraryDependency) { // android gradle 2.2.0+
if (lib.hasProperty("libraryDependencies")) {
// >= 2.2.0
lib.getLibraryDependencies().each {
collectLibManifests(it, outFiles)
}
} else { // android gradle 2.2.0-
} else {
// < 2.2.0
lib.getManifestDependencies().each {
collectLibManifests(it, outFiles)
}
Expand All @@ -209,6 +239,11 @@ class AppPlugin extends BundlePlugin {

protected void hookProcessDebugManifest(Task processDebugManifest,
List libs) {
if (processDebugManifest.hasProperty('providers')) {
processDebugManifest.providers = []
return
}

processDebugManifest.doFirst {
def libManifests = new HashSet<File>()
libs.each {
Expand Down Expand Up @@ -902,10 +937,9 @@ class AppPlugin extends BundlePlugin {
def jniDirs = android.sourceSets.main.jniLibs.srcDirs
if (jniDirs == null) jniDirs = []
// Collect ABIs from AARs
small.explodeAarDirs.each { dir ->
File jniDir = new File(dir, 'jni')
if (!jniDir.exists()) return
jniDirs.add(jniDir)
mCompiledProjects.each {
com.android.build.gradle.BaseExtension libAndrioid = it.android
jniDirs += libAndrioid.sourceSets.main.jniLibs.srcDirs
}
def filters = android.defaultConfig.ndkConfig.abiFilters
jniDirs.each { dir ->
Expand Down Expand Up @@ -1071,6 +1105,9 @@ class AppPlugin extends BundlePlugin {

// 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)
}

// Collect aar(s) in host
Expand All @@ -1097,22 +1134,26 @@ class AppPlugin extends BundlePlugin {
// manifests, the `processManifest` task will raise an conflict error.
// Cause the release mode doesn't need to merge the manifest of lib.*, simply split
// out the manifest dependencies from them.
processManifest.doFirst { MergeManifests it ->
if (pluginType != PluginType.App) return
if (processManifest.hasProperty('providers')) {
processManifest.providers = []
} else {
processManifest.doFirst { MergeManifests it ->
if (pluginType != PluginType.App) return

def libs = it.libraries
def smallLibs = []
libs.each {
def components = it.name.split(':') // e.g. 'Sample:lib.style:unspecified'
if (components.size() != 3) return
def libs = it.libraries
def smallLibs = []
libs.each {
def components = it.name.split(':') // e.g. 'Sample:lib.style:unspecified'
if (components.size() != 3) return

def projectName = components[1]
if (!rootSmall.isLibProject(projectName)) return
def projectName = components[1]
if (!rootSmall.isLibProject(projectName)) return

smallLibs.add(it)
smallLibs.add(it)
}
libs.removeAll(smallLibs)
it.libraries = libs
}
libs.removeAll(smallLibs)
it.libraries = libs
}
// Hook process-manifest task to remove the `android:icon' and `android:label' attribute
// which declared in the plugin `AndroidManifest.xml' application node. (for #11)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,9 @@ class LibraryPlugin extends AppPlugin {
} else { //< apply: 'com.android.library'
// Cause `isBuildingRelease()' return false, at this time, super's
// `hookJavacTask' will not be triggered. Provided the necessary jars here.
def smallJar = project.fileTree(
dir: rootSmall.preBaseJarDir, include: [SMALL_JAR_PATTERN])
def libJars = project.fileTree(dir: rootSmall.preLibsJarDir,
include: mDependentLibProjects.collect { "$it.name-${it.version}.jar" })
project.dependencies.add('provided', smallJar)
project.dependencies.add('provided', libJars)
getLibraryJars().each {
project.dependencies.add('provided', project.files(it))
}

// Resolve the transform tasks
project.preBuild.doLast {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,22 @@ public class StripAarTransform extends Transform {
return
}

// 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) {
return
}
}

// Copy the jar and rename
File version = src.parentFile
String versionName = version.name
Expand All @@ -99,11 +115,19 @@ public class StripAarTransform extends Transform {
// **/support-v4/23.2.1/jars/libs/internal_impl-23.2.1.jar
// => support-v4-internal_impl-23.2.1.jar
moduleName = version.parentFile.parentFile.parentFile.name
} else if (version.parentFile.name == 'default') {
// Compat for android plugin 2.3.0
// Sample/lib.utils/build/intermediates/bundles/default/libs/mylib.jar
moduleName = version.parentFile.parentFile.parentFile.parentFile.parentFile.name
} else {
// [projectDir]/libs/mylib.jar
// => [projectName]-mylib.jar
moduleName = "${project.name}"
}
} else if (versionName == 'default') {
// Compat for android plugin 2.3.0
// Sample/jni_plugin/intermediates/bundles/default/classes.jar
moduleName = version.parentFile.parentFile.parentFile.parentFile.name
} else {
moduleName = "${version.parentFile.parentFile.name}-${version.parentFile.name}"
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* 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://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

import com.android.build.gradle.internal.tasks.PrepareLibraryTask

import java.lang.reflect.Field

public class TaskUtils {

public static File getAarExplodedDir(PrepareLibraryTask task) {
if (task.hasProperty('explodedDir')) {
return task.explodedDir
}

File version = task.bundle.parentFile
File name = version.parentFile
File module = name.parentFile
String dir = "$module.name/$version.name/$name.name"
println "bundle $task.bundle"
while (module.parentFile != null && module.parentFile.name != 'm2repository') {
module = module.parentFile
dir = module.name + "." + dir
}

return new File(task.project.buildDir, "intermediates/exploded-aar/$dir")
}
}

0 comments on commit 9df12c5

Please sign in to comment.