Skip to content

Set DevelopmentPlatform info for Flutter apps #3206

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 1 commit into from
Dec 7, 2021
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
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@

package com.google.firebase.crashlytics.ndk;

import static com.google.firebase.crashlytics.internal.DevelopmentPlatformProvider.UNITY_PLATFORM;

import android.content.Context;
import com.google.firebase.components.Component;
import com.google.firebase.components.ComponentContainer;
Expand Down Expand Up @@ -43,8 +41,7 @@ private CrashlyticsNativeComponent buildCrashlyticsNdk(ComponentContainer contai
Context context = container.get(Context.class);
// The signal handler is installed immediately for non-Unity apps. For Unity apps, it will
// be installed when the Firebase Unity SDK explicitly calls installSignalHandler().
boolean installHandlerDuringPrepSession =
!UNITY_PLATFORM.equals(new DevelopmentPlatformProvider(context).getDevelopmentPlatform());
boolean installHandlerDuringPrepSession = !DevelopmentPlatformProvider.isUnity(context);
return FirebaseCrashlyticsNdk.create(context, installHandlerDuringPrepSession);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ public void writeSessionApp(String sessionId, StaticSessionData.AppData appData)
appData.versionName(),
appData.installUuid(),
appData.deliveryMechanism(),
appData.developmentPlatform(),
appData.developmentPlatformVersion());
appData.developmentPlatformProvider().getDevelopmentPlatform(),
appData.developmentPlatformProvider().getDevelopmentPlatformVersion());
writeSessionJsonFile(fileStore, sessionId, json, APP_METADATA_FILE);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
matt says hi
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package com.google.firebase.crashlytics.internal;

import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;

import androidx.annotation.NonNull;
import androidx.test.runner.AndroidJUnit4;
Expand Down Expand Up @@ -57,7 +58,7 @@ public void whenAvailable(

StaticSessionData.AppData appData =
StaticSessionData.AppData.create(
"appId", "123", "1.2.3", "install_id", 0, "unity", "unityVersion");
"appId", "123", "1.2.3", "install_id", 0, mock(DevelopmentPlatformProvider.class));
StaticSessionData.OsData osData = StaticSessionData.OsData.create("release", "codeName", false);
StaticSessionData.DeviceData deviceData =
StaticSessionData.DeviceData.create(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

package com.google.firebase.crashlytics.internal;

import static com.google.firebase.crashlytics.internal.DevelopmentPlatformProvider.UNITY_PLATFORM;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
Expand All @@ -27,20 +26,36 @@

public class DevelopmentPlatformProviderTest extends CrashlyticsTestCase {
private static final String PACKAGE_NAME = "package.name";
private static final String UNITY_PLATFORM = "Unity";
private static final String UNITY_VERSION = "2.0.0";
private static final String FLUTTER_PLATFORM = "Flutter";

public void testDevelopmentPlatformInfo_withUnity_returnsPlatformAndVersion() throws Exception {
Context context = createMockContext(/*withUnityResource=*/ true);

assertTrue(DevelopmentPlatformProvider.isUnity(context));

DevelopmentPlatformProvider provider = new DevelopmentPlatformProvider(context);

assertEquals(UNITY_PLATFORM, provider.getDevelopmentPlatform());
assertEquals(UNITY_VERSION, provider.getDevelopmentPlatformVersion());
}

public void testDevelopmentPlatformInfo_withFlutter_returnsPlatformAndNoVersion() {
Context context = getContext(); // has asset in DevelopmentPlatformProvider.FLUTTER_ASSETS_PATH

DevelopmentPlatformProvider provider = new DevelopmentPlatformProvider(context);

assertEquals(FLUTTER_PLATFORM, provider.getDevelopmentPlatform());
assertNull(provider.getDevelopmentPlatformVersion());
assertFalse(DevelopmentPlatformProvider.isUnity(context));
}

public void testDevelopmentPlatformInfo_unknownPlatform_returnsNull() throws Exception {
Context context = createMockContext(/*withUnityResource=*/ false);

assertFalse(DevelopmentPlatformProvider.isUnity(context));

DevelopmentPlatformProvider provider = new DevelopmentPlatformProvider(context);

assertNull(provider.getDevelopmentPlatform());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import com.google.firebase.FirebaseApp;
import com.google.firebase.crashlytics.internal.CrashlyticsNativeComponent;
import com.google.firebase.crashlytics.internal.CrashlyticsTestCase;
import com.google.firebase.crashlytics.internal.DevelopmentPlatformProvider;
import com.google.firebase.crashlytics.internal.NativeSessionFileProvider;
import com.google.firebase.crashlytics.internal.analytics.AnalyticsEventLogger;
import com.google.firebase.crashlytics.internal.log.LogFileManager;
Expand Down Expand Up @@ -142,8 +143,7 @@ public CrashlyticsController build() {
"packageName",
"versionCode",
"versionName",
/*developmentPlatform=*/ null,
/*developmentPlatformVersion=*/ null);
mock(DevelopmentPlatformProvider.class));

final CrashlyticsController controller =
new CrashlyticsController(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import com.google.firebase.crashlytics.internal.CrashlyticsNativeComponent;
import com.google.firebase.crashlytics.internal.CrashlyticsNativeComponentDeferredProxy;
import com.google.firebase.crashlytics.internal.CrashlyticsTestCase;
import com.google.firebase.crashlytics.internal.DevelopmentPlatformProvider;
import com.google.firebase.crashlytics.internal.analytics.UnavailableAnalyticsEventLogger;
import com.google.firebase.crashlytics.internal.breadcrumbs.DisabledBreadcrumbSource;
import com.google.firebase.crashlytics.internal.persistence.FileStore;
Expand Down Expand Up @@ -263,8 +264,7 @@ private void setupAppData(String buildId) {
"packageName",
"versionCode",
"versionName",
"Unity",
"1.0");
mock(DevelopmentPlatformProvider.class));
}

private void setupResource(Integer resId, String type, String name, String value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import com.google.firebase.crashlytics.internal.CrashlyticsNativeComponent;
import com.google.firebase.crashlytics.internal.CrashlyticsNativeComponentDeferredProxy;
import com.google.firebase.crashlytics.internal.CrashlyticsTestCase;
import com.google.firebase.crashlytics.internal.DevelopmentPlatformProvider;
import com.google.firebase.crashlytics.internal.analytics.UnavailableAnalyticsEventLogger;
import com.google.firebase.crashlytics.internal.breadcrumbs.BreadcrumbHandler;
import com.google.firebase.crashlytics.internal.breadcrumbs.BreadcrumbSource;
Expand Down Expand Up @@ -343,8 +344,7 @@ private Task<CrashlyticsCore> startCoreAsync(CrashlyticsCore crashlyticsCore) {
"packageName",
"versionCode",
"versionName",
"Unity",
"1.0");
mock(DevelopmentPlatformProvider.class));

crashlyticsCore.onPreExecute(appData, mockSettingsController);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

package com.google.firebase.crashlytics.internal.common;

import static com.google.firebase.crashlytics.internal.DevelopmentPlatformProvider.UNITY_PLATFORM;
import static com.google.firebase.crashlytics.internal.common.CrashlyticsReportDataCapture.GENERATOR;
import static com.google.firebase.crashlytics.internal.common.CrashlyticsReportDataCapture.GENERATOR_TYPE;
import static org.junit.Assert.assertArrayEquals;
Expand Down Expand Up @@ -51,6 +50,7 @@

@RunWith(AndroidJUnit4.class)
public class CrashlyticsReportDataCaptureTest {
private static final String UNITY_PLATFORM = "Unity";

private Context context = getContext();
private IdManager idManager;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,22 @@

import android.content.Context;
import androidx.annotation.Nullable;
import java.io.IOException;

/** Provider for the development platform info. */
public class DevelopmentPlatformProvider {
public static final String UNITY_PLATFORM = "Unity";
private static final String UNITY_PLATFORM = "Unity";
private static final String FLUTTER_PLATFORM = "Flutter";

private static final String UNITY_VERSION_FIELD = "com.google.firebase.crashlytics.unity_version";
private static final String FLUTTER_ASSETS_PATH = "flutter_assets";

@Nullable private final String developmentPlatform;
@Nullable private final String developmentPlatformVersion;
private final Context context;
@Nullable private DevelopmentPlatform developmentPlatform;

public DevelopmentPlatformProvider(Context context) {
// Unity
int unityEditorId = getResourcesIdentifier(context, UNITY_VERSION_FIELD, "string");
if (unityEditorId != 0) {
developmentPlatform = UNITY_PLATFORM;
developmentPlatformVersion = context.getResources().getString(unityEditorId);
Logger.getLogger().v("Unity Editor version is: " + developmentPlatformVersion);
return;
}

this.context = context;
developmentPlatform = null;
developmentPlatformVersion = null;
}

/**
Expand All @@ -49,7 +43,7 @@ public DevelopmentPlatformProvider(Context context) {
*/
@Nullable
public String getDevelopmentPlatform() {
return developmentPlatform;
return initDevelopmentPlatform().developmentPlatform;
}

/**
Expand All @@ -59,6 +53,65 @@ public String getDevelopmentPlatform() {
*/
@Nullable
public String getDevelopmentPlatformVersion() {
return developmentPlatformVersion;
return initDevelopmentPlatform().developmentPlatformVersion;
}

/**
* Returns if the development platform is Unity, without initializing the rest of the
* DevelopmentPlatform object.
*
* <p>This is useful for the NDK to avoid an expensive file operation during start up.
*/
public static boolean isUnity(Context context) {
return getResourcesIdentifier(context, UNITY_VERSION_FIELD, "string") != 0;
}

/** Quickly and safely check if the given asset path exists. */
private boolean assetPathExists(String path) {
try {
if (context.getAssets() == null) {
return false;
}
String[] list = context.getAssets().list(path);
return list != null && list.length > 0;
} catch (IOException ex) {
return false;
}
}

private DevelopmentPlatform initDevelopmentPlatform() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this method need to be synchronized?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's ok to not synchronize it. Because even if we have two instances, it's read-only so nothing bad will happen.

if (developmentPlatform == null) {
developmentPlatform = new DevelopmentPlatform();
}
return developmentPlatform;
}

private class DevelopmentPlatform {
@Nullable private final String developmentPlatform;
@Nullable private final String developmentPlatformVersion;

private DevelopmentPlatform() {
// Unity
int unityEditorId = getResourcesIdentifier(context, UNITY_VERSION_FIELD, "string");
if (unityEditorId != 0) {
developmentPlatform = UNITY_PLATFORM;
developmentPlatformVersion = context.getResources().getString(unityEditorId);
Logger.getLogger().v("Unity Editor version is: " + developmentPlatformVersion);
return;
}

// Flutter
if (assetPathExists(FLUTTER_ASSETS_PATH)) {
developmentPlatform = FLUTTER_PLATFORM;
// TODO: Get the version when available - https://github.com/flutter/flutter/issues/92681
developmentPlatformVersion = null;
Logger.getLogger().v("Development platform is: " + FLUTTER_PLATFORM);
return;
}

// Unknown/no development platform
developmentPlatform = null;
developmentPlatformVersion = null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import androidx.annotation.Nullable;
import com.google.firebase.crashlytics.internal.DevelopmentPlatformProvider;

/** Carries static information about the app. */
Expand All @@ -31,8 +30,7 @@ public class AppData {
public final String versionCode;
public final String versionName;

@Nullable public final String developmentPlatform;
@Nullable public final String developmentPlatformVersion;
public final DevelopmentPlatformProvider developmentPlatformProvider;

public static AppData create(
Context context,
Expand All @@ -56,8 +54,7 @@ public static AppData create(
packageName,
versionCode,
versionName,
developmentPlatformProvider.getDevelopmentPlatform(),
developmentPlatformProvider.getDevelopmentPlatformVersion());
developmentPlatformProvider);
}

public AppData(
Expand All @@ -67,15 +64,13 @@ public AppData(
String packageName,
String versionCode,
String versionName,
@Nullable String developmentPlatform,
@Nullable String developmentPlatformVersion) {
DevelopmentPlatformProvider developmentPlatformProvider) {
this.googleAppId = googleAppId;
this.buildId = buildId;
this.installerPackageName = installerPackageName;
this.packageName = packageName;
this.versionCode = versionCode;
this.versionName = versionName;
this.developmentPlatform = developmentPlatform;
this.developmentPlatformVersion = developmentPlatformVersion;
this.developmentPlatformProvider = developmentPlatformProvider;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -696,8 +696,7 @@ private static StaticSessionData.AppData createAppData(IdManager idManager, AppD
appData.versionName,
idManager.getCrashlyticsInstallId(),
DeliveryMechanism.determineFrom(appData.installerPackageName).getId(),
appData.developmentPlatform,
appData.developmentPlatformVersion);
appData.developmentPlatformProvider);
}

private static StaticSessionData.OsData createOsData(Context context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,9 @@ private CrashlyticsReport.Session.Application populateSessionApplicationData() {
.setVersion(appData.versionCode)
.setDisplayVersion(appData.versionName)
.setInstallationUuid(idManager.getCrashlyticsInstallId())
.setDevelopmentPlatform(appData.developmentPlatform)
.setDevelopmentPlatformVersion(appData.developmentPlatformVersion);
.setDevelopmentPlatform(appData.developmentPlatformProvider.getDevelopmentPlatform())
.setDevelopmentPlatformVersion(
appData.developmentPlatformProvider.getDevelopmentPlatformVersion());
return builder.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@

package com.google.firebase.crashlytics.internal.model;

import androidx.annotation.Nullable;
import com.google.auto.value.AutoValue;
import com.google.firebase.crashlytics.internal.DevelopmentPlatformProvider;

@AutoValue
public abstract class StaticSessionData {
Expand Down Expand Up @@ -44,28 +44,22 @@ public abstract static class AppData {

public abstract int deliveryMechanism();

@Nullable
public abstract String developmentPlatform();

@Nullable
public abstract String developmentPlatformVersion();
public abstract DevelopmentPlatformProvider developmentPlatformProvider();

public static AppData create(
String appIdentifier,
String versionCode,
String versionName,
String installUuid,
int deliveryMechanism,
@Nullable String developmentPlatform,
@Nullable String developmentPlatformVersion) {
DevelopmentPlatformProvider developmentPlatformProvider) {
return new AutoValue_StaticSessionData_AppData(
appIdentifier,
versionCode,
versionName,
installUuid,
deliveryMechanism,
developmentPlatform,
developmentPlatformVersion);
developmentPlatformProvider);
}
}

Expand Down
Loading