Skip to content

Instantly share code, notes, and snippets.

View autonomousapps's full-sized avatar

Tony Robalik autonomousapps

View GitHub Profile
@autonomousapps
autonomousapps / PluginTest.kt
Created November 16, 2023 18:43
Example usage of gradle-testkit-support
/**
* An example usage of https://github.com/autonomousapps/dependency-analysis-gradle-plugin/tree/main/testkit.
* This uses JUnit5 as the test framework. The test is paramaterized as well. The custom task `printVersions`,
* in the test fixture, unzips the jar and prints the contents of a resource file in that jar. The resource file
* is generated by "my-plugin", which is an internal plugin that bundles the runtime classpath artifact names
* (group:artifact:version) into the final jar built by the application.
*/
internal class PluginTest {
private companion object {
@autonomousapps
autonomousapps / AndroidApks.kt
Last active April 22, 2022 23:18
Provider for single APK using latest AGP APIs
import org.gradle.api.file.Directory
import org.gradle.api.provider.Property
import org.gradle.api.provider.Provider
import org.gradle.api.tasks.InputFiles
import java.io.File
import javax.inject.Inject
/**
* This essentially encapsulates the pattern described in
* [ExamplePlugin in AGP's "Gradle Recipes" repo](https://github.com/android/gradle-recipes/blob/agp-7.0/BuildSrc/getApksTest/buildSrc/src/main/kotlin/ExamplePlugin.kt#L45-L48).
@autonomousapps
autonomousapps / ResolveDependenciesPlugin.kt
Created March 16, 2022 20:13
Plugin for resolving dependencies in a task action, to help with pre-populating a docker image
class ResolveDependenciesPlugin : Plugin<Project> {
override fun apply(target: Project): Unit = target.run {
pluginManager.withPlugin("java") {
registerTask(theJars = artifactFilesProvider("compileClasspath", "jar"))
}
pluginManager.withPlugin("com.android.base") {
// This lets us call `./gradlew resolveDependencies` and have it Just Work.
val lifecycleTask = tasks.register(RESOLVE_DEPENDENCIES_TASK_NAME)
alias.lg log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --date=relative
alias.last log -1 HEAD
alias.unstage reset HEAD --
alias.amend commit --amend --no-edit
alias.co checkout
alias.alias config --get-regexp ^alias\.
alias.st status
alias.up push --force-with-lease
alias.aa !git add . && git commit --amend --no-edit
alias.fix !git add . && git commit --amend --no-edit && git push --force-with-lease
@autonomousapps
autonomousapps / build.gradle
Created January 12, 2021 22:32
Print Gradle's class loader hierarchy
// app/build.gradle
def printClassLoaders(classLoader) {
while (classLoader != null) {
println(classLoader)
classLoader = classLoader.parent
}
}
class A {
}
@autonomousapps
autonomousapps / AndroidUnitTest.java
Created December 22, 2020 19:23
buildSrc/src/main/java/com/android/build/gradle/tasks/factory/AndroidUnitTest.java
//buildSrc/src/main/java/com/android/build/gradle/tasks/factory/AndroidUnitTest.java
package com.android.build.gradle.tasks.factory;
import com.android.annotations.NonNull;
import com.android.annotations.Nullable;
import com.android.build.api.artifact.impl.ArtifactsImpl;
import com.android.build.api.component.TestComponentProperties;
import com.android.build.api.component.impl.ComponentPropertiesImpl;
import com.android.build.api.component.impl.UnitTestPropertiesImpl;
import com.android.build.api.variant.impl.VariantPropertiesImpl;
@autonomousapps
autonomousapps / gist:fc5aff10403b5a07f34391fcfd88945d
Created November 5, 2020 19:24
jar tf simple-grammar-1.0-SNAPSHOT.jar
META-INF/
META-INF/MANIFEST.MF
com/
com/autonomousapps/
com/autonomousapps/internal/
com/autonomousapps/internal/grammar/
com/autonomousapps/internal/grammar/KotlinParser$ExclContext.class
com/autonomousapps/internal/grammar/KotlinParser$LambdaParametersContext.class
com/autonomousapps/internal/grammar/KotlinParser$FunctionBodyContext.class
com/autonomousapps/internal/grammar/KotlinParser$MultiLineStringLiteralContext.class
https://scans.gradle.com/s/h3lttvotbd7ve
failure:
```
Could not resolve all files for configuration ':app:labDebugAndroidTestCompileClasspath'.
> Failed to transform tguard.jar (project :tguard) to match attributes {artifactType=android-classes-jar, org.gradle.category=library, org.gradle.dependency.bundling=external, org.gradle.jvm.version=8, org.gradle.libraryelements=jar, org.gradle.usage=java-api, org.jetbrains.kotlin.localToProject=public, org.jetbrains.kotlin.platform.type=jvm}.
> Execution failed for IdentityTransform: /Users/trobalik/workspace/hiya/aegis-android/tguard/build/libs/tguard.jar.
> Expecting a file or a directory: /Users/trobalik/workspace/hiya/aegis-android/tguard/build/libs/tguard.jar
```
@autonomousapps
autonomousapps / pub.gradle.kts
Created August 28, 2020 18:11
Publishing android libraries -- the "all" component
import com.android.build.gradle.LibraryExtension
plugins {
`maven-publish`
}
var sourcesJar: TaskProvider<Jar>? = null
pluginManager.withPlugin("com.android.library") {
// Task for generating the source jar so one can attach the source when debugging in other
// projects. Added to debug publication automatically. The 'android' (LibraryExtension) extension
@autonomousapps
autonomousapps / gist:209dee9be7e3a3c9de9611b811443e68
Last active August 20, 2020 17:57
Using a custom Gradle distribution
# How do I package a custom Gradle distribution with init scripts?
I want to be able to configure Gradle Enterprise, build scans, and the cache, all from one centralized location.
# Answer
In order to customize your Gradle distribution, you basically have to add one or more init scripts to the `init.d/` directory in the distribution `.zip` file. That's talked about here:
https://docs.gradle.org/current/userguide/init_scripts.html#sec:using_an_init_script
> Put a file that ends with `.gradle` (or `.init.gradle.kts` for Kotlin) in the `GRADLE_HOME/init.d/` directory, in the Gradle distribution. This allows you to package up a custom Gradle distribution containing some custom build logic and plugins. You can combine this with the Gradle wrapper as a way to make custom logic available to all builds in your enterprise.
The actual script would look something like **Example 9** from https://docs.gradle.org/current/userguide/build_cache.html#sec:build_cache_configure_remote