mirror of
https://github.com/ethauvin/kobalt.git
synced 2025-04-26 08:27:12 -07:00
Android variant work.
This commit is contained in:
parent
861bac9f12
commit
f574274c9e
10 changed files with 137 additions and 98 deletions
|
@ -8,20 +8,20 @@ import java.io.File
|
||||||
/**
|
/**
|
||||||
* Capture the product flavor and the build type of a build.
|
* Capture the product flavor and the build type of a build.
|
||||||
*/
|
*/
|
||||||
class Variant(val productFlavor: ProductFlavorConfig? = null, val buildType: BuildTypeConfig? = null) {
|
class Variant(val initialProductFlavor: ProductFlavorConfig? = null,
|
||||||
val isDefault : Boolean
|
val initialBuildType: BuildTypeConfig? = null) {
|
||||||
get() = productFlavor == null && buildType == null
|
|
||||||
|
|
||||||
fun toTask(taskName: String) = taskName +
|
val productFlavor: ProductFlavorConfig by lazy {
|
||||||
(productFlavor?.name?.capitalize() ?: "") +
|
initialProductFlavor ?: Variant.DEFAULT_PRODUCT_FLAVOR
|
||||||
(buildType?.name?.capitalize() ?: "")
|
|
||||||
|
|
||||||
fun variantSourceDirectories(context: KobaltContext) : List<File> {
|
|
||||||
val result =
|
|
||||||
if (isDefault) listOf("src/main")
|
|
||||||
else (listOf(buildType?.name) + listOf(productFlavor?.name)).filterNotNull()
|
|
||||||
return result.map { File(it) }
|
|
||||||
}
|
}
|
||||||
|
val buildType: BuildTypeConfig by lazy {
|
||||||
|
initialBuildType ?: Variant.DEFAULT_BUILD_TYPE
|
||||||
|
}
|
||||||
|
|
||||||
|
val isDefault : Boolean
|
||||||
|
get() = productFlavor == DEFAULT_PRODUCT_FLAVOR && buildType == DEFAULT_BUILD_TYPE
|
||||||
|
|
||||||
|
fun toTask(taskName: String) = taskName + productFlavor.name.capitalize() + buildType.name.capitalize()
|
||||||
|
|
||||||
fun sourceDirectories(project: Project) : List<File> {
|
fun sourceDirectories(project: Project) : List<File> {
|
||||||
val sourceDirectories = project.sourceDirectories.map { File(it) }
|
val sourceDirectories = project.sourceDirectories.map { File(it) }
|
||||||
|
@ -29,12 +29,12 @@ class Variant(val productFlavor: ProductFlavorConfig? = null, val buildType: Bui
|
||||||
else {
|
else {
|
||||||
val result = arrayListOf<File>()
|
val result = arrayListOf<File>()
|
||||||
// The ordering of files is: 1) build type 2) product flavor 3) default
|
// The ordering of files is: 1) build type 2) product flavor 3) default
|
||||||
buildType?.let {
|
buildType.let {
|
||||||
val dir = File(KFiles.joinDir("src", it.name, project.projectInfo.sourceDirectory))
|
val dir = File(KFiles.joinDir("src", it.name, project.projectInfo.sourceDirectory))
|
||||||
log(2, "Adding source for build type ${it.name}: ${dir.path}")
|
log(2, "Adding source for build type ${it.name}: ${dir.path}")
|
||||||
result.add(dir)
|
result.add(dir)
|
||||||
}
|
}
|
||||||
productFlavor?.let {
|
productFlavor.let {
|
||||||
val dir = File(KFiles.joinDir("src", it.name, project.projectInfo.sourceDirectory))
|
val dir = File(KFiles.joinDir("src", it.name, project.projectInfo.sourceDirectory))
|
||||||
log(2, "Adding source for product flavor ${it.name}: ${dir.path}")
|
log(2, "Adding source for product flavor ${it.name}: ${dir.path}")
|
||||||
result.add(dir)
|
result.add(dir)
|
||||||
|
@ -52,23 +52,19 @@ class Variant(val productFlavor: ProductFlavorConfig? = null, val buildType: Bui
|
||||||
}
|
}
|
||||||
|
|
||||||
fun archiveName(project: Project, archiveName: String?, suffix: String) : String {
|
fun archiveName(project: Project, archiveName: String?, suffix: String) : String {
|
||||||
|
val base = if (archiveName != null) archiveName.substring(0, archiveName.length - suffix.length)
|
||||||
|
else project.name + "-" + project.version
|
||||||
val result: String =
|
val result: String =
|
||||||
if (isDefault) archiveName ?: project.name + "-" + project.version + suffix
|
base + "-${productFlavor.name}" + "-${buildType.name}"
|
||||||
else {
|
|
||||||
val base = if (archiveName != null) archiveName.substring(0, archiveName.length - suffix.length)
|
|
||||||
else project.name + "-" + project.version
|
|
||||||
base +
|
|
||||||
if (productFlavor == null) "" else "-${productFlavor.name}" +
|
|
||||||
if (buildType == null) "" else "-${buildType.name}" +
|
|
||||||
suffix
|
|
||||||
|
|
||||||
}
|
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
val shortArchiveName = productFlavor.name + "-" + buildType.name
|
||||||
|
|
||||||
val hasBuildConfig: Boolean
|
val hasBuildConfig: Boolean
|
||||||
get() {
|
get() {
|
||||||
return productFlavor?.buildConfig != null || buildType?.buildConfig != null
|
return productFlavor.buildConfig != null || buildType.buildConfig != null
|
||||||
}
|
}
|
||||||
|
|
||||||
var generatedSourceDirectory: File? = null
|
var generatedSourceDirectory: File? = null
|
||||||
|
@ -77,14 +73,14 @@ class Variant(val productFlavor: ProductFlavorConfig? = null, val buildType: Bui
|
||||||
* If either the Project or the current variant has a build config defined, generate BuildConfig.java
|
* If either the Project or the current variant has a build config defined, generate BuildConfig.java
|
||||||
*/
|
*/
|
||||||
fun maybeGenerateBuildConfig(project: Project, context: KobaltContext) {
|
fun maybeGenerateBuildConfig(project: Project, context: KobaltContext) {
|
||||||
fun generated(project: Project) = KFiles.joinDir(project.buildDirectory!!, "generated", "source")
|
fun generated(project: Project) = KFiles.joinDir(project.buildDirectory, "generated", "source")
|
||||||
|
|
||||||
if (project.buildConfig != null || context.variant.hasBuildConfig) {
|
if (project.buildConfig != null || context.variant.hasBuildConfig) {
|
||||||
val buildConfigs = arrayListOf<BuildConfig>()
|
val buildConfigs = arrayListOf<BuildConfig>()
|
||||||
if (project.buildConfig != null) buildConfigs.add(project.buildConfig!!)
|
if (project.buildConfig != null) buildConfigs.add(project.buildConfig!!)
|
||||||
with (context.variant) {
|
with (context.variant) {
|
||||||
if (buildType?.buildConfig != null) buildConfigs.add(buildType?.buildConfig!!)
|
if (buildType.buildConfig != null) buildConfigs.add(buildType.buildConfig!!)
|
||||||
if (productFlavor?.buildConfig != null) buildConfigs.add(productFlavor?.buildConfig!!)
|
if (productFlavor.buildConfig != null) buildConfigs.add(productFlavor.buildConfig!!)
|
||||||
}
|
}
|
||||||
var pkg = project.packageName ?: project.group
|
var pkg = project.packageName ?: project.group
|
||||||
?: throw KobaltException(
|
?: throw KobaltException(
|
||||||
|
@ -100,6 +96,9 @@ class Variant(val productFlavor: ProductFlavorConfig? = null, val buildType: Bui
|
||||||
override fun toString() = toTask("")
|
override fun toString() = toTask("")
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
|
val DEFAULT_PRODUCT_FLAVOR = ProductFlavorConfig("")
|
||||||
|
val DEFAULT_BUILD_TYPE = BuildTypeConfig("debug")
|
||||||
|
|
||||||
fun allVariants(project: Project): List<Variant> {
|
fun allVariants(project: Project): List<Variant> {
|
||||||
val result = arrayListOf<Variant>()
|
val result = arrayListOf<Variant>()
|
||||||
|
|
||||||
|
@ -129,4 +128,12 @@ class Variant(val productFlavor: ProductFlavorConfig? = null, val buildType: Bui
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun toIntermediateDir() : String {
|
||||||
|
if (isDefault) {
|
||||||
|
throw AssertionError("DEFAULT VARIANT NOT IMPLEMENTED")
|
||||||
|
} else {
|
||||||
|
return KFiles.joinDir(productFlavor.name, buildType.name)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -30,11 +30,14 @@ abstract public class BasePlugin : IPlugin {
|
||||||
* Register dynamic tasks corresponding to the variants found in the project,e.g. assembleDevDebug,
|
* Register dynamic tasks corresponding to the variants found in the project,e.g. assembleDevDebug,
|
||||||
* assembleDevRelease, etc...
|
* assembleDevRelease, etc...
|
||||||
*/
|
*/
|
||||||
protected fun addVariantTasks(project: Project, taskName: String, runAfter : List<String>,
|
protected fun addVariantTasks(project: Project, taskName: String,
|
||||||
|
runBefore : List<String> = emptyList(),
|
||||||
|
runAfter : List<String> = emptyList(),
|
||||||
runTask: (Project) -> TaskResult) {
|
runTask: (Project) -> TaskResult) {
|
||||||
Variant.allVariants(project).forEach { variant ->
|
Variant.allVariants(project).forEach { variant ->
|
||||||
val taskName = variant.toTask(taskName)
|
val taskName = variant.toTask(taskName)
|
||||||
addTask(project, taskName, taskName,
|
addTask(project, taskName, taskName,
|
||||||
|
runBefore = runBefore.map { variant.toTask(it) },
|
||||||
runAfter = runAfter.map { variant.toTask(it) },
|
runAfter = runAfter.map { variant.toTask(it) },
|
||||||
task = { p: Project ->
|
task = { p: Project ->
|
||||||
context.variant = variant
|
context.variant = variant
|
||||||
|
|
|
@ -11,7 +11,7 @@ open public class Project(
|
||||||
@Directive open var name: String,
|
@Directive open var name: String,
|
||||||
@Directive open var version: String? = null,
|
@Directive open var version: String? = null,
|
||||||
@Directive open var directory: String = ".",
|
@Directive open var directory: String = ".",
|
||||||
@Directive open var buildDirectory: String? = KFiles.KOBALT_BUILD_DIR,
|
@Directive open var buildDirectory: String = KFiles.KOBALT_BUILD_DIR,
|
||||||
@Directive open var group: String? = null,
|
@Directive open var group: String? = null,
|
||||||
@Directive open var artifactId: String? = null,
|
@Directive open var artifactId: String? = null,
|
||||||
@Directive open var packaging: String? = null,
|
@Directive open var packaging: String? = null,
|
||||||
|
@ -104,7 +104,10 @@ open public class Project(
|
||||||
productFlavors.put(name, pf)
|
productFlavors.put(name, pf)
|
||||||
}
|
}
|
||||||
|
|
||||||
val buildTypes = hashMapOf<String, BuildTypeConfig>()
|
val buildTypes = hashMapOf(
|
||||||
|
"debug" to BuildTypeConfig("debug"),
|
||||||
|
"release" to BuildTypeConfig("release")
|
||||||
|
)
|
||||||
|
|
||||||
fun addBuildType(name: String, bt: BuildTypeConfig) {
|
fun addBuildType(name: String, bt: BuildTypeConfig) {
|
||||||
buildTypes.put(name, bt)
|
buildTypes.put(name, bt)
|
||||||
|
|
|
@ -56,7 +56,7 @@ abstract class JvmCompilerPlugin @Inject constructor(
|
||||||
super.apply(project, context)
|
super.apply(project, context)
|
||||||
project.projectProperties.put(BUILD_DIR, project.buildDirectory + File.separator + "classes")
|
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", emptyList(), { taskCompile(project) })
|
addVariantTasks(project, "compile", runTask = { taskCompile(project) })
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -26,8 +26,8 @@ open class RunCommand(val command: String) {
|
||||||
|
|
||||||
val pb = ProcessBuilder(allArgs)
|
val pb = ProcessBuilder(allArgs)
|
||||||
pb.directory(directory)
|
pb.directory(directory)
|
||||||
log(2, "Running command: " + allArgs.joinToString(" ") +
|
log(2, "Running command in directory ${directory.absolutePath}" +
|
||||||
"\n Current directory: ${directory.absolutePath}")
|
"\n " + allArgs.joinToString(" ").replace("\\", "/"))
|
||||||
val process = pb.start()
|
val process = pb.start()
|
||||||
pb.environment().let { pbEnv ->
|
pb.environment().let { pbEnv ->
|
||||||
env.forEach {
|
env.forEach {
|
||||||
|
|
|
@ -25,6 +25,7 @@ import java.io.File
|
||||||
import java.net.URI
|
import java.net.URI
|
||||||
import java.nio.file.Path
|
import java.nio.file.Path
|
||||||
import java.nio.file.Paths
|
import java.nio.file.Paths
|
||||||
|
import java.nio.file.StandardCopyOption
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
|
||||||
class AndroidConfig(var compileSdkVersion : String = "23",
|
class AndroidConfig(var compileSdkVersion : String = "23",
|
||||||
|
@ -52,20 +53,22 @@ public class AndroidPlugin @Inject constructor(val javaCompiler: JavaCompiler)
|
||||||
|
|
||||||
override fun apply(project: Project, context: KobaltContext) {
|
override fun apply(project: Project, context: KobaltContext) {
|
||||||
super.apply(project, context)
|
super.apply(project, context)
|
||||||
|
log(1, "Applying plug-in Android on project $project")
|
||||||
if (accept(project)) {
|
if (accept(project)) {
|
||||||
project.compileDependencies.add(FileDependency(androidJar(project).toString()))
|
project.compileDependencies.add(FileDependency(androidJar(project).toString()))
|
||||||
|
|
||||||
|
addVariantTasks(project, "generateR", runBefore = listOf("compile"),
|
||||||
|
runTask = { taskGenerateRFile(project) })
|
||||||
}
|
}
|
||||||
context.pluginInfo.classpathContributors.add(this)
|
context.pluginInfo.classpathContributors.add(this)
|
||||||
|
|
||||||
// TODO: Find a more flexible way of enabling this, e.g. creating a contributor for it
|
// 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")
|
// (Kobalt.findPlugin("java") as JvmCompilerPlugin).addCompilerArgs(project, "-target", "1.6", "-source", "1.6")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
override fun accept(project: Project) = isAndroid(project)
|
override fun accept(project: Project) = isAndroid(project)
|
||||||
|
|
||||||
val flavor = "debug"
|
|
||||||
|
|
||||||
fun compileSdkVersion(project: Project) = configurationFor(project)?.compileSdkVersion
|
fun compileSdkVersion(project: Project) = configurationFor(project)?.compileSdkVersion
|
||||||
|
|
||||||
fun buildToolsVersion(project: Project): String {
|
fun buildToolsVersion(project: Project): String {
|
||||||
|
@ -93,13 +96,14 @@ public class AndroidPlugin @Inject constructor(val javaCompiler: JavaCompiler)
|
||||||
fun androidJar(project: Project): Path =
|
fun androidJar(project: Project): Path =
|
||||||
Paths.get(androidHome(project), "platforms", "android-${compileSdkVersion(project)}", "android.jar")
|
Paths.get(androidHome(project), "platforms", "android-${compileSdkVersion(project)}", "android.jar")
|
||||||
|
|
||||||
private fun generated(project: Project) = Paths.get(project.buildDirectory, "generated")
|
private fun generated(project: Project) = KFiles.joinDir(project.directory, project.buildDirectory, "generated")
|
||||||
private fun intermediates(project: Project) = Paths.get(project.buildDirectory, "intermediates")
|
private fun intermediates(project: Project) = KFiles.joinDir(project.directory, project.buildDirectory,
|
||||||
|
"intermediates")
|
||||||
|
|
||||||
private fun aapt(project: Project) = "${androidHome(project)}/build-tools/${buildToolsVersion(project)}/aapt"
|
private fun aapt(project: Project) = "${androidHome(project)}/build-tools/${buildToolsVersion(project)}/aapt"
|
||||||
|
|
||||||
private fun temporaryApk(project: Project, flavor: String)
|
private fun temporaryApk(project: Project, flavor: String)
|
||||||
= KFiles.joinFileAndMakeDir(intermediates(project).toFile().path, "resources", "resources-$flavor.ap_")
|
= KFiles.joinFileAndMakeDir(intermediates(project), "res", "resources-$flavor.ap_")
|
||||||
|
|
||||||
private fun apk(project: Project, flavor: String)
|
private fun apk(project: Project, flavor: String)
|
||||||
= KFiles.joinFileAndMakeDir(project.buildDirectory!!, "outputs", "apk", "app-$flavor.apk")
|
= KFiles.joinFileAndMakeDir(project.buildDirectory!!, "outputs", "apk", "app-$flavor.apk")
|
||||||
|
@ -109,13 +113,50 @@ public class AndroidPlugin @Inject constructor(val javaCompiler: JavaCompiler)
|
||||||
fun taskGenerateRFile(project: Project): TaskResult {
|
fun taskGenerateRFile(project: Project): TaskResult {
|
||||||
|
|
||||||
val intermediates = intermediates(project)
|
val intermediates = intermediates(project)
|
||||||
val resDir = KFiles.joinDir(intermediates.toFile().path, "res", flavor)
|
val resDir = "temporaryBogusResDir"
|
||||||
explodeAarFiles(project, intermediates, File(resDir))
|
explodeAarFiles(project, intermediates, File(resDir))
|
||||||
val generated = generated(project)
|
val generated = generated(project)
|
||||||
generateR(project, generated, aapt(project), resDir)
|
generateR(project, generated, aapt(project))
|
||||||
return TaskResult()
|
return TaskResult()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inner class AaptCommand(project: Project, aapt: String, val aaptCommand: String,
|
||||||
|
cwd: File = File(project.directory)) : AndroidCommand(project, aapt) {
|
||||||
|
init {
|
||||||
|
directory = cwd
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun call(args: List<String>) = super.run(arrayListOf(aaptCommand) + args)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun mergedResources(project: Project, variant: Variant) =
|
||||||
|
KFiles.joinAndMakeDir(intermediates(project), "res", "merged", variant.toIntermediateDir())
|
||||||
|
|
||||||
|
fun mergedManifest(project: Project, variant: Variant) : String {
|
||||||
|
val dir = KFiles.joinAndMakeDir(intermediates(project), "manifests", "full", variant.toIntermediateDir())
|
||||||
|
return KFiles.joinDir(dir, "AndroidManifest.xml")
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* TODO: not implemented yet, just copying the manifest to where the merged manifest should be.
|
||||||
|
*/
|
||||||
|
private fun mergeAndroidManifest(project: Project, variant: Variant) {
|
||||||
|
val dest = mergedManifest(project, variant)
|
||||||
|
log(1, "Manifest merging not implemented, copying it to $dest")
|
||||||
|
KFiles.copy(Paths.get("app/src/main/AndroidManifest.xml"),
|
||||||
|
Paths.get(dest),
|
||||||
|
StandardCopyOption.REPLACE_EXISTING)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* TODO: not implemented yet, just copying the resources into the variant dir
|
||||||
|
*/
|
||||||
|
private fun mergeResources(project: Project, variant: Variant) {
|
||||||
|
val dest = mergedResources(project, variant)
|
||||||
|
log(1, "Resource merging not implemented, copying app/src/main/res to $dest")
|
||||||
|
KFiles.copyRecursively(File("app/src/main/res"), File(dest))
|
||||||
|
}
|
||||||
|
|
||||||
inner open class AndroidCommand(project: Project, command: String, cwd: File = File(project.directory))
|
inner open class AndroidCommand(project: Project, command: String, cwd: File = File(project.directory))
|
||||||
: RunCommand(command) {
|
: RunCommand(command) {
|
||||||
init {
|
init {
|
||||||
|
@ -123,7 +164,7 @@ public class AndroidPlugin @Inject constructor(val javaCompiler: JavaCompiler)
|
||||||
directory = cwd
|
directory = cwd
|
||||||
}
|
}
|
||||||
|
|
||||||
fun call(args: List<String>) = run(args,
|
open fun call(args: List<String>) = run(args,
|
||||||
successCallback = { output ->
|
successCallback = { output ->
|
||||||
log(1, "$command succeeded:")
|
log(1, "$command succeeded:")
|
||||||
output.forEach {
|
output.forEach {
|
||||||
|
@ -138,62 +179,47 @@ public class AndroidPlugin @Inject constructor(val javaCompiler: JavaCompiler)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// inner class AaptCommand(project: Project, aapt: String, val aaptCommand: String,
|
private fun generateR(project: Project, generated: String, aapt: String) {
|
||||||
// cwd: File = File(project.directory)) : AndroidCommand(project, aapt) {
|
|
||||||
// init {
|
|
||||||
// directory = cwd
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// override val commandName = "$aapt $aaptCommand"
|
|
||||||
// }
|
|
||||||
|
|
||||||
private fun findResDirs(project: Project) = project.sourceDirectories.filter { it.contains("res") }
|
mergeAndroidManifest(project, context.variant)
|
||||||
|
mergeResources(project, context.variant)
|
||||||
|
|
||||||
private fun findManifests(variant: Variant) = variant.variantSourceDirectories(context).map {
|
|
||||||
File(it, "AndroidManifest.xml")
|
|
||||||
}.filter {
|
|
||||||
it.exists()
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun generateR(project: Project, generated: Path, aapt: String, resDir: String) {
|
|
||||||
val compileSdkVersion = compileSdkVersion(project)
|
val compileSdkVersion = compileSdkVersion(project)
|
||||||
val androidJar = Paths.get(androidHome(project), "platforms", "android-$compileSdkVersion", "android.jar")
|
val androidJar = Paths.get(androidHome(project), "platforms", "android-$compileSdkVersion", "android.jar")
|
||||||
val applicationId = configurationFor(project)?.applicationId!!
|
val applicationId = configurationFor(project)?.applicationId!!
|
||||||
val manifests = findManifests(context.variant)
|
val intermediates = intermediates(project)
|
||||||
|
val crunchedPngDir = KFiles.joinAndMakeDir(intermediates(project).toString(), "res")
|
||||||
|
|
||||||
val crunchedPngDir = KFiles.joinAndMakeDir(intermediates(project).toString(), "res", flavor)
|
// AaptCommand(project, aapt, "crunch").call(listOf(
|
||||||
|
// "-v",
|
||||||
|
// "-C", mergedResources(project, context.variant),
|
||||||
|
// "-S", crunchedPngDir
|
||||||
|
// ))
|
||||||
|
|
||||||
val resDirArgs = arrayListOf("-S", resDir) + findResDirs(project).filter {
|
val variantDir = context.variant.toIntermediateDir()
|
||||||
File(it).exists()
|
|
||||||
}.map {
|
|
||||||
"-S $it"
|
|
||||||
}.joinToString(" ").split(" ")
|
|
||||||
AndroidCommand(project, aapt).call(listOf("crunch") + resDirArgs + listOf(
|
|
||||||
"-v",
|
|
||||||
"-C", crunchedPngDir
|
|
||||||
))
|
|
||||||
|
|
||||||
val manifestArgs = manifests.map { "-M ${it.path}" }.joinToString(" ").split(" ")
|
val rDirectory = KFiles.joinAndMakeDir(generated, "source", "r", variantDir).toString()
|
||||||
AndroidCommand(project, aapt).call(listOf("package") + resDirArgs + manifestArgs + listOf(
|
AaptCommand(project, aapt, "package").call(listOf(
|
||||||
"-f",
|
"-f",
|
||||||
"--no-crunch",
|
"--no-crunch",
|
||||||
"-I", androidJar.toString(),
|
"-I", androidJar.toString(),
|
||||||
|
"-M", mergedManifest(project, context.variant),
|
||||||
|
"-S", mergedResources(project, context.variant),
|
||||||
// where to find more assets
|
// where to find more assets
|
||||||
"-A", KFiles.joinAndMakeDir(intermediates(project).toString(), "assets", flavor),
|
"-A", KFiles.joinAndMakeDir(intermediates, "assets", variantDir),
|
||||||
"-m", // create directory
|
"-m", // create directory
|
||||||
// where all gets generated
|
// where all gets generated
|
||||||
"-J", KFiles.joinAndMakeDir(generated.toString(), "sources", "r", flavor).toString(),
|
"-J", rDirectory,
|
||||||
"-F", temporaryApk(project, flavor),
|
"-F", temporaryApk(project, context.variant.shortArchiveName),
|
||||||
"--debug-mode",
|
"--debug-mode",
|
||||||
"-0", "apk",
|
"-0", "apk",
|
||||||
"--auto-add-overlay",
|
"--auto-add-overlay",
|
||||||
"--custom-package", applicationId,
|
"--custom-package", applicationId
|
||||||
"--output-text-symbols", KFiles.joinAndMakeDir(intermediates(project).toString(), "symbol", flavor))
|
// "--output-text-symbols", KFiles.joinAndMakeDir(intermediates(project).toString(), "symbol", flavor)
|
||||||
)
|
))
|
||||||
|
|
||||||
val rDirectory = KFiles.joinDir(generated.toFile().path, "sources", "r", flavor,
|
val rOutputDirectory = KFiles.joinDir(rDirectory, applicationId.replace(".", File.separator))
|
||||||
applicationId.replace(".", File.separator))
|
val generatedBuildDir = compile(project, rOutputDirectory)
|
||||||
val generatedBuildDir = compile(project, rDirectory)
|
|
||||||
project.compileDependencies.add(FileDependency(generatedBuildDir.path))
|
project.compileDependencies.add(FileDependency(generatedBuildDir.path))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -201,12 +227,12 @@ public class AndroidPlugin @Inject constructor(val javaCompiler: JavaCompiler)
|
||||||
* Extract all the .aar files found in the dependencies and add the android.jar to classpathEntries,
|
* Extract all the .aar files found in the dependencies and add the android.jar to classpathEntries,
|
||||||
* which will be added to the classpath at compile time
|
* which will be added to the classpath at compile time
|
||||||
*/
|
*/
|
||||||
private fun explodeAarFiles(project: Project, outputDir: Path, resDir: File) {
|
private fun explodeAarFiles(project: Project, outputDir: String, resDir: File) {
|
||||||
project.compileDependencies.filter {
|
project.compileDependencies.filter {
|
||||||
it.jarFile.get().name.endsWith(".aar")
|
it.jarFile.get().name.endsWith(".aar")
|
||||||
}.forEach {
|
}.forEach {
|
||||||
val mavenId = MavenId(it.id)
|
val mavenId = MavenId(it.id)
|
||||||
val destDir = Paths.get(outputDir.toFile().absolutePath, "exploded-aar", mavenId.groupId,
|
val destDir = Paths.get(outputDir, "exploded-aar", mavenId.groupId,
|
||||||
mavenId.artifactId, mavenId.version)
|
mavenId.artifactId, mavenId.version)
|
||||||
.toFile()
|
.toFile()
|
||||||
log(2, "Exploding ${it.jarFile.get()} to $destDir")
|
log(2, "Exploding ${it.jarFile.get()} to $destDir")
|
||||||
|
@ -226,19 +252,20 @@ public class AndroidPlugin @Inject constructor(val javaCompiler: JavaCompiler)
|
||||||
|
|
||||||
// Copy all the resources from this aar into the same intermediate directory
|
// Copy all the resources from this aar into the same intermediate directory
|
||||||
log(2, "Copying the resources to $resDir")
|
log(2, "Copying the resources to $resDir")
|
||||||
KFiles.copyRecursively(destDir.resolve("res"), resDir, deleteFirst = true)
|
KFiles.copyRecursively(destDir.resolve("res"), resDir, deleteFirst = false)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun compile(project: Project, rDirectory: String): File {
|
private fun compile(project: Project, rDirectory: String): File {
|
||||||
val sourceFiles = arrayListOf(Paths.get(rDirectory, "R.java").toFile().path)
|
val sourceFiles = arrayListOf(Paths.get(rDirectory, "R.java").toFile().path)
|
||||||
val c = sourceFiles.javaClass
|
|
||||||
val buildDir = Paths.get(project.buildDirectory, "generated", "classes").toFile()
|
val buildDir = Paths.get(project.buildDirectory, "generated", "classes").toFile()
|
||||||
val cai = CompilerActionInfo(project.directory, listOf(), sourceFiles, buildDir, emptyList())
|
val cai = CompilerActionInfo(project.directory, listOf(), sourceFiles, buildDir, listOf(
|
||||||
|
"-source", "1.6", "-target", "1.6"))
|
||||||
javaCompiler.compile(project, context, cai)
|
javaCompiler.compile(project, context, cai)
|
||||||
return buildDir
|
return buildDir
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Implements ICompilerFlagContributor
|
* Implements ICompilerFlagContributor
|
||||||
* Make sure we compile and generate 1.6 sources unless the build file defined those (which can
|
* Make sure we compile and generate 1.6 sources unless the build file defined those (which can
|
||||||
|
@ -273,30 +300,29 @@ 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).toFile().path, "dex", flavor)
|
val classesDexDir = KFiles.joinDir(intermediates(project), "dex", context.variant.shortArchiveName)
|
||||||
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,
|
val args = listOf("--dex", "--output", outClassesDex)
|
||||||
project.projectProperties.getString(JvmCompilerPlugin.BUILD_DIR)!!)
|
|
||||||
val otherArgs =
|
val otherArgs =
|
||||||
project.dependencies?.let {
|
project.dependencies?.let {
|
||||||
it.dependencies.map {
|
it.dependencies.map {
|
||||||
it.jarFile.get().path
|
it.jarFile.get().path
|
||||||
}.filter { ! it.endsWith(".aar") && ! it.endsWith("android.jar") }
|
}.filter { ! it.endsWith(".aar") && ! it.endsWith("android.jar") }
|
||||||
} ?: emptyList()
|
} ?: emptyList()
|
||||||
AndroidCommand(project, dx).run(args + otherArgs)
|
RunCommand(dx).run(args + otherArgs)
|
||||||
|
|
||||||
//
|
//
|
||||||
// Add classes.dex to existing .ap_
|
// Add classes.dex to existing .ap_
|
||||||
// Because aapt doesn't handle directory moving, we need to cd to classes.dex's directory so
|
// Because aapt doesn't handle directory moving, we need to cd to classes.dex's directory so
|
||||||
// that classes.dex ends up in the root directory of the .ap_.
|
// that classes.dex ends up in the root directory of the .ap_.
|
||||||
//
|
//
|
||||||
AndroidCommand(project, aapt(project)).apply {
|
AaptCommand(project, aapt(project), "add").apply {
|
||||||
directory = File(outClassesDex).parentFile
|
directory = File(outClassesDex).parentFile
|
||||||
}.call(listOf("add") + listOf("-v", KFiles.joinDir(File(temporaryApk(project, flavor)).absolutePath),
|
}.call(listOf("-v", KFiles.joinDir(
|
||||||
classesDex))
|
File(temporaryApk(project, context.variant.shortArchiveName)).absolutePath), classesDex))
|
||||||
|
|
||||||
return TaskResult()
|
return TaskResult()
|
||||||
}
|
}
|
||||||
|
@ -311,8 +337,8 @@ public class AndroidPlugin @Inject constructor(val javaCompiler: JavaCompiler)
|
||||||
@Task(name = "signApk", description = "Sign the apk file", runAfter = arrayOf(TASK_GENERATE_DEX),
|
@Task(name = "signApk", description = "Sign the apk file", runAfter = arrayOf(TASK_GENERATE_DEX),
|
||||||
runBefore = arrayOf("assemble"))
|
runBefore = arrayOf("assemble"))
|
||||||
fun signApk(project: Project): TaskResult {
|
fun signApk(project: Project): TaskResult {
|
||||||
val apk = apk(project, flavor)
|
val apk = apk(project, context.variant.shortArchiveName)
|
||||||
val temporaryApk = temporaryApk(project, flavor)
|
val temporaryApk = temporaryApk(project, context.variant.shortArchiveName)
|
||||||
RunCommand("jarsigner").run(listOf(
|
RunCommand("jarsigner").run(listOf(
|
||||||
"-keystore", homeDir(".android", "debug.keystore"),
|
"-keystore", homeDir(".android", "debug.keystore"),
|
||||||
"-storepass", "android",
|
"-storepass", "android",
|
||||||
|
|
|
@ -45,7 +45,7 @@ class ApplicationPlugin @Inject constructor(val executors: KobaltExecutors,
|
||||||
|
|
||||||
override fun apply(project: Project, context: KobaltContext) {
|
override fun apply(project: Project, context: KobaltContext) {
|
||||||
super.apply(project, context)
|
super.apply(project, context)
|
||||||
addVariantTasks(project, "run", listOf("assemble"), { taskRun(project) })
|
addVariantTasks(project, "run", runAfter = listOf("assemble"), runTask = { taskRun(project) })
|
||||||
}
|
}
|
||||||
|
|
||||||
@Task(name = "run", description = "Run the main class", runAfter = arrayOf("assemble"))
|
@Task(name = "run", description = "Run the main class", runAfter = arrayOf("assemble"))
|
||||||
|
|
|
@ -15,7 +15,7 @@ public class JavaProject(
|
||||||
override var directory: String = ".",
|
override var directory: String = ".",
|
||||||
/** The build directory, relative to the project directory */
|
/** The build directory, relative to the project directory */
|
||||||
@Directive
|
@Directive
|
||||||
override var buildDirectory: String? = "kobaltBuild",
|
override var buildDirectory: String = "kobaltBuild",
|
||||||
@Directive
|
@Directive
|
||||||
override var group: String? = null,
|
override var group: String? = null,
|
||||||
@Directive
|
@Directive
|
||||||
|
|
|
@ -15,7 +15,7 @@ public class KotlinProject(
|
||||||
override var directory: String = ".",
|
override var directory: String = ".",
|
||||||
/** The build directory, relative to the project directory */
|
/** The build directory, relative to the project directory */
|
||||||
@Directive
|
@Directive
|
||||||
override var buildDirectory: String? = "kobaltBuild",
|
override var buildDirectory: String = "kobaltBuild",
|
||||||
@Directive
|
@Directive
|
||||||
override var group: String? = null,
|
override var group: String? = null,
|
||||||
@Directive
|
@Directive
|
||||||
|
|
|
@ -58,7 +58,7 @@ class PackagingPlugin @Inject constructor(val dependencyManager : DependencyMana
|
||||||
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(LIBS_DIR, libsDir(project))
|
project.projectProperties.put(LIBS_DIR, libsDir(project))
|
||||||
addVariantTasks(project, "assemble", listOf("compile"), { taskAssemble(project) })
|
addVariantTasks(project, "assemble", runAfter = listOf("compile"), runTask = { taskAssemble(project) })
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun libsDir(project: Project) = KFiles.makeDir(buildDir(project).path, "libs").path
|
private fun libsDir(project: Project) = KFiles.makeDir(buildDir(project).path, "libs").path
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue