Technology Blogs by SAP
Learn how to extend and personalize SAP applications. Follow the SAP technology blog for insights into SAP BTP, ABAP, SAP Analytics Cloud, SAP HANA, and more.
cancel
Showing results for 
Search instead for 
Did you mean: 
Dan_vL
Product and Topic Expert
Product and Topic Expert
This blog series makes use of the SAP Cloud Platform SDK for Android. Specifically the app created in this blog series will use the Kotlin language which is the now recommended language to build Android apps. The application will onboard with Mobile Services, make a proxied OData request, and in part two, use SAP Fiori Mentor to explore and use some of the available UI controls that are included in the SAP Cloud Platform SDK for Android.

There are different approaches to creating an application with the SAP Cloud Platform SDK for Android.

The Step by Step blog series shows how to use many of the SDK features with a from scratch approach.

The mission Get Started with SAP Cloud Platform SDK for Android explores and customizes the starter or template application that can be created with the integrated SAP wizard.

The mission Use Flows with SAP Cloud Platform SDK for Android demonstrates how the Flows library can be used to more easily onboard the application and make use of the provided onboarding screens.

This blog will make use of the SAP wizard to generate a Kotlin project without a UI. The UI will be created using sample code from the SAP Fiori Mentor.

Before proceeding, ensure that you have an Android development environment, the SAP Cloud Platform SDK for Android installed, and a Mobile Services trial account. This is covered in more detail in the Try Out SAP Cloud Platform SDK for Android Wizard tutorial.

Create the Project


The following steps demonstrate how to create a new Kotlin project that will onboard with Mobile Services and contain proxy classes that enable data to be accessed and updated.

  1. Create the project.

  2. Specify the Mobile Services server to be used.
    Additional details on how to configure the Mobile Services server to connect to are available in the tutorial Try Out SAP Cloud Platform SDK for Android Wizard.

  3. On the Cloud Configuration page, choose Create and specify the values as shown below.

  4. On the OData Services tab, choose Add.
    Select the sample OData service.

  5. On the project properties tab, ensure the target language is Kotlin.

  6. On the Project Features tab, ensure the checkbox to create a user interface is unchecked.


Run the Project


In this section, the previously generated project will be examined and run.

  1. Notice that the generated app contains Java proxy classes which provide methods for retrieving (or creating, deleting and updating) data such as an asynchronous method to request the supplier data.

  2. The code to onboard the application with Mobile Services and to display the initial activity is in Kotlin. This can be seen as the extension for the files is .kt rather than .java.

  3. Run the app. The welcome screen appears the first time the app is run.
    Enter your Mobile Services credentials to complete the onboarding.


    Once the onboarding completes, the main screen of the app is shown. This will be enhanced in part 2 of this blog with the help of SAP Fiori Mentor and Fiori for Android controls.


Retrieving Data


In this section, code will be added to retrieve the list of suppliers.

  1. Add the following function to the class SimpleActivity.kt.

    fun getSuppliers() {
    val sapServiceManager = (application as SAPWizardApplication).sapServiceManager
    var espmContainer = sapServiceManager.eSPMContainer
    // import com.sap.cloud.android.odata.espmcontainer.Supplier
    val query = DataQuery().orderBy(Supplier.supplierName)
    espmContainer?.getSuppliersAsync(query, { suppliers: List<Supplier> ->
    for (supplier in suppliers) {
    Log.d(this.javaClass.name, "${supplier.supplierName} from ${supplier.city}")
    }
    //showRecycler(suppliers);
    }, { re: RuntimeException -> Log.d(this.toString(), "An error occurred when querying for suppliers: " + re.message) })
    }

    If there are compilation errors in the code, it is likely due to missing. This can be corrected by using Android Studio's quick fix (Alt + Enter on Windows or Option + Enter on a Mac) or the Android Studio setting Add unambiguous imports on the fly.



    The following are a few things to notice if you are new to Kotlin.

    • In the Kotlin code above, notice that there are no semicolons at the end of each line.

    • Variables whose value will not be changed are declared using val rather than var.

    • Variable types are optional when they can be inferred, and variable names come before variable types.
      var espmContainer: ESPMContainer? = sapServiceManager.eSPMContainer
      var espmContainer = sapServiceManager.eSPMContainer // inferred type


    • Variable names come before the type.

    • Kotlin provides additional null checks. The getSuppliersAsync method will only be executed if espmContainer is not null due to the safe call operator ?.

    • The async variant of getSuppliers is used as synchronous networking requests are not allowed on the UI thread.

    • Notice that the getSuppliersAsync method uses Lambdas which are unnamed functions. Essentially, the getSuppliersAsync method takes three paraemters, a DataQuery, then a function that is called if the supplier list is returned and an error callback if the data cannot be returned.

    • The line that writes the supplier name and city to the log makes use of string templates.

    • Examples of the ability to mix Java and Kotlin code can be seen as the above Kotlin function uses Java classes ESPMContainer and Log.


    Additional information on the above topics can be found at Learn Kotlin by Example.

  2. At the end of the onCreate function, add the below call.
    getSuppliers()


  3. Run the project and examine the logcat. Notice that each supplier is listed.


In part 2, the SAP Fiori Mentor will be used to help with the code needed to display a progress indicator while the data is being retrieved and the returned suppliers will be shown using contact cells in a recycler view.

Next (Part 2 Fiori Mentor App)
4 Comments