This content has been archived.

Setting up the Mashup SDK for Android app development in Pega 7.2.1

This article describes how to set up the Mashup SDK in the Android Studio development environment so that its API can be used in the development of native Android apps.

Prerequisites

The native Android app that uses the Mashup SDK must be developed using:

  • Java programming language
  • Android Studio or another IDE to create apps for Android 4.1 and later, 5.x, and 6.x

Make sure that you have already done the following tasks:

  • Read the Mashup SDK overview article
  • Have experience developing Android apps
  • Downloaded the Mashup Demo application that is distributed in the same package as the Mashup SDK

Add the Mashup SDK to Android Studio

Perform the following steps:

  1. Create a project or open an existing one in Android Studio or another IDE.
  2. In the android{} section of the application build.gradle file, add the following lines at the beginning of the file:

    apply plugin: 'base'
    apply plugin: 'extensionDetector'

  3. If the repositories{} section in the application build.gradle file does not already exist, create it with the entries shown below. Add the same entries to the buildscript{repositories{}} section of the project build.gradle file that is in the root folder of the Android Studio project. In both cases, do not forget to replace the <MASHUP_SDK_PATH> string with the path to where the extracted Mashup SDK for the Android distribution files are located.

    maven {
      url 'file:///<MASHUP_SDK_PATH>/repository/modules'
    }
    ivy {
      url 'file:///<MASHUP_SDK_PATH>/repository/libraries'
    }

  4. In the dependencies section in the buildscript{} section of the project build.gradle file, add the following lines:

    classpath 'com.android.tools.build:gradle:1.2.3'
    classpath 'com.antennasoftware:commontools.extensiondetector:1.6.0.2'
    classpath 'org.reflections:reflections:0.9.9.vol1'

  5. In the top level dependencies{} section of the application build.gradle file, add the following lines:

    compile 'com.squareup.dagger:dagger-compiler:1.2.2.vol1'
    compile 'com.pega:base:6.21.0.0'
    compile 'com.pega:prpcAuthentication:6.21.0.0'
    compile 'com.pega.prpcSnapStart:6.21.0.0'

  6. Edit the AndroidManifest.xml file and in the <application> element add the following new attribute:
    android:name="com.antennasoftware.mashup.dagger.ContainerApplication"

  7. Copy the nativecontainer.properties file from the <MASHUP_SDK_PATH>/samples/mashup/src/main/assets folder to the assets folder for your application (./app/src/main/assets by default).

  8. In the Java code for your Android native app, create a class to allow injecting your activity into the Mashup SDK.
    Make sure to generate the import statements manually or by using your IDE. Doing this is necessary for the code to successfully compile.
    import com.squareup.dagger:Module;
    import com.squareup.dagger:Provides;

    import javax.inject.Singleton;

    @Module(
      addsTo = AppModule.class, library = true, injects = {
        MainActivity.class
        }
    )
    public class AppModule {
      private final MainActivity activity;

      public AppModule( final MainActivity activity ) {
        this.activity = activity;
      }

      @Provides
      @Singleton
      MainActivity provideActivity() {
        return activity;
      }
    }

  9. In your MainActivity class, add the following annotated field:
    @Inject
    ContainerDelegate container;

  10. Append the following code to the onCreate() method right after the call to super.OnCreate():
    ContainerApplication.instance.addModule( new BaseActivityModule(this) );
    ContainerApplication.instance.addModule( new MainActivityModule(this) );
    ContainerApplication.instance.inject( this );
  11. Add the following callback delegates:

    @Override
    protected void onDestroy() {
      container.onDestroy();
      super.onDestroy();
    }
    @Override
    public void onBackPressed() {
      if (!container.onBackPressed()) {
        moveTaskToBack(true);
      }
    }
    @Override
    public void onResume() {
      super.onResume();
      container.onResume();
    }
    @Override
    public void onPause() {
      container.onPause();
      super.onPause();
    }
    @Override
    public void onStop() {
      container.onStop();
      super.onStop();
    }
    @Override
    public void onStart() {
      super.onStart();
      container.onStart();
    }
    @Override
    public void onRestart() {
      super.onRestart();
      container.onRestart();
    }
    @Override
      public void onActivityResult( int requestCode, int resultCode, Intent data ) {
        super.onActivityResult( requestCode, resultCode, data );
        container.onActivityResult( requestCode, resultCode, data );
    }

    The Mashup SDK is now set up in your project.

Verify that the Mashup SDK is correctly configured

To verify that the Mashup SDK is correctly configured in your project, create a HybridWebView component in your layout file, and then load a URL into it:

  1. Make sure that your layout file (for example, main_layout.xml), has the following contents:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:id="@+id/layout"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical"
      tools:context=".MainActivity"
    >
      <com.pega.mobile.webview.HybridWebView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/webview">
      </com.pega.mobile.webview.HybridWebView>
    </LinearLayout>

  2. Append the following Java code to the end of the onCreate() method in the MainActivity Java object. Make sure to replace the following sample URL string with one that points to your Pega 7 Platform application instance.

    setContentView(R.layout.activity_main);
    HybridWebView webview = (HybridWebView) findViewById(R.id.webview);
    webview.init("webViewName");
    webview.loadUrl("http://test.server.com:8243/prweb");


Related Content

Have a question? Get answers now.

Visit the Collaboration Center to ask questions, engage in discussions, share ideas, and help others.