mirror of
https://github.com/ethauvin/kobalt.git
synced 2025-04-27 00:38:11 -07:00
Add "run" to the Android plug-in.
This commit is contained in:
parent
f2d15c31da
commit
a45640c5be
4 changed files with 99 additions and 3 deletions
|
@ -1,6 +1,7 @@
|
|||
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
|
||||
|
||||
|
@ -11,6 +12,10 @@ class AndroidFiles {
|
|||
fun intermediates(project: Project) = KFiles.joinDir(project.directory, project.buildDirectory,
|
||||
"intermediates")
|
||||
|
||||
fun manifest(project: Project, context: KobaltContext) : String {
|
||||
return KFiles.joinDir(project.directory, "src/main", "AndroidManifest.xml")
|
||||
}
|
||||
|
||||
fun mergedManifest(project: Project, variant: Variant) : String {
|
||||
val dir = KFiles.joinAndMakeDir(intermediates(project), "manifests", "full", variant.toIntermediateDir())
|
||||
return KFiles.joinDir(dir, "AndroidManifest.xml")
|
||||
|
|
|
@ -0,0 +1,74 @@
|
|||
package com.beust.kobalt.plugin.android
|
||||
|
||||
import java.io.InputStream
|
||||
import javax.xml.bind.JAXBContext
|
||||
import javax.xml.bind.annotation.XmlAttribute
|
||||
import javax.xml.bind.annotation.XmlElement
|
||||
import javax.xml.bind.annotation.XmlRootElement
|
||||
|
||||
/**
|
||||
* Parse AndroidManifest.xml and expose its content.
|
||||
*/
|
||||
class AndroidManifest(val ins: InputStream) {
|
||||
val manifest: AndroidManifestXml by lazy {
|
||||
val jaxbContext = JAXBContext.newInstance(AndroidManifestXml::class.java)
|
||||
jaxbContext.createUnmarshaller().unmarshal(ins) as AndroidManifestXml
|
||||
}
|
||||
|
||||
val pkg by lazy {
|
||||
manifest.pkg
|
||||
}
|
||||
|
||||
val mainActivity: String? by lazy {
|
||||
fun isLaunch(act: ActivityXml) : Boolean {
|
||||
val r = act.intentFilters.filter { inf: IntentFilter ->
|
||||
inf.action?.name == "android.intent.action.MAIN" &&
|
||||
inf.category?.name == "android.intent.category.LAUNCHER"
|
||||
}
|
||||
return r.size > 0
|
||||
}
|
||||
val act = manifest.application?.activities?.filter { isLaunch(it) }
|
||||
if (act != null && act.size > 0) {
|
||||
act.get(0).name?.let { n ->
|
||||
if (n.startsWith(".")) pkg + "." + n.substring(1) else n
|
||||
}
|
||||
} else {
|
||||
null
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@XmlRootElement(name = "manifest")
|
||||
class AndroidManifestXml {
|
||||
@XmlAttribute(name = "package") @JvmField
|
||||
val pkg: String? = null
|
||||
var application: ApplicationXml? = null
|
||||
}
|
||||
|
||||
class ApplicationXml {
|
||||
@XmlElement(name = "activity") @JvmField
|
||||
var activities: List<ActivityXml> = arrayListOf()
|
||||
}
|
||||
|
||||
class ActivityXml {
|
||||
@XmlAttribute(namespace = "http://schemas.android.com/apk/res/android", name = "name") @JvmField
|
||||
var name: String? = null
|
||||
|
||||
@XmlElement(name = "intent-filter") @JvmField
|
||||
var intentFilters: List<IntentFilter> = arrayListOf()
|
||||
}
|
||||
|
||||
class IntentFilter {
|
||||
var action: ActionXml? = null
|
||||
var category: CategoryXml? = null
|
||||
}
|
||||
|
||||
class ActionXml {
|
||||
@XmlAttribute(namespace = "http://schemas.android.com/apk/res/android", name = "name") @JvmField
|
||||
var name: String? = null
|
||||
}
|
||||
|
||||
class CategoryXml {
|
||||
@XmlAttribute(namespace = "http://schemas.android.com/apk/res/android", name = "name") @JvmField
|
||||
var name: String? = null
|
||||
}
|
|
@ -19,6 +19,7 @@ import com.google.common.collect.HashMultimap
|
|||
import com.google.inject.Inject
|
||||
import com.google.inject.Singleton
|
||||
import java.io.File
|
||||
import java.io.FileInputStream
|
||||
import java.net.URI
|
||||
import java.nio.file.Path
|
||||
import java.nio.file.Paths
|
||||
|
@ -43,7 +44,7 @@ fun Project.android(init: AndroidConfig.() -> Unit) : AndroidConfig {
|
|||
@Singleton
|
||||
public class AndroidPlugin @Inject constructor(val javaCompiler: JavaCompiler, val merger: Merger)
|
||||
: ConfigPlugin<AndroidConfig>(), IClasspathContributor, IRepoContributor, ICompilerFlagContributor,
|
||||
ICompilerInterceptor, IBuildDirectoryIncerceptor {
|
||||
ICompilerInterceptor, IBuildDirectoryIncerceptor, IRunContributor {
|
||||
override val name = "android"
|
||||
|
||||
fun isAndroid(project: Project) = configurationFor(project) != null
|
||||
|
@ -371,5 +372,21 @@ public class AndroidPlugin @Inject constructor(val javaCompiler: JavaCompiler, v
|
|||
return result
|
||||
}
|
||||
|
||||
// IRunContributor
|
||||
override fun runAffinity(project: Project, context: KobaltContext): Int {
|
||||
val manifest = AndroidFiles.manifest(project, context)
|
||||
return if (File(manifest).exists()) IRunContributor.DEFAULT_POSITIVE_AFFINITY else 0
|
||||
}
|
||||
|
||||
override fun run(project: Project, context: KobaltContext): TaskResult {
|
||||
val manifest = AndroidFiles.manifest(project, context)
|
||||
FileInputStream(File(manifest)).use { ins ->
|
||||
// adb shell am start -n com.package.name/com.package.name.ActivityName
|
||||
val manifest = AndroidManifest(ins)
|
||||
RunCommand(adb(project)).useErrorStreamAsErrorIndicator(false).run(args = listOf(
|
||||
"shell", "am", "start", "-n", manifest.pkg + "/" + manifest.mainActivity))
|
||||
return TaskResult()
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -43,10 +43,10 @@ class ApplicationPlugin @Inject constructor(val executors: KobaltExecutors,
|
|||
|
||||
override fun apply(project: Project, context: KobaltContext) {
|
||||
super.apply(project, context)
|
||||
addVariantTasks(project, "run", runAfter = listOf("assemble"), runTask = { taskRun(project) })
|
||||
addVariantTasks(project, "run", runAfter = listOf("install"), runTask = { taskRun(project) })
|
||||
}
|
||||
|
||||
@Task(name = "run", description = "Run the main class", runAfter = arrayOf("assemble"))
|
||||
@Task(name = "run", description = "Run the main class", runAfter = arrayOf("install"))
|
||||
fun taskRun(project: Project): TaskResult {
|
||||
val runContributor = context.pluginInfo.runContributors.maxBy { it.runAffinity(project, context)}
|
||||
if (runContributor != null && runContributor.runAffinity(project, context) > 0) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue