Showing posts with label android. Show all posts
Showing posts with label android. Show all posts

Wednesday, November 16, 2016

Scala, Gradle and Android Studio

This post describes my experience with Scala, Gradle and Android Studio - (TL;DR - it works for me.)

Old keys
Photo credits: Kit 

Motivation


Android Studio is the default IDE for Android Development, and the same applies for Gradle as Android's "native" build system. Applications for Android are written predominantly in Java.

Another way to write apps for Android is to use Scala. This choice is not an easy one, since the build gets a little more involved, and the build time also increases significantly. However, this can be circumvented to a huge part, since there is an effort for writing android apps with Scala and its own ecosystem evolved around that idea. Those guys made a good progress.

For the scope of this blog post, I was just interested on how much a switch to Scala would affect the normal workflow in Android Studio. For this reason, I tried to stay with gradle, not switching to sbt and not using maven for this.

It turns out that Scala and Gradle play well together. At least this is my first impression, I'm sure gradle can't deliver all the functionality one is accustomed to when using sbt.

Regarding the user experience in Android Studio: An impact on the workflow can't be denied, since there is no development for Android and Scala without Proguard, which is known to be slow; Instant Run is not really as instant when using Scala with Android Studio.

If you can live with this, I would say one could give it a try and develop Android Apps with Scala and Gradle.

Case Study: Basic Multitouch


I thought it would be a good idea to port a small project to Scala and see what problems I would run into.

Here is what I did:

- I forked android-scala-gradle plugin and applied an existing pull request such that the plugin works together with Android Studio 2.2.2
- forked android-BasicMultiTouch example project from google samples
- added necessary tweaks to the Gradle build to support Scala
- installed Scala Plugin in Android Studio
- ported all java code to scala code - IntelliJ Android Studio helps here a lot
- got it to compile and work on a physical android device (and simulator as well)
- "instarun" runs almost instantly ;-)

In the process of installing my environment, I've stumbled over http://sdkman.io/, which helps a lot managing different versions of gradle (or other java based libraries for that matter) - check it out!

Summary


It went well. I've downloaded half of the internet during the process, but after this initial hurdle the work was straightforward.

All the code is in github, so this time no screenshots or code listings.

Check out the github repositories for the patched android-scala-gradle plugin as well as the toy multitouch project which can be used as starting points for your own experiments.





Sunday, January 17, 2016

fx-tictactoe - A TicTacToe JavaFX application on Android

This post describes how you can install a JavaFX application on an Android device.

Mt. Hood, Oregon, in summer garb


I recently created fx-tictactoe which is a simple tic tac toe game with a computer opponent. I wondered how easy it would be to port it to Android.

TL;DR: It is easy, you'll need the javafxmobile plugin.

To start from scratch, there would be two approaches to do something like this:

- implement game logic in its own module, create a dedicated android gui
- reuse almost everything from the original javafx based game and use javafxports to run it

The first variant is more conservative and more work: you have to partition your original implementation to a gui and backend layer and reprogram from scratch your gui layer in order to fit the android api's needs. I did something like that in the sudokufx application.

For fx-tictactoe, out of curiosity I used the second approach.

Ok, so what has to be done to get a JavaFX app to the android platform?

Step 0: Install Gradle


The javafxmobile-plugin demands a rather recent version of Gradle, so make sure you've downloaded and installed at least Gradle 2.2. You can download Gradle here.

Step 1: Setup gradle build


javafxports uses, like Javafx itself, Gradle as its build system.

Johan Vos and his team have been doing a remarkable great job in tooling support for JavaFX on Android, thus setting up a build system for a JavaFX project for Android is a rather easy task like you can see in the simple gradle.build script below.





At the time of writing, the javafxmobile-plugin was not yet published in maven central. To get things going, I had to check out the source code of the plugin from here and install it myself on my local machine (which worked out of the box).  If you want to do this as well, the only command which was necessary for this was

gradle install

Note: I saw that the 1.0.8-SNAPSHOT version already resides at the sonatype snapshot repository, so it won't take long until it is deployed as a release there I assume.

You can see that among other things I'm using the Gradle Scala plugin and the javafxmobile-plugin together. Luckily those two guys work together nicely, which whas my greatest concern for this blog post.

But back to the example javafx project I want to see on my android device:

The only thing which is necessary to add to the fx-tictactoe project in order to make it run on the android platform was the build.gradle file.

After creating the build.gradle (see gist above) file, all you need to do is type following command:

gradle android

And if you want to deploy the app to a (connected) device:

gradle androidInstall


This should deploy the apk with a debug configuration to your device.

Like stated in the official docs, the javafxmobile-plugin downloads everything what is necessary, which is great. I can't repeat often enough that it amazes me when I try out projects and they work without much hassle since I know that this very much work and some quality piece of engineering.

I would like to invite you to watch at the final result of my explorations, the video shows a working fx-tictactoe on Android:



These are the basic steps for a proof of concept that the application works on the android device. For deploying it to the app store, some more steps are required, optimizing the apk and wiping out stuff which is not used or needed is also a thing to remember. But for first experiments this should suffice.

As always, I've pushed the source code for the app to my github site.

Wednesday, January 6, 2016

Encapsulate OpenCV 3.1 as Android AAR

In this post I describe how to encapsulate OpenCV as an Android AAR package such that it is easier to include it as a maven dependency.

[High Street, Guildford, England]  (LOC)


Disclaimer: Apparently there exist many other tutorials about OpenCV, my approach is a little bit unconventional. I also have to mention that there is a very well maintained library called JavaCV which does essentially the same.

The motivation for this blog post is that I want to have an convenient way to use OpenCV with my Android applications. Below I describe what I had to do to achieve this.

Step 1: Download the OpenCV library


On www.opencv.org there is a link to download the library for Android. Download it, unpack it.

If you've followed the post about compiling OpenCV yourself, you already have a directory in your
homedirectory somewhere:

opencv/
opencv/opencv-3.1/
opencv/build/

now, add the unpacked Android SDK:

opencv/
opencv/opencv-3.1/
opencv/build/
opencv/OpenCV-android-sdk-3/

You'll find the usual suspects in the directory, some samples, javadoc for the API, already pre build apk's:

apk/
samples/
sdk/

Now I want to discuss briefly the contents of those directories.

Directory apk: OpenCV Manager

OpenCV encourages you to use a separate application called OpenCV Manager which sole purpose is to make sure you have installed a compatible OpenCV library on your phone. This approach is fine but requires your users to install a second app on their phone. For the technical inclined this is no problem, but for end users this may seem a little bit awkward. I prefer to deliver a self contained app which has no apparent third party dependencies.

The apk directory contains this Manager application for environments where you don't want to use the OpenCV Manager from the play store.


Directory samples: OpenCV Android Samples


The samples directory contains several example apps which demonstrate various aspects of the OpenCV API for android.

./samples/example-15-puzzle.apk
./samples/example-camera-calibration.apk
./samples/example-color-blob-detection.apk
./samples/example-face-detection.apk
./samples/example-image-manipulations.apk
./samples/example-tutorial-1-camerapreview.apk
./samples/example-tutorial-2-mixedprocessing.apk
./samples/example-tutorial-3-cameracontrol.apk

I recommend to install some of the apk's on your device:

  adb install <example.apk>

This is the best way to get a feeling what can be done with the OpenCV Android API, so I suggest to play around with the samples. The source code for those samples is also included.

Directory sdk: Android OpenCV Java API

Here you'll find what you will need for your own app. There are  pre-built android libraries for various architectures and one java API to use the native code. The directory structure you'll find on the top of this directory looks like follows:

etc/ ... some configuration for special routines you could use 
java/ ... the java glue code you will program against
native/ ... prebuilt binaries for the android platform

Since I use maven for most of my projects and all of my open source stuff, I need some way to use the provided java glue code and the binaries in my projects. As long as you just use the default API for OpenCV, you can take the code provided almost off the shelf.

Create an OpenCV AAR ready to use with Maven

The following approach shows how you can create a maven module containing the OpenCV bindings - thanks to the android-maven-plugin it can then be used like a 'normal' maven dependency. The plugin will take care about including the AAR in the final APK, you just have to declare it as a dependency (see below).

For this to happen, I've restructured the source code in the following way:

Restructuring of the sdk subfolder
This is the default structure which works together with the android-maven-plugin and includes only code and binaries you'll need at runtime. The pom looks as follows:

pom.xml for an opencv aar

You can see that I'm referring to the standard android api of a certain version - this is needed in order to properly compile the OpenCV Java API.

In order to get the standard android api, you have to clone yet another project named maven-android-sdk-deployer and install the proper API level in your local maven repository. This can be done for
example by issuing following command:

mvn install -P 5.0

A prerequisite for this command to finish successfully is however that you have already installed the Android SDK itself.

Hint: It seems that the OpenCV 3.1 bindings for Android need at least API level 21, maybe you save some time by just downloading this API Level.

Anyway, if you look closely at the pom.xml you'll notice it is using a custom packaging method, namely 'aar' - this is possible since the android-maven-plugin provides the capabilities for maven to properly create such a file type.

Aar's are bundles which contain libraries (Java code, resources or native code) ready to use in Android Applications. Luckily, android-maven-plugin makes it possible to use aar's like normal maven dependencies.

By using this approach you can deploy the OpenCV bindings in your maven repository. OpenCV can then be treated like any other maven dependency, which is a nice thing.

To recap:

After a successful deploy or local install of this maven module (with mvn install) all you need is to include it in the dependency list of your main app, just like shown below:

dependency declaration for your homebrew opencv maven module

That's all there is to it - you should be able now to use OpenCV in your project. Of course, the maven coordinates change depending on which you've chosen before.

One nice aspect is that the download of the OpenCV Manager is not needed anymore. The drawback is of course that your apk is getting bigger - nothing comes without a price.

For a complete example have a look at the SudokuFX project. Thanks for reading.


Sunday, June 21, 2015

Sudoku Capturer Release 1.6

Sudoku Capturer presents itself now as a self contained app, without the need of OpenCV Manager as a separate download.

Carter Buton Album Loan_00115
From a album belonging to barnstormer/daredevil Carter Buton.

I've decided to use the possibility to integrate OpenCV as a native library without the OpenCV Manager functionality since it improves dramatically the user experience for the application.

Many people had been asking themselves why a "third party" dependency had to be installed for the Sudoku Capturer application, and thus I've decided to go the "deprecated" road and link the OpenCV libraries "statically".

In retrospect I should have done this much earlier, since I've learned that I don't have to include every static lib which is offered by OpenCV, I get away with only a subset of those libraries, and the download is in sum even smaller than with the OpenCV Manager.

Since OpenCV is the only native library I need it suffices to copy the libs to the appropriate place and thankfully the android maven plugin does the rest. Sudoku Capturer supports with armeabi-v7a architecture.

About Sudoku Capturer


Sudoku Capturer is an app to solve Sudokus by using your mobile phone camera. The app uses OpenCV and and Scala, and runs on Android. Furthermore a testbed exists which targets JavaFX on the desktop which makes the development process and debugging of the image processing part much more practical than the typical mobile development workflow would ever permit. The whole source code is hosted on GitHub and can be used for your own experiments.


Wednesday, July 2, 2014

Akka 2.3.4 and Scala 2.11.1 - on Android

This post outlines what is necessary to set up a Maven project to deploy a Scala application which utilizes Akka to Android.


Is Akka not a server framework?

Well, it is. I just wanted to see if I could manage to come up with a configuration for Maven and its Android plugin - not to forget the wonder tool every Android developer is always very happy to configure - Proguard :)

Besides, there are also others who want to get it to run on the mobile devices or did it already.

Why Scala on Android? Maven???

I do a little bit of Android programming also in my spare time, I even <shameless plug>created an app which you definitely should check out</shameless plug>, albeit I'm not using Akka for this one.
You can tell by reading my blog that I'm struggling with Maven on a daily basis - that is why I gave it a shot (because of work/life balance you know.)

I know that the 'normal' way to build scala projects is to "use the sbt", and like others  I do use it for some projects successfully. Sometimes however Maven is just ... here.

If you have the choice you should check out sbt and one of the android plugins available. The  proguard step is mandatory in the presented setup, without it it won't work. You can ease the pain of waiting for the proguard step to finish a little bit by tweaking the proguard configuration file and comment out the optimization steps.

If you still want to do it with Maven, read on or jump directly to the source on github.

Prerequisites

For this project, I used the exciting maven-android-sdk-deployer project which deploys everything you need in your local maven repository. Just follow the installation instructions on the project website.

Macroid Akka Fragments

An important piece in the puzzle is also the macroid project - check it out. For the example project I've stolen borrowed some code from the macroid-akka-fragment project which provides the necessary glue code for Akka.

In order to compile the example project, you don't need to do anything, the links were provided for reference.

Some remarks on the pom

Have a look at the configuration of the scala-maven-plugin in the project pom.xml - there are some interesting flags which enable various warnings/features for the scala compiler which help you write more correct code. See this blog post for more details.

In my opinion, the most precious thing about the whole post is the proguard configuration file. In my experience, it is quite cumbersome to come up with a working proguard configuration.

Rant: The typical user experience is that in the first few minutes you hunt for an "optimal" proguard file, after fiddling around for some hours you'll turn of all warnings and stick to the first "working" configuration.

Finally ... I get a compile error.

The maven configuration for the scala plugin is set to "incremental" - This incremental mode only works if the zinc server is also running. That means you have to start it via "zinc -start" once. I used zinc 0.3.5-SNAPSHOT for the compilation.

In theory, after cloning the repository from here and after entering

mvn clean package android:deploy

You should see something similar like this:

[INFO] --- scala-maven-plugin:3.1.6:compile (default) @ android-akka ---
[INFO] Using zinc server for incremental compilation

[info] Compiling 2 Scala sources and 2 Java sources to /Users/lad/temp/android-akka/target/classes...

- dont forget to connect your android device and activate the usb debugging mode

The app will look like this:

screenshot of the app (created with genymotion, a great android emulator!)
The app doesn't do anything interesting - it uses just one Actor (and one button), but it could serve as a start project for your own experiments with Akka, Scala, Maven and Android.

Thanks for reading!

Saturday, May 17, 2014

Sudoku Capturer 1.4

Today I released a new version of my Sudoku solver app for Android.

Sudoku Capturer 1.4 with incremental number detection

From a user perspective, the most prominent new feature is that the app now shows incremental progress of the numbers which were recognized successfully. This fixes one of the biggest problem with the approach the application had before - numbers which were identified erroneously and which led to a deadlock in the solving algorithm itself.

Currently, on each frame the application makes a quick sanity check if a number would violate the basic rules of the sudoku game - that is to say if at a given cell for example the number seven would be identified, the application now checks if there is already a seven in the same row, column or section. If yes, the whole Sudoku is rendered invalid and the detection algorithm starts from scratch.

In older versions of the application, only one frame of the video stream would be the input for the solving algorithm, which frequently led to non terminating behavior of the solving algorithm itself.

The application is now counting how often a certain number is recognized for a given cell, and after hitting a certain threshold the probability that the detection was correct is certainly higher than without using this simple strategy.

Furthermore, if the Sudoku Capturer app is not able to build up a library of all number from 1 to 9 it paints the number with a internal font - this should happen only very rarely, though.

Give it a try on on your android device, I would be interested in your feedback.

You can download the Sudoku Capturer application in the play store:

Monday, February 24, 2014

Sudoku Solver - on Android

I've ported my Sudoku application now to Android - here is the proof:

You can download it from the play store and support me and my blog by clicking like crazy on the ads! Go for it!!!

Here is what you get: