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

Proguard preliminary work.

This commit is contained in:
Cedric Beust 2015-11-28 01:18:53 -08:00
parent 21aca77947
commit dfa429336b
4 changed files with 48 additions and 14 deletions

View file

@ -131,7 +131,7 @@ class Variant(val initialProductFlavor: ProductFlavorConfig? = null,
companion object { companion object {
val DEFAULT_PRODUCT_FLAVOR = ProductFlavorConfig("") val DEFAULT_PRODUCT_FLAVOR = ProductFlavorConfig("")
val DEFAULT_BUILD_TYPE = BuildTypeConfig("") val DEFAULT_BUILD_TYPE = BuildTypeConfig(null, "")
fun allVariants(project: Project): List<Variant> { fun allVariants(project: Project): List<Variant> {
val result = arrayListOf<Variant>() val result = arrayListOf<Variant>()

View file

@ -1,10 +1,13 @@
package com.beust.kobalt.api package com.beust.kobalt.api
import com.beust.kobalt.Plugins
import com.beust.kobalt.api.annotation.Directive import com.beust.kobalt.api.annotation.Directive
import com.beust.kobalt.internal.IProjectInfo import com.beust.kobalt.internal.IProjectInfo
import com.beust.kobalt.maven.IClasspathDependency import com.beust.kobalt.maven.IClasspathDependency
import com.beust.kobalt.maven.MavenDependency import com.beust.kobalt.maven.MavenDependency
import com.beust.kobalt.misc.KFiles import com.beust.kobalt.misc.KFiles
import com.beust.kobalt.plugin.android.AndroidPlugin
import com.beust.kobalt.plugin.android.Proguard
import java.util.* import java.util.*
open public class Project( open public class Project(
@ -188,12 +191,20 @@ fun Project.productFlavor(name: String, init: ProductFlavorConfig.() -> Unit) =
addProductFlavor(name, this) addProductFlavor(name, this)
} }
class BuildTypeConfig(val name: String) : IBuildConfig { class BuildTypeConfig(val project: Project?, val name: String) : IBuildConfig {
var minifyEnabled = false
var proguardFile: String? = null
fun getDefaultProguardFile(name: String) : String {
val androidPlugin = Plugins.findPlugin(AndroidPlugin.PLUGIN_NAME) as AndroidPlugin
return Proguard(androidPlugin.androidHome(project)).getDefaultProguardFile(name)
}
override var buildConfig: BuildConfig? = null override var buildConfig: BuildConfig? = null
} }
@Directive @Directive
fun Project.buildType(name: String, init: BuildTypeConfig.() -> Unit) = BuildTypeConfig(name).apply { fun Project.buildType(name: String, init: BuildTypeConfig.() -> Unit) = BuildTypeConfig(this, name).apply {
init() init()
addBuildType(name, this) addBuildType(name, this)
} }

View file

@ -31,6 +31,8 @@ public class AndroidPlugin @Inject constructor(val javaCompiler: JavaCompiler, v
companion object { companion object {
const val PLUGIN_NAME = "Android" const val PLUGIN_NAME = "Android"
const val TASK_GENERATE_DEX = "generateDex" const val TASK_GENERATE_DEX = "generateDex"
const val TASK_SIGN_APK = "signApk"
const val TASK_INSTALL= "install"
} }
override val name = PLUGIN_NAME override val name = PLUGIN_NAME
@ -50,6 +52,8 @@ public class AndroidPlugin @Inject constructor(val javaCompiler: JavaCompiler, v
runTask = { taskSignApk(project) }) runTask = { taskSignApk(project) })
addVariantTasks(project, "install", runAfter = listOf("signApk"), addVariantTasks(project, "install", runAfter = listOf("signApk"),
runTask = { taskInstall(project) }) runTask = { taskInstall(project) })
addVariantTasks(project, "proguard", runBefore = listOf("install"), runAfter = listOf("compile"),
runTask = { taskProguard(project) })
} }
context.pluginInfo.classpathContributors.add(this) context.pluginInfo.classpathContributors.add(this)
} }
@ -232,6 +236,22 @@ public class AndroidPlugin @Inject constructor(val javaCompiler: JavaCompiler, v
} }
} }
@Task(name = "proguard", description = "Run Proguard, if enabled", runBefore = arrayOf(TASK_GENERATE_DEX),
runAfter = arrayOf("compile"))
fun taskProguard(project: Project): TaskResult {
val config = configurationFor(project)
if (config != null) {
val buildType = context.variant.buildType
if (buildType.minifyEnabled) {
log(1, "minifyEnabled is true, running Proguard")
val classesDir = project.classesDir(context)
val proguardHome = KFiles.joinDir(androidHome(project), "tools", "proguard")
val proguardCommand = KFiles.joinDir(proguardHome, "bin", "proguard.sh")
}
}
return TaskResult()
}
@Task(name = TASK_GENERATE_DEX, description = "Generate the dex file", runBefore = arrayOf("assemble"), @Task(name = TASK_GENERATE_DEX, description = "Generate the dex file", runBefore = arrayOf("assemble"),
runAfter = arrayOf("compile")) runAfter = arrayOf("compile"))
fun taskGenerateDex(project: Project): TaskResult { fun taskGenerateDex(project: Project): TaskResult {
@ -258,14 +278,6 @@ public class AndroidPlugin @Inject constructor(val javaCompiler: JavaCompiler, v
//KFiles.joinDir(intermediates(project), "dex", context.variant.toIntermediateDir()), //KFiles.joinDir(intermediates(project), "dex", context.variant.toIntermediateDir()),
project.classesDir(context) 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_
@ -292,7 +304,7 @@ public class AndroidPlugin @Inject constructor(val javaCompiler: JavaCompiler, v
* jarsigner -keystore ~/.android/debug.keystore -storepass android -keypass android -signedjar a.apk a.ap_ * jarsigner -keystore ~/.android/debug.keystore -storepass android -keypass android -signedjar a.apk a.ap_
* androiddebugkey * androiddebugkey
*/ */
@Task(name = "signApk", description = "Sign the apk file", runAfter = arrayOf(TASK_GENERATE_DEX), @Task(name = TASK_SIGN_APK, description = "Sign the apk file", runAfter = arrayOf(TASK_GENERATE_DEX),
runBefore = arrayOf("assemble")) runBefore = arrayOf("assemble"))
fun taskSignApk(project: Project): TaskResult { fun taskSignApk(project: Project): TaskResult {
val apk = apk(project, context.variant.shortArchiveName) val apk = apk(project, context.variant.shortArchiveName)
@ -320,10 +332,10 @@ public class AndroidPlugin @Inject constructor(val javaCompiler: JavaCompiler, v
)) ))
log(1, "Created $apk") log(1, "Created $apk")
return TaskResult(success == 0) return TaskResult(success == 0)
} }
@Task(name = "install", description = "Install the apk file", runAfter = arrayOf(TASK_GENERATE_DEX, "assemble")) @Task(name = TASK_INSTALL, description = "Install the apk file", runAfter = arrayOf(TASK_GENERATE_DEX, "assemble"))
fun taskInstall(project: Project): TaskResult { fun taskInstall(project: Project): TaskResult {
val apk = apk(project, context.variant.shortArchiveName) val apk = apk(project, context.variant.shortArchiveName)
RunCommand(adb(project)).useErrorStreamAsErrorIndicator(false).run(args = listOf( RunCommand(adb(project)).useErrorStreamAsErrorIndicator(false).run(args = listOf(
@ -438,3 +450,4 @@ fun AndroidConfig.signingConfig(name: String, init: SigningConfig.() -> Unit) :
} }
} }

View file

@ -0,0 +1,10 @@
package com.beust.kobalt.plugin.android
import com.beust.kobalt.misc.KFiles
class Proguard(val androidHome: String) {
val proguardHome = KFiles.joinDir(androidHome, "tools", "proguard")
val proguardCommand = KFiles.joinDir(proguardHome, "bin", "proguard.sh")
fun getDefaultProguardFile(name: String) = KFiles.joinDir(proguardHome, name)
}