diff --git a/easylauncher/src/main/kotlin/com/project/starter/easylauncher/plugin/AgpUtils.kt b/easylauncher/src/main/kotlin/com/project/starter/easylauncher/plugin/AgpUtils.kt new file mode 100644 index 00000000..575260b8 --- /dev/null +++ b/easylauncher/src/main/kotlin/com/project/starter/easylauncher/plugin/AgpUtils.kt @@ -0,0 +1,23 @@ +package com.project.starter.easylauncher.plugin + +import com.android.build.gradle.AppExtension +import com.android.build.gradle.LibraryExtension +import com.android.build.gradle.api.BaseVariant +import org.gradle.api.DomainObjectSet +import org.gradle.api.Project + +private val supportedPlugins = listOf( + "com.android.application", + "com.android.library", +) + +internal fun Project.configureSupportedPlugins(block: (DomainObjectSet) -> Unit) { + supportedPlugins.forEach { pluginId -> + pluginManager.withPlugin(pluginId) { block(findVariants()) } + } +} + +internal fun Project.findVariants(): DomainObjectSet = + extensions.findByType(AppExtension::class.java)?.applicationVariants + ?: extensions.findByType(LibraryExtension::class.java)?.libraryVariants + ?: this.objects.domainObjectSet(BaseVariant::class.java) diff --git a/easylauncher/src/main/kotlin/com/project/starter/easylauncher/plugin/EasyLauncherPlugin.kt b/easylauncher/src/main/kotlin/com/project/starter/easylauncher/plugin/EasyLauncherPlugin.kt index ba6d2cb0..980fb48f 100644 --- a/easylauncher/src/main/kotlin/com/project/starter/easylauncher/plugin/EasyLauncherPlugin.kt +++ b/easylauncher/src/main/kotlin/com/project/starter/easylauncher/plugin/EasyLauncherPlugin.kt @@ -1,7 +1,7 @@ package com.project.starter.easylauncher.plugin -import com.android.build.gradle.AppExtension -import com.android.build.gradle.api.ApplicationVariant +import com.android.build.gradle.BaseExtension +import com.android.build.gradle.api.BaseVariant import org.gradle.api.Plugin import org.gradle.api.Project import org.gradle.api.tasks.TaskProvider @@ -14,12 +14,12 @@ class EasyLauncherPlugin : Plugin { logger.info("Running gradle version: ${gradle.gradleVersion}") - pluginManager.withPlugin("com.android.application") { - val android = extensions.getByType(AppExtension::class.java) + configureSupportedPlugins { variants -> + val android = extensions.getByType(BaseExtension::class.java) val easyLauncherTasks = mutableListOf>() - android.applicationVariants.configureEach { variant -> + variants.configureEach { variant -> val configs = extension.variants.filter { it.name == variant.name }.takeIf { it.isNotEmpty() } ?: findConfigs(variant, extension.productFlavors, extension.buildTypes) @@ -75,13 +75,13 @@ class EasyLauncherPlugin : Plugin { } private fun findConfigs( - variant: ApplicationVariant, + variant: BaseVariant, ribbonProductFlavors: Iterable, ribbonBuildTypes: Iterable ): List = ribbonProductFlavors.filter { config -> variant.productFlavors.any { config.name == it.name } } + ribbonBuildTypes.filter { it.name == variant.buildType.name } - private fun Project.getGeneratedResDir(variant: ApplicationVariant) = + private fun Project.getGeneratedResDir(variant: BaseVariant) = File(project.buildDir, "generated/easylauncher/res/${variant.name}") } diff --git a/easylauncher/src/main/kotlin/com/project/starter/easylauncher/plugin/EasyLauncherTask.kt b/easylauncher/src/main/kotlin/com/project/starter/easylauncher/plugin/EasyLauncherTask.kt index e840f4f3..d290e594 100644 --- a/easylauncher/src/main/kotlin/com/project/starter/easylauncher/plugin/EasyLauncherTask.kt +++ b/easylauncher/src/main/kotlin/com/project/starter/easylauncher/plugin/EasyLauncherTask.kt @@ -1,7 +1,7 @@ package com.project.starter.easylauncher.plugin -import com.android.build.gradle.AppExtension -import com.android.build.gradle.api.ApplicationVariant +import com.android.build.gradle.BaseExtension +import com.android.build.gradle.api.BaseVariant import com.project.starter.easylauncher.filter.EasyLauncherFilter import com.project.starter.easylauncher.plugin.models.AdaptiveIcon import org.gradle.api.DefaultTask @@ -36,8 +36,8 @@ open class EasyLauncherTask : DefaultTask() { } val taskExecutionTime = measureTimeMillis { - val android = project.extensions.getByType(AppExtension::class.java) - val variant = android.applicationVariants.find { it.name == variantName.get() } + val android = project.extensions.getByType(BaseExtension::class.java) + val variant = project.findVariants().find { it.name == variantName.get() } ?: throw GradleException("invalid variant name ${variantName.get()}") val names = (iconsNames.orNull?.takeIf { it.isNotEmpty() } ?: android.getLauncherIconNames(variant)).toSet() @@ -59,11 +59,11 @@ open class EasyLauncherTask : DefaultTask() { logger.info("task finished in $taskExecutionTime ms") } - private fun ApplicationVariant.getAllSourceSets() = + private fun BaseVariant.getAllSourceSets() = sourceSets.flatMap { sourceSet -> sourceSet.resDirectories } .filterNot { resDirectory -> resDirectory == outputDir.asFile.get() } - private fun ApplicationVariant.processIcon(adaptiveIcon: AdaptiveIcon) { + private fun BaseVariant.processIcon(adaptiveIcon: AdaptiveIcon) { getAllSourceSets().forEach { resDir -> val icons = resDir.getIconFiles(adaptiveIcon.foreground) icons.forEach { iconFile -> @@ -90,11 +90,11 @@ open class EasyLauncherTask : DefaultTask() { } } - private fun AppExtension.getLauncherIconNames(variant: ApplicationVariant) = + private fun BaseExtension.getLauncherIconNames(variant: BaseVariant) = getAndroidManifestFiles(variant) .mapNotNull { manifestFile -> manifestFile.getLauncherIcon() } - private fun AppExtension.getAndroidManifestFiles(variant: ApplicationVariant): Iterable { + private fun BaseExtension.getAndroidManifestFiles(variant: BaseVariant): Iterable { return listOf("main", variant.name, variant.buildType.name, variant.flavorName) .filter { it.isNotEmpty() } .distinct() diff --git a/easylauncher/src/test/kotlin/com/project/starter/easylauncher/plugin/EasyLauncherConfigTest.kt b/easylauncher/src/test/kotlin/com/project/starter/easylauncher/plugin/EasyLauncherConfigTest.kt index beb5aaaa..5517439c 100644 --- a/easylauncher/src/test/kotlin/com/project/starter/easylauncher/plugin/EasyLauncherConfigTest.kt +++ b/easylauncher/src/test/kotlin/com/project/starter/easylauncher/plugin/EasyLauncherConfigTest.kt @@ -2,6 +2,7 @@ package com.project.starter.easylauncher.plugin import com.project.starter.easylauncher.plugin.utils.WithGradleProjectTest import com.project.starter.easylauncher.plugin.utils.buildScript +import com.project.starter.easylauncher.plugin.utils.libraryBuildscript import org.junit.jupiter.api.BeforeEach import org.junit.jupiter.api.Test @@ -78,4 +79,29 @@ internal class EasyLauncherConfigTest : WithGradleProjectTest() { runTask("assembleDebug", "--stacktrace") } + + @Test + fun `library config`() { + rootDirectory.resolve("build.gradle").libraryBuildscript( + androidBlock = { + """ + buildTypes { + debug { } + release { } + } + """.trimIndent() + }, + easylauncherBlock = { + """ + buildTypes { + debug { + filters = redRibbonFilter() + } + } + """.trimIndent() + } + ) + + runTask("assembleDebug", "--stacktrace") + } } diff --git a/easylauncher/src/test/kotlin/com/project/starter/easylauncher/plugin/utils/Factories.kt b/easylauncher/src/test/kotlin/com/project/starter/easylauncher/plugin/utils/Factories.kt index 7dff308d..12f415cc 100644 --- a/easylauncher/src/test/kotlin/com/project/starter/easylauncher/plugin/utils/Factories.kt +++ b/easylauncher/src/test/kotlin/com/project/starter/easylauncher/plugin/utils/Factories.kt @@ -37,6 +37,40 @@ fun File.buildScript(androidBlock: () -> String, easylauncherBlock: () -> String writeText(buildScript) } +fun File.libraryBuildscript(androidBlock: () -> String, easylauncherBlock: () -> String = { "" }) { + @Language("groovy") + val buildScript = + """ + plugins { + id 'com.android.library' + id 'com.starter.easylauncher' + } + + repositories { + jcenter() + google() + } + + android { + defaultConfig { + compileSdkVersion 29 + minSdkVersion 23 + } + ${androidBlock()} + } + + easylauncher { + ${easylauncherBlock()} + } + + dependencies { + testImplementation 'junit:junit:4.13' + } + + """.trimIndent() + writeText(buildScript) +} + internal fun vectorFile() = """ + variant.mergedFlavor.applicationIdSuffix = variant.name + if (variant.flavorName == "") { + variant.mergedFlavor.manifestPlaceholders = [appName: "$project.name"] + } else { + variant.mergedFlavor.manifestPlaceholders = [appName: "$variant.flavorName"] + } + } +} +tasks.register("verifyAll") { + android.libraryVariants.configureEach { variant -> + dependsOn("verify${variant.flavorName.capitalize()}DebugAndroidTestScreenshotTest") + } +} +tasks.register("recordAll") { + android.libraryVariants.configureEach { variant -> + dependsOn("record${variant.flavorName.capitalize()}DebugAndroidTestScreenshotTest") + } +} + +tasks.register("installAll") { + android.libraryVariants.configureEach { variant -> + dependsOn("install${variant.name.capitalize()}") + } +} + +tasks.withType(Test).configureEach { + useJUnitPlatform() +} +screenshots { + failureDir = "${buildDir}/failedScreenshots" +} + +dependencies { + implementation project(":adaptive-support") + androidTestImplementation project(":screenshot-test-helpers") +} diff --git a/sample/example-library/screenshots/Icons_doScreenshot(library).png b/sample/example-library/screenshots/Icons_doScreenshot(library).png new file mode 100644 index 00000000..0e05f5af Binary files /dev/null and b/sample/example-library/screenshots/Icons_doScreenshot(library).png differ diff --git a/sample/example-library/src/androidTest/kotlin/com/starter/easylauncher/screenshot/IconsTest.kt b/sample/example-library/src/androidTest/kotlin/com/starter/easylauncher/screenshot/IconsTest.kt new file mode 100644 index 00000000..05bf6749 --- /dev/null +++ b/sample/example-library/src/androidTest/kotlin/com/starter/easylauncher/screenshot/IconsTest.kt @@ -0,0 +1,19 @@ +package com.starter.easylauncher.screenshot + +import android.Manifest +import androidx.test.rule.GrantPermissionRule +import com.example.custom.adaptive.MainActivity +import com.starter.easylauncher.recordScreenshot +import org.junit.Rule +import org.junit.Test + +internal class IconsTest { + + @get:Rule + val grantPermission = GrantPermissionRule.grant(Manifest.permission.WRITE_EXTERNAL_STORAGE) + + @Test + fun doScreenshot() { + recordScreenshot("library") + } +} diff --git a/sample/example-library/src/main/AndroidManifest.xml b/sample/example-library/src/main/AndroidManifest.xml new file mode 100644 index 00000000..abdc260c --- /dev/null +++ b/sample/example-library/src/main/AndroidManifest.xml @@ -0,0 +1,12 @@ + + + + + + diff --git a/sample/example-library/src/main/res/drawable/ic_launcher_background.xml b/sample/example-library/src/main/res/drawable/ic_launcher_background.xml new file mode 100644 index 00000000..07d5da9c --- /dev/null +++ b/sample/example-library/src/main/res/drawable/ic_launcher_background.xml @@ -0,0 +1,170 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/sample/example-library/src/main/res/drawable/ic_launcher_foreground.xml b/sample/example-library/src/main/res/drawable/ic_launcher_foreground.xml new file mode 100644 index 00000000..b0cc9f9f --- /dev/null +++ b/sample/example-library/src/main/res/drawable/ic_launcher_foreground.xml @@ -0,0 +1,17 @@ + + + + + + diff --git a/sample/example-library/src/main/res/mipmap/ic_launcher.xml b/sample/example-library/src/main/res/mipmap/ic_launcher.xml new file mode 100644 index 00000000..6b78462d --- /dev/null +++ b/sample/example-library/src/main/res/mipmap/ic_launcher.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/sample/settings.gradle b/sample/settings.gradle index 82de1f7b..ed4f71e0 100644 --- a/sample/settings.gradle +++ b/sample/settings.gradle @@ -3,6 +3,7 @@ include ':example-custom' include ':example-vector' include ':example-scripted' include ':example-activity-alias' +include ':example-library' include ':adaptive-support' include ':screenshot-test-helpers'