Skip to content
This repository has been archived by the owner on Nov 12, 2024. It is now read-only.

Commit

Permalink
Use gmazzo/gradle-buildconfig-plugin
Browse files Browse the repository at this point in the history
This allows us to move inject build config into respective
modules. We use build config for injecting API keys, so we
can stop using the AGP functionality in `:android-app` as a
proxy.
  • Loading branch information
chrisbanes committed Jun 29, 2023
1 parent 7c90caf commit 9bd3a5d
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 24 deletions.
8 changes: 0 additions & 8 deletions android-app/app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -81,21 +81,13 @@ android {
signingConfig = signingConfigs["debug"]
versionNameSuffix = "-dev"
applicationIdSuffix = ".debug"

buildConfigField("String", "TRAKT_CLIENT_ID", "\"" + propOrDef("TIVI_DEBUG_TRAKT_CLIENT_ID", "TIVI_TRAKT_CLIENT_ID", "") + "\"")
buildConfigField("String", "TRAKT_CLIENT_SECRET", "\"" + propOrDef("TIVI_DEBUG_TRAKT_CLIENT_SECRET", "TIVI_TRAKT_CLIENT_SECRET", "") + "\"")
buildConfigField("String", "TMDB_API_KEY", "\"" + propOrDef("TIVI_DEBUG_TMDB_API_KEY", "TIVI_TMDB_API_KEY", "") + "\"")
}

release {
signingConfig = signingConfigs[if (useReleaseKeystore) "release" else "debug"]
isShrinkResources = true
isMinifyEnabled = true
proguardFiles("proguard-rules.pro")

buildConfigField("String", "TRAKT_CLIENT_ID", "\"" + propOrDef("TIVI_TRAKT_CLIENT_ID", "") + "\"")
buildConfigField("String", "TRAKT_CLIENT_SECRET", "\"" + propOrDef("TIVI_TRAKT_CLIENT_SECRET", "") + "\"")
buildConfigField("String", "TMDB_API_KEY", "\"" + propOrDef("TIVI_TMDB_API_KEY", "") + "\"")
}

create("benchmark") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@ import app.tivi.appinitializers.AppInitializer
import app.tivi.appinitializers.AppInitializers
import app.tivi.appinitializers.EmojiInitializer
import app.tivi.common.imageloading.ImageLoadingComponent
import app.tivi.data.traktauth.TraktOAuthInfo
import app.tivi.home.ContentViewSetterComponent
import app.tivi.tasks.TiviWorkerFactory
import app.tivi.tmdb.TmdbOAuthInfo
import java.io.File
import java.util.concurrent.TimeUnit
import me.tatarka.inject.annotations.Component
Expand Down Expand Up @@ -52,20 +50,6 @@ abstract class AndroidApplicationComponent(
},
)

@ApplicationScope
@Provides
fun provideTmdbApiKey(): TmdbOAuthInfo = TmdbOAuthInfo(BuildConfig.TMDB_API_KEY)

@ApplicationScope
@Provides
fun provideTraktOAuthInfo(
appInfo: ApplicationInfo,
): TraktOAuthInfo = TraktOAuthInfo(
clientId = BuildConfig.TRAKT_CLIENT_ID,
clientSecret = BuildConfig.TRAKT_CLIENT_SECRET,
redirectUri = "${appInfo.packageName}://auth/oauth2callback",
)

@Provides
@IntoSet
fun provideEmojiInitializer(bind: EmojiInitializer): AppInitializer = bind
Expand Down
13 changes: 13 additions & 0 deletions api/tmdb/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@

plugins {
id("app.tivi.kotlin.multiplatform")
alias(libs.plugins.buildConfig)
}

buildConfig {
packageName("app.tivi.tmdb")

buildConfigField("String", "TMDB_DEBUG_API_KEY", "\"${propOrDef("TIVI_DEBUG_TMDB_API_KEY", "")}\"")
buildConfigField("String", "TMDB_API_KEY", "\"${propOrDef("TIVI_TMDB_API_KEY", "")}\"")
}

kotlin {
Expand Down Expand Up @@ -35,3 +43,8 @@ kotlin {
}
}
}

fun <T : Any> propOrDef(propertyName: String, defaultValue: T): T {
@Suppress("UNCHECKED_CAST")
return project.properties[propertyName] as T? ?: defaultValue
}
15 changes: 15 additions & 0 deletions api/tmdb/src/commonMain/kotlin/app/tivi/tmdb/TmdbComponent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

package app.tivi.tmdb

import app.tivi.app.ApplicationInfo
import app.tivi.inject.ApplicationScope
import me.tatarka.inject.annotations.Provides

Expand All @@ -11,6 +12,20 @@ interface TmdbComponent : TmdbCommonComponent, TmdbPlatformComponent
expect interface TmdbPlatformComponent

interface TmdbCommonComponent {
@ApplicationScope
@Provides
fun provideTmdbApiKey(
appInfo: ApplicationInfo,
): TmdbOAuthInfo = TmdbOAuthInfo(
apiKey = when {
appInfo.debugBuild -> {
BuildConfig.TMDB_DEBUG_API_KEY.ifEmpty { BuildConfig.TMDB_API_KEY }
}

else -> BuildConfig.TMDB_API_KEY
},
)

@ApplicationScope
@Provides
fun provideTmdbImageUrlProvider(tmdbManager: TmdbManager): TmdbImageUrlProvider {
Expand Down
15 changes: 15 additions & 0 deletions api/trakt/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@

plugins {
id("app.tivi.kotlin.multiplatform")
alias(libs.plugins.buildConfig)
}

buildConfig {
packageName("app.tivi.trakt")

buildConfigField("String", "TRAKT_DEBUG_CLIENT_SECRET", "\"${propOrDef("TIVI_DEBUG_TRAKT_CLIENT_SECRET", "")}\"")
buildConfigField("String", "TRAKT_DEBUG_CLIENT_ID", "\"${propOrDef("TIVI_DEBUG_TRAKT_CLIENT_ID", "")}\"")
buildConfigField("String", "TRAKT_CLIENT_SECRET", "\"${propOrDef("TIVI_TRAKT_CLIENT_SECRET", "")}\"")
buildConfigField("String", "TRAKT_CLIENT_ID", "\"${propOrDef("TIVI_TRAKT_CLIENT_ID", "")}\"")
}

kotlin {
Expand Down Expand Up @@ -38,3 +48,8 @@ kotlin {
}
}
}

fun <T : Any> propOrDef(propertyName: String, defaultValue: T): T {
@Suppress("UNCHECKED_CAST")
return project.properties[propertyName] as T? ?: defaultValue
}
27 changes: 27 additions & 0 deletions api/trakt/src/commonMain/kotlin/app/tivi/trakt/TraktComponent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,40 @@ import app.moviebase.trakt.api.TraktSeasonsApi
import app.moviebase.trakt.api.TraktShowsApi
import app.moviebase.trakt.api.TraktSyncApi
import app.moviebase.trakt.api.TraktUsersApi
import app.tivi.app.ApplicationInfo
import app.tivi.data.traktauth.TraktOAuthInfo
import app.tivi.inject.ApplicationScope
import me.tatarka.inject.annotations.Provides

interface TraktComponent : TraktCommonComponent, TraktPlatformComponent

expect interface TraktPlatformComponent

interface TraktCommonComponent {

@ApplicationScope
@Provides
fun provideTraktOAuthInfo(
appInfo: ApplicationInfo,
): TraktOAuthInfo = TraktOAuthInfo(
clientId = when {
appInfo.debugBuild -> {
BuildConfig.TRAKT_DEBUG_CLIENT_ID.ifEmpty { BuildConfig.TRAKT_CLIENT_ID }
}

else -> BuildConfig.TRAKT_CLIENT_ID
},
clientSecret = when {
appInfo.debugBuild -> {
BuildConfig.TRAKT_DEBUG_CLIENT_SECRET
.ifEmpty { BuildConfig.TRAKT_CLIENT_SECRET }
}

else -> BuildConfig.TRAKT_CLIENT_SECRET
},
redirectUri = "${appInfo.packageName}://auth/oauth2callback",
)

@Provides
fun provideTraktUsersService(trakt: Trakt): TraktUsersApi = trakt.users

Expand Down
1 change: 1 addition & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ kapt.include.compile.classpath=false
# Disable buildFeatures flags by default
android.defaults.buildfeatures.resvalues=false
android.defaults.buildfeatures.shaders=false
android.defaults.buildFeatures.buildConfig=false

kotlin.mpp.androidSourceSetLayoutVersion=2
kotlin.mpp.androidGradlePluginCompatibility.nowarn=true
1 change: 1 addition & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ android-application = { id = "com.android.application", version.ref = "agp" }
android-library = { id = "com.android.library", version.ref = "agp" }
android-lint = { id = "com.android.lint", version.ref = "agp" }
android-test = { id = "com.android.test", version.ref = "agp" }
buildConfig = "com.github.gmazzo.buildconfig:4.1.1"
cacheFixPlugin = { id = "org.gradle.android.cache-fix", version = "2.7.2" }
kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
kotlin-parcelize = { id = "org.jetbrains.kotlin.plugin.parcelize", version.ref = "kotlin" }
Expand Down

0 comments on commit 9bd3a5d

Please sign in to comment.