Skip to content

Check for a specific flutter asset file instead of listing the path #3631

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
Apr 8, 2022
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 @@ -42,7 +42,7 @@ public void testDevelopmentPlatformInfo_withUnity_returnsPlatformAndVersion() th
}

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

DevelopmentPlatformProvider provider = new DevelopmentPlatformProvider(context);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@
import android.content.Context;
import androidx.annotation.Nullable;
import java.io.IOException;
import java.io.InputStream;

/** Provider for the development platform info. */
public class DevelopmentPlatformProvider {
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";
private static final String FLUTTER_ASSET_FILE = "flutter_assets/NOTICES.Z";

private final Context context;
@Nullable private DevelopmentPlatform developmentPlatform;
Expand Down Expand Up @@ -66,14 +67,13 @@ 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;
/** Quickly and safely check if the given asset file exists. */
private boolean assetFileExists(String file) {
if (context.getAssets() == null) {
return false;
}
try (InputStream ignored = context.getAssets().open(file)) {
return true;
} catch (IOException ex) {
return false;
}
Expand Down Expand Up @@ -101,7 +101,7 @@ private DevelopmentPlatform() {
}

// Flutter
if (assetPathExists(FLUTTER_ASSETS_PATH)) {
if (assetFileExists(FLUTTER_ASSET_FILE)) {
developmentPlatform = FLUTTER_PLATFORM;
// TODO: Get the version when available - https://github.com/flutter/flutter/issues/92681
developmentPlatformVersion = null;
Expand Down