Skip to content

Instantly share code, notes, and snippets.

View realdadfish's full-sized avatar

Thomas Keller realdadfish

View GitHub Profile
@realdadfish
realdadfish / build.gradle
Last active January 27, 2020 08:22
JVM Integration Tests with the Android Gradle Plugin. ATTENTION: Does not work!
android.applicationVariants.all { variant ->
configurations.create("${variant.name}IntegrationTestImplementation") {
extendsFrom configurations.find { it.name == "implementation" }
extendsFrom configurations.find { it.name == "${variant.name}Implementation" }
}
def sourceSet = android.sourceSets.create("${variant.name}IntegrationTest") {
// this fails here because `compileClasspath` is not existant in `AndroidSourceSet`
compileClasspath += variant.javaCompile.classpath
runtimeClasspath += variant.javaCompile.classpath
@realdadfish
realdadfish / FragmentExt.kt
Last active February 13, 2020 08:59
getOrAddFragment
fun <T : Fragment> FragmentActivity.getOrAddFragment(tag: String, commitNow: Boolean = true, initFun: () -> T): T =
supportFragmentManager.findFragmentByTag(tag) as? T ?: run {
initFun().also {
addFragment(supportFragmentManager, tag, commitNow, it)
}
}
fun <T : Fragment> Fragment.getOrAddFragment(tag: String, commitNow: Boolean = true, initFun: () -> T): T =
childFragmentManager.findFragmentByTag(tag) as? T ?: run {
initFun().also {
@realdadfish
realdadfish / StubbedObserver.kt
Last active March 26, 2020 11:19
Test Utilities
val singleParamUseCase = ...
// single execution (param is stubbed as "any()")
singleParamUseCase.stubObserver {
onSuccess("yay")
}
viewModel.triggerSomething()
singleParamUseCase.execute("some-param")
@realdadfish
realdadfish / detekt.yml
Created March 30, 2020 21:17
Detekt configuration
#
# This file configures the static code analysis done by Detekt (https://arturbosch.github.io/detekt/index.html)
#
# Derivations from the defaults (https://github.com/arturbosch/detekt/blob/master/detekt-cli/src/main/resources/default-detekt-config.yml)
# are not listed here, unless we specifically voted on a rule to be used / not used.
#
# If adaptions to a default rule are made, the _complete_ configuration of that rule should be included here, not
# only the part that is actually overridden. This is important to see the rule in context.
#
# If you want to introduce changes to existing rules and / or want to introduce new rules, please use the
@realdadfish
realdadfish / Crash.txt
Created April 3, 2020 15:00
Material Components
java.lang.NoSuchMethodError: com.google.android.material.shape.MaterialShapeDrawable.<init>(Landroid/content/Context;Landroid/util/AttributeSet;II)V
at com.google.android.material.dialog.MaterialAlertDialogBuilder.<init>(MaterialAlertDialogBuilder.java:121)
at com.google.android.material.dialog.MaterialAlertDialogBuilder.<init>(MaterialAlertDialogBuilder.java:103)
at our.code.onCreate(OurFragment.kt:112)
at androidx.fragment.app.Fragment.performCreate(Fragment.java:2684)
at androidx.fragment.app.FragmentStateManager.create(FragmentStateManager.java:280)
at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1175)
at androidx.fragment.app.FragmentTransition.addToFirstInLastOut(FragmentTransition.java:1255)
at androidx.fragment.app.FragmentTransition.calculateFragments(FragmentTransition.java:1138)
at androidx.fragment.app.FragmentTransition.startTransitions(FragmentTransition.java:136)
@realdadfish
realdadfish / MyCodemarkerPlugin.kt
Last active May 29, 2020 21:53
Kotlin Gradle Plugin that shows how the generation and consumption of artifacts between different Gradle projects works
open class MyCodemarkerPlugin : Plugin<Project> {
override fun apply(project: Project) {
val androidExtension = project.extensions.findByType(BaseExtension::class.java)
if (androidExtension != null) {
project.createCodemarkerTask {
sources.from(project.files(androidExtension.sourceSets.map {
it.java.srcDirs + it.assets.srcDirs + it.res.srcDirs + it.manifest.srcFile
}))
}
return
@realdadfish
realdadfish / CpdPlugin.kt
Last active July 18, 2020 21:55
PMD CPD Kotlin Plugin
import com.android.build.gradle.BaseExtension
import com.android.build.gradle.api.BaseVariant
import com.android.build.gradle.internal.tasks.factory.dependsOn
import de.aaschmid.gradle.plugins.cpd.Cpd
import de.aaschmid.gradle.plugins.cpd.CpdExtension
import org.gradle.api.GradleException
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.internal.HasConvention
import org.gradle.api.plugins.JavaPluginConvention
@realdadfish
realdadfish / build.gradle.kts
Last active February 27, 2023 07:22
Unmatched attributes issue and solution
val configuration = project.configurations.getByName("prodConsumerReleaseRuntimeClasspath")
val artifactType = Attribute.of("artifactType", String::class.java)
// Some plugin can only work with configurations, while the Android Gradle Plugin (AGP) has the newer "artifact view"
// paradigm implemented. This makes it impossible to resolve most of the created, variant-aware
// configurations from AGP "by hand" without getting unmatched attribute exceptions.
// We now pick one artifact that holds our dependencies and add a custom compatibility rule
// for it which basically accepts all incoming compatibility issues as long as the produced value
// on "the other side" is a JAR or AAR artifact.
configuration.attributes {
attribute(artifactType, "android-classes-directory")
@realdadfish
realdadfish / RetrofitBug.kt
Created August 24, 2020 10:45
Retrofit Bug
import io.reactivex.rxjava3.core.Completable
import okhttp3.Interceptor
import okhttp3.Response
import okhttp3.mockwebserver.MockWebServer
import org.junit.Rule
import org.junit.Test
import retrofit2.Retrofit
import retrofit2.adapter.rxjava3.RxJava3CallAdapterFactory
import retrofit2.http.POST
@realdadfish
realdadfish / gradle-4.7-with-remote-buildcache.log
Last active November 4, 2020 15:51
Slow buildSrc compilation Gradle 4.7 vs 4.8-milestone-1
[15:46:41] Welcome to Gradle 6.7!
[15:46:41]
[15:46:41] Here are the highlights of this release:
[15:46:41] - File system watching is ready for production use
[15:46:41] - Declare the version of Java your build requires
[15:46:41] - Java 15 support
[15:46:41]
[15:46:41] For more details see https://docs.gradle.org/6.7/release-notes.html
[15:46:41]
[15:46:41] To honour the JVM settings for this build a new JVM will be forked. Please consider using the daemon: https://docs.gradle.org/6.7/userguide/gradle_daemon.html.