Monday, August 14, 2017

Sending an APK over bluetooth on Android

If you want to allow users to install an app offline, sending the APK file over bluetooth seems like a reasonable way forward. Regrettably, and without any real documentation the issue, some versions of Android block this. Perhaps it's anti-piracy.

APK receive over bluetooth was blocked on:
  • Nexus 4 (Android 5)
  • Nexus Galaxy (Android 4.3)
  • Yoga Tablet 2 Pro (Android 5)
APK receive over bluetooth works fine on:
  • Samsung SM-G313F (Android 4.4.2)
  • Wileyfox Swift (Android 7.1)
  • Moto E2 (Android 6)
The enable installation from unknown sources setting makes no difference. If receiving APK files are blocked on a device, no notification at all appears on the client device (thanks) when one is sent. The sender should receive a notification that sending failed. If sending an APK is blocked, the workaround is to send a zip file. There we are hoping that the phone has something capable of unzipping it. Most manufacturers install something for that, but stock Android has no file browser. You can get to a file manager through settings, storage, explore.

Wednesday, August 9, 2017

Android JUnit @BeforeClass flakiness for longer running routines

Using @BeforeClass in a JUnit test seems like a great way to make tests run more efficiently. Surely it's a natural place to put long-running setup routines!

Unfortunately, Android's test runner in Android Studio will, depending on how it feels, might run the next test, might skip the next test, might skip a couple more tests just for good measure, or it might just give up the tests entirely. Looking for an error message or a hint?

So instead of a nice elegant solution:
@BeforeClass
public static void someSlowSetupRoutine() {
    //run long running code
}
We must put the long running procedure in the test itself and use an if statement to avoid running it multiple times to speed things up:
@Test
public void someTest() {
    if(!ready){
        //run setup
    }
}




Monday, May 29, 2017

AVD emulator mysteriously packed up on ubuntu?

Your Android emulator mysteriously decides to give up for no apparent reason... The Android Studio UI won't tell you anything about why (I guess that interrupts the "experience" of tearing your hair out).

Run it from the command line:

$ ~/Android/Sdk/emulator/emulator -avd Nexus_S_API_21 -gpu mesa

Where Nexus_S_API_21 is the name of the avd we want to run. Those can be listed using the -list-avds argument. The -gpu-mesa argument tells the emulator to use software rendering: which is somewhat slower but supposedly reliable.

If you then see:
X Error of failed request: BadValue (integer parameter out of range for operation)
Major opcode of failed request: 155 (GLX)
Minor opcode of failed request: 24 (X_GLXCreateNewContext)
Value in failed request: 0x0
Serial number of failed request: 39
Current serial number in output stream: 40
QObject::~QObject: Timers cannot be stopped from another thread
Segmentation fault (core dumped)

Then apparently we need to use the libstdc++6 from the system : not the one Android helpfully supplied.
$ sudo apt-get install lib64stdc++6
$ cd ~/Android/Sdk/emulator/lib64/libstdc++
$ mv libstdc++.so.6 libstdc++.so.6.original
$ ln -s /usr/lib64/libstdc++.so.6 ~/Android/Sdk/tools/lib64/libstdc++

All based on this article here - just slightly modified the paths to reflect Android's current directory structure.

Saturday, January 14, 2017

ORM Persistence design for GWT, Android, iOS and J2SE

Our persistence challenge is to have Object Relationship Management (ORM) that works on Android, with GWT RequestFactory, with iOS using j2objc, and using "normal" J2SE for an eventual desktop app.

ORMLite provides a very nice ORM framework that works both using JDBC (e.g. Desktop/Server side java).  GWT requires proxy interfaces so it can manage the back/forth from the server.  SharkORM requires objective c classes.  It all gets very repetitive:

It requires ORM classes like so:

package com.example.helloandroid;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import com.j256.ormlite.field.DatabaseField;

/**
 * A simple demonstration object we are creating and persisting to the database.
 */
public class SimpleData {

	// id is generated by the database and set on the object automagically
	@DatabaseField(generatedId = true)
	int id;
	@DatabaseField(index = true)
	String string;
	@DatabaseField
	long millis;
	@DatabaseField
	Date date;

	SimpleData() {
		// needed by ormlite
	}
}


GWT's Request Factory requires a proxy interface that it uses to send data back and forth like so:

@ProxyFor(SimpleData.class)
public interface SimpleDataProxy extends EntityProxy {

       public int getId();
       public void setId(int id);

       public String getString();
       public void setString(String string);
         
       public Date getDate();
       public void setDate(Date date);
}

SharkORM wants something like (haven't tested this yet - but the gist is correct):

//  Header File : SimpleData.h
#import "SharkORM.h"
@interface SimpleData : SRKObject
@property int               id;
@property NSString* string; @property long date;
@end

// Source File : SimpleData.m
#import "Person.h"
@implementation Person
@dynamic id,string,date;
@end

Enter Roaster where you can parse and generate java code.  In NanoLRS (our in progress implementation of an Experience API server designed for embedding in apps) we just make the proxy interface and the rest is generated by roaster by our EntityGeneratorOrmLite class like so:

JavaInterfaceSource proxyInterface = Roaster.parse(JavaInterfaceSource.class, proxyStr);
Iterator<MethodSource<JavaInterfaceSource>> iterator = proxyInterface.getMethods().iterator();

It's a work in progress - but at least it seems like I've found a way around writing the same code slightly differently by hand three times.