[[PageOutline]] = App Development = [=#overview] == Overview == This page explains the basic steps to begin development on Android applications, as well as the set up required to install and use the Android Studio IDE. The examples on this page assume that you have little to no Android development experience. For further training on Android Studio and Android app development in general, see [http://developer.android.com/training/index.html Android's Developer Training page]. [=#setup] == Setting Up a Development Environment == While there are numerous development environment set ups available to android developers, the recommended option for users new to android would be the '''Android Studio IDE''', which is now the official IDE supported by Google. Support for Eclipse has been planned to discontinue and it is suggested to migrate your eclipse projects to Android Studio in order to ensure continued updates and support. For linux users wishing to avoid IDEs, steps will also be provided for '''command line''' installation of the tools required to begin development. === Android Studio IDE === Here are the steps for installing and initially configuring android studio on a Linux system or VM. In all cases below be sure to replace the items within <> to those appropriate to what you downloaded. 1. Update your machine with the Software Updater or via commandline: {{{#!bash sudo apt-get upgrade }}} 2. Verify you have JDK version 6 or higher (7 is required for development of Android 5.0 and above) from the command line: {{{#!bash javac --version }}} * Note that the OpenJDK can generally be used in place of the Oracle JDK. 2a. If you do not, browse to Oracle's [http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html JDK Downloads] page and download the linux 32/64 bit tar.gz after accepting the license agreement (above the download link). 2b. Navigate to a directory of your choice and install the jdk: {{{#!bash tar -zxvf }}} 2c. Set JAVA_HOME (you can add these to the bottom of your ~/.bashrc file to do this every time you open a shell - if you do this make sure you open a new shell for them to take effect): {{{#!bash export JAVA_HOME=// export JDK_HOME=// }}} 3. Visit the Android [http://developer.android.com/sdk/index.html Developer Downloads] page and download the appropriate Android Studio bundle for your system. If you are just interested in the SDK tools, they are also available [http://developer.android.com/sdk/index.html#Other here]. 4. Navigate to an appropriate directory and extract Android Studio with your !File/Archive Manager or with the command line: {{{#!bash unzip }}} 5. Launch Android Studio. From the command line: {{{#!bash android-studio/bin/studio.sh }}} Note: You may want to add android-studio/bin/ to your PATH environmental variable so that you can start Android Studio from any directory. 6. Select the SDK Manager button in the Android Studio toolbar [[Image(http://developer.android.com/images/tools/sdk-manager-studio.png)]] and install the preselected packages which should at least include: * Android SDK tools * Android SDK Platform-tools * Android SDK Build-tools * The latest Android version's SDK platform * A target system image such as ARM EABI v7a System Image. You can always come back and install new packages here as your project requires. [=#cmdline] === Command Line === Execute the following commands below. If you already have version 1.6 or greater of the oracle jdk skip to step 4. You can verify your jdk version with {{{javac --version}}}. 1. Download the oracle jdk (replace with latest version of JDK as required by desired Android API): {{{#!bash wget --no-check-certificate --no-cookies \ --header "Cookie: oraclelicense=accept-securebackup-cookie" \ http://download.oracle.com/otn-pub/java/jdk/7u79-b15/jdk-7u79-linux-x64.tar.gz }}} * Note that the OpenJDK can generally be used in place of the Oracle JDK. 2. Extract contents {{{#!bash tar -zxvf jdk-7u79-linux-x64.tar.gz }}} 3. Set JAVA_HOME and JDK_HOME to the directory of the extracted jdk in your preferred .bash file. Example: {{{#!bash #!/bin/sh echo "export JAVA_HOME=/home/user/jdk1.7.0_79" >> ~/.bashrc echo "export JDK_HOME=/home/user/jdk1.7.0_79" >> ~/.bashrc . ~/.bashrc }}} 4. Download the Android SDK Tools (replace with current version found [http://developer.android.com/sdk/index.html#Other here]): {{{#!bash wget http://dl.google.com/android/android-sdk_r24.4.1-linux.tgz }}} 5. Extract contents: {{{#!bash tar -zxvf android-sdk_r24.4.1-linux.tgz }}} 6. Add SDK packages: {{{#!bash android-sdk-linux/tools/android sdk }}} [=#avd] === (Optional) Create an Android Virtual Device === For developmental or compatibility testing purposes a virtual device can be added to android studio to run your apps on. You can create a new virtual device from the precomposed profiles or edit a new profile to your specifications using the Android Virtual Device Manager. You can access the manager by either * The AVD Manager button in the Android Studio toolbar [[Image(http://developer.android.com/images/tools/avd-manager-studio.png)]] * From your Android SDK installation directory: {{{#!bash android-sdk-linux/tools/android avd }}} An example Virtual device configuration that mimics the hardware and software profile of our Ventana products would be the following: * Device: 7" WSVGA (Tablet)(1024x600: mdpi) * Target: Android 4.4.2 - API Level 19 (You may need to download this via the package manager first) * CPU/ABI: x86 - if you will be using any pre-compiled objects for an ARMv7 target like the Gateworks Ventana use ARM(armeabi-v7a), otherwise use x86 to avoid the large performance hit of emulation * No hardware keyboard present * Skin: Skin with dynamic hardware controls * Front Camera: Emulated * Back Camera: None * RAM: 512, VM Heap: 16 * Internal Storage: 2GiB * SD Card: 100 MiB * Emulation Options: Use Host GPU You can always create new or edit your existing virtual devices using the manager at any time. [=#helloworld] == Creating a Simple “Hello World!” App == To show the basic fundamentals of creating a new project with Android Studio we will make an app that simply displays the static text Hello World! The following paragraphs will explain each dialogue screen in the Android Studio ''Create New Project'' wizard. Begin by selecting {{{File > New > New Project}}} in the studio toolbar. The first new project screen goes over naming and placing your Android application. The conventional naming scheme for Android apps involves constructing the package name as the reverse company domain followed by the application name. For this example, use "!HelloWorld" as the application name and "gateworks.com" as the company domain. Keep the default project location as is and select {{{Next}}} to continue. The next screen covers what type of devices will be able to run your application. Leave "Phone and Tablet" selected and assign the Minimum SDK to '''API 19: Android 4.4 (!KitKat)'''. As Android Studio states, lowering the API level will make your app compatible with a larger percentage of the devices being publicly used but as a trade off you will not have access to the latest Android features. Using API level 19 will ensure that your app will be compatible with most Gateworks Ventana boards which use Android !KitKat. Select {{{Next}}} to continue. The following screen displays a number of template activities that the user can select from to get a head start on development. Any activity selected here will be considered the launcher activity of the application, or in other words, will be the first activity displayed when a user runs your application. For the purposes of this tutorial, select the '''Empty Activity''' then select {{{Next}}}. The final screen covers naming the source files that will be used for your application (Note that this final page can differ depending on which activity you selected from the previous menu). Firstly, make sure that the '''Generate Layout File''' option is selected. The Activity Name will dictate the name of the .java source file that handles the procedural logic executed when your app is first launched. The Layout Name specifies the name of the .xml file that is responsible for the formatting of visible elements in the app window. Leaving these names as their defaults is acceptable. Select {{{Finish}}} to finally create the project. Running your app can be done by selecting the run button in the studio toolbar, or via {{{Run > Run 'app'}}}. If you have no connected Android devices, and wish to start your app on an emulated device, select the {{{Launch emulator}}} radio button and your virtual device from the dropdown menu then select ok (if you have not created a virtual device, see the section on [#avd creating a virtual device]). If your virtual device is not already running, it may take a few minutes for it to start up depending on your selected hardware configuration, this is normal. If you have multiple devices connected such as multiple emulators, or a physical device as well as an emulator you will be prompted to choose a device to run on. To use a physical device simply connect it to the host computer over USB OTG and enable USB debugging on the device. === Modifying your app === To showcase the process of making a simple modification to our example app, we'll step through the process of adding a button to the main activity that randomizes the color of the existing "Hello World!" text. Android Applications separate layout (presentation) from logical code. The layout is in XML files and the code is in java files. Within the IDE the '''Component Tree''' will provide a hierarchical representation of the various graphical components. Selecting a component will let you see and alter its physical properties in the '''Properties''' box. You can choose to view the layout in '''Design''' form or '''Text''' form by selecting the appropriate tab at the bottom of the Design box. When in '''Design''' view, you can drag-and-drop various graphical components from the '''Palette''' onto the design and arrange them as desired. The '''Text''' view lets you edit the XML representation of the layout, which is how it is stored. 1. In the '''Text''' view mode (view modes can be selected from the tabs at the bottom of the view window when editing an xml file), modify the !TextView in addition to adding a Button widget to the activity_main.xml inside of the !RelativeLayout tag. In the '''Design''' view mode you can also find the !TextView in the '''Component Tree''' within the IDE nested under Device Screen -> !RelativeLayout. Make the !TextView and Button tags appear like so: {{{#!xml