Skip to content

Commit

Permalink
Fix compatibility with AS 2.2 gradle plugin
Browse files Browse the repository at this point in the history
    #KT-13594 fixed
  • Loading branch information
AlexeyTsvetkov committed Sep 5, 2016
1 parent 6e4bbfe commit 2afd79c
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -383,10 +383,14 @@ open class KotlinAndroidPlugin(

configureJavaTask(kotlinTask, javaTask, logger)
createSyncOutputTask(project, kotlinTask, javaTask, kotlinAfterJavaTask, variantDataName)
val artifactFile = project.tryGetSingleArtifact(variantData)
val artifactDifferenceRegistryWrapper = ArtifactDifferenceRegistryAndroidWrapper(artifactDifferenceRegistry, variantData)
configureMultiProjectIncrementalCompilation(project, kotlinTask, javaTask, kotlinAfterJavaTask,
artifactDifferenceRegistryWrapper, artifactFile)

if ((kotlinAfterJavaTask ?: kotlinTask).incremental) {
val jarToAarMapping = AndroidGradleWrapper.getJarToAarMapping(variantData)
val artifactFile = project.tryGetSingleArtifact(variantData)
val artifactDifferenceRegistryWrapper = ArtifactDifferenceRegistryAndroidWrapper(artifactDifferenceRegistry, jarToAarMapping)
configureMultiProjectIncrementalCompilation(project, kotlinTask, javaTask, kotlinAfterJavaTask,
artifactDifferenceRegistryWrapper, artifactFile)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import com.android.build.gradle.api.BaseVariant
import com.android.build.gradle.api.TestVariant
import com.android.build.gradle.internal.VariantManager
import com.android.build.gradle.internal.variant.BaseVariantData
import com.android.builder.dependency.DependencyContainer
import com.android.builder.dependency.LibraryDependency
import com.android.builder.model.SourceProvider
import org.gradle.api.file.ConfigurableFileTree
import org.gradle.api.internal.DefaultDomainObjectSet
Expand Down Expand Up @@ -153,4 +155,50 @@ class AndroidGradleWrapper {

return result.toList()
}

@NotNull
static def Map<File, File> getJarToAarMapping(BaseVariantData variantData) {
def jarToLibraryArtifactMap = new HashMap<File, File>()

def libraries = getVariantLibraryDependencies(variantData)
if (libraries == null) return jarToLibraryArtifactMap

for (lib in libraries) {
jarToLibraryArtifactMap[lib.jarFile] = lib.bundle

// local dependencies are detected as changed by gradle, because they are seem to be
// rewritten every time when bundle changes
// when local dep will actually change, record for bundle will be removed from registry
for (localDep in lib.localJars) {
if (localDep instanceof File) {
// android tools 2.2
jarToLibraryArtifactMap[localDep] = lib.bundle
}
else if (localDep.metaClass.getMetaMethod("jarFile") != null) {
// android tools < 2.2
jarToLibraryArtifactMap[localDep.jarFile] = lib.bundle
}
}
}

return jarToLibraryArtifactMap
}

@Nullable
private static def Iterable<LibraryDependency> getVariantLibraryDependencies(BaseVariantData variantData) {
def variantDependency = variantData.variantDependency
if (variantDependency instanceof DependencyContainer) {
// android tools < 2.2
return variantDependency.getAndroidDependencies()
}

def variantDependencyMeta = variantData.variantDependency.getMetaClass()
def getCompileDependencies = variantDependencyMeta.getMetaMethod("getCompileDependencies")
if (getCompileDependencies != null && getCompileDependencies.returnType.metaClass == DependencyContainer.metaClass) {
// android tools 2.2
return variantDependency.getCompileDependencies().getAndroidDependencies()
}

return null
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,36 +16,18 @@

package org.jetbrains.kotlin.gradle.tasks.incremental.android

import com.android.build.gradle.internal.variant.BaseVariantData
import org.jetbrains.kotlin.gradle.tasks.ArtifactDifference
import org.jetbrains.kotlin.gradle.tasks.ArtifactDifferenceRegistry
import java.io.File
import java.util.*

// When lib is compiled, changes are associated with .aar files.
// However when app is compiled, there is just .jar in classpath.
// This class maps jar to aar via BaseVariantData
internal class ArtifactDifferenceRegistryAndroidWrapper(
private val registry: ArtifactDifferenceRegistry,
variantData: BaseVariantData<*>
private val jarToAarMapping: Map<File, File>
): ArtifactDifferenceRegistry by registry {
private val jarToLibraryArtifactMap: MutableMap<File, File> = HashMap()

init {
for (lib in variantData.variantDependency.libraries) {
jarToLibraryArtifactMap[lib.jarFile] = lib.bundle

// local dependencies are detected as changed by gradle, because they are seem to be
// rewritten every time when bundle changes
// when local dep will actually change, record for bundle will be removed from registry
for (localDep in lib.localDependencies) {
jarToLibraryArtifactMap[localDep.jarFile] = lib.bundle
}
}
}

override fun get(artifact: File): Iterable<ArtifactDifference>? {
val mappedFile = jarToLibraryArtifactMap[artifact] ?: return null
val mappedFile = jarToAarMapping[artifact] ?: return null
return registry[mappedFile]
}
}

0 comments on commit 2afd79c

Please sign in to comment.