Skip to content

Android Usage

wiki de pasquale edited this page Aug 17, 2022 · 4 revisions

Your project needs to contain multiple source sets. To be precise you need main, debug and release.

main source set

Create a class named InterceptorFactory and paste this to the newly created class:

abstract class InterceptorFactory {
    abstract fun create(): Interceptor
}

debug source set

  1. Switch your project to the debug variant.
  2. Click on "Android" in the top left corner of the Project tab.
  3. Switch to "Project".
  4. Navigate to your module where you've just created the InterceptorFactory.
  5. Right click on the module and select "New > Directory" and enter src/debug/java/com/your/package (ex. src/debug/java/gecko/ui).
  6. Right click on the module and select "New > Directory" and enter src/release/java/com/your/package (ex. src/debug/java/gecko/ui).
  7. Right click on the newly bottom-most directory created in debug source set and create a new class InterceptorFactoryImpl
  8. Paste following into the newly created file
class InterceptorFactoryDefault : InterceptorFactory() {

    override fun create(): Interceptor {
        return GeckoInterceptor()
    }

}

release source set

Please follow debug source set's step 6)

  1. Switch your active variant to release
  2. Switch to "Project" (as per step 3) in debug)
  3. Navigate to the release source set and create a new class InterceptorFactoryImpl in the same exact package (!important!)
  4. Paste following into the newly created file
class InterceptorFactoryDefault : InterceptorFactory() {

    override fun create(): Interceptor {
        return Interceptor { it.proceed(it.request()) }
    }

}

Back to main source set

Locate the OkHttp builder and add Gecko as the last interceptor:

OkHttpClient.Builder()
    .addInterceptor(RandomInterceptor())
    .addInterceptor(AnotherInterceptor())
    .addInterceptor(YetAnotherInterceptor())
    // more interceptors here
    .addInterceptor(InterceptorFactoryDefault().create()) // <--
    .build()

Test!

Run ./gradlew assemble on your project and observe if any errors occur. Nothing? Nice! You're now all set up.

What did you just do?

You managed to successfully implement Gecko as a Debug only library. There will be absolutely no trace of Gecko in your release builds.

Clone this wiki locally