Android error: Error generating final archive: Debug Certificate expired on [xxx]

I got this error when I tried to start my android application:


Error generating final archive: Debug Certificate expired on 2012-01-14 21:26 TestingAndroid Unknown Android Packaging Problem

I hadn’t been androiding for a while and I’d never seen this before. Luckily google could help me on this one, but I’ll put the solution here anyway:

  1. Go to %USERPROFILE%/.android
  2. Delete the debug.keystore file there
  3. Clean your project and rebuild
  4. Done!

The error should be gone now!

C#: Using the Debug class part 2

In the last entry I showed you how to use the System.Diagnostics namespace to write debug data to the Output window. Now we’ll use System.Diagnostics but go further by overriding the ToString() method for a class.
Normally when you debug in Visual Studio you add a breakpoint in your code and then hover the object you want to examine, it looks something like this:

Breakpoint debugging
Breakpoint

So you have to unfold every individual section of the object to check its properties. Getting the full picture quickly isn’t easy by default, but that can be changed by overriding the basic ToString() method which all objects inherit from Object.

This is an example of a standard example Person class which overrides ToString()

    class Person
    {
        public int Age { get; set; }
        public string Name { get; set; }
 
        public override string ToString()
        {
            return string.Format("Name: {0}, Age: {1}", Name, Age);
        }
    }
ToString() override in debug mode
ToString() override in debug mode

But we can also do this with an attribute, namely the DebuggerDisplay attribute which does the same thing but doesn’t interfere with our implementation:

    [DebuggerDisplay("Name: {Name}, Age: {Age}")]
    class Person
    {
        public int Age { get; set; }
        public string Name { get; set; }
    }

If you look at the list in debug mode you’ll see the same thing as with a ToString() override. The difference is that you are free to override ToString() for other purposes. Debugging should never interfere with your implementation and the attribute can be removed without any ripple effects.
In this example we get the same output, but if we create a class called Coordinate (which will represent a, well, coordinate) we want to output the coordinate like this:
14, 9 (This would be X: 14, Y: 9)
However, since we’re lousy at reading coordinates and we always forget which axis comes first (in general) we want to output “Top: 14, Left: 9” to ease our debugging experiance. Now we want different information in debugging and ToString. This is how we implement it:

    [DebuggerDisplay("Top: {X}, Left: {Y}")]
    class Coordinate
    {
        public int X { get; set; }
        public int Y { get; set; }
 
        public override string ToString()
        {
            return string.Format("{0}, {1}", X, Y);
        }
    }

One thing worth noting is that in debug mode the DebuggerDisplay attribute supersedes the ToString() implementation making DebuggerDisplay more specific when examining an object. This also applies in inheritance situations.

C#: Using the Debug class part 1

Sometimes you want to know what kind of data you are working with in your application. The standard solution for this is to add a breakpoint and hover it with your mouse when i hits:

Breakpoint debugging
Breakpoint

There are easier ways though if you need to check large amounts on information. In the namespace System.Diagnostics there is a class called Debug which is very handy. It allows you to write information to the Output-window (can be opened in Visual Studio with View => Output or Ctrl + W, O).

Here’s an example of using the Debug class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Diagnostics;
 
namespace MaffeluDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            IList<Person> people = new List<Person>()
            {
                new Person(){Name = "Kalle", Age = 91},
                new Person(){Name = "Lisa", Age = 26},
                new Person(){Name = "Zlatan", Age = 29},
            };
 
            //Direct user output
            Console.WriteLine("There are {0} people in the collection!", people.Count());
 
            //Debug
            foreach (Person person in people)
            {
                Debug.WriteLine("Name: " + person.Name);
                Debug.WriteLineIf((person.Age > 65), "Senior citizen found!");
            }
 
            Console.Read();
        }
    }
 
    class Person
    {
        public string Name { get; set; }
        public int Age { get; set; }
    }
}

If you run the following code you should see something like this:

Output
This is what it should look like, check the Output window

If you want more information about the Debug class I’ve written a follow up on this article here: http://maffelu.net/c-using-the-debug-class-part/!