Skip to content

Step 2: Build the app

What you’ll do: create an empty Flutter app, add the CARP packages, and do the small Android and iPhone setup CARP needs.

You need:

  1. Create an empty Flutter app. In a terminal:

    Terminal window
    flutter create my_first_carp_app
    cd my_first_carp_app
  2. Add the CARP packages.

    Terminal window
    flutter pub add carp_core carp_mobile_sensing
  3. Do the platform setup.

    In android/app/src/main/AndroidManifest.xml, add this just above <application, so the app may count steps:

    android/app/src/main/AndroidManifest.xml
    <manifest xmlns:android="http://schemas.android.com/apk/res/android">
    <uses-permission android:name="android.permission.ACTIVITY_RECOGNITION"/>
    <application ...>
    </manifest>

    In android/app/build.gradle.kts, add the two marked parts:

    android/app/build.gradle.kts
    android {
    compileOptions {
    isCoreLibraryDesugaringEnabled = true
    // ...leave the existing lines as they are
    }
    // ...
    }
    dependencies {
    coreLibraryDesugaring("com.android.tools:desugar_jdk_libs:2.1.4")
    }
  4. Add your protocol. Create lib/protocol.dart with the code from Step 1:

    lib/protocol.dart
    import 'package:carp_core/carp_core.dart' hide Smartphone;
    import 'package:carp_mobile_sensing/carp_mobile_sensing.dart';
    SmartphoneStudyProtocol createProtocol() {
    final protocol = SmartphoneStudyProtocol(
    name: 'My first study',
    dataEndPoint: SQLiteDataEndPoint(),
    );
    final phone = Smartphone();
    protocol.addPrimaryDevice(phone);
    protocol.addParticipantRole(ParticipantRole('Participant'));
    protocol.addTaskControl(
    ImmediateTrigger(),
    BackgroundTask(name: 'Steps and battery', measures: [
    Measure(type: SensorSamplingPackage.STEP_EVENT),
    Measure(type: DeviceSamplingPackage.BATTERY_STATE),
    ]),
    phone,
    );
    return protocol;
    }

    Smartphone exists in both packages; hide tells Dart to use the one from carp_mobile_sensing.

Terminal window
flutter run

The default Flutter counter app starts on your phone. CARP isn’t running yet; that’s the next step.

Next: Step 3: Deploy