main

square/leakcanary

Last updated at: 29/12/2023 09:40

ProguardMappingHelper.kt

TLDR

The ProguardMappingHelper.kt file contains a Kotlin class ProguardMappingHelper that provides helper methods for generating proguard mapping. It also includes extension functions and an inner class to support the functionality.

Methods

clazz

This method creates a new instance of the inner class Class and adds the given class name mapping to the proguard mapping. It also provides a callback fieldsBlock to customize the field mappings for the class.

Class.field

This method is an extension function of the inner class Class. It adds a field mapping to the fieldMappings set.

ProguardMapping.create

This method is an extension function of the ProguardMapping class. It creates a new instance of ProguardMappingHelper and applies the given block function to it. Finally, it returns the modified ProguardMapping instance.

Classes

Class ProguardMappingHelper

This class provides helper methods for generating proguard mapping. It takes a ProguardMapping instance as a parameter and provides methods to define class and field mappings.

Inner Class Class

This inner class represents a class in the proguard mapping. It contains a pair of class name mappings and a set of field mappings.

package shark

class ProguardMappingHelper(
  private val proguardMapping: ProguardMapping
) {
  fun clazz(
    className: Pair<String, String>,
    fieldsBlock: Class.() -> Unit = {}
  ) {
    val clazz = Class(className)
    fieldsBlock(clazz)
    proguardMapping.addMapping(clazz.nameMapping.second, clazz.nameMapping.first)
    clazz.fieldMappings.forEach { field ->
      proguardMapping.addMapping("${clazz.nameMapping.second}.${field.second}", field.first)
    }
  }

  inner class Class(val nameMapping: Pair<String, String>) {
    val fieldMappings = mutableSetOf<Pair<String, String>>()
  }

  fun Class.field(block: () -> Pair<String, String>) {
    fieldMappings.add(block())
  }
}

fun ProguardMapping.create(block: ProguardMappingHelper.() -> Unit): ProguardMapping {
  block(ProguardMappingHelper(this))
  return this
}