Converting a MobileFirst project to an Android Studio-based project

Follow these instructions if you want to work with a project created either with the MobileFirst Platform Command Line Interface or with MobileFirst Studio in Android Studio, and use Gradle building capabilities.

About this task

This procedure enables you to make changes to your project, build and deploy to MobileFirst Server, go to Android Studio, and build and deploy your app to an emulator or a device.

Procedure

  1. Only for projects created with MobileFirst Platform Command Line Interface (for projects created with MobileFirst Studio, go directly to step 2), import your project into MobileFirst Studio:
    1. In MobileFirst Studio, go to File > Import > Android > Existing Android Code into Workspace, and click Next.
    2. In Root Directory, click Browse and go to your_project_name > apps > your_app_name > android > native, and click OK.
    3. Select your project from Project to Import, and click Finish.
  2. Export the Gradle file:
    1. In MobileFirst Studio, go to File > Export > Android > Generate Gradle build files
    2. Follow the wizard instructions to select your Android project, and click Finish when the export is completed.
    The build.gradle file is now generated in your Android project.
  3. Import the build.gradle file from your project to Android Studio:
    1. In Android Studio, click Import project (Eclipse ADT, Gradle, etc.).
    2. Go to your MobileFirst project under apps > android > native, and click build.gradle.
      Note: Make sure to click build.gradle and not the native folder.
    3. Click OK and accept any Gradle configuration that Android Studio prompts for.
    You might now have to fix sync errors.
  4. Optional: After your Gradle file is imported, an error message might report that your Gradle plug-in version is out of date. To fix the issue, you can either:
    • Select the auto-fix by clicking Fix plugin version and sync project.
    • Or manually edit the build.gradle file:
      1. From the Project tab, expand the Gradle Scripts drop-down menu, and open build.gradle.
      2. Change the version of Gradle tools to classpath 'com.android.tools.build:gradle:1.5.0' (or later) and sync Gradle again.
  5. Optional: To fix a Gradle sync error about the Gradle wrapper version:
    1. Open the Project drop-down menu, go to native > gradle > wrapper, and open the gradle-wrapper.properties file. Set the Gradle version as shown for Gradle 2.8 (set the version value based on the Gradle version you are using).
    2. Set the distributionUrl version to 2.8: distributionUrl = https \: //services.gradle.org/distributions/gradle-2.8-all.zip
  6. Optional: If other errors prevent Gradle from syncing, fix them before you go to the next step.

    The most common issues that you must resolve are dependencies issues. To know how to fix those problems, see the Gradle documentation: https://docs.gradle.org/current/userguide/dependency_management.html and https://docs.gradle.org/current/userguide/java_plugin.html#sec:java_plugin_and_dependenc%20y_management

  7. Run your application in Android Studio. If the application is deployed successfully, continue with this procedure. If you have other build errors, you might have to edit the build.gradle file again to adapt it to your project.
  8. Edit the index.html file from the common folder of your project, make a change and save the file, either in MobileFirst Studio or with any other editor.
  9. Build the environment:
    • For a project created with the MobileFirst Platform Command Line Interface, run the mfp push command.
    • For a project created with MobileFirst Studio, go to your android app folder in the MobileFirst hybrid project directory, right-click android, click Run as and select any of the first three options.
      Note: Build Android Environment is the fastest option, but you can also choose to deploy your app to MobileFirst Server. If you want to open your app automatically in Android Studio, you can also click Android Studio project. All three options build the hybrid Android project.
  10. In Android Studio, from the Project drop-down menu, go to native > assets > www > default, open the index.html file, and make sure the changes that you made in step 8 are present.
    Note: If your changes are not there, refresh your project in Android Studio
  11. Run the application.

Results

The application is now deployed successfully in Android Studio.