microg/GmsCore · error · IllegalStateException

type must be set

Error message

type must be set

What it means

Thrown by DataSource.Builder.build() when the source type (raw/derived) was never set — represented internally as type < 0. A DataSource must specify whether it is a raw sensor source or a derived/computed source, so the builder rejects construction without it.

Source

Thrown at play-services-fitness/src/main/java/com/google/android/gms/fitness/data/DataSource.java:200

     * A builder that can be used to construct new data source objects. In general, a built data source should be saved in memory to avoid the cost
     * of re-constructing it for every request.
     */
    public static class Builder {
        private DataType dataType;
        private Device device;
        private Application application;
        private int type = -1;
        private String streamName = "";

        /**
         * Finishes building the data source and returns a DataSource object.
         *
         * @throws IllegalStateException If the builder didn't have enough data to build a valid data source.
         */
        @NonNull
        public DataSource build() {
            if (dataType == null) throw new IllegalStateException("dataType must be set");
            if (type < 0) throw new IllegalStateException("type must be set");
            return new DataSource(dataType, type, device, application, streamName);
        }

        /**
         * Sets the package name for the application that is recording or computing the data. Used for data sources that aren't built into the platform
         * (local sensors and BLE sensors are built-in). It can be used to identify the data source, to disambiguate between data from different
         * applications, and also to link back to the original application for a detailed view.
         */
        @NonNull
        public Builder setAppPackageName(@NonNull String packageName) {
            Application application = Application.GMS_APP;
            this.application = Constants.GMS_PACKAGE_NAME.equals(packageName) ? Application.GMS_APP : new Application(packageName);
            return this;
        }

        /**
         * Sets the package name for the application that is recording or computing the data based on the app's context. This method should be
         * preferred when an application is creating a data source that represents its own data. When creating a data source to query data from other

View on GitHub (pinned to 157c9d86ac)

Solutions

  1. Add .setType(DataSource.TYPE_RAW) (or TYPE_DERIVED as appropriate) before build().
  2. Order the builder calls so setType immediately follows setDataType and neither can be skipped.
  3. For derived/computed data use DataSource.TYPE_DERIVED; use TYPE_RAW for direct sensor data.
  4. Validate your builder config in unit tests so missing setType fails fast in CI.

Example fix

// before
DataSource src = new DataSource.Builder()
    .setDataType(DataType.TYPE_STEP_COUNT_DELTA)
    .build(); // throws
// after
DataSource src = new DataSource.Builder()
    .setDataType(DataType.TYPE_STEP_COUNT_DELTA)
    .setType(DataSource.TYPE_RAW)
    .build();
Defensive patterns

Strategy: validation

Validate before calling

if (sourceType < 0) throw new IllegalStateException("setType must be called before build()");

Type guard

boolean sourceTypeSet(int type) { return type == DataSource.TYPE_RAW || type == DataSource.TYPE_DERIVED || type == DataSource.TYPE_CLOUD; }

Try / catch

try { src = dataSourceBuilder.build(); } catch (IllegalStateException e) { throw new ConfigException("DataSource missing type", e); }

Prevention

When it happens

Trigger: Calling DataSource.Builder.build() without calling setType(DataSource.TYPE_RAW) (or TYPE_DERIVED / TYPE_CLOUD, etc.). Note this is the data-source type, not the DataType, so it can be missed even when setDataType was called.

Common situations: Developers confusing DataType with source type and setting only setDataType; snippets from older samples predating the required setType; builders assembled programmatically where setType is conditionally skipped.

Understand the failure class

Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.

Related errors


AI-assisted analysis of microg/GmsCore@157c9d86ac (2026-09-06). Data as JSON: /api/errors/9e172a2a02c9cf73. Report an issue: GitHub.