Skip to content

Commit e58f07c

Browse files
authored
Add runtime kotlin version detection for platform info purposes. (#693)
* Add runtime kotlin version detection for platform info purposes. * Encapsulate Kotlin detection into a dedicated class. This is done to minimize the scope of `-dontwarn` in the consumer proguard spec which is required for proguard to succeed in developers' apps. Note: the `-dontwarn` does not seem to be required when building the app with R8 instead of proguard. * Rename kotlin lib name.
1 parent f096bbe commit e58f07c

File tree

7 files changed

+52
-4
lines changed

7 files changed

+52
-4
lines changed

firebase-common/firebase-common.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ dependencies {
6464
api 'com.google.auto.value:auto-value-annotations:1.6.5'
6565
compileOnly 'com.google.code.findbugs:jsr305:3.0.2'
6666

67+
// needed for Kotlin detection to compile, but not necessarily present at runtime.
68+
compileOnly "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlinVersion"
69+
6770
testImplementation 'androidx.test:runner:1.2.0'
6871
testImplementation 'androidx.test.ext:junit:1.1.1'
6972
testImplementation "org.robolectric:robolectric:$robolectricVersion"

firebase-common/ktx/ktx.gradle

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ android {
2727
minSdkVersion project.minSdkVersion
2828
targetSdkVersion project.targetSdkVersion
2929
versionName version
30-
buildConfigField('String', 'KOTLIN_VERSION', "\"$kotlinVersion\"")
3130
}
3231
sourceSets {
3332
main.java.srcDirs += 'src/main/kotlin'

firebase-common/ktx/src/main/kotlin/com/google/firebase/ktx/Firebase.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ internal const val LIBRARY_NAME: String = "fire-core-ktx"
5757
class FirebaseCommonKtxRegistrar : ComponentRegistrar {
5858
override fun getComponents(): List<Component<*>> {
5959
return listOf(
60-
LibraryVersionComponent.create(LIBRARY_NAME, BuildConfig.VERSION_NAME),
61-
LibraryVersionComponent.create("kotlin", BuildConfig.KOTLIN_VERSION))
60+
LibraryVersionComponent.create(LIBRARY_NAME, BuildConfig.VERSION_NAME))
6261
}
6362
}

firebase-common/proguard.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
-dontwarn com.google.firebase.components.Component$Instantiation
22
-dontwarn com.google.firebase.components.Component$ComponentType
3+
-dontwarn com.google.firebase.platforminfo.KotlinDetector
34
-keep class * implements com.google.firebase.components.ComponentRegistrar

firebase-common/src/main/java/com/google/firebase/FirebaseApp.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import com.google.firebase.events.Publisher;
4747
import com.google.firebase.internal.DataCollectionConfigStorage;
4848
import com.google.firebase.platforminfo.DefaultUserAgentPublisher;
49+
import com.google.firebase.platforminfo.KotlinDetector;
4950
import com.google.firebase.platforminfo.LibraryVersionComponent;
5051
import java.nio.charset.Charset;
5152
import java.util.ArrayList;
@@ -100,6 +101,7 @@ public class FirebaseApp {
100101

101102
private static final String FIREBASE_ANDROID = "fire-android";
102103
private static final String FIREBASE_COMMON = "fire-core";
104+
private static final String KOTLIN = "kotlin";
103105

104106
private final Context applicationContext;
105107
private final String name;
@@ -397,6 +399,8 @@ protected FirebaseApp(Context applicationContext, String name, FirebaseOptions o
397399

398400
List<ComponentRegistrar> registrars =
399401
ComponentDiscovery.forContext(applicationContext).discover();
402+
403+
String kotlinVersion = KotlinDetector.detectVersion();
400404
componentRuntime =
401405
new ComponentRuntime(
402406
UI_EXECUTOR,
@@ -406,7 +410,9 @@ protected FirebaseApp(Context applicationContext, String name, FirebaseOptions o
406410
Component.of(options, FirebaseOptions.class),
407411
LibraryVersionComponent.create(FIREBASE_ANDROID, ""),
408412
LibraryVersionComponent.create(FIREBASE_COMMON, BuildConfig.VERSION_NAME),
413+
kotlinVersion != null ? LibraryVersionComponent.create(KOTLIN, kotlinVersion) : null,
409414
DefaultUserAgentPublisher.component());
415+
410416
dataCollectionConfigStorage =
411417
new Lazy<>(
412418
() ->

firebase-common/src/main/java/com/google/firebase/components/ComponentRuntime.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,11 @@ public ComponentRuntime(
5555
for (ComponentRegistrar registrar : registrars) {
5656
componentsToAdd.addAll(registrar.getComponents());
5757
}
58-
Collections.addAll(componentsToAdd, additionalComponents);
58+
for (Component<?> additionalComponent : additionalComponents) {
59+
if (additionalComponent != null) {
60+
componentsToAdd.add(additionalComponent);
61+
}
62+
}
5963

6064
CycleDetector.detect(componentsToAdd);
6165

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright 2019 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package com.google.firebase.platforminfo;
16+
17+
import androidx.annotation.Nullable;
18+
19+
/**
20+
* Detects presence of Kotlin stdlib on the classpath.
21+
*
22+
* <p>If it is present, it is inferred that the application or its subset is written in Kotlin.
23+
*/
24+
public final class KotlinDetector {
25+
private KotlinDetector() {}
26+
27+
/** Returns the version of Kotlin stdlib if found, {@code null} otherwise. */
28+
@Nullable
29+
public static String detectVersion() {
30+
try {
31+
return kotlin.KotlinVersion.CURRENT.toString();
32+
} catch (NoClassDefFoundError ex) {
33+
return null;
34+
}
35+
}
36+
}

0 commit comments

Comments
 (0)