Skip to content

Packaging prototype #5154

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 11 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions firebase-common/firebase-common.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

plugins {
id("firebase-library")
id("kotlin-android")
}

firebaseLibrary {
Expand Down Expand Up @@ -56,6 +57,8 @@ dependencies {
implementation(libs.androidx.futures)
implementation(libs.playservices.basement)
implementation(libs.playservices.tasks)
api(libs.kotlin.coroutines.tasks)
implementation(libs.kotlin.stdlib)

annotationProcessor(libs.autovalue)

Expand All @@ -73,6 +76,8 @@ dependencies {
testImplementation(libs.org.json)
testImplementation(libs.robolectric)
testImplementation(libs.truth)
testImplementation(libs.androidx.test.core)
testImplementation(libs.kotlin.coroutines.test)

androidTestImplementation(libs.androidx.test.junit)
androidTestImplementation(libs.androidx.test.runner)
Expand Down
2 changes: 0 additions & 2 deletions firebase-common/ktx/ktx.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,9 @@ dependencies {
implementation("com.google.firebase:firebase-annotations:16.2.0")
implementation(project(":firebase-common"))
implementation("com.google.firebase:firebase-components:17.1.0")
implementation(libs.androidx.annotation)

// We"re exposing this library as a transitive dependency so developers can
// get Kotlin Coroutines support out-of-the-box for methods that return a Task
api(libs.kotlin.coroutines.tasks)

testImplementation(libs.robolectric)
testImplementation(libs.junit)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,9 @@ import android.content.Context
import androidx.annotation.Keep
import com.google.firebase.FirebaseApp
import com.google.firebase.FirebaseOptions
import com.google.firebase.annotations.concurrent.Background
import com.google.firebase.annotations.concurrent.Blocking
import com.google.firebase.annotations.concurrent.Lightweight
import com.google.firebase.annotations.concurrent.UiThread
import com.google.firebase.components.Component
import com.google.firebase.components.ComponentRegistrar
import com.google.firebase.components.Dependency
import com.google.firebase.components.Qualified
import com.google.firebase.platforminfo.LibraryVersionComponent
import java.util.concurrent.Executor
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.asCoroutineDispatcher

/**
* Single access point to all firebase SDKs from Kotlin.
Expand Down Expand Up @@ -67,18 +58,6 @@ class FirebaseCommonKtxRegistrar : ComponentRegistrar {
override fun getComponents(): List<Component<*>> {
return listOf(
LibraryVersionComponent.create(LIBRARY_NAME, BuildConfig.VERSION_NAME),
coroutineDispatcher<Background>(),
coroutineDispatcher<Lightweight>(),
coroutineDispatcher<Blocking>(),
coroutineDispatcher<UiThread>()
)
}
}

private inline fun <reified T : Annotation> coroutineDispatcher(): Component<CoroutineDispatcher> =
Component.builder(Qualified.qualified(T::class.java, CoroutineDispatcher::class.java))
.add(Dependency.required(Qualified.qualified(T::class.java, Executor::class.java)))
.factory { c ->
c.get(Qualified.qualified(T::class.java, Executor::class.java)).asCoroutineDispatcher()
}
.build()
6 changes: 5 additions & 1 deletion firebase-common/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
android:name="com.google.firebase.components.ComponentDiscoveryService"
android:directBootAware="true"
android:exported="false"
tools:targetApi="n" />
tools:targetApi="n" >
<meta-data android:name="com.google.firebase.components:com.google.firebase.FirebaseCommonKtxRegistrar"
android:value="com.google.firebase.components.ComponentRegistrar" />
</service>

</application>
</manifest>
82 changes: 82 additions & 0 deletions firebase-common/src/main/java/com/google/firebase/Firebase.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.firebase

import android.content.Context
import androidx.annotation.Keep
import com.google.firebase.annotations.concurrent.Background
import com.google.firebase.annotations.concurrent.Blocking
import com.google.firebase.annotations.concurrent.Lightweight
import com.google.firebase.annotations.concurrent.UiThread
import com.google.firebase.components.Component
import com.google.firebase.components.ComponentRegistrar
import com.google.firebase.components.Dependency
import com.google.firebase.components.Qualified
import com.google.firebase.platforminfo.LibraryVersionComponent
import java.util.concurrent.Executor
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.asCoroutineDispatcher

/**
* Single access point to all firebase SDKs from Kotlin.
*
* <p>Acts as a target for extension methods provided by sdks.
*/
object Firebase

/** Returns the default firebase app instance. */
val Firebase.app: FirebaseApp
get() = FirebaseApp.getInstance()

/** Returns a named firebase app instance. */
fun Firebase.app(name: String): FirebaseApp = FirebaseApp.getInstance(name)

/** Initializes and returns a FirebaseApp. */
fun Firebase.initialize(context: Context): FirebaseApp? = FirebaseApp.initializeApp(context)

/** Initializes and returns a FirebaseApp. */
fun Firebase.initialize(context: Context, options: FirebaseOptions): FirebaseApp =
FirebaseApp.initializeApp(context, options)

/** Initializes and returns a FirebaseApp. */
fun Firebase.initialize(context: Context, options: FirebaseOptions, name: String): FirebaseApp =
FirebaseApp.initializeApp(context, options, name)

/** Returns options of default FirebaseApp */
val Firebase.options: FirebaseOptions
get() = Firebase.app.options

internal const val LIBRARY_NAME: String = "fire-core-ktx"

/** @suppress */
@Keep
class FirebaseCommonKtxRegistrar : ComponentRegistrar {
override fun getComponents(): List<Component<*>> {
return listOf(
LibraryVersionComponent.create(LIBRARY_NAME, BuildConfig.VERSION_NAME),
coroutineDispatcher<Background>(),
coroutineDispatcher<Lightweight>(),
coroutineDispatcher<Blocking>(),
coroutineDispatcher<UiThread>()
)
}
}

private inline fun <reified T : Annotation> coroutineDispatcher(): Component<CoroutineDispatcher> =
Component.builder(Qualified.qualified(T::class.java, CoroutineDispatcher::class.java))
.add(Dependency.required(Qualified.qualified(T::class.java, Executor::class.java)))
.factory { c ->
c.get(Qualified.qualified(T::class.java, Executor::class.java)).asCoroutineDispatcher()
}
.build()
140 changes: 140 additions & 0 deletions firebase-common/src/test/java/com/google/firebase/Tests.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.firebase

import androidx.test.core.app.ApplicationProvider
import com.google.android.gms.tasks.Tasks
import com.google.common.truth.Truth.assertThat
import com.google.firebase.platforminfo.UserAgentPublisher
import kotlinx.coroutines.tasks.await
import kotlinx.coroutines.test.runTest
import org.junit.Assert.fail
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner

fun withApp(name: String, block: FirebaseApp.() -> Unit) {
val app =
Firebase.initialize(
ApplicationProvider.getApplicationContext(),
FirebaseOptions.Builder().setApplicationId("appId").build(),
name
)
try {
block(app)
} finally {
app.delete()
}
}

class TestException(message: String) : Exception(message)

@RunWith(RobolectricTestRunner::class)
class VersionTests {
@Test
fun libraryVersions_shouldBeRegisteredWithRuntime() {
withApp("ktxTestApp") {
val uaPublisher = get(UserAgentPublisher::class.java)
assertThat(uaPublisher.userAgent).contains("kotlin")
assertThat(uaPublisher.userAgent).contains(LIBRARY_NAME)
}
}
}

@RunWith(RobolectricTestRunner::class)
class KtxTests {
@Test
fun `Firebase#app should delegate to FirebaseApp#getInstance()`() {
withApp(FirebaseApp.DEFAULT_APP_NAME) {
assertThat(Firebase.app).isSameInstanceAs(FirebaseApp.getInstance())
}
}

@Test
fun `Firebase#app(String) should delegate to FirebaseApp#getInstance(String)`() {
val appName = "testApp"
withApp(appName) {
assertThat(Firebase.app(appName)).isSameInstanceAs(FirebaseApp.getInstance(appName))
}
}

@Test
fun `Firebase#options should delegate to FirebaseApp#getInstance()#options`() {
withApp(FirebaseApp.DEFAULT_APP_NAME) {
assertThat(Firebase.options).isSameInstanceAs(FirebaseApp.getInstance().options)
}
}

@Test
fun `Firebase#initialize(Context, FirebaseOptions) should initialize the app correctly`() {
val options = FirebaseOptions.Builder().setApplicationId("appId").build()
val app = Firebase.initialize(ApplicationProvider.getApplicationContext(), options)
try {
assertThat(app).isNotNull()
assertThat(app.name).isEqualTo(FirebaseApp.DEFAULT_APP_NAME)
assertThat(app.options).isSameInstanceAs(options)
assertThat(app.applicationContext)
.isSameInstanceAs(ApplicationProvider.getApplicationContext())
} finally {
app.delete()
}
}

@Test
fun `Firebase#initialize(Context, FirebaseOptions, String) should initialize the app correctly`() {
val options = FirebaseOptions.Builder().setApplicationId("appId").build()
val name = "appName"
val app = Firebase.initialize(ApplicationProvider.getApplicationContext(), options, name)
try {
assertThat(app).isNotNull()
assertThat(app.name).isEqualTo(name)
assertThat(app.options).isSameInstanceAs(options)
assertThat(app.applicationContext)
.isSameInstanceAs(ApplicationProvider.getApplicationContext())
} finally {
app.delete()
}
}
}

class CoroutinesPlayServicesTests {
// We are only interested in the await() function offered by kotlinx-coroutines-play-services
// So we're not testing the other functions provided by that library.

@Test
fun `Task#await() resolves to the same result as Task#getResult()`() = runTest {
val task = Tasks.forResult(21)

val expected = task.result
val actual = task.await()

assertThat(actual).isEqualTo(expected)
assertThat(task.isSuccessful).isTrue()
assertThat(task.exception).isNull()
}

@Test
fun `Task#await() throws an Exception for failing Tasks`() = runTest {
val task = Tasks.forException<TestException>(TestException("some error happened"))

try {
task.await()
fail("Task#await should throw an Exception")
} catch (e: Exception) {
assertThat(e).isInstanceOf(TestException::class.java)
assertThat(task.isSuccessful).isFalse()
}
}
}
3 changes: 2 additions & 1 deletion firebase-functions/firebase-functions.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
plugins {
id("firebase-library")
id("firebase-vendor")
id("kotlin-android")
}

firebaseLibrary {
Expand Down Expand Up @@ -46,7 +47,7 @@ android {

dependencies {
implementation("com.google.firebase:firebase-annotations:16.2.0")
implementation("com.google.firebase:firebase-common:20.3.1")
implementation(project(":firebase-common"))
implementation("com.google.firebase:firebase-components:17.1.0")
implementation(project(":appcheck:firebase-appcheck-interop"))
implementation(libs.playservices.base)
Expand Down
4 changes: 2 additions & 2 deletions firebase-functions/ktx/ktx.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ android {
}

dependencies {
implementation("com.google.firebase:firebase-common:20.3.1")
implementation(project(":firebase-common"))
implementation("com.google.firebase:firebase-components:17.1.0")
implementation("com.google.firebase:firebase-common-ktx:20.3.1")
implementation(project(":firebase-common:ktx"))
implementation(project(":firebase-functions"))
implementation(libs.kotlin.stdlib)
implementation(libs.androidx.annotation)
Expand Down
Loading