mirror of
https://github.com/ethauvin/kobalt.git
synced 2025-04-26 00:17:11 -07:00
Refactor.
This commit is contained in:
parent
3bc211bd80
commit
ad99281ab5
3 changed files with 80 additions and 52 deletions
|
@ -0,0 +1,22 @@
|
||||||
|
package com.beust.kobalt.plugin.android
|
||||||
|
|
||||||
|
import com.beust.kobalt.Variant
|
||||||
|
import com.beust.kobalt.api.Project
|
||||||
|
import com.beust.kobalt.misc.KFiles
|
||||||
|
|
||||||
|
class AndroidFiles {
|
||||||
|
companion object {
|
||||||
|
fun generated(project: Project) = KFiles.joinDir(project.directory, project.buildDirectory, "generated")
|
||||||
|
|
||||||
|
fun intermediates(project: Project) = KFiles.joinDir(project.directory, project.buildDirectory,
|
||||||
|
"intermediates")
|
||||||
|
|
||||||
|
fun mergedManifest(project: Project, variant: Variant) : String {
|
||||||
|
val dir = KFiles.joinAndMakeDir(intermediates(project), "manifests", "full", variant.toIntermediateDir())
|
||||||
|
return KFiles.joinDir(dir, "AndroidManifest.xml")
|
||||||
|
}
|
||||||
|
|
||||||
|
fun mergedResources(project: Project, variant: Variant) =
|
||||||
|
KFiles.joinAndMakeDir(AndroidFiles.intermediates(project), "res", "merged", variant.toIntermediateDir())
|
||||||
|
}
|
||||||
|
}
|
|
@ -22,7 +22,6 @@ 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",
|
||||||
|
@ -100,28 +99,24 @@ 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) = KFiles.joinDir(project.directory, project.buildDirectory, "generated")
|
|
||||||
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 adb(project: Project) = "${androidHome(project)}/platform-tools/adb"
|
private fun adb(project: Project) = "${androidHome(project)}/platform-tools/adb"
|
||||||
|
|
||||||
private fun temporaryApk(project: Project, flavor: String)
|
private fun temporaryApk(project: Project, flavor: String)
|
||||||
= KFiles.joinFileAndMakeDir(intermediates(project), "res", "resources-$flavor.ap_")
|
= KFiles.joinFileAndMakeDir(AndroidFiles.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")
|
||||||
|
|
||||||
@Task(name = "generateR", description = "Generate the R.java file",
|
@Task(name = "generateR", description = "Generate the R.java file",
|
||||||
runBefore = arrayOf("compile"), runAfter = arrayOf("clean"))
|
runBefore = arrayOf("compile"), runAfter = arrayOf("clean"))
|
||||||
fun taskGenerateRFile(project: Project): TaskResult {
|
fun taskGenerateRFile(project: Project): TaskResult {
|
||||||
|
|
||||||
val intermediates = intermediates(project)
|
val intermediates = AndroidFiles.intermediates(project)
|
||||||
val resDir = "temporaryBogusResDir"
|
val resDir = "temporaryBogusResDir"
|
||||||
explodeAarFiles(project, intermediates, File(resDir))
|
explodeAarFiles(project, intermediates, File(resDir))
|
||||||
val generated = generated(project)
|
val generated = AndroidFiles.generated(project)
|
||||||
generateR(project, generated, aapt(project))
|
generateR(project, generated, aapt(project))
|
||||||
return TaskResult()
|
return TaskResult()
|
||||||
}
|
}
|
||||||
|
@ -137,48 +132,12 @@ public class AndroidPlugin @Inject constructor(val javaCompiler: JavaCompiler)
|
||||||
override fun call(args: List<String>) = super.run(arrayListOf(aaptCommand) + args)
|
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
|
|
||||||
* Spec: http://developer.android.com/sdk/installing/studio-build.html
|
|
||||||
*/
|
|
||||||
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")
|
|
||||||
listOf("main", variant.productFlavor.name, variant.buildType.name).forEach {
|
|
||||||
log(1, " Copying app/src/$it/res into $dest")
|
|
||||||
KFiles.copyRecursively(File("app/src/$it/res"), File(dest), deleteFirst = false)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun generateR(project: Project, generated: String, aapt: String) {
|
private fun generateR(project: Project, generated: String, aapt: String) {
|
||||||
|
|
||||||
mergeAndroidManifest(project, context.variant)
|
|
||||||
mergeResources(project, context.variant)
|
|
||||||
|
|
||||||
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 intermediates = intermediates(project)
|
val intermediates = AndroidFiles.intermediates(project)
|
||||||
val crunchedPngDir = KFiles.joinAndMakeDir(intermediates(project).toString(), "res")
|
val crunchedPngDir = KFiles.joinAndMakeDir(AndroidFiles.intermediates(project).toString(), "res")
|
||||||
|
|
||||||
// AaptCommand(project, aapt, "crunch").call(listOf(
|
// AaptCommand(project, aapt, "crunch").call(listOf(
|
||||||
// "-v",
|
// "-v",
|
||||||
|
@ -193,8 +152,8 @@ public class AndroidPlugin @Inject constructor(val javaCompiler: JavaCompiler)
|
||||||
"-f",
|
"-f",
|
||||||
"--no-crunch",
|
"--no-crunch",
|
||||||
"-I", androidJar.toString(),
|
"-I", androidJar.toString(),
|
||||||
"-M", mergedManifest(project, context.variant),
|
"-M", AndroidFiles.mergedManifest(project, context.variant),
|
||||||
"-S", mergedResources(project, context.variant),
|
"-S", AndroidFiles.mergedResources(project, context.variant),
|
||||||
// where to find more assets
|
// where to find more assets
|
||||||
"-A", KFiles.joinAndMakeDir(intermediates, "assets", variantDir),
|
"-A", KFiles.joinAndMakeDir(intermediates, "assets", variantDir),
|
||||||
"-m", // create directory
|
"-m", // create directory
|
||||||
|
@ -255,7 +214,6 @@ public class AndroidPlugin @Inject constructor(val javaCompiler: JavaCompiler)
|
||||||
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
|
||||||
|
@ -291,7 +249,8 @@ 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.toIntermediateDir())
|
val classesDexDir = KFiles.joinDir(AndroidFiles.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)
|
||||||
|
@ -386,7 +345,8 @@ public class AndroidPlugin @Inject constructor(val javaCompiler: JavaCompiler)
|
||||||
|
|
||||||
// IBuildDirectoryInterceptor
|
// IBuildDirectoryInterceptor
|
||||||
override fun intercept(project: Project, context: KobaltContext, buildDirectory: String): String {
|
override fun intercept(project: Project, context: KobaltContext, buildDirectory: String): String {
|
||||||
val result = KFiles.joinDir(intermediates(project), "classes", context.variant.toIntermediateDir())
|
val result = KFiles.joinDir(AndroidFiles.intermediates(project), "classes",
|
||||||
|
context.variant.toIntermediateDir())
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
46
src/main/kotlin/com/beust/kobalt/plugin/android/Merger.kt
Normal file
46
src/main/kotlin/com/beust/kobalt/plugin/android/Merger.kt
Normal file
|
@ -0,0 +1,46 @@
|
||||||
|
package com.beust.kobalt.plugin.android
|
||||||
|
|
||||||
|
import com.beust.kobalt.Variant
|
||||||
|
import com.beust.kobalt.api.KobaltContext
|
||||||
|
import com.beust.kobalt.api.Project
|
||||||
|
import com.beust.kobalt.misc.KFiles
|
||||||
|
import com.beust.kobalt.misc.log
|
||||||
|
import com.google.inject.Inject
|
||||||
|
import java.io.File
|
||||||
|
import java.nio.file.Paths
|
||||||
|
import java.nio.file.StandardCopyOption
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Merges manifests and resources.
|
||||||
|
*/
|
||||||
|
class Merger @Inject constructor() {
|
||||||
|
fun merge(project: Project, context: KobaltContext) {
|
||||||
|
mergeAndroidManifest(project, context.variant)
|
||||||
|
mergeResources(project, context.variant)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* TODO: not implemented yet, just copying the manifest to where the merged manifest should be.
|
||||||
|
*/
|
||||||
|
private fun mergeAndroidManifest(project: Project, variant: Variant) {
|
||||||
|
val dest = AndroidFiles.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
|
||||||
|
* Spec: http://developer.android.com/sdk/installing/studio-build.html
|
||||||
|
*/
|
||||||
|
private fun mergeResources(project: Project, variant: Variant) {
|
||||||
|
val dest = AndroidFiles.Companion.mergedResources(project, variant)
|
||||||
|
log(1, "Resource merging not implemented, copying app/src/main/res to $dest")
|
||||||
|
listOf("main", variant.productFlavor.name, variant.buildType.name).forEach {
|
||||||
|
log(1, " Copying app/src/$it/res into $dest")
|
||||||
|
KFiles.copyRecursively(File("app/src/$it/res"), File(dest), deleteFirst = false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue