1
0
Fork 0
mirror of https://github.com/ethauvin/kobalt.git synced 2025-04-27 08:38:13 -07:00

Android variant work.

This commit is contained in:
Cedric Beust 2015-11-22 09:27:41 -08:00
parent c004431946
commit b422fb1b83
11 changed files with 133 additions and 29 deletions

View file

@ -3,6 +3,7 @@ package com.beust.kobalt
import com.beust.kobalt.api.* import com.beust.kobalt.api.*
import com.beust.kobalt.misc.KFiles import com.beust.kobalt.misc.KFiles
import com.beust.kobalt.misc.log import com.beust.kobalt.misc.log
import com.beust.kobalt.misc.warn
import java.io.File import java.io.File
/** /**
@ -131,7 +132,8 @@ class Variant(val initialProductFlavor: ProductFlavorConfig? = null,
fun toIntermediateDir() : String { fun toIntermediateDir() : String {
if (isDefault) { if (isDefault) {
throw AssertionError("DEFAULT VARIANT NOT IMPLEMENTED") warn("DEFAULT VARIANT NOT IMPLEMENTED")
return ""
} else { } else {
return KFiles.joinDir(productFlavor.name, buildType.name) return KFiles.joinDir(productFlavor.name, buildType.name)
} }

View file

@ -0,0 +1,9 @@
package com.beust.kobalt.api
/**
* Plug-ins can alter the build directory by implementing this interface.
*/
interface IBuildDirectoryIncerceptor : IPluginActor {
fun intercept(project: Project, context: KobaltContext, buildDirectory: String) : String
}

View file

@ -0,0 +1,10 @@
package com.beust.kobalt.api
import com.beust.kobalt.internal.CompilerActionInfo
/**
* Plug-ins can alter what is passed to the compiler by implementing this interface.
*/
interface ICompilerInterceptor : IPluginActor {
fun intercept(project: Project, context: KobaltContext, actionInfo: CompilerActionInfo) : CompilerActionInfo
}

View file

@ -1,4 +1,7 @@
package com.beust.kobalt.api package com.beust.kobalt.api
interface IContributor interface IPluginActor
interface IContributor : IPluginActor

View file

@ -0,0 +1,11 @@
package com.beust.kobalt.api
import java.io.File
/**
* Plug-ins can alter the source directories by implementing this interface.
*/
interface ISourceDirectoriesIncerceptor : IPluginActor {
fun intercept(project: Project, context: KobaltContext, sourceDirectories: List<File>) : List<File>
}

View file

@ -112,6 +112,14 @@ open public class Project(
fun addBuildType(name: String, bt: BuildTypeConfig) { fun addBuildType(name: String, bt: BuildTypeConfig) {
buildTypes.put(name, bt) buildTypes.put(name, bt)
} }
fun classesDir(context: KobaltContext): String {
val initial = KFiles.joinDir(buildDirectory, "classes")
val result = context.pluginInfo.buildDirectoryInterceptors.fold(initial, { dir, intercept ->
intercept.intercept(this, context, dir)
})
return result
}
} }
public class Sources(val project: Project, val sources: HashSet<String>) { public class Sources(val project: Project, val sources: HashSet<String>) {

View file

@ -28,9 +28,6 @@ abstract class JvmCompilerPlugin @Inject constructor(
open val jvmCompiler: JvmCompiler) : BasePlugin(), IProjectContributor { open val jvmCompiler: JvmCompiler) : BasePlugin(), IProjectContributor {
companion object { companion object {
@ExportedProjectProperty(doc = "The location of the build directory", type = "String")
const val BUILD_DIR = "buildDir"
@ExportedProjectProperty(doc = "Projects this project depends on", type = "List<ProjectDescription>") @ExportedProjectProperty(doc = "Projects this project depends on", type = "List<ProjectDescription>")
const val DEPENDENT_PROJECTS = "dependentProjects" const val DEPENDENT_PROJECTS = "dependentProjects"
@ -54,7 +51,6 @@ abstract class JvmCompilerPlugin @Inject constructor(
override fun apply(project: Project, context: KobaltContext) { override fun apply(project: Project, context: KobaltContext) {
super.apply(project, context) super.apply(project, context)
project.projectProperties.put(BUILD_DIR, project.buildDirectory + File.separator + "classes")
project.projectProperties.put(DEPENDENT_PROJECTS, projects()) project.projectProperties.put(DEPENDENT_PROJECTS, projects())
addVariantTasks(project, "compile", runTask = { taskCompile(project) }) addVariantTasks(project, "compile", runTask = { taskCompile(project) })
} }
@ -176,17 +172,23 @@ abstract class JvmCompilerPlugin @Inject constructor(
project.compileDependencies) project.compileDependencies)
val projectDirectory = File(project.directory) val projectDirectory = File(project.directory)
val buildDirectory = File(project.projectProperties.getString(BUILD_DIR)) val buildDirectory = File(project.classesDir(context))
buildDirectory.mkdirs() buildDirectory.mkdirs()
val sourceDirectories = context.variant.sourceDirectories(project) val initialSourceDirectories = context.variant.sourceDirectories(project)
val sourceDirectories = context.pluginInfo.sourceDirectoriesInterceptors.fold(initialSourceDirectories,
{ sd, interceptor -> interceptor.intercept(project, context, sd) })
val sourceFiles = files.findRecursively(projectDirectory, sourceDirectories, val sourceFiles = files.findRecursively(projectDirectory, sourceDirectories,
{ it .endsWith(project.sourceSuffix) }) { it .endsWith(project.sourceSuffix) })
.map { File(projectDirectory, it).absolutePath } .map { File(projectDirectory, it).path }
val cai = CompilerActionInfo(projectDirectory.path, classpath, sourceFiles, buildDirectory, val initialActionInfo = CompilerActionInfo(projectDirectory.path, classpath, sourceFiles, buildDirectory,
emptyList()) emptyList())
return cai val result = context.pluginInfo.compilerInterceptors.fold(initialActionInfo, { ai, interceptor ->
interceptor.intercept(project, context, ai)
})
return result
} }
abstract fun doCompile(project: Project, cai: CompilerActionInfo) : TaskResult abstract fun doCompile(project: Project, cai: CompilerActionInfo) : TaskResult

View file

@ -52,6 +52,15 @@ class KobaltPluginXml {
@XmlElement(name = "compiler-flag-contributors") @JvmField @XmlElement(name = "compiler-flag-contributors") @JvmField
var compilerFlagContributors: ClassNameXml? = null var compilerFlagContributors: ClassNameXml? = null
@XmlElement(name = "compiler-interceptors") @JvmField
var compilerInterceptors: ClassNameXml? = null
@XmlElement(name = "source-directories-interceptors") @JvmField
var sourceDirectoriesInterceptors: ClassNameXml? = null
@XmlElement(name = "build-directory-interceptors") @JvmField
var buildDirectoryInterceptors: ClassNameXml? = null
} }
class ContributorXml { class ContributorXml {
@ -76,6 +85,9 @@ class PluginInfo(val xml: KobaltPluginXml, val classLoader: ClassLoader?) {
val initContributors = arrayListOf<IInitContributor>() val initContributors = arrayListOf<IInitContributor>()
val repoContributors = arrayListOf<IRepoContributor>() val repoContributors = arrayListOf<IRepoContributor>()
val compilerFlagContributors = arrayListOf<ICompilerFlagContributor>() val compilerFlagContributors = arrayListOf<ICompilerFlagContributor>()
val compilerInterceptors = arrayListOf<ICompilerInterceptor>()
val sourceDirectoriesInterceptors = arrayListOf<ISourceDirectoriesIncerceptor>()
val buildDirectoryInterceptors = arrayListOf<IBuildDirectoryIncerceptor>()
// Future contributors: // Future contributors:
// source files // source files
@ -140,6 +152,15 @@ class PluginInfo(val xml: KobaltPluginXml, val classLoader: ClassLoader?) {
xml.compilerFlagContributors?.className?.forEach { xml.compilerFlagContributors?.className?.forEach {
compilerFlagContributors.add(factory.instanceOf(forName(it)) as ICompilerFlagContributor) compilerFlagContributors.add(factory.instanceOf(forName(it)) as ICompilerFlagContributor)
} }
xml.compilerInterceptors?.className?.forEach {
compilerInterceptors.add(factory.instanceOf(forName(it)) as ICompilerInterceptor)
}
xml.sourceDirectoriesInterceptors?.className?.forEach {
sourceDirectoriesInterceptors.add(factory.instanceOf(forName(it)) as ISourceDirectoriesIncerceptor)
}
xml.buildDirectoryInterceptors?.className?.forEach {
buildDirectoryInterceptors.add(factory.instanceOf(forName(it)) as IBuildDirectoryIncerceptor)
}
} }
fun addPluginInfo(pluginInfo: PluginInfo) { fun addPluginInfo(pluginInfo: PluginInfo) {

View file

@ -4,7 +4,6 @@ import com.beust.kobalt.api.IClasspathContributor
import com.beust.kobalt.api.KobaltContext import com.beust.kobalt.api.KobaltContext
import com.beust.kobalt.api.Project import com.beust.kobalt.api.Project
import com.beust.kobalt.api.ProjectDescription import com.beust.kobalt.api.ProjectDescription
import com.beust.kobalt.internal.JvmCompilerPlugin
import com.beust.kobalt.misc.KFiles import com.beust.kobalt.misc.KFiles
import com.beust.kobalt.misc.KobaltExecutors import com.beust.kobalt.misc.KobaltExecutors
import com.beust.kobalt.misc.warn import com.beust.kobalt.misc.warn
@ -101,7 +100,7 @@ public class DependencyManager @Inject constructor(val executors: KobaltExecutor
it.project.name == project?.name it.project.name == project?.name
}.forEach { pd -> }.forEach { pd ->
pd.dependsOn.forEach { p -> pd.dependsOn.forEach { p ->
val classesDir = p.projectProperties.getString(JvmCompilerPlugin.BUILD_DIR) val classesDir = p.classesDir(context)
if (classesDir != null) { if (classesDir != null) {
result.add(FileDependency(KFiles.joinDir(p.directory, classesDir))) result.add(FileDependency(KFiles.joinDir(p.directory, classesDir)))
} else { } else {

View file

@ -1,12 +1,9 @@
package com.beust.kobalt.plugin.android package com.beust.kobalt.plugin.android
import com.beust.kobalt.OperatingSystem import com.beust.kobalt.*
import com.beust.kobalt.TaskResult
import com.beust.kobalt.Variant
import com.beust.kobalt.api.* import com.beust.kobalt.api.*
import com.beust.kobalt.api.annotation.Directive import com.beust.kobalt.api.annotation.Directive
import com.beust.kobalt.api.annotation.Task import com.beust.kobalt.api.annotation.Task
import com.beust.kobalt.homeDir
import com.beust.kobalt.internal.CompilerActionInfo import com.beust.kobalt.internal.CompilerActionInfo
import com.beust.kobalt.internal.JvmCompilerPlugin import com.beust.kobalt.internal.JvmCompilerPlugin
import com.beust.kobalt.maven.FileDependency import com.beust.kobalt.maven.FileDependency
@ -41,12 +38,13 @@ fun Project.android(init: AndroidConfig.() -> Unit) : AndroidConfig {
return pd return pd
} }
val Project.isAndroid : Boolean //val Project.isAndroid : Boolean
get() = (Kobalt.findPlugin("android") as AndroidPlugin).isAndroid(this) // get() = (Kobalt.findPlugin("android") as AndroidPlugin).isAndroid(this)
@Singleton @Singleton
public class AndroidPlugin @Inject constructor(val javaCompiler: JavaCompiler) public class AndroidPlugin @Inject constructor(val javaCompiler: JavaCompiler)
: ConfigPlugin<AndroidConfig>(), IClasspathContributor, IRepoContributor, ICompilerFlagContributor { : ConfigPlugin<AndroidConfig>(), IClasspathContributor, IRepoContributor, ICompilerFlagContributor,
ICompilerInterceptor, ISourceDirectoriesIncerceptor, IBuildDirectoryIncerceptor {
override val name = "android" override val name = "android"
fun isAndroid(project: Project) = configurationFor(project) != null fun isAndroid(project: Project) = configurationFor(project) != null
@ -305,19 +303,30 @@ public class AndroidPlugin @Inject constructor(val javaCompiler: JavaCompiler)
val buildToolsDir = buildToolsVersion(project) val buildToolsDir = buildToolsVersion(project)
val dx = "${androidHome(project)}/build-tools/$buildToolsDir/dx" + val dx = "${androidHome(project)}/build-tools/$buildToolsDir/dx" +
if (OperatingSystem.current().isWindows()) ".bat" else "" if (OperatingSystem.current().isWindows()) ".bat" else ""
val classesDexDir = KFiles.joinDir(intermediates(project), "dex", context.variant.shortArchiveName) val classesDexDir = KFiles.joinDir(intermediates(project), "dex", context.variant.toIntermediateDir())
File(classesDexDir).mkdirs() File(classesDexDir).mkdirs()
val classesDex = "classes.dex" val classesDex = "classes.dex"
val outClassesDex = KFiles.joinDir(classesDexDir, classesDex) val outClassesDex = KFiles.joinDir(classesDexDir, classesDex)
val args = listOf("--dex", "--output", outClassesDex) // java.exe -Xmx1024M -Dfile.encoding=windows-1252 -Duser.country=US -Duser.language=en -Duser.variant -cp D:\android\adt-bundle-windows-x86_64-20140321\sdk\build-tools\23.0.1\lib\dx.jar com.android.dx.command.Main --dex --verbose --num-threads=4 --output C:\Users\cbeust\android\android_hello_world\app\build\intermediates\dex\pro\debug C:\Users\cbeust\android\android_hello_world\app\build\intermediates\classes\pro\debug
val otherArgs =
project.dependencies?.let { val javaExecutable = JavaInfo.create(File(SystemProperties.javaBase)).javaExecutable!!
it.dependencies.map { RunCommand(javaExecutable.absolutePath).run(listOf(
it.jarFile.get().path "-cp", KFiles.joinDir(androidHome(project), "build-tools", buildToolsVersion(project), "lib", "dx.jar"),
}.filter { ! it.endsWith(".aar") && ! it.endsWith("android.jar") } "com.android.dx.command.Main",
} ?: emptyList() "--dex", "--verbose", "--num-threads=4",
RunCommand(dx).run(args + otherArgs) "--output", outClassesDex,
//KFiles.joinDir(intermediates(project), "dex", context.variant.toIntermediateDir()),
project.classesDir(context)
))
// val args = listOf("--dex", "--output", outClassesDex)
// val otherArgs =
// project.dependencies?.let {
// it.dependencies.map {
// it.jarFile.get().path
// }.filter { ! it.endsWith(".aar") && ! it.endsWith("android.jar") }
// } ?: emptyList()
// RunCommand(dx).run(args + otherArgs)
// //
// Add classes.dex to existing .ap_ // Add classes.dex to existing .ap_
@ -376,4 +385,25 @@ public class AndroidPlugin @Inject constructor(val javaCompiler: JavaCompiler)
emptyList() emptyList()
} }
} }
// IBuildDirectoryInterceptor
override fun intercept(project: Project, context: KobaltContext, buildDirectory: String): String {
val result = KFiles.joinDir(intermediates(project), "classes", context.variant.toIntermediateDir())
return result
}
// ISourceDirectoriesInterceptor
override fun intercept(project: Project, context: KobaltContext, sourceDirectories: List<File>): List<File> {
return sourceDirectories.map { File("app", it.path)}
}
// ICompilerInterceptor
override fun intercept(project: Project, context: KobaltContext, actionInfo: CompilerActionInfo)
: CompilerActionInfo {
val newOutputDir = KFiles.joinDir("kobaltBuild", "intermediates", "classes",
context.variant.toIntermediateDir())
return actionInfo.copy(outputDir = File(newOutputDir))
}
} }

View file

@ -31,4 +31,13 @@
<class-name>com.beust.kobalt.plugin.android.AndroidPlugin</class-name> <class-name>com.beust.kobalt.plugin.android.AndroidPlugin</class-name>
<class-name>com.beust.kobalt.plugin.apt.AptPlugin</class-name> <class-name>com.beust.kobalt.plugin.apt.AptPlugin</class-name>
</compiler-flag-contributors> </compiler-flag-contributors>
<build-directory-interceptors>
<class-name>com.beust.kobalt.plugin.android.AndroidPlugin</class-name>
</build-directory-interceptors>
<source-directories-interceptors>
<class-name>com.beust.kobalt.plugin.android.AndroidPlugin</class-name>
</source-directories-interceptors>
<compiler-interceptors>
<class-name>com.beust.kobalt.plugin.android.AndroidPlugin</class-name>
</compiler-interceptors>
</kobalt-plugin> </kobalt-plugin>