Category Archives: Setup

AOSP: Build a ROM

Continuing with the AOSP theme, this week we’re going to build ourselves an Android ROM and run it in an emulator.

I recommend you check out my two previous AOSP posts, AOSP: Download and AOSP: Architecture, if you plan on following along. Particularly AOSP: Download as you’ll need to make sure your machine is setup.

Before we build there are some changes I should mention. Before Android 7.0 (Nougat) was released the build was done using GNU Make. Unfortunately, due to the size and complexity of Android this build system became slow, error prone, unscalable, and difficult to test. So from Android 7.0 the build is now done by the Soong build system (prior to Android 7.0 I hadn’t used Soong). Which itself makes use of Kati and Ninja

Kati is an clone of the GNU make tool with the goal of speeding up Android builds. The developers say it currently doesn’t do that but it does convert our Makefile to a Ninja file.

Ninja on the other hand is a small build system which focuses on speed.  The developers of this tool say Ninja differs from other build systems in two ways. The first is it is designed to have the input files generated by a higher level build system and to run build as fast as possible.

Given one of the reasons AOSP changed to this system was they were concerned about speed thats a good statement to make. If you’d like to learn more about this then check out the AOSP Build System article which goes into more details about this (certainly required if you want to understand the system indepth).

Lets build ourselves an AOSP ROM. Make sure you’re in a terminal and navigate to the directory you downloaded your AOSP to.

First we need to initialise our environment, this (as the name indicates) sets up our build environment for Android by defining a series of commands. In the root directory of the AOSP source, run:

. build/envsetup.sh

You should see the following output

envsetup.sh – output

Now we can run hmm to get the list of commands we can use. Feel free to run that now to see what are available. The main ones that you’ll use are as follows:

Command Description
lunch Displays the product-name and build_variants to build. You need to select the one you want to build.
m Runs builds from the top of the tree. You can use this to build the whole source or provide arguements to build specific targets.
mma Builds all the modules in the current directory, including their dependencies.
mmma Builds all the modules in the supplied directories, including their dependencies.
croot Changes directory to the AOSP source root.

Now we can run lunch and select the target we want to build. 

All of the build targets will be shown as BUILD-BUILDTYPEBUILD is the codename for the target product and BUILDTYPE is the target build variant.

The buildtype will be one of the following:

Buildtype Use
user Limited access; suited for production
userdebug Like user but with root access and debug capability; preferred for debugging
eng Development configuration with additional debugging tools

So lets make our selection, run:

lunch

You should get a lunch menu of BUILD-BUILDTYPE configurations to pick from. I will select aosp_arm-eng as this will give us a useful build for the emulator. 

Please note this will NOT run on your device so if you’re looking at other sources do no try to flash this. 

You can now build so run:

m

m can run with parallel tasks using the -jN argument, where N will be the task count. You don’t have to provide the argument and the build system will assign it for you for your system if you don’t.

This will take a while so grab a coffee or something and come back to it in a bit.

So long as nothing when wrong you should now be ready to run you newly built Android distribution in an emulator by running:

emulator

Congratulations, you’ve just built the AOSP and ran it. In the future we’ll take a look at where we need to look to modify the different layers depending on what it is we are looking to do and how we can build a distribution to run on a commercial device.

AOSP: Download

I wrote a Bitcoin Node setup post recently (hopefully you had a go?) and thought I’d show you how to setup an Android Open Source Project (AOSP) development environment and how to download the AOSP. 

This tutorial will be for a Ubuntu 18.04 machine only. Other systems should be ok, such as ElementaryOS or newer versions of Ubuntu, but I’ve not tried any other. Feel free to get in touch if there is an OS you’d like me to demonstrate or if you’re having any issues

First things first, open a terminal using ctrl+tab+t and type the following:

sudo apt-get update

Followed by:

sudo apt-get upgrade

Once they are done we will have an up to date machine and should be good to get stuck into setting up for AOSP.

Our plan is to get the official AOSP source on our machines, if you want to check it out now you can visit https://android.googlesource.com/ for more on what we’re going to download.

If you popped by the link you’ll see there are a lot of repos available to you. Don’t manually download each one, we’ll get to that using the repo tool.

We need to install some packages to get our machine ready, so run:

sudo apt-get install git-core gnupg flex bison gperf build-essential zip curl zlib1g-dev gcc-multilib g++-multilib libc6-dev-i386 lib32ncurses5-dev x11proto-core-dev libx11-dev lib32z-dev libgl1-mesa-dev libxml2-utils xsltproc unzip

Let’s install repo then:

sudo apt-get install curl
mkdir ~/bin
curl https://storage.googleapis.com/git-repo-downloads/repo > ~/bin/repo

Now that we have the repo tool we need to make it executable

chmod a+x ~/bin/repo

Incase you run into any problems I had to update my repo so it called python3 instead of python. To do this navigate to ~/bin, open repo, and run edit the first line from

#!/usr/bin/env python

to

#!/usr/bin/env python3

This tool will allow us to pull from multiple git repos to create our Android™ distribution.

We tell it the repos we want to pull using a manifest.xml file, which describes the projects to pull and their locations.

Now we want to get git, run the following:

sudo apt-get install git

Moving on to the reason we are here getting ourselves a copy of the AOSP:

First, lets create our directory.

mkdir -p ~/android/android-9.0.0_r50

Next up, navigate to the new directory

cd ~/android/android-9.0.0_r50

Then download the version of the source you want, see below for more details

repo init -u https://android.googlesource.com/platform/manifest.git -b android-9.0.0_r50

Then finally sync it, this will take a while.

repo sync

Same as above and worth considering is using the -j flag with your sync, it allows you to sync repos in parallel. By default it will do 4 parallel downloads.

To get a different version of the AOSP visit https://source.android.com/setup/start/build-numbers#source-code-tags-and-builds and pick the one you’re after. Simply update ‘android-9.0.0_r50’ above with the Tag of the build you choose.

Great, so now we have our Android™ repos downloaded. 

We’ll leave it at that for this post but I’ll be putting another out soon where we will build and create our own Android™ Distribution to run on an emulator.

 

Android is a trademark of Google LLC.

C#: Hello World

Let’s write some actual code today and there are few places better to start than with the traditional ‘Hello World’ program.

For this post, I want to give an introduction to writing our first functional C# program. It will not do much but hopefully, for those new to software development it will be interesting.

We’ll develop the app by using PowerShell (or command line but some of the commands will be different) and a simple text editor (Notepad) but if you want to you can follow through in Visual Studio, I’ve put a setup guide together for Visual Studio 2017 if you need/want it.

The first thing we’ll do is check we have dotnet installed. Open PowerShell and run the command

dotnet --version

You should see something along the lines of the following:

dotnet version

If you don’t see this or you’re told dotnet isn’t installed, check out this guide to install it.

Great, so dotnet is installed and we can now start writing our code.

We’ll want to go ahead and create our project directory and change into it.

mkdir hello_world
Make project directory

Now that we have our project directory we can create the project itself. We are going to be making a console app and all we want it to do is print ‘Hello World!’ to the console. The easiest way to create the app is from the command line, as we are there already. There are a few options available to us, and not just for console apps, but I’ll put a post together about that another time. For now, we just want a console app created with the following command.

dotnet new console
dotnet new console

We should now have two files inside our hello_world directory, a Program.cs file and a hello_world.csproj file.

new project contents

The Program.cs file will contain our code, it currently has the default codebase provided by dotnet for a console app, shown below in the red box.

Some of you may notice that it appears this app does what we want it to already. We’ll make some changes so we can introduce some other components in software development.

The hello_world.csproj file is our project file, this is an MSBuild project file for a C# project. It contains information about the project and how to build it. For the purposes of this post, its not something we need to worry about just that we need the default.

Let’s modify our Program.cs file to provide us with a bit more to work with, to open the Program.cs file in a text editor (on Windows) run the following command in the hello_world project directory.

Notepad Program.cs
Open Program.cs in Notepad

This will open the Program.cs file in Notepad as shown below.

Program.cs open in Notepad

Great stuff, now let’s make some changes. Update your Program.cs file inline with the code below.

using System;    // 1. Importing namespace

namespace hello_world    // 2. Hello World namespace
{
    public class Program    // 3. Class declaration
    {
        static void Main(string[] args) // 4. Method declaration
        {
            string helloWorldMessage = "Hello World!"; // 5. Statement 1
            Console.WriteLine(helloWorldMessage); // 6. Statement 2
        } // End of Method
    } // End of Class
} // End of namespace

Now that we have some code to work with let’s go through it.

Firstly, I’d like to explain what ‘//’ means, everything on the line after the ‘//’ is called a comment and is ignored by the app. That means it doesn’t matter what we put there the compiler isn’t interested in it. The text (in grey) is for humans only.

Now let’s start going over the code line by line.

using System;

This line allows us to reduce our codebase, by including it we don’t have to specify the fully qualified domain name to access the contents of the System namespace. See the below example

// Without 'using System;' we'd have to use the fully qualified domain name to use WriteLine
System.Console.WriteLine(helloWorldMessage);

// But because we did include 'using System;' at the beginning we can omit it and still access WriteLine
Console.WriteLine(helloWorldMessage);

Following that, we have our namespace declaration.

namespace hello_world
{
... // Rest of the code.
}

The first line declares the name of the namespace which is hello_world, think of a namespace as a container so we can organise our code. Everything between the opening { and closing } is contained within the scope of the namespace and so is part of it.

NOTE: Any code caught between ‘{‘ and ‘}’ is said to be in the scope of whatever created that scope; namespaces, classes, structs, methods, loops etc.

Next up we have our class definition:

public class Program
{ // No need to talk about scopes again
... 
}

To define a class with follow the class definition syntax

// [access modifier] - [class] - [identifier]
public class Program

Our access modifier is public, there are several access modifiers in C# but for now public means this class can be accessed by any other code either in the assembly or any other assembly that references it. We can skip adding the access modifier and the compiler will then use the default internalwhich allows only code in the same assembly to use it.

NOTE: If you omit the access modifier anywhere in C# the complier will plug in the most restricted access that can be declared for that member.

As we are creating a class we use the class keyword to define it as such.

Finally, we provide the identifier for the class, that is we provide the name and for us that’s Program.

Next, we define the first and only method within our Program class, called Main

As with a class definition we have a set method definition syntax we can follow.

// [access modifier] - [optional modifier] - [return type] - [Identifier] (Parameter List)
static void Main(string[] args)

Main‘ is important as it is the entry point for our program, this is where the app starts from as far as we are concerned. Interesting because the ‘Main‘ method is marked as the entry point in IL, see this post for more information on how .Net works, it doesn’t have to be marked with an access modifier, i.e. made public, because behind the scenes the CLR knows where to start from.

First up then we have the static keyword which is an optional modifier. Because Main is our entry point we don’t need to worry about static for now but I’ll link to the relevant post explaining static in detail in the future. For now, if you’ve read my OO post static associates the method with the class and not with an object created from the class.

Next is our return type, by default, we don’t return anything from
Main so we use the keyword void to identify it as such however this can be any data type. Traditionally the type has been returned by value but as of C# 7.0 it can not be returned by reference, again not something to worry about now, I’ll do a post explaining value and reference types.

Following our return type, we have the identifier, again this is the name. Quite straight forward this one, the name is ‘Main‘.

Finally, we have our parameter list, this is simply a list of 0 or more types and names required by the method and the corresponding arguments provided by the client. In this case, it would be any arguments passed in when starting the app.

NOTE: To confirm and avoid any confusion in the future, a method defines what parameters are required and a caller of the method provides arguments for each parameter.

Now that we have our method defined we can move onto our first (and only) statement block, this contains the statements (actions) which are performed sequentially within the scope of the method.

We have two statements, the first:

string helloWorldMessage = "Hello World!";

This is a declaration statement, we are declaring a new local variable of type string and naming it helloWorldMessage. We use an expression to assign the value “Hello World!” to our new variable using the assignment operator ‘=‘. To access the value of the string we then use helloWorldMessage, as shown in the next line.

A variable is a storage location which can contain different values over time but not different types, this is because C# is a strongly-typed language. We must know what each storage location will contain at compile time. So each variable is of a specific type and the type defines the characteristics of the storage location. In our case, we have a string type which means we can only put text (a sequence of read-only characters) into our storage location.

Our next statement is:

Console.WriteLine(helloWorldMessage);

Here we are using a method provided by .Net Core found in the System namespace. The method itself is static, provides no return type, and is in the Console class. There are multiple versions of the WriteLine method each having a different collection of parameters. For us, the parameter is a string, as we are passing in our string contained in helloWorldMessage, so we are using the following fully qualified Console.WriteLine

System.Console.WriteLine(string value);

This method, as described by the Microsoft documentation:

Writes the specified string value, followed by the current line terminator, to the standard output stream.

The remaining content in the file is scope closing ‘}”s described above.

Ok, so we’ve written our program and we’ve worked through it and have an understanding of what each part does. Now let’s run it.

In your terminal, make sure you’re inside the project directory, run the following:

dotnet run

This will build our code using the dotnet build command and then run it for us. You should see something shown below.

dotnet run

Congratulations. Doesn’t do much and has no real value except to us but a ‘Hello World’ app is the starting point for almost all developers.

I’ll add a post providing C# keywords.

For now, why not play around with it and have it print different things to the screen. For those, a bit more curious you might’ve noticed my text is a different colour to yours, have a go and find out how you can change the text colour from inside your app.

Update: Code can be found here.

Magic Mirror

Apologies this post has taken longer than I expected to get out. Its a bit different from the others as its about a specific project.

I decided to give making the Magic Mirror a go. It was my first (and I expect not my last) DIY project and I must say I really enjoyed doing it. I’ve been wanting to do it for a while but as with many people the first road block instantly made me move onto the next thing to do. Not very productive right?

So this time round when the urge to make it took me I put myself on the spot, I told my fiance I’d make it for her as a gift for her birthday, though I didn’t tell her exactly what it was only that I was making something myself.

So what is the Magic Mirror? If you haven’t checked out the above link I’d recommend you do they can probably explain it better than me. Essentially it’s a mirror that gives you information, be it the time, weather, news headlines, and a lot more. The image below shows my end product :

Magic Mirror

What I needed to make the Magic Mirror was:

  • A Monitor
  • A Raspberry Pi
  • A Frame
  • A Two-way / See through mirror
  • Wood
  • Tools

The Monitor

The monitor I used was Iiyama ProLite E2207WS, mainly because I’ve had it for years and wasn’t doing anything with it.

Monitor

First thing I needed to do was get the frame off it, which wasn’t as bad as I thought it would be. Was quite worried I’d end up damaging the buttons but it worked out well. You do definitely need to be careful not to damage them as you may not be able to use the monitor if you do.

Monitor – No Frame
Monitor – Buttons

If you don’t have a monitor lying around make sure you get one where the HDMI connector would not be facing the wall. They should be facing towards the floor if the monitor was standing up right. Another useful option for the monitor would be to ensure it has a USB port capable of powering a Raspberry Pi, that way you only need to worry about one cable. Unfortunately for me my monitor did not have a USB port so I have two cables on display.

Raspberry Pi

I think it’s recommended you use the Raspberry Pi 3 to make the Magic Mirror but again I had a Raspberry PI 2 Model B+ lying about the house that I wanted to do something with. So instead of buying a new Raspberry Pi I just used that instead (and now have an excuse to get the Pi 3). There wasn’t really much to this bit to be fair, those behind the Magic Mirror software make it very easy to get things installed and moving, this is what you need to do:

Update OS

Generally its considered best practice to ensure your OS is up to date before you get started.

Install MagicMirror²

Open a bash terminal

Run the following:

bash -c "$(curl -sL https://raw.githubusercontent.com/MichMich/MagicMirror/master/installers/raspberry.sh)"

This will pull down and install everything we need to get the MagicMirror up and running. Once complete you should be presented with the MagicMirror UI.

The next thing you can do is modify the configuration slightly to better suit your needs. For example

  • Update the news feeds to a provider of your choice
  • Update the weather to your location or one you are interested in
  • Update the Calendar to your own
  • You can also add additional modules:
    • For information on this please check out the GitHub page here

Now finally for the Pi we need to make sure the Magic Mirror starts for us on boot. It is recommended to use PM2 for this.

Setting up PM2

First, we need to install it; so run the following from terminal:

sudo npm install -g pm2

Second, we need ensure PM2 starts on boot; run:

pm2 startup

Once that is finished, run the command it shows you.

Next up we need to add a start script for our Magic Mirror, it is recommended to put this script in the Magic Mirror directory (and its just cleaner)

cd ~
vi mm.sh 

If you’re not experienced with ‘vi’ try ‘nano‘ instead. Now we want to add our startup commands

cd ~/MagicMirror
DISPLAY=:0 npm stat

Save and close vi. Ensure script is executable by running:

chmod a+x mm.sh

Now lets start MagicMirror using PM2; run:

pm2 start mm.sh

MagicMirror should now start up. Once it does press the super key + tab to navigate back to your terminal and run the following to ensure PM2 starts your MagicMirror on boot.

pm2 save

That’s it, our software side of things is complete next up lets check out the Mirror itself.

The Mirror

The mirror was one of the things that stopped me going forward in the past. I’d spoken to a local Mirror shop and they made it sound like getting the two way sheet/glass was going to be really difficult. Annoyingly if I’d just taken a look on the web I’d have found it. This time round I took to Amazon a tried to hunt something down, luckily I found a seller called Sign Materials Direct who sold something along the lines that I needed.

Unfortunately they didn’t sell it the size I needed as the sheet needs to match the monitor. Luckily, again, however someone had had a similar issue as me and had asked the question about customisation. Turns out they can cut the material to “any shape or size” you need. So I got in touch with them at the email address in the link (who got back to me very quickly) and within a few days I had my custom cut sheet that fit perfectly.

If you’re in the UK I would highly recommend you give them a try, I was really happy with the price and speed.

Also best to add I’m not affiliated with Sign Materials Direct, just a happy customer.

Custom Cut Acrylic Mirror

The Frame

At first I was thinking I might make the frame myself as I was going to have to make a housing unit to put the monitor in and then attach it to the frame anyway. Then I realised, I am neither a carpenter nor an artist and threw that idea a way.

We have a large Mirror in our living room and thought getting a matching frame would look quite good. So off to the local Mirror shop I went again, that’s where the larger mirror came from, and that was a no go as their supplier doesn’t do custom sizes. But moving on anyway I gave the internet a go and found a company who make custom frames and thought they looked really nice. So I got the frame from there.

Frame

Now I was a bit worried about was may be I’d measured something wrong or had typed something wrong, made a “school boy error” or one of the pieces didn’t fit right etc. But nope it was all good, the frame and Mirror fit perfectly.

Frame and Mirror

The Wood

The next step was to get some decent wood and cut it to house the monitor and Raspberry Pi. I didn’t have a saw so this was a pretty good excuse to get an over the top tool for the job right?

The wood I used was Redwood planed timber, apparently normally used for door frames. But did the job quite nicely in this instance. Unfortunately I didn’t take any pictures for that part which may have been the most interesting, wood cuttings everywhere and a large circular saw.

Anyway I ended up with a bit more than I needed, mainly because I cut one piece slightly too small so had to go buy some more. Though I’m sure I’ll be needing to cut it for a shelf or something… or to just cut it. So got the wood cut to the right size, screwed the pieces together, and painted the whole thing white. Eventually, once dried, got the housing unit attached to the back of the frame with perhaps a few too many screws than was needed but better safe than sorry right?

One thing I would add if you decided to do this yourself, I can’t imagine the unit would get too hot but I drilled two large(ish) holes into the top of the housing unit to allow some kind of air flow just in case. I’m not planning on leaving it powered on all the time but I would say it’s worth putting the holes there.

On the wall

The final job, getting it on the wall. This step was easier to do than I thought it would be as the wall had a large screw in it already for the Magic Mirror to rest on all I needed to add was something to stabilise it. This was done by adding some 90° hooks to the underside of the Housing unit to give the overall structure more support. I may add some more hooks to the top of the housing unit to make sure but it is certainly well secure now.

Finally

Well thanks for sticking all the way through this post. I know it’s not my normal style but I thought I might try adding this as it’s the first project I gave a go and am quite pleased with the results. The fiance really likes it so that’s good.

With all the modules available for the Magic Mirror and no doubt some more really cool ones that will come out the in future I’m going to add to what I’ve got so far. Things like facial recognition, Alexa integration, calendar notifications, and there’s plenty more.

Hopefully you decide to give it a go yourself and if there’s anything I can help with please get in touch.

Setting up a Visual Studio 2017 Environment

Intro

Hey all, so today I’m going to give you a quick run through for setting up a development environment using Visual Studio 2017 Community on Windows 10. There will be a tutorial on how to do this on a Linux environment with a different IDE but for now as I will be focusing on Visual Studio for development.

Not much else to say there, let’s get into it…


Steps:

Up to date Windows 10

The first thing we want to do is ensure that our Windows 10 development machine is up to date.

  • This can be found in Settings -> Update & Security -> Windows Update

Download Visual Studio 2017

Now we are happy our machine is up to date next we want to download the Visual Studio 2017 installer. So the next thing we want to do is download Visual Studio 2017

  • There are other versions available for those with the required Key or Subscription level, they are:
      • Visual Studio 2017 Professional
    • Visual Studio 2017 Enterprise

We will only be looking at Community Edition, but the steps will be the same.

Install Visual Studio 2017 Community Edition

  • Once you’ve downloaded Visual Studio lets install it

  • Double click on the installer
      • You’ll find it in your Downloads folder normally set to ‘C:\Users\<Your Username>\Downloads’
    • You may need to approve the installation via the User Account Control authorisation window. I assume as you are here you’re happy to install Visual Studio 2017, so click ‘Yes’

  • Next up will be to review Microsoft’s Privacy Statement and Software Licence Terms
    • Pretty standard stuff.
    • Click ‘Yes’

  • The Installer will then fetch and install some additional dependencies

  • So, now we get to configure our Development Environment. For our purposes I’m only going to choose those components we MAY want to use in the future, that is components I intend to use in any posts I make. You can, of course, install any components you wish outside of my choices. So we will want:
      • .NET desktop development
      • Desktop development with C++
      • Universal Windows Platform development
      • ASP.NET and web development
      • Azure development
      • Python development
      • Data storage and processing
      • Mobile development with .NET
      • Visual Studio extension development
    • .NET Core cross-platform development

NOTE: You will need to ensure you have enough space to install the components. Please see the red box the above image it shows the ‘Total space required’ for your configuration.

    • You can change where you want the installation to go, see the green box in the above image
      • In some cases, some of the components will have a set installation path which you cannot change.
  • That’s it, for now at least, the Visual Studio Installer will now install Visual Studio, this will take a little while. Could be worth grabbing a brew or something and if you haven’t done so already check out some of my other posts.

Start Visual Studio 2017 Community Edition

Great, so Visual Studio should now be installed for you, I recommend you restart your machine but it’s not necessary. Let’s open Visual Studio now then, there’s a couple of ways to do this:

  • Start Menu
      • Open the Start Menu using the Windows key
      • Scroll to ‘V’ named apps
    • You will see ‘Visual Studio 2017’, click it.
    • Desktop Shortcut
        • Navigate to your desktop, press Windows Key + D
      • Double click, ‘Visual Studio 2017’
  • Search bar
      • Open the Start Menu by pressing the ‘Windows Key’
      • Type ‘Visual Studio 2017’
    • Visual Studio 2017 will now be in the Start Menu Best match options

There will be some configuration we need to do as part of our first startup

  • You’ll be asked to sign in to your Microsoft account if you have one.
    • If you don’t, I’d recommend you get one and sign into it.
    • If you do, sign in
      • Especially if you have an MSDN subscription
  • Next up and nearing the end, some simple IDE configuring
    • Leave Development Settings as ‘General’
      • Though feel free to look into this in more detail and selection and option more suitable for your plans
    • Then choose your Color Theme

We are done, Visual Studio  2017 will now be installed and you can take a look round if you want.

Hopefully, my steps were useful and you managed to install it. If you have any problems feel free to get in touch and I’ll help in any way I can (though I imagine you’ll likely google any issues).

Next up then we’ll make a very simple C# application, should be out shortly.