DX Application Performance Management

Expand all | Collapse all

Tuesday Tip - CA MAA: Integrate MAA Wrapping into the Android Studio Build Process

  • 1.  Tuesday Tip - CA MAA: Integrate MAA Wrapping into the Android Studio Build Process

    Broadcom Employee
    Posted 05-26-2015 06:34 PM

    CA Mobile Application Analytics (MAA) Tuesday Tip by Bryan Whitmarsh, Sr Principal Technical Consultant for Tuesday, May 26, 2015


    In this Tuesday tip we will walk you through how to setup Android Studio to automatically wrap your mobile application with the MAA SDK leveraging the Gradle build system. In many ways this is the easiest and most logical wrapping method. The wrapping process happens as a natural part of the build process, and as a result the wrapping process is reliable and interoperates properly with obfuscation.

     

    Step 1

    Download the Wrapper Utility and application PLIST file from the MAA Console, which can be found under the “APPS” menu.

    Image_01.png

     

    Step 2

    Add the .plist to your project. The file needs to be copied to the “assets/conf” directory in the project. If this directory doesn’t exist it will need to be created. (In Android Studio, in the project hierarchy, right-click on the “app” element, click on the “New” menu, then the “Folder” menu, and finally the “Assets” folder, as shown below)

    Image_02.png

    A confirmation dialog is displayed. Don’t change anything. Just click “Finish”.

     

    A new folder called “assets” under the project’s “src/main” folder will be created.

     

    Right-click on this “assets” folder. Click the “New” menu and then select the “Directory” option.

    A dialog will appear asking for the folder name. Enter “conf” and click OK, as shown below.

     

    Now copy your <app_name>.plist to this new conf directory.

     

    Step 3

    Copy the downloaded wrapper to a more convenient location. The purpose of this step and the next is to set a CA_EMM_HOME environment variable pointing to the “emm” subfolder of the location where the MAA wrapper script was downloaded. As an example we will copy the wrapper to the C:\maa directory for Windows and the ~\maa directory for Mac OS X. Unzip the downloaded MAA wrapper zip file to this directory. After doing this you should have the ca-maa-android-sdk-wrapper-nn.n.jar and wrap.sh files in the “maa” directory and an “emm” subdirectory.

    Image_03.png

     

    Step 4

    Create the CA_EMM_HOME Environment Variable. Exit Android Studio if it is running.The environment variable CA_EMM_HOME must be set to C:\maa\emm on Windows, and ~/maa/emm on Mac OS X. The instructions for doing this follow.

     

    Windows:

    Start the Control Panel.

    Choose the “System and Security” icon.

    Choose the “System” icon.

    Choose the “Advanced System Settings” option. You should see a screen like this:

    Image_04.png

     

    Add a new ‘User Environment Variable”: click the “Environment Variables” button. There are two buttons labeled “New…”. Click the topmost “New…” button. Create a new CA_EMM_HOME “User Environment Variable” as shown below:

    Image_05.png

    Mac OS X, start the Terminal.

    Enter the following command: launchctl setenv CA_EMM_HOME ~/maa/emm

     

     

    Step 5

    Configure Gradle Plugin settings. (Gradle is the Android Studio build system. It supports a “Plugin” architecture to allow new build tasks to be integrated. The MAA Android Wrapper library provides a Gradle plugin that provides a new “wrapper” task that can be called by Gradle during the app build process)

     

    In the project organizer view double-click the entry for the “build.gradle” file. The contents of the file will be displayed in the editor window. BE CAREFUL: there are two instances of “build.gradle”. Make sure you are editing the one in the project root, AND NOT the one under the “app” directory in the project, as shown below:

    Image_06.png

    Add the following line of text at the location shown below to the build.gradle:

    Windows: classpath files('C:/maa/ca-maa-android-sdk-wrapper-nn.n.jar') Note that forward slashes (“/”) are used instead of the usual Windows backslash (“\”).

    Image_07.png

    Mac OS X: Using the value of CA_EMM_HOME you recorded earlier, add the equivalent line to build.gradle:

    Image_08.png

    Windows and Mac OS X: Add the following to the end of the gradle.build file:  apply plugin: ‘ca-maa-wrapper-plugin’, as shown below:

    Image_09.png

     

    As can be seen in the screenshot above a yellow bar saying that “A project sync may be necessary…” with a blue “Sync Now” link. This link should be clicked if it appears.

     

    Verify that things have worked: Click the “Gradle” link at the very right-hand side of the screen where shown below, and expand the tasks to verify that the “maaWrapperTask” has been added. If you do not see this re-check these instructions and try again

    Image_10.png

    Step 6

    Integrate Wrapping into the Gradle Build script. Now that the MAA Gradle plugin is available to Android Studio the final stage is to define the Wrapping process to Gradle. For this stage we will be working with THE OTHER build.gradle file, underneath the “app” folder in the project organizer view as shown below:

    Image_11.png

     

    Add the following lines to the end of the build.gradle file:


    afterEvaluate {

        android.applicationVariants.all { variant ->

     

            def newWrapperTask = tasks.create(name: "${variant.name}MAAWrap", type: com.ca.android.wrapper.gradle.CAMAAWrapperTask)

            variant.outputs.each { output ->

                newWrapperTask.ext.apkFilePath = output.outputFile.toString()

                newWrapperTask.dependsOn output.packageApplication

            }

            newWrapperTask.ext.plistFilePath = 'app/src/main/assets/conf/RESTHelloWorld_camdo.plist'

     

            variant.assemble.dependsOn newWrapperTask

        }

    }

     

    Another “yellow bar” requesting a “project sync” will appear. As before click the blue link to synchronise the project.

     

    Step 7

    Build and test the app. Ensure that the “Gradle Tasks” window on the right-hand side of the screen is still open. If not use the “Gradle” button at the extreme right of the Android Studio screen to re-open it. Expand the tasks underneath “app”:

    Image_12.png

    Note that there are various build tasks such as “assembleRelease”, “assembleDebug” etc. Ensure that the Gradle console is displayed by clicking the “Gradle Console” button at the bottom right of the Android Studio window. 4. From the Gradle Tasks, double-click the assembleDebug entry. The Gradle Console window will show the progress of the build process. After a short time the MAA wrapping sequence will begin. When the wrapping is complete the location of the newly-wrapped APK file is shown.

     

    You now have a one step process to build and wrap your Android application.

     

     

     

     

     



  • 2.  Re: Tuesday Tip - CA MAA: Integrate MAA Wrapping into the Android Studio Build Process

    Posted 05-27-2015 07:18 PM

    whibr08 very helpful tip with screenshots as the gradle plugin option is a bit more developer oriented. sessh01 you may want to add some of this to the the MAA Android integration document in 15.1 and for the updates we are doing for 15.2. Also if a development team at the customer would rather reference the wrapper jar from their maven repository instead of the full path listed by Bryan in his tip, then step 6 of the 15.1 gradle plugin doc shows you how to do that.



  • 3.  Re: Tuesday Tip - CA MAA: Integrate MAA Wrapping into the Android Studio Build Process

    Posted 09-18-2015 06:17 PM

    Excuse me, someone can send me the ca-maa-android-sdk-wrapper-15.1.zip.



  • 4.  Re: Tuesday Tip - CA MAA: Integrate MAA Wrapping into the Android Studio Build Process

    Broadcom Employee
    Posted 05-27-2015 06:20 AM

    Hi Brian and thanks for the tip.

    Just one question: with Gradle is it possible to use also the "traditional" integration using the SDK (the one that is shown on the console), or yours has to replace it? in case both are valid, is there any use case in which it's better using one or another?

     

    Thanks again and regards!

    Francesco



  • 5.  Re: Tuesday Tip - CA MAA: Integrate MAA Wrapping into the Android Studio Build Process

    Posted 05-26-2015 09:57 PM
    Thank you for sharing this tip Bryan. 


  • 6.  Re: Tuesday Tip - CA MAA: Integrate MAA Wrapping into the Android Studio Build Process

    Broadcom Employee
    Posted 09-18-2015 06:50 PM

    Martin,

    Is there a reason you are requesting the 15.1 version of the wrapper? The current version of our wrapper is 15.2.1. Are you using an on-premise install or our SaaS solution(mdo.mobility.ca.com)?



  • 7.  Re: Tuesday Tip - CA MAA: Integrate MAA Wrapping into the Android Studio Build Process

    Broadcom Employee
    Posted 05-27-2015 03:46 PM

    Yes, the "traditional" MAA SDK integration option is still possible. If you are manually integrating our MAA SDK you wouldn't leverage a custom Gradle task. You would simply inject the necessary lines of code into your Android project to initialize the SDK, name custom Transactions, etc. then use the standard Gradle tasks like "assemble", "assembleDebug", etc. to build the apk.

     

    Regards,

     

    Bryan Whitmarsh

    Sr Principal Technical Consultant

     

    CA Technologies | Idaho

    Mobile: +1 208 505 7598 | Bryan.Whitmarsh@ca.com<mailto:Bryan.Whitmarsh@ca.com>



  • 8.  Re: Tuesday Tip - CA MAA: Integrate MAA Wrapping into the Android Studio Build Process

    Posted 09-18-2015 06:59 PM

    I trying to integrate the API for Javascript on a mobile framework cordova, but the app can't send the data.



  • 9.  Re: Tuesday Tip - CA MAA: Integrate MAA Wrapping into the Android Studio Build Process

    Posted 05-27-2015 07:23 PM

    Bryan is correct for 15.1 SDK option.  For 15.2 we are making some changes to unify these two approaches in order to avoid boiler plate code like you have to in steps 4 through 8 of the 15.1 SDK based integration. In 15.2 smart container a.k.a. wrapping is used all the time and code changes are only required when using custom APIs like manually marking transactions, sending customer id etc.



  • 10.  Re: Tuesday Tip - CA MAA: Integrate MAA Wrapping into the Android Studio Build Process

    Broadcom Employee
    Posted 09-20-2015 02:40 PM

    Hi Martin,

     

    The integration APIs for JavaScript supported apps (Phonegap and Cordova) are available starting 15.2.1.

     

    Regards,

    Rosy