Showing posts with label Android. Show all posts
Showing posts with label Android. Show all posts

Tuesday, May 26, 2015

Android: One Configuration To Build Them All

In the past, finding an Android build configuration that works on Android Studio, IntelliJ as well as command line has proven to be extremely difficult. On each new SDK-, IDE- or Gradle version, things seemed to be broken again. It wasn't a stable working environment at all, and I'm glad that Google decided to work on that (source: http://tools.android.com/tech-docs/new-build-system/version-compatibility).

Now, I'm using a build configuration that seems to work on all platforms, and I'd like to explain to you what I've done to make it work (and hopefully help you save a lot of time).


1. Install the Android SDK


First, I've downloaded android-sdk_r24.0.2-macosx.zip (Android SDK 24.0.2) from the Android website, and unzipped it (I've used directory /Users/Stefan/android-sdk).  I'm building on Mac, but you can also find the Windows and Linux releases on the website.

After unzipping, you create an environment variable called ANDROID_HOME, that points to the directory of the SDK that you've just unzipped:
export ANDROID_HOME="/Users/Stefan/android-sdk"
(on Mac or Linux, just add it to your ~/.profile file so that it will automatically be set each time you open your terminal)

Next, start the Android SDK manager, using:
$ANDROID_HOME/tools/android
Here, you have to install the following packages (this will take some time):

  • Android SDK Tools v24.0.2
  • Android 5.0.1 (API 21)
  • Android SDK Build-tools v21.1.2

Your SDK is now ready.


2. Install the IDE


Install the IDE of your choosing (Android Studio or IntelliJ).

For testing, I've downloaded both:
  • android-studio-ide-135.1740770-mac.dmg (Android Studio 1.1.0 build 135.1740770)
  • ideaIC-14.0.3.dmg (IntelliJ 14.0.3)


3. Create a new project (Hello World)


Now we're ready to create a new project.

Execute in your terminal:

$ANDROID_HOME/tools/android create project  
-t "Google Inc.:Google APIs:21" 
-p testproject  
-a TestActivity  
-k be.ad.testproject 
-g  
-v 1.1.0

This will create a directory called testproject, in which a Hello World application is generated (under package name be.ad.testproject).  The application will just have one activity, called TestActivity.  It will use Google APIs version 21 and Gradle build tools version 1.1.0.

Next, open testproject/gradle/wrapper/gradle-wrapper.properties and
  • change the Gradle version to gradle-2.2.1-all.zip.

Open testproject/build.gradle and
  • remove runProguard false line
  • replace the Android plugin with "com.android.application"


4. Test the build configuration


Ready to test it?

Execute in your terminal (in the testproject directory):

./gradlew clean assembleDebug

It should compile without any problems.

Next, try to open the build.gradle file using IntelliJ and/or Android Studio (be sure to select Use Default Gradle Wrapper when opening the project.  And when asked, choose to use the Project SDK instead of the IDE SDK).

It should open and build on both IDEs, without ANY problems.

And if you deploy the application on your device or emulator, you will see a nice Hello World application...

I prefer creating my projects command line, not by IDE.  When creating a project using the IDE, a lot of additional files will be generated that you might not need in your project.


5. Links


Wednesday, March 12, 2014

Odo Meter: Behind the Magic

An odo meter is a clever component that measures the total distance travelled (like the mileage counter in your car).

In Mobile software development, we also use an odo meter for calculating the travelled distance in:
  • track-and-trace applications
  • mileage administration applications
  • sports tracker applications (like Endomondo, RunKeeper, Strava...)
  • bike computers
  • taxi meters
  • ...
There are multiple strategies for calculating the total distance travelled.


STRATEGY 1: Calculate distance using delta time and current speed

Imagine a process that fetches the current GPS speed at a fixed interval of x time.

At each interval, we can calculate the distance by multiplying the delta time with the current speed:

d = v * t

for example (at an interval of 5 seconds, the current speed is 10 meters/second):

d = 10 meters/second * 5 seconds = 50 m

The problem with this strategy is that, in order to have an accurate result, time (t) has to be very small. In other words: you have to fetch the GPS speed a lot (draining your device battery).

A second problem is: sudden (short) accelerations, can cause an huge increase in distance, even if this speed has only occurred for just a little while... causing the result to be less accurate.


STRATEGY 2: Calculate distance between two points, at a fixed interval

Instead of fetching the GPS speed, we can also use the GPS position (longitude/latitude).

At each interval, we calculate the distance between the previous position and the current position.

As with the previous strategy, the results become more accurate when the interval becomes smaller. But the results aren't affected by speed fluctuations. The problem with this strategy is: we don't drive a straight line.  If we were driving circles, passing the same location at each interval, we could get into a situation where the calculated distance is zero.


STRATEGY 3: Calculate distance between positions, at a dynamic interval

When calculating the distance between positions at a dynamic interval instead of a fixed interval, we could decrease the problems of the previous strategies.

The interval can increase or decrease due to criteria like:

  • movement detected by an hardware acceleration- or gyro-meter.
  • ignition detected by an input port, canbus or sensing power drop.
  • an on/off switch.
  • increased speed levels at the previous interval.
  • distance threshold (for example, see Android LocationManager).
  • course changes (like a compass).
  • maps street information.
  • ...

Conclusion

Applications that operate in environments with a fairly stable speed (like space applications, airplanes, machine measurements, ...), strategy 1 (calculate distance using delta time and current speed) will be suitable.

But for most applications, strategy 2 (calculate distance between two points, at a fixed interval) will be the best solution.

Just keep in mind, when you require more control (to save battery life or in environments with repetitive reoccurring locations, ...), strategy 3 (calculate distance between positions at a dynamic interval) can also be a solution.

Friday, October 18, 2013

Geo Fencing - Sample Code

My first blog was about geofencing (read it here). The point-in-polygon algorithm may seem easy enough, but when actually trying to implement it, you will notice that there are some tricky parts to it (like when your point is on the edge of the polygon, or when your polygon contains horizontal lines, etc.). Therefor, I've decided to add some java sample code.

So, here goes...

Points, lines and polygons


First, we'll create some objects to represent our points, lines and polygons:
A point is a position with an x- and an y-coordinate. To apply it to geofencing, you can just think of x-coordinates as longitudes and y-coordinates as latitudes.

A line is a straigth line with a direction (vertex). It has a from-point and a to-point. We will use it to represent the edges of our polygon.

A polygon, obviously, is a multi-sided shape that consists of a number of points.


Objective


Our objective is to create a method for calculating if we are inside the polygon or not.

As described in my first blog post, this method will:

  • calculate the lines of the polygon
  • filter the lines that intersect with our y-position
  • calculate the exact points on which the lines intersect with the y-position
  • sort the points by x-position
  • use ray casting (out-in-out-in) algorithm for checking if we are inside or outside of the polygon.

In java, it would look something like this:
Now, let's try to implement each sub-method separately...


Calculate the polygon lines


First, the method for calculating the lines of a polygon.

We just take the points of the polygon and connect them together. We then close the polygon by connecting the last point to the first point:
There's no real magic here, it's a very simple method.


Filter the lines that intersect with the y-axis


Next, we need to filter the lines that intersect with our y-axis...


Calculate the x-intersection points at the y-axis


Next we calculate the x-intersection points of the lines at the given y-position, using the following method:
We calculate the x-position of every line, at the provided y, using standard calculus.


Sort the points by x-position


Sorting the points by X-position is easy, we just use a java double comparator:


Check if we are inside or outside of the polygon


And finally, we check if we are inside or outside the polygon, using the ray-casting algorithm:


Initially, we are outside of the polygon. At each point, we invert our status (inside - outside - inside - outside - ...). Until we have reached our x-position. We then know if we're inside or outside of the polygon.

That's it!

We just use simple java code, it can easily be ported to Android, iOS, ...

This code can still be optimized a lot, but the idea was to show how geofencing works (technically).

References: