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
Høgskolen i Telemark Telemark University College Department of Electrical Engineering, Information Technology and Cybernetics Android Programming Cuong Nguyen, 2013.06.18 Faculty of Technology, Postboks 203, Kjølnes ring 56, N-3901 Porsgrunn, Norway. Tel: +47 35 57 50 00 Fax: +47 35 57 54 01 Table of Contents Introduction............................................................................................................................................. 3 1 2 Technical Implementation .............................................................................................................. 4 1.1 Step1: Download and install ADT bundle ................................................................................ 4 1.2 Step 2: Get Java Development Kit ........................................................................................... 6 1.3 Step 3: Set-up Java-ADT........................................................................................................... 8 1.4 Step 4: Set-up Android Virtual Device ..................................................................................... 9 Create, execute & run app ............................................................................................................ 11 2 1 Introduction This tutorial will go through how to set-up an Android programming environment on Windows 7 platform. Once the Android programming platform is set-up you can start to create (develop) your own Android applications with no limitations. Previously to set-up an Android programming interface one has to down load several different packages from various locations. Recently all of the different packages are gathered into one single package called for ADT (Android Development) Bundle. With one single download you have a package which includes all the necessary equipment ready to begin developing Android applications. The package includes: Eclipse + ADT (Android Development Tools) plugin Android SDK (Soft Ware Development) Tools Android Platform-tools The latest Android platform The latest Android system image for the emulator To have an Android android development Tool up and running, the following steps are involves: Step 1: Download and Install ADT Bundle Step 2: Download Java Development Kit (JDK) Step 3: Set-up Java –ADT Step 4: Set-up Android Virtual Device This tutorial also includes how to create your first Android application and execute the code on the Android Virtual Device which prints a “Hello World!” text string. After finishing this complete tutorial you will also be able to install and run your application on a real Android device (smart phones or tab boards). Figure 1-1 illustrates a simple app running on an Android Virtual Device. Figure 1-1: Android application that prints "Hello world" TextView. 3 2 Technical Implementation This section will explain how to set-up an Android programming environment (JAVA - ADT) process in details, which covers all the involving four steps stated above. By following the instructions, as the result you will create your first Android application (Hello World!). Additionally, you will also be able to run the created application on both Android Virtual Device and on a real Android device. 2.1 Step1: Download and install ADT bundle First of all we need to download an Android SDK (Software Development Kit) zip file. The Android SDK provides the API libraries and developer tools necessary to build, test and debug apps for Android. To download Android SDK Open a web browser and go to the link given below. http://developer.android.com/sdk/index.html Click on “Download the SDK ADT Bundle for windows” button to download. Note: if you are using other Operative System you have to find a corresponding version. Figure 2-1: ADT Bundle for windows download button icon. Check box: I have read and agree with the above terms and conditions Choose: 32-bit or 64-bit INFO: depending on your Windows version. Click: OK to save the zip-file to your computer. As seen on figure 2-2. 4 5 Technical Implementation Figure 2-2: Prompt window for options selection. To install Android SDK Locate: the zip-file location and extract → Open: the adt-bundle folder → Open: eclipse folder → double click “eclipse” file to start the installation. Figure 2-3: Eclipse installing file. During installation a new pop up window will appear asking for Java Development Kit (JDK) → OK to continue. Figure 2-4: Java Development Kit required. Android Programming 6 Technical Implementation 2.2 Step 2: Get Java Development Kit To download & install Java Development Kit (JDK) Open a web browser → enter the link stated below. http://www.oracle.com/technetwork/java/downloads/index.html. Click Java Platfrom (JDK) 7u17. Note! Always download the latest version. Click: Accept License Agreement Click: jdk-7u17-windows-x64.exe. Note: Choose the version which corresponds to your current Operative System. Locate the saved exe file → double click to install JDK. Just click on “NEXT” (no changes is needed) until the program finish the installation. Figure 2-5: JDK successful installed confirmation. When JDK is successfully installed, just click on close. (Registration is not necessary) Now double click on eclipse to start the installation again. Click: OK on the pop up window will appear as shown on figure 2-6. Note! Or you can also specify a location to create workspace folder. This folder will contain all the files for your project (Android application). Figure 2-6: Specify the projects location. Android Programming 7 Technical Implementation After a successful installation, Java-ADT (Android programming environment) will be launch as seen in figure 2-7. You can either click on “Build Your First App” to learn step by step tutorial on how to create your first android application from the android homepage. As second alternative you can continue on this documentation to create your first Android application. If you still want to follow this tutorial. Click: on the “x” button (next to “Android IDE”) to close the instructed window. Figure 2-7: Options selection: Start create new app or study tutorials. After clicked on the “x” button you will enter into Android developing (programming) environment as seen in figure 2-8. Figure 2-8 illustrates the empty development interface without any android project specified yet. To start creating your Android app you need to setup Java-ADT first. Figure 2-8: An empty Android Development environment. Android Programming 8 Technical Implementation 2.3 Step 3: Set-up Java-ADT The Android developing environment needs to be prepared unless any application is able to create. We need to load all the Android versions (old versions and latest version) to the Java-ADT. Start Android SDK Manager Click on the android icon: INFO: Choose one of the options shown in figure 2-9. Figure 2-9: Start Android SDK Manager. Tick all the necessary packages as seen on figure 2-10 to install INFO: We choose to select from Android 2.2 to the currently latest version which is Android 4.2.2. Additionally we also need to select Extra package. At the compilation stage Java-ADT will ensure that the codes are compatible to all the selected versions. Figure 2-10: Install neccessary Android versions. Android Programming 9 Technical Implementation Accept License for each package as seen in figure 2-11 → Install Note! This installing process is very time consuming and may take approximately up to one hour. Figure 2-11: Accept License for all packages to proceed installing. Close the Android SDK Manager when “Done loading packages” appears. 2.4 Step 4: Set-up Android Virtual Device To test the creating Android app we need to push the existing codes to an Android Virtual Device, from which the app will runs as simulated on a real device. Start Android Virtual Device Manager Click: Window → Android Virtual Device Manager. INFO: Choose one of the options illustrated in figure 2-12. Figure 2-12: Create an Android emulator (AVD). Android Programming 10 Technical Implementation Create New Android Virtual Device Click: New Fill in all the necessary information as sown in figure 2-13. INFO: The chosen feature from figure 2-13 fits the characteristic to an Android mobile which the app is created towards. The only changes needed here is “Device” (change the size that best corresponds to the real device). Additionally, always select the latest android version in “Target”. Figure 2-13: Set-up parameters for emulator. Your new created Android Virtual Device by the name MyDevice (can be given any name) is listed as shown in figure 2-14. Close the window. Figure 2-14: Emulator successfully created confirmation. The Android development environment (JAVA-ADT) on your computer is now ready to create any Android application. Next step is to create an Android app and run the app on an Android Virtual and on a real device. Android Programming 3 Create, execute & run app To create your first Android application, first of all we need to create a new project and give it a name and other necessary features to the project. Create a new Android application File → New → Android Application Project NOTE!! Package Name should be unique. A good practice is to give a reverse of the companies site name. → Click Next to continue. Figure 3-1: Setting for new creating app. Specify where you want to save the Android project. INFO: Good practice is to always have your project in the “workspace” folder. Figure 3-2: Projects source location. 11 12 Create, execute & run app Click “Next” button until Finish. INFO: no changes required. Once the project set-up is finished the Android developing interface will appears as seen in figure 3-3. As default the system launch a new created app with a “TextView” string (Hello world!). Basically your first application is created automatically. Figure 3-3 illustrates the working interface from which Android developers create, manipulate, test, debug and execute the codes. Figure 3-3: Android development interface with "MyFirstApp" project. A few important point outs regarding Android developing interface. The important point outs can be seen on the figure 3-4. Package Explorer → values → strings.xml All the text strings in the project are specified in the strings.xml file Android Programming 13 Create, execute & run app Package Explorer → layout → activity_main.xml All the layout related codes are specified from activity_main.xml file. From here developers are able to add Edit Texts, TextViews, buttons and modify the size and colors. Package Explorer → src → MainActivity.java Code related to events, interactions, methods, functions in other words functional codes are specified in this MainActivity.java. Palette → Form Widgets, Text Fields, Layouts, Composite, Images & Media, Time & Date… Developer can add functions listed in different libraries belong to their categories to the developing app. All the basic functions are provided. Figure 3-4: Important files and features regarding app development. Android Programming 14 Create, execute & run app When an xml file is double clicked it appears on the right hand side column. Developers are able to modify the code from here. Figure 3-5 shows an example three xml files (activity_main, strings.xml, MainActivity.java) are opened and ready to be modified. Click on each of the xml file that is being opened and study the containing. MainActivity.java contains codes with focused on class, object, methods. activity_main.xml contains a codes for layout of an TextView (hello_world). strings_xml contains all the needed text strings. From the figure 3-5 the strings.xml for this app contains three necessary text strings. INFO: “app_name” is name of your app, which were given by you during the setup of the project. “hello_world” is the TextView string name. Figure 3-5: Open files for editing. Android Programming 15 Create, execute & run app Regarding activity_main.xml: when you click on the “activity_main.xml” below you can find two buttons “Graphical Layout”, ”activity_main.xml”. The codes are written in the activity_main.xml, to see how the written codes effects the actual layout just simply click on “Graphical Layout” button. For every code modified on the activity_main.xml will effect the layout on Graphical Layout. Figure 3-6: Swich between graphical layout and code file. When you created a new Android project (My First Application) the Java-ADT automatically created a TextView string (Hello world!). Actually no changes are needed, you can just execute and run the app directly on the Virtual Device. Execute and run the app Simulate an application in a Virtual Device File → Save to save all the changes Click: “My First App” → Project → Clean… Click: “My First App” → Run → Run as → 1 Android Application Recommend: Repeat all the three steps above every time you run your app. NOTE!! Important to click on “My First App”. Wait approximately a few minutes for your computer to load the Virtual Device (AVD). In case a window is pop-up as, seen in Figure 3-7, just click Yes → OK to continue. INFO: unlike other programming languages, to compile and run codes on Android is a very slow process, due to the reason the required time to launch AVD. Once AVD appears → unlock → Wait. Android Programming 16 Create, execute & run app Figure 3-7: Enable logcat function. Your new created “My First App” is running on Android Virtual Device by the given name MyDevice. Figure 3-8: "My First App" running on Android emulator. Run the app on a real Android Device All the written codes are compressed in a “apk” file, in this case the file name is MyFirstApp.apk. This specific file has to be downloaded (transferred) and installed on the real device. The file can be passed to the android device throug Bluetooth technology or simply by using a cable. Location of apk file: workspace → MyFirstApp → bin folder → MyFirstApp.apk INFO: In my case the whole path to the MyFirstApp.apk is located in C:/User/levo/workspace/MyFirstApp/bin/MyFirstApp.apk Note!! Levo is my user name, so it is different name in your computer. Android Programming Høgskolen i Telemark Telemark University College Department of Electrical Engineering, Information Technology and Cybernetics Telemark University College Faculty of Technology Kjølnes Ring 56 N-3918 Porsgrunn, Norway www.hit.no Faculty of Technology, Postboks 203, Kjølnes ring 56, N-3901 Porsgrunn, Norway. Tel: +47 35 57 50 00 Fax: +47 35 57 54 01