# Ubuntu SDK

## Introduction

In the previous chapter we have seen an introduction to the programming course with Ubuntu Touch. The next step is to prepare the development environment. When programming in any language a SDK (Software Development Kit) is used. The SDK consists of a set of tools that process the source code and generate the executable for the platform that is being used. Ubuntu Touch is not an exception and also has its own SDK.

Applications in GNU / Linux are in repositories. The repository contains a set of applications that can be easily installed. On some occasions there are applications that are not in the official repositories. For these cases, it is possible to use personal repositories, also known as PPA repositories. The Ubuntu SDK is in a PPA repository that will have to be added to the system in order to be installed. By this limitation, it can only be programmed on distributions that can work with PPA repositories, that is to say, all the distributions that use Ubuntu as a base. If the distribution uses RPM packages, as in the case of OpenSuse or Fedora, alternative measures will have to be taken.

There are two other ways to do this: create a Live USB with Ubuntu or use a virtual machine that has Ubuntu installed. The first one is simpler and works reasonably well if the USB memory is fast. The second way is more comfortable since it does not depend on a USB stick, but requires a more powerful computer to run the virtualized operating system. I will not go into how to create a Live USB or virtual machine. Anyone in one of these two cases can ask in the mailing list and I will guide him with the most important steps.

My development environment is:

* Ubuntu 16.04 LTS.
* Aquaris E4.5 with OTA-14.
* Aquaris E5 HD with OTA-14.
* Aquaris M10 FHD with OTA-14.

It is possible that in other distributions there are some intermediate steps that need to be done to configure the SDK. If that is the case you can indicate it.

## Installing the Ubuntu Touch SDK

Installing the SDK is very simple and should not take more than a couple of minutes. It is advisable to have the system updated before you start. For the case of Ubuntu, this can be done with the commands:

```bash
sudo apt-get update && sudo apt-get upgrade && sudo apt-get dist-upgrade
```

You use sudo because package installation requires administrator permissions. The first command updates the list of repositories. If there is no error, the second command is executed, which is responsible for updating the applications. Finally the third command updates some operating system packages that are not updated by default.

Add the Ubuntu Touch SDK repository with the command:

```bash
sudo add-apt-repository ppa:ubuntu-sdk-team/ppa
```

![Add Ubuntu SDK PPA](https://1107340174-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LJ3ewldYFKSKUp8WQEF%2F-LOFACJTXSDckbQ-p33m%2F-LOFACsoc3msU7NY47vZ%2F01_add_ppa.jpg?generation=1538943445306942\&alt=media)

The repository information will be displayed. You can continue by pressing the ENTER key.

![PPA Info](https://1107340174-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LJ3ewldYFKSKUp8WQEF%2F-LOFACJTXSDckbQ-p33m%2F-LOFACsqPBDqg-zafQaR%2F02_ppa_info.jpg?generation=1538943441313999\&alt=media)

All repositories have a signature that assures us that the installed packages come from the repository. This signature is added to the system and will be used when repository packages are installed.

![PPA Key](https://1107340174-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LJ3ewldYFKSKUp8WQEF%2F-LOFACJTXSDckbQ-p33m%2F-LOFACssqYMKUiqNrnu3%2F03_ppa_key.jpg?generation=1538943434607494\&alt=media)

After adding a repository it is necessary to update the information of the packages that it contains. You can do this with the command:

```bash
sudo apt-get update
```

Everything is ready. The last step of this section is to install the Ubuntu Touch SDK.

```bash
sudo apt-get install ubuntu-sdk
```

![Ubuntu SDK Packages](https://1107340174-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LJ3ewldYFKSKUp8WQEF%2F-LOFACJTXSDckbQ-p33m%2F-LOFACsuEqHqxn9cI8ah%2F04_packages.jpg?generation=1538943432302798\&alt=media)

Although only one package (ubuntu-sdk) is put in the command, all the dependencies of that package are automatically installed, so it works without problems. The installation may take a while depending on the Internet connection. It's a good time to let the computer work and have a good coffee.

## Setting up the Environment

The Ubuntu Touch SDK includes tools to generate applications and an editor to make programming easier. The tools take the source code of the application and process it. If the target is a computer there is no problem. Instead, if the target is a device that uses Ubuntu Touch, we are in a different scenario. These devices use the ARM architecture, which is different from the one used by the PC. To generate the executable you have to use a cross-compiler that runs in a container (LXD in the latest versions of the SDK).

To work with LXD you have to add the user that is used on the computer to the group lxd.

```bash
sudo usermod -a -G lxd user
```

![Usermod](https://1107340174-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LJ3ewldYFKSKUp8WQEF%2F-LOFACJTXSDckbQ-p33m%2F-LOFACswK3W3Pm2arNih%2F05_usermod.jpg?generation=1538943441174122\&alt=media)

After writing the command you have to close the user's session and login again. You can start working with this small modification. To start, we need to run the ubuntu-sdk-ide application, it can be launched from the desktop start menu or by pressing ALT + F2 and typing its name. The first time it’s launched, a screen appears asking in which lxd has to be configured. Just press ‘Yes’ to generate the default configuration.

![Container Initialization](https://1107340174-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LJ3ewldYFKSKUp8WQEF%2F-LOFACJTXSDckbQ-p33m%2F-LOFACsy2aERfdB4zEH7%2F06_container.jpg?generation=1538943442290083\&alt=media)

![Container Generation](https://1107340174-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LJ3ewldYFKSKUp8WQEF%2F-LOFACJTXSDckbQ-p33m%2F-LOFACt-Sp1as7y5jlhx%2F07_container_generation.jpg?generation=1538943441724322\&alt=media)

If the user is not in lxd group, the following error shown in the screen will be output. In that case, it is sufficient to logout and login again. When launching the IDE of Ubuntu Touch should give you no errors.

![LXD Error](https://1107340174-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LJ3ewldYFKSKUp8WQEF%2F-LOFACJTXSDckbQ-p33m%2F-LOFACt16lOh_Xzly6uS%2F08_lxd_error.jpg?generation=1538943433981388\&alt=media)

## Configuration Wizard

The first screen of the wizard is an introduction to Qt Creator. To continue, click the ‘Next’ button.

![Wizard Intro](https://1107340174-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LJ3ewldYFKSKUp8WQEF%2F-LOFACJTXSDckbQ-p33m%2F-LOFACt3sP_ZCtGe9C24%2F09_wizard_intro.jpg?generation=1538943442462380\&alt=media)

Although the course is focused on mobile devices such as phones or tablets, you will also apply the knowledge learn to create desktop applications. Each of these devices has associated a kit, a set of tools, which are based on the code that is programmed and generates the necessary files for each device.

![Kits Creation](https://1107340174-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LJ3ewldYFKSKUp8WQEF%2F-LOFACJTXSDckbQ-p33m%2F-LOFACt5S5H4WHBuwK5i%2F10_kits_empty.jpg?generation=1538943443624630\&alt=media)

Click the ‘Create new Kit button’. The first option must be selected.

![Kit Desktop](https://1107340174-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LJ3ewldYFKSKUp8WQEF%2F-LOFACJTXSDckbQ-p33m%2F-LOFACt7ICfew1JA7gZu%2F11_kit_desktop.jpg?generation=1538943442448470\&alt=media)

In the list there are several kits to download. Ubuntu Touch is currently based on Vivid so you have to select that option.

![Kits Versions for Desktop](https://1107340174-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LJ3ewldYFKSKUp8WQEF%2F-LOFACJTXSDckbQ-p33m%2F-LOFACt9ljSikK1yYTA2%2F12_kit_versions.jpg?generation=1538943435783257\&alt=media)

Finally write the name of the kit.

![Kit Name for Desktop](https://1107340174-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LJ3ewldYFKSKUp8WQEF%2F-LOFACJTXSDckbQ-p33m%2F-LOFACtBx9o2hqcHK18b%2F13_kit_name_desktop.jpg?generation=1538943441709030\&alt=media)

When you complete the last step, you return to the initial screen of the kits. Press the ‘Create new Kit’ button again and repeat the process by selecting Ubuntu Device.

![Kit Device](https://1107340174-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LJ3ewldYFKSKUp8WQEF%2F-LOFACJTXSDckbQ-p33m%2F-LOFACtDT8JkxSiLAyan%2F14_kit_device.jpg?generation=1538943439947507\&alt=media)

With this kit there are more options to choose from. Phones or tablets that use Ubuntu Touch work with the armhf architecture. In the list you have to select a kit that has that architecture. The version of Ubuntu Touch is Vivid, as in the previous case. Finally you have to choose 686 or x64 depending on whether the computer uses 32 or 64 bits.

![Kit Versions for Device](https://1107340174-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LJ3ewldYFKSKUp8WQEF%2F-LOFACJTXSDckbQ-p33m%2F-LOFACtFeZLQnlcxN0-v%2F15_kit_device_versions.jpg?generation=1538943432191152\&alt=media)

After pressing the ‘Next’ button, you must write the Kit name.

![Kit Name for Device](https://1107340174-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LJ3ewldYFKSKUp8WQEF%2F-LOFACJTXSDckbQ-p33m%2F-LOFACtHucmqEmL432X4%2F16_kit_name_device.jpg?generation=1538943442360802\&alt=media)

At this time it is not necessary to create additional kits.

![Kit List](https://1107340174-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LJ3ewldYFKSKUp8WQEF%2F-LOFACJTXSDckbQ-p33m%2F-LOFACtJOKZGZl4qkdlE%2F17_kits_defined.jpg?generation=1538943446428415\&alt=media)

In the last step of the wizard you can configure the physical devices and the emulator. There are some problems with the emulator. For now, I recommend not to configure the emulator. Applications can be tested natively on the computer. For this reason you must uncheck the ‘Create emulator’ box and click on the ‘Finish’ button.

![Emulators List](https://1107340174-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LJ3ewldYFKSKUp8WQEF%2F-LOFACJTXSDckbQ-p33m%2F-LOFACtLWuqZo9ejY4ak%2F18_emulator.jpg?generation=1538943453675004\&alt=media)

## Hello World

Maintaining good tradition, the first step is to program a minimal application that allows us to check the correct operation of the SDK. The application will run on the computer natively and on a tablet. As you will see it is quite simple to do. Kits are updated frequently. If the following screen appears, all the kits must be marked and updated.

![Kit Updates](https://1107340174-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LJ3ewldYFKSKUp8WQEF%2F-LOFACJTXSDckbQ-p33m%2F-LOFACtNLbQR5cmPByoP%2F19_kit_update.jpg?generation=1538943433378793\&alt=media)

### Project Creation

Click on the File menu, New file or project. A window will appear with the types of projects that can be used. The user interface is made with QML which is a scripting language oriented to the creation of graphical interfaces. The logic of the application can be done with several languages. JavaScript will be used for now. Select the first option and then click on the ‘Choose’ button.

![New Project](https://1107340174-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LJ3ewldYFKSKUp8WQEF%2F-LOFACJTXSDckbQ-p33m%2F-LOFACtP3z7fgP30RnJE%2F20_new_project.jpg?generation=1538943443778911\&alt=media)

You must choose the folder in which the projects will be saved. The project name cannot contain spaces.

![Project Name](https://1107340174-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LJ3ewldYFKSKUp8WQEF%2F-LOFACJTXSDckbQ-p33m%2F-LOFACtRc9QAdj93U2JX%2F21_project_name.jpg?generation=1538943441184357\&alt=media)

Applications need basic information: user and maintainer. All other parameters should be left as they come by default. It is important to respect the structure in the Maintainer field so that it allows us to continue.

![Click Parameters](https://1107340174-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LJ3ewldYFKSKUp8WQEF%2F-LOFACJTXSDckbQ-p33m%2F-LOFACtTkmcBlwfubA-y%2F22_click_parameters.jpg?generation=1538943442966957\&alt=media)

You have to select the kits that you want to use. By default the two will be selected in order to run the application on the computer and on the mobile device.

![Project Kits](https://1107340174-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LJ3ewldYFKSKUp8WQEF%2F-LOFACJTXSDckbQ-p33m%2F-LOFACtV7IWgBasaFmcC%2F23_project_kits.jpg?generation=1538943444813785\&alt=media)

The last screen contains a summary of the wizard steps. Click on the ‘Finish’ button.

![Project Summary](https://1107340174-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LJ3ewldYFKSKUp8WQEF%2F-LOFACJTXSDckbQ-p33m%2F-LOFACtXe0lRBbfmDWZq%2F24_summary.jpg?generation=1538943439993441\&alt=media)

The project will open automatically.

![Project Opened](https://1107340174-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LJ3ewldYFKSKUp8WQEF%2F-LOFACJTXSDckbQ-p33m%2F-LOFACtZZwDPF9NxA9pV%2F25_project_opened.jpg?generation=1538943444828584\&alt=media)

### Running the Application on Your Computer

To run the application you have to press the ‘Play’ button in the lower left part of Qt Creator.

![Run App on PC](https://1107340174-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LJ3ewldYFKSKUp8WQEF%2F-LOFACJTXSDckbQ-p33m%2F-LOFACtas-CTIThVycPz%2F26_run_app_pc.jpg?generation=1538943444181502\&alt=media)

### Running the Application on a Real Device

Before running the application, it is necessary to configure the device. For testing I have used an Aquaris M10 FHD tablet with the OTA-14 —although the procedure is the same on the other terminals. Leave ubuntu-sdk-ide open and connect the device via USB to the computer. To activate the development options, access the System Settings.

![System Settings](https://1107340174-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LJ3ewldYFKSKUp8WQEF%2F-LOFACJTXSDckbQ-p33m%2F-LOFACtc8jw19GqEm25S%2F27_device_settings.jpg?generation=1538943432294654\&alt=media)

Click on About.

![About](https://1107340174-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LJ3ewldYFKSKUp8WQEF%2F-LOFACJTXSDckbQ-p33m%2F-LOFACteRYzWwRzJ0yM6%2F28_device_about.jpg?generation=1538943441706444\&alt=media)

Select Developer Mode. This mode is disabled by default because it allows remote control of the device if it is connected by USB to the computer. Check the box.

![Developer Mode](https://1107340174-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LJ3ewldYFKSKUp8WQEF%2F-LOFACJTXSDckbQ-p33m%2F-LOFACtgUbXHbzK56VOu%2F29_developer_mode.jpg?generation=1538943435377436\&alt=media)

After a few seconds, a notification must appear with the connection request of the computer.

![SSH notification](https://1107340174-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LJ3ewldYFKSKUp8WQEF%2F-LOFACJTXSDckbQ-p33m%2F-LOFACtiFmJaJwrK4DOU%2F30_ssh_key.jpg?generation=1538943412064841\&alt=media)

You have now completed all the steps on the side of the tablet. In the IDE, you must select the tablet as the compilation target. To do this, click on the button above the Play and select the device we just configured as the destination.

A few seconds after pressing the Play button, the application will appear on the device.

![Run App on Device](https://1107340174-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LJ3ewldYFKSKUp8WQEF%2F-LOFACJTXSDckbQ-p33m%2F-LOFACtkj31pk8Morw-1%2F31_run_app_device.jpg?generation=1538943411958431\&alt=media)

The error shown in the following image may appear:

![SDK Error](https://1107340174-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LJ3ewldYFKSKUp8WQEF%2F-LOFACJTXSDckbQ-p33m%2F-LOFACtmuzcifzgZXXQ6%2F32_sdk_error.jpg?generation=1538943421858111\&alt=media)

In the sidebar of Qt Creator you have to click on the ‘Devices’ button. Then click on the device and Kits click on the ‘Remove’ button. Then click on the ‘Create’ button.

![Ubuntu Devices](https://1107340174-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LJ3ewldYFKSKUp8WQEF%2F-LOFACJTXSDckbQ-p33m%2F-LOFACtoDo6qIBgG5KHm%2F33_ubuntu_devices.jpg?generation=1538943412089094\&alt=media)

Now you have to associate the Kit back to the Project. Click on ‘Projects’ (on the sidebar), ‘Add Kit’ and select the Kit that appears in the dropdown.

![Kit Selection](https://1107340174-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LJ3ewldYFKSKUp8WQEF%2F-LOFACJTXSDckbQ-p33m%2F-LOFACtqak37N6Vg3OxU%2F34_kit_selection.jpg?generation=1538943435253663\&alt=media)

Wait a couple of seconds and select the device as the compilation target. It is important that you try the compilation in both cases. In the next chapter I will start with the source code and assume that everything works correctly on both the computer and the test device. If you have any problems you can ask on the mailing list.

## People who have collaborated

* Larrea Mikel: revision of the chapter in Spanish.
* Cesar Herrera: revision of the English translation.
* Joan CiberSheep: revision of the English translation.
