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

dex working.

This commit is contained in:
Cedric Beust 2015-11-03 00:03:20 -08:00
parent 8f12f19238
commit bcee0dc4ae
6 changed files with 64 additions and 25 deletions

View file

@ -7,10 +7,10 @@ import kotlin.properties.Delegates
abstract public class BasePlugin : Plugin {
override val tasks: ArrayList<PluginTask> = arrayListOf()
override var taskManager : TaskManager by Delegates.notNull()
override var taskManager: TaskManager by Delegates.notNull()
override var methodTasks = arrayListOf<Plugin.MethodTask>()
override fun accept(project: Project) = true
var plugins : Plugins by Delegates.notNull()
var plugins: Plugins by Delegates.notNull()
protected val projects = arrayListOf<ProjectDescription>()
@ -18,4 +18,13 @@ abstract public class BasePlugin : Plugin {
projects.add(ProjectDescription(project, dependsOn.toList()))
}
/**
* Plugins can expose properties to other plugins through this map.
*/
val pluginProperties = hashMapOf<String, Any>()
companion object {
val BUILD_DIR = "buildDir"
val LIBS_DIR = "libsDir"
}
}

View file

@ -42,7 +42,9 @@ abstract class JvmCompilerPlugin @Inject constructor(
var context: KobaltContext? = null
override fun apply(project: Project, context: KobaltContext) {
super.apply(project, context)
this.context = context
pluginProperties.put(BUILD_DIR, project.buildDirectory + File.separator + "classes")
}
/**
@ -113,6 +115,12 @@ abstract class JvmCompilerPlugin @Inject constructor(
lp(project, "No resources to copy for $sourceSet")
}
}
protected val compilerArgs = arrayListOf<String>()
fun addCompilerArgs(vararg args: String) {
compilerArgs.addAll(args)
}
}
class TestConfig(val project: Project) {

View file

@ -1,8 +1,10 @@
package com.beust.kobalt.plugin.android
import com.beust.kobalt.Plugins
import com.beust.kobalt.api.*
import com.beust.kobalt.api.annotation.Directive
import com.beust.kobalt.api.annotation.Task
import com.beust.kobalt.internal.JvmCompilerPlugin
import com.beust.kobalt.internal.TaskResult
import com.beust.kobalt.maven.FileDependency
import com.beust.kobalt.maven.IClasspathDependency
@ -45,6 +47,9 @@ public class AndroidPlugin @Inject constructor(val javaCompiler: JavaCompiler) :
project.compileDependencies.add(FileDependency(androidJar(project).toString()))
}
context.classpathContributors.add(this)
// TODO: Find a more flexible way of enabling this, e.g. creating a contributor for it
(Kobalt.findPlugin("java") as JvmCompilerPlugin).addCompilerArgs("-target", "1.6", "-source", "1.6")
}
val configurations = hashMapOf<String, AndroidConfig>()
@ -70,24 +75,44 @@ public class AndroidPlugin @Inject constructor(val javaCompiler: JavaCompiler) :
fun androidJar(project: Project) : Path =
Paths.get(ANDROID_HOME, "platforms", "android-${compileSdkVersion(project)}", "android.jar")
fun generated(project: Project) = Paths.get(project.directory, "app", "build", "generated")
@Task(name = "generateR", description = "Generate the R.java file",
runBefore = arrayOf("compile"), runAfter = arrayOf("clean"))
fun taskGenerateRFile(project: Project) : TaskResult {
val buildToolsDir = buildToolsVersion(project)
val generated = generated(project)
explodeAarFiles(project, generated)
generateR(project, generated, buildToolsDir)
return TaskResult()
}
@Task(name = "generateDex", description = "Generate the dex file", alwaysRunAfter = arrayOf("compile"))
fun taskGenerateDex(project: Project) : TaskResult {
val generated = generated(project)
val buildToolsDir = buildToolsVersion(project)
val dx = "$ANDROID_HOME/build-tools/$buildToolsDir/dx"
val buildDir = (Plugins.findPlugin("java") as BasePlugin).pluginProperties[BUILD_DIR]
val libsDir = (Plugins.findPlugin("packaging") as BasePlugin).pluginProperties[LIBS_DIR]
File(libsDir!!.toString()).mkdirs()
RunCommand(dx).run(listOf("--dex", "--output", KFiles.joinDir(libsDir!!.toString(), "classes.dex"),
buildDir!!.toString()))
return TaskResult()
}
private fun generateR(project: Project, generated: Path, buildToolsDir: String?) {
val flavor = "debug"
val compileSdkVersion = compileSdkVersion(project)
val buildToolsVersion = buildToolsVersion(project)
val androidJar = Paths.get(ANDROID_HOME, "platforms", "android-$compileSdkVersion", "android.jar")
val applicationId = configurations[project.name!!]?.applicationId!!
val intermediates = Paths.get(project.directory, "app", "build", "intermediates")
val manifestDir = Paths.get(project.directory, "app", "src", "main").toString()
// val manifestIntermediateDir = dirGet(intermediates, "manifests", "full", flavor)
// val manifestIntermediateDir = dirGet(intermediates, "manifests", "full", flavor)
val manifest = Paths.get(manifestDir, "AndroidManifest.xml")
val generated = Paths.get(project.directory, "app", "build", "generated")
val aapt = "$ANDROID_HOME/build-tools/$buildToolsVersion/aapt"
val aapt = "$ANDROID_HOME/build-tools/$buildToolsDir/aapt"
val outputDir = dirGet(intermediates, "resources", "resources-$flavor")
explodeAarFiles(project, generated)
val crunchedPngDir = dirGet(intermediates, "res", flavor)
RunCommand(aapt).apply {
@ -123,7 +148,6 @@ public class AndroidPlugin @Inject constructor(val javaCompiler: JavaCompiler) :
applicationId.replace(".", File.separator))
val generatedBuildDir = compile(project, rDirectory)
project.compileDependencies.add(FileDependency(generatedBuildDir.path))
return TaskResult()
}
/**

View file

@ -101,12 +101,6 @@ public class JavaPlugin @Inject constructor(
.map { File(projectDir, it).absolutePath }
}
private val compilerArgs = arrayListOf<String>()
fun addCompilerArgs(vararg args: String) {
compilerArgs.addAll(args)
}
override fun projects() = projects
}
@ -114,13 +108,13 @@ public class JavaPlugin @Inject constructor(
public fun javaProject(vararg project: Project, init: JavaProject.() -> Unit): JavaProject {
return JavaProject().apply {
init()
(Kobalt.findPlugin("kotlin") as BasePlugin).addProject(this, project)
(Kobalt.findPlugin("java") as BasePlugin).addProject(this, project)
}
}
class JavaCompilerConfig {
fun args(vararg options: String) {
(Kobalt.findPlugin("java") as JavaPlugin).addCompilerArgs(*options)
(Kobalt.findPlugin("java") as JvmCompilerPlugin).addCompilerArgs(*options)
}
}

View file

@ -43,8 +43,6 @@ class KotlinPlugin @Inject constructor(
override fun accept(project: Project) = project is KotlinProject
private val compilerArgs = arrayListOf<String>()
@Task(name = TASK_COMPILE, description = "Compile the project")
fun taskCompile(project: Project): TaskResult {
copyResources(project, JvmCompilerPlugin.SOURCE_SET_MAIN)
@ -66,10 +64,6 @@ class KotlinPlugin @Inject constructor(
return TaskResult()
}
fun addCompilerArgs(vararg args: String) {
compilerArgs.addAll(args)
}
@Task(name = TASK_COMPILE_TEST, description = "Compile the tests", runAfter = arrayOf(TASK_COMPILE))
fun taskCompileTest(project: Project): TaskResult {
copyResources(project, JvmCompilerPlugin.SOURCE_SET_TEST)
@ -113,7 +107,7 @@ fun kotlinProject(vararg project: Project, init: KotlinProject.() -> Unit): Kotl
class KotlinCompilerConfig {
fun args(vararg options: String) {
(Kobalt.findPlugin("kotlin") as KotlinPlugin).addCompilerArgs(*options)
(Kobalt.findPlugin("kotlin") as JvmCompilerPlugin).addCompilerArgs(*options)
}
}

View file

@ -5,6 +5,7 @@ import com.beust.kobalt.IFileSpec.FileSpec
import com.beust.kobalt.IFileSpec.Glob
import com.beust.kobalt.api.BasePlugin
import com.beust.kobalt.api.Kobalt
import com.beust.kobalt.api.KobaltContext
import com.beust.kobalt.api.Project
import com.beust.kobalt.api.annotation.Directive
import com.beust.kobalt.api.annotation.Task
@ -46,6 +47,13 @@ class PackagingPlugin @Inject constructor(val dependencyManager : DependencyMana
private val packages = arrayListOf<Package>()
override fun apply(project: Project, context: KobaltContext) {
super.apply(project, context)
pluginProperties.put(LIBS_DIR, libsDir(project))
}
private fun libsDir(project: Project) = KFiles.makeDir(buildDir(project).path, "libs")
@Task(name = TASK_ASSEMBLE, description = "Package the artifacts", runAfter = arrayOf(JavaPlugin.TASK_COMPILE))
fun taskAssemble(project: Project) : TaskResult {
packages.filter { it.project.name == project.name }.forEach { pkg ->
@ -112,7 +120,7 @@ class PackagingPlugin @Inject constructor(val dependencyManager : DependencyMana
//
// Add all the applicable files for the current project
//
val buildDir = KFiles.makeDir(project.directory, project.buildDirectory!!)
val buildDir = buildDir(project)
val allFiles = arrayListOf<IncludedFile>()
val classesDir = KFiles.makeDir(buildDir.path, "classes")
@ -164,6 +172,8 @@ class PackagingPlugin @Inject constructor(val dependencyManager : DependencyMana
true /* expandJarFiles */, jarFactory)
}
private fun buildDir(project: Project) = KFiles.makeDir(project.directory, project.buildDirectory!!)
private fun findIncludedFiles(directory: String, files: List<IncludedFile>, excludes: List<IFileSpec.Glob>)
: List<IncludedFile> {
val result = arrayListOf<IncludedFile>()
@ -208,8 +218,8 @@ class PackagingPlugin @Inject constructor(val dependencyManager : DependencyMana
expandJarFiles : Boolean = false,
outputStreamFactory: (OutputStream) -> ZipOutputStream = DEFAULT_STREAM_FACTORY) : File {
val buildDir = KFiles.makeDir(project.directory, project.buildDirectory!!)
val archiveDir = KFiles.makeDir(buildDir.path, "libs")
val fullArchiveName = archiveName ?: arrayListOf(project.name!!, project.version!!).joinToString("-") + suffix
val archiveDir = libsDir(project)
val result = File(archiveDir.path, fullArchiveName)
val outStream = outputStreamFactory(FileOutputStream(result))
log(2, "Creating $result")