Skip to content

Add runtime kotlin version detection for platform info purposes. #693

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

Merged
merged 3 commits into from
Aug 8, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions firebase-common/firebase-common.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ dependencies {
api 'com.google.auto.value:auto-value-annotations:1.6.5'
compileOnly 'com.google.code.findbugs:jsr305:3.0.2'

// needed for Kotlin detection to compile, but not necessarily present at runtime.
compileOnly "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlinVersion"

testImplementation 'androidx.test:runner:1.2.0'
testImplementation 'androidx.test.ext:junit:1.1.1'
testImplementation "org.robolectric:robolectric:$robolectricVersion"
Expand Down
1 change: 0 additions & 1 deletion firebase-common/ktx/ktx.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ android {
minSdkVersion project.minSdkVersion
targetSdkVersion project.targetSdkVersion
versionName version
buildConfigField('String', 'KOTLIN_VERSION', "\"$kotlinVersion\"")
}
sourceSets {
main.java.srcDirs += 'src/main/kotlin'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ internal const val LIBRARY_NAME: String = "fire-core-ktx"
class FirebaseCommonKtxRegistrar : ComponentRegistrar {
override fun getComponents(): List<Component<*>> {
return listOf(
LibraryVersionComponent.create(LIBRARY_NAME, BuildConfig.VERSION_NAME),
LibraryVersionComponent.create("kotlin", BuildConfig.KOTLIN_VERSION))
LibraryVersionComponent.create(LIBRARY_NAME, BuildConfig.VERSION_NAME))
}
}
1 change: 1 addition & 0 deletions firebase-common/proguard.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
-dontwarn com.google.firebase.components.Component$Instantiation
-dontwarn com.google.firebase.components.Component$ComponentType
-dontwarn com.google.firebase.platforminfo.KotlinDetector
-keep class * implements com.google.firebase.components.ComponentRegistrar
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import com.google.firebase.events.Publisher;
import com.google.firebase.internal.DataCollectionConfigStorage;
import com.google.firebase.platforminfo.DefaultUserAgentPublisher;
import com.google.firebase.platforminfo.KotlinDetector;
import com.google.firebase.platforminfo.LibraryVersionComponent;
import java.nio.charset.Charset;
import java.util.ArrayList;
Expand Down Expand Up @@ -100,6 +101,7 @@ public class FirebaseApp {

private static final String FIREBASE_ANDROID = "fire-android";
private static final String FIREBASE_COMMON = "fire-core";
private static final String KOTLIN = "kotlin";

private final Context applicationContext;
private final String name;
Expand Down Expand Up @@ -397,6 +399,8 @@ protected FirebaseApp(Context applicationContext, String name, FirebaseOptions o

List<ComponentRegistrar> registrars =
ComponentDiscovery.forContext(applicationContext).discover();

String kotlinVersion = KotlinDetector.detectVersion();
componentRuntime =
new ComponentRuntime(
UI_EXECUTOR,
Expand All @@ -406,7 +410,9 @@ protected FirebaseApp(Context applicationContext, String name, FirebaseOptions o
Component.of(options, FirebaseOptions.class),
LibraryVersionComponent.create(FIREBASE_ANDROID, ""),
LibraryVersionComponent.create(FIREBASE_COMMON, BuildConfig.VERSION_NAME),
kotlinVersion != null ? LibraryVersionComponent.create(KOTLIN, kotlinVersion) : null,
DefaultUserAgentPublisher.component());

dataCollectionConfigStorage =
new Lazy<>(
() ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,11 @@ public ComponentRuntime(
for (ComponentRegistrar registrar : registrars) {
componentsToAdd.addAll(registrar.getComponents());
}
Collections.addAll(componentsToAdd, additionalComponents);
for (Component<?> additionalComponent : additionalComponents) {
if (additionalComponent != null) {
componentsToAdd.add(additionalComponent);
}
}

CycleDetector.detect(componentsToAdd);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// 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.platforminfo;

import androidx.annotation.Nullable;

/**
* Detects presence of Kotlin stdlib on the classpath.
*
* <p>If it is present, it is inferred that the application or its subset is written in Kotlin.
*/
public final class KotlinDetector {
private KotlinDetector() {}

/** Returns the version of Kotlin stdlib if found, {@code null} otherwise. */
@Nullable
public static String detectVersion() {
try {
return kotlin.KotlinVersion.CURRENT.toString();
} catch (NoClassDefFoundError ex) {
return null;
}
}
}