Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
SAP How-to Guide SAP Mobility SAP Mobile Platform (SMP) How To... Use MAF Resources in Android Studio provided by SAP Mobile - Rapid Innovation Group Applicable Releases: SAP Mobile Platform 3.0 SP04+ SAP Mobile SDK 3.0 SP05+ Version 1.0 March 2015 © Copyright 2015 SAP AG. All rights reserved. All other product and service names mentioned are the trademarks of No part of this publication may be reproduced or transmitted in any form their respective companies. Data contained in this document serves or for any purpose without the express permission of SAP AG. The informational purposes only. National product specifications may vary. information contained herein may be changed without prior notice. The information in this document is proprietary to SAP. No part of this Some software products marketed by SAP AG and its distributors document may be reproduced, copied, or transmitted in any form or for contain proprietary software components of other software vendors. any purpose without the express prior written permission of SAP AG. Microsoft, Windows, Excel, Outlook, and PowerPoint are registered This document is a preliminary version and not subject to your license trademarks of Microsoft Corporation. agreement or any other agreement with SAP. This document contains IBM, DB2, DB2 Universal Database, System i, System i5, System p, System p5, System x, System z, System z10, System z9, z10, z9, iSeries, pSeries, xSeries, zSeries, eServer, z/VM, z/OS, i5/OS, S/390, OS/390, OS/400, AS/400, S/390 Parallel Enterprise Server, PowerVM, Power Architecture, POWER6+, POWER6, POWER5+, POWER5, POWER, only intended strategies, developments, and functionalities of the SAP® product and is not intended to be binding upon SAP to any particular course of business, product strategy, and/or development. Please note that this document is subject to change and may be changed by SAP at any time without notice. OpenPower, PowerPC, BatchPipes, BladeCenter, System Storage, GPFS, SAP assumes no responsibility for errors or omissions in this document. HACMP, RETAIN, DB2 Connect, RACF, Redbooks, OS/2, Parallel Sysplex, SAP does not warrant the accuracy or completeness of the information, MVS/ESA, AIX, Intelligent Miner, WebSphere, Netfinity, Tivoli and text, graphics, links, or other items contained within this material. This Informix are trademarks or registered trademarks of IBM Corporation. document is provided without a warranty of any kind, either express or Linux is the registered trademark of Linus Torvalds in the U.S. and other countries. Adobe, the Adobe logo, Acrobat, PostScript, and Reader are either trademarks or registered trademarks of Adobe Systems Incorporated in the United States and/or other countries. Oracle is a registered trademark of Oracle Corporation. UNIX, X/Open, OSF/1, and Motif are registered trademarks of the Open Group. Citrix, ICA, Program Neighborhood, MetaFrame, WinFrame, VideoFrame, and MultiWin are trademarks or registered trademarks of Citrix Systems, Inc. HTML, XML, XHTML and W3C are trademarks or registered trademarks of W3C®, World Wide Web Consortium, Massachusetts Institute of Technology. implied, including but not limited to the implied warranties of merchantability, fitness for a particular purpose, or non-infringement. SAP shall have no liability for damages of any kind including without limitation direct, special, indirect, or consequential damages that may result from the use of these materials. This limitation shall not apply in cases of intent or gross negligence. The statutory liability for personal injury and defective products is not affected. SAP has no control over the information that you may access through the use of hot links contained in these materials and does not endorse your use of third-party Web pages nor provide any warranty whatsoever relating to third-party Web pages. SAP “How-to” Guides are intended to simplify the product implementtation. While specific product features and procedures typically are explained in a practical business context, it is not implied that those features and procedures are the only approach in solving a specific Java is a registered trademark of Sun Microsystems, Inc. business problem using SAP NetWeaver. Should you wish to receive JavaScript is a registered trademark of Sun Microsystems, Inc., used additional information, clarification or support, please refer to SAP under license for technology invented and implemented by Netscape. Consulting. SAP, R/3, SAP NetWeaver, Duet, PartnerEdge, ByDesign, SAP Any software coding and/or code lines / strings (“Code”) included in this BusinessObjects Explorer, StreamWork, and other SAP products and documentation are only examples and are not intended to be used in a services mentioned herein as well as their respective logos are productive system environment. The Code is only intended better explain trademarks or registered trademarks of SAP AG in Germany and other and visualize the syntax and phrasing rules of certain coding. SAP does countries. not warrant the correctness and completeness of the Code given herein, Business Objects and the Business Objects logo, BusinessObjects, and SAP shall not be liable for errors or damages caused by the usage of Crystal Reports, Crystal Decisions, Web Intelligence, Xcelsius, and other the Code, except if such damages were caused by SAP intentionally or Business Objects products and services mentioned herein as well as their grossly negligent. respective logos are trademarks or registered trademarks of Business Disclaimer Objects Software Ltd. Business Objects is an SAP company. Some components of this product are based on Java™. Any code change Sybase and Adaptive Server, iAnywhere, Sybase 365, SQL Anywhere, in these components may cause unpredictable and severe malfunctions and other Sybase products and services mentioned herein as well as their and is therefore expressively prohibited, as is any decompilation of these respective logos are trademarks or registered trademarks of Sybase, Inc. components. Sybase is an SAP company. Any Java™ Source Code delivered with this product is only to be used by SAP’s Support Services and may not be modified or altered in any way. Document History Document Version Description 1.00 First official release of this guide Typographic Conventions Icons Type Style Description Icon Example Text Words or characters quoted from the screen. These include field names, screen titles, pushbuttons labels, menu names, menu paths, and menu options. Cross-references to other documentation Example text Emphasized words or phrases in body text, graphic titles, and table titles Example text File and directory names and their paths, messages, names of variables and parameters, source text, and names of installation, upgrade and database tools. Example text User entry texts. These are words or characters that you enter in the system exactly as they appear in the documentation. <Example text> Variable user entry. Angle brackets indicate that you replace these words and characters with appropriate entries to make entries in the system. EXAMPLE TEXT Keys on the keyboard, for example, F2 or ENTER. Description Caution Note or Important Example Recommendation or Tip Table of Contents 1. Business Scenario .....................................................................................................................1 2. Background Information ...........................................................................................................1 3. Prerequisites..............................................................................................................................1 4. Step-by-Step Procedure .......................................................................................................... 2 4.1 4.2 4.3 4.4 4.5 4.6 4.7 4.8 4.9 5. Setup gradle in the PATH system variable ................................................................... 2 Extract the content of the MAF resource apklibs into separate folders .................... 5 Create New Android Studio Project .............................................................................. 7 Import MAF Resources as Modules in Android Studio .............................................. 12 Set Up MAF Dependencies ...........................................................................................17 Import SMP Libraries ................................................................................................... 19 Implement MAFLogonActivity..................................................................................... 24 Review AndroidManifest.xml ....................................................................................... 29 Run the Application ......................................................................................................30 Appendix ................................................................................................................................. 33 How To... Use MAF Resources in Android Studio 1. Business Scenario In December 2014, Google launched Android Studio, Google’s official Integrated Development Environment (IDE) based on IntelliJ. Google is recommending developers to leave Eclipse behind; hence the purpose of this guide is to help you set up the MAF logon component in an Android Studio project. The MAF logon component is available in the SMP 3.0 SDK and provides easy integration for applications that use logon UI behavior. 2. Background Information The goal of this exercise is to show the key pieces of code and information needed to integrate MAF Logon component to a new or an existing Android Project 3. Prerequisites This exercise has the following prerequisites: Android Studio installed Access to a SAP Mobile Platform 3.0 Server SP04+ to test the onboarding SAP Mobile SDK 3.0 SP05+ downloaded To get the most out of this exercise, some experience with Java is recommended. March 2015 1 How To... Use MAF Resources in Android Studio 4. Step-by-Step Procedure The following sections provide a detailed step-by-step procedure on how to set up your development environment to develop android applications with Android Studio 4.1 Setup gradle in the PATH system variable Check Appendix D – Running gradle command on Mac setup gradle in the path on Mac 1. Find where gradle was installed. By default gradle is installed on the Android Studio directory. 2. Set your PATH variable to the bin folder where the gradle files are located 3. Right-click the My Computer icon on your desktop and select Properties. March 2015 2 How To... Use MAF Resources in Android Studio 4. Click the Advanced tab. 5. Click the Environment Variables button. March 2015 3 How To... Use MAF Resources in Android Studio 6. Select path in the System variables and click on Edit 7. Add semicolon to separate the different paths and add the gradle path in your machine (i.e … ;C:\Program Files\Android\Android Studio\gradle\gradle-2.2.1\bin). 8. Click OK until you close all the windows March 2015 4 How To... Use MAF Resources in Android Studio 4.2 Extract the content of the MAF resource apklibs into separate folders To use the MAF Login Component you must import a number of Libraries and Resources. MAF Libraries were installed with SMP Client SDK installer into the folder specified by you when you executed the installer. MAF is shipped with various resources. These projects have Android nature, but don’t ship any source code. Their only purpose is to ship all Android resource files like: images, layouts, localization, XMLs As a result of the SDK installer you will find the following .apklib files in the installation folder: <Client SDK dir>\NativeSDK\ODataFramework\Android\resources\maflogonui.akplib <Client SDK dir>\NativeSDK\MAFReuse\Android\resources\mafsettingscreen.apklib <Client SDK dir>\NativeSDK\MAFReuse\Android\resources\mafuicomponents.apklib The following steps will extract the content of all resources apklibs into separate folders 1. Open a command terminal and type gradle to make sure you set up the PATH environment variable correctly 2. Create a file name setupSDK.gradle with the following graddle script task prepareResourceProjects() { def FileTree tree = fileTree(dir: '.', include: ['MAFReuse/**/*.apklib', 'ODataFramework/**/*.apklib']) def fileSet = tree.getFiles() for (file in fileSet) { def filename = file.getName() def dirname = filename.substring(0, filename.lastIndexOf('.')) def zipFilePath = file.getPath() def intoDir = file.getParent() + File.separator + dirname def Copy resCopy = task('extract'+dirname, type: Copy) resCopy.from zipTree(zipFilePath) resCopy.into intoDir March 2015 5 How To... Use MAF Resources in Android Studio resCopy.execute() new File(intoDir + '/src').mkdirs() } def Delete cleanup = task('cleanup', type: Delete) cleanup.delete 'build' cleanup.execute() } 3. Save setupSDK.gradle in the SMP3.0 SDK installation folder (i.e ‘C:\SAP\MobileSDK3\NativeSDK’) 4. Open a command prompt at the file system location ‘C:\SAP\MobileSDK3\NativeSDK’. 5. Run the gradle task that transforms the resources apklib files into Android Studio modules with the following command: ‘gradle -b setupSDK.gradle prepareResourceProjects’ March 2015 6 How To... Use MAF Resources in Android Studio 6. Confirm the last step extracted the content of the MAF resource apklibs into separate folders 4.3 Create New Android Studio Project In this section we will create an Android project from scratch using the simple template in Android Studio. 1. Open Android Studio and select the option: Start a new Android Studio project March 2015 7 How To... Use MAF Resources in Android Studio March 2015 8 How To... Use MAF Resources in Android Studio 2. Enter the application name, company domain and project location and click Next. 3. Select Phone and Tablet as the target android devices we want to support and keep the Minimum SDK as API 15 to cover most of the Android devices active on Google Play Store. You can change it according to your requirements. March 2015 9 How To... Use MAF Resources in Android Studio 4. Select the type of activity you want to create and click Next March 2015 10 How To... Use MAF Resources in Android Studio 5. Customize the activity and click Next 6. Your application should look like the image below March 2015 11 How To... Use MAF Resources in Android Studio 4.4 Import MAF Resources as Modules in Android Studio You have created an android app and now you want to include the MAF resources as modules. 1. Select Import Module from the File menu. 2. Click on the Browse button and navigate to the SMP 3.0 SDK installation folder and find the mafsettingscreen-res-xxx resource folder. Click on Next <Client SDK dir>\NativeSDK\MAFReuse\Android\resources\mafsettingscreen-res-xxx March 2015 12 How To... Use MAF Resources in Android Studio 3. Choose all options and click Finish. The first two options detect source and binary libraries and will replace these with maven dependencies instead. Since MAF resources don’t contain other libraries, it shouldn’t make a difference whether you select these options or not. The third option rewrites the project using the canonical Android Gradle project structure. Please select this option. 4. When the import finishes, an import-summary.txt will be displayed that explains the files that were moved. Your Android Studio should look like the image below March 2015 13 How To... Use MAF Resources in Android Studio 5. If you see errors in the bottom section, you can click on the suggested fixes to eliminate the errors. Appendix A show some errors that may appear 6. Open the build.gradle file of the imported module 7. Change apply plugin: com.android.application to apply plugin: com.android.library 8. Remove applicationId from the defaultConfig section 9. The build.gradle file for the mafsettingscreen module should look like the image below March 2015 14 How To... Use MAF Resources in Android Studio 10. Repeat steps 1-9 to import mafuicomponents-res-xxx resource folder as module <Client SDK dir>\NativeSDK\MAFReuse\Android\resources\mafuicomponents-res-xxx 11. The build.gradle file for the mafuicomponents module should look like the image below 12. Repeat steps 1-9 to import maflogoui-res-xxx resource folder as module <Client SDK dir>\NativeSDK\ODataFramework\Android\resources\maflogonui-res-xxx March 2015 15 How To... Use MAF Resources in Android Studio 13. The build.gradle file for the mafuicomponents module should look like the image below 14. Your project should look like the image below March 2015 16 How To... Use MAF Resources in Android Studio 4.5 Set Up MAF Dependencies This section explains how to set up the dependencies to the MAF resources we just imported 1. Right-click the dependent module and select Open > Module Settings. 2. Right-click the plus icon to add a new dependencies. March 2015 17 How To... Use MAF Resources in Android Studio 3. Select the MAF resources and click OK 4. Click Apply to create the dependency. 5. If you open build.gradle file in the app module you check the dependencies were added March 2015 18 How To... Use MAF Resources in Android Studio 4.6 Import SMP Libraries 1. Locate your SMP 3.0 SDK installation folder. You will find the jar files in the two different folders: <Client SDK dir>\NativeSDK\ODataFramework\Android\libraries <Client SDK dir>\NativeSDK\MAFReuse\Android\libraries 2. Using the Project view, copy the following jar libraries from the SDK installation folders and paste it into the libs folder in Android Studio: AfariaSLL.jar HttpConvAuthFlows.jar ODataAPI.jar ClientHubSLL HttpConversation.jar odataoffline.jar (offline store) ClientLog.jar maflogger.jar ODataOnline.jar (online store) Common.jar maflogoncore.jar perflib.jar Connectivity.jar maflogonui.jar Request.jar CoreServices.jar mafuicomponents.jar sap-e2etrace.jar DataVaultLib.jar mafsettingscreen.jar SupportabilityFacade.jar E2ETrace.jar MobilePlace.jar XscriptParser.jar March 2015 19 How To... Use MAF Resources in Android Studio 3. Go to the Project view 4. Paste the libraries into the libs folder 5. A confirmation window will appear. Click OK Remember all the required jar file are locates in two different folders, make sure you include all the libraries indicated in step 2 March 2015 20 How To... Use MAF Resources in Android Studio 6. Select the libraries, right click on the selected libraries and select Add as library from the context menu 7. Add it to the app module and click OK March 2015 21 How To... Use MAF Resources in Android Studio 8. You can check the libraries were added in the build.gradle file located in the app module March 2015 22 How To... Use MAF Resources in Android Studio Mandatory if using Offline Store If you are going to use the Offline store, remember you need to integrate native libraries (.so resources) in your app. The following steps explains how to do it in Android Studio 9. Right-click on main folder and select New -> Directory from the context menu 10. Enter jniLibs in the directory name field and click OK 11. Locate your SMP 3.0 SDK installation folder. You will find libmlcrsa16.so and the libodataofflinejni.so files in the following folder: <Client SDK dir>\NativeSDK\ODataFramework\Android\libraries\armeabi March 2015 23 How To... Use MAF Resources in Android Studio 12. Copy the armeabi folder and paste it into jniLibs in Android Studio. For the Offline Store you will need libmlcrsa16.so and libodataofflinejni.so, you can delete the rest. 4.7 Implement MAFLogonActivity Now that we have successfully set up the Android project, we can use the MAF Logon resources in our project. 1. Go to src ->main -> java -> sample.sap.comp.travelagency 2. Right-click on the sample.sap.comp.travelagency package to open the context menu March 2015 24 How To... Use MAF Resources in Android Studio 3. 4. Select New -> Activity -> Blank Activity Enter the following information to create an Activity and click Finish. Check the Launcher Activity option to define this activity as the main screen. March 2015 25 How To... Use MAF Resources in Android Studio 5. Both MAFLogonActivity and activity_maflogon layout will open. Select MAFLogonActivity 6. Type “implements LogonListener” to the class signature and use the autocomplete option March 2015 26 How To... Use MAF Resources in Android Studio 7. With the cursor on the “public” descriptor, a bulb icon will appear. Click on the arrow and choose Implement Methods from the options 8. Select Implement Methods and then select all the methods and click OK March 2015 27 How To... Use MAF Resources in Android Studio 9. Go to the onCreate method and replace the implementation with the following code. @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // set context reference Context mContext = this; // get an instance of the LogonUIFacade LogonUIFacade mLogonUIFacade = LogonUIFacade.getInstance(); //Initialize the Logon UI Facade mLogonUIFacade.init(this, mContext, <name of your application id>); // ask LogonUIFacede to present the logon screen // set the resulting view as the content view for this activity setContentView(mLogonUIFacade.logon()); } setContentView method is used to present the logon screen from the MAFLogonActivity 10. If you have compilation errors it may be because your settings in Android Studio don’t allow for Auto Import during pasting. you can check Appendix B to change your Auto import settings you can select the classes and hit Alt+Enter to import it automatically 11. Locate onLogonFinished and complete the method with the following code. This method redirects to the main screen when the registration finishes This callback method handles BOTH success and error cases. @Override public void onLogonFinished(String s, boolean b, LogonContext logonContext) { String TAG = MAFLogonActivity.class.getSimpleName(); Log.d(TAG, "onLogonFinished: "+s); //Check if it finished successfully if (b) { try { //For debugging purposes will log the app connection id and // the end point url. // In a productive app, remember to remove these logs String appConnID = LogonCore.getInstance().getLogonContext() .getConnId(); Log.d(TAG, "onLogonFinished: appcid:"+ appConnID); March 2015 28 How To... Use MAF Resources in Android Studio Log.d(TAG, "onLogonFinished: endpointurl:"+ logonContext.getEndPointUrl()); } catch (LogonManagerException e) { Log.e(TAG, e.getLocalizedMessage(), e); } catch (LogonCoreException e) { Log.e(TAG, e.getLocalizedMessage(), e); } // Navigate to the Main menu screen Intent goToNextActivity = new Intent(this, MainActivity.class); startActivity(goToNextActivity); finish(); } } 7. Save the changes 4.8 1. Review AndroidManifest.xml Add in the AndroidManifest.xml the INTERNET permission: <!-- allow connections to Internet Services. --> <uses-permission android:name="android.permission.INTERNET" /> 2. Delete the intent-filter from the MainActivity. This was automatically added by Android Studio during the project creation wizard. 3. The main screen is the MAFLogonActivity, make sure it contains the <intent-filter> tag with the action MAIN and the category LAUNCHER. 4. Your manifest file should look like the image below. March 2015 29 How To... Use MAF Resources in Android Studio 4.9 1. Run the Application Click on the Run button and wait for the Android Studio to launch the app March 2015 30 How To... Use MAF Resources in Android Studio 2. If you have a test device to run the app, it will appear in the Device window. Select your device and click OK. If you don’t have a test device you can set up an emulator following Appendix C 1. Click on Login March 2015 2. Click on Cancel, because we are not using Mobile Place for the purpose of this exercise 31 How To... Use MAF Resources in Android Studio 3. Enter SMP Host (10.0.2.2 if your SMP server is localhost) and login credentials. Scroll down to enter values in the next fields 4. Enter SMP port (8080 default) and check the Unsecured Connection if is HTTP or uncheck it if it’s HTTPS and click Next 5. Uncheck “use application code” and click Done 6. You should see the main screen with text “Hello World” 3. Congratulations you finished the exercise March 2015 32 How To... Use MAF Resources in Android Studio 5. Appendix Appendix A – Errors during MAF resource imports These are the errors I received when I import the MAF resource as module. I was able to follow the links to get the platforms and tools needed to synchronize the project. 1. One of the errors was that Android Studio couldn’t find target android-8 required by the MAF resources 2. You can click on Install missing platform(s) and sync project and follow the wizard 3. Another error I received was Failed to find Build Tools revision 21.1.1 March 2015 33 How To... Use MAF Resources in Android Studio 4. You can click on Install Build Tools 21.1.1 and sync project and follow the wizard 5. After I imported the Build Tools and the missing platforms, the Android Studio was able to build the project successfully March 2015 34 How To... Use MAF Resources in Android Studio Appendix B – Change Auto Import Settings 1. For Windows, you can go to File -> Settings -> Editor -> Auto Import -> Java and make the following changes: Change Insert imports on paste value to All Mark Add unambiguous imports on the fly option as checked 2. On a Mac, do the same thing in Android Studio -> Preferences Appendix C – Emulator Set Up 1. From Android Studio open the AVD Manager 2. Click on Create Virtual Device 3. Follow the wizard. This is an example, but you can experiment with different emulators March 2015 35 How To... Use MAF Resources in Android Studio 4. Select armeabi CPU, because the SMP 3.0 SDK supports armeabi. 5. Enter an AVD name and click Finish March 2015 36 How To... Use MAF Resources in Android Studio 6. Launch the emulator 7. When you run the app, you can choose “Launch Emulator” and select the emulator you just created. March 2015 37 How To... Use MAF Resources in Android Studio Appendix D – Running gradle command on Mac 1. Go to https://gradle.org/downloads/ 2. Download the Binary only distribution 3. Unzipped gradle-2.3-bin.zip in a local folder 4. Open .bash_profile and add the following lines to include the Gradle folder in the PATH variable. GRADLE_HOME=<your location>/gradle-2.3/ export PATH=$GRADLE_HOME/bin:$PATH 5. Open a command terminal and type gradle to make sure you set up the PATH environment variable correctly March 2015 38 www.sap.com/contactsap http://scn.sap.com/community/mobile http://developers.sap.com/mobile SAP Mobile Platform How-To Guides