Monthly Archives: November 2015

Android Genymotion Mock Location Google Play

To get Android Genymotion Mock Location Google Play fused location provider working in an emulator these are the necessary components and steps.  (For Genymotion 2.5.x emulator versions)

Download:

Steps

  1. Install these two zipped APK archive files by dragging the zip files directly onto your running Genymotion Emulator in order.  Start first with ARM Translation v1.1.  It will ask for confirmation.  Click OK.
    Install genymotion ARM Translation v1.1
  2. After the APK file is unpacked and installed, genymotion emulator will ask to restart.  To restart your genymotion emulator, in a command prompt on your computer, issue an emulator reboot command: adb reboot.  The emulator will reboot several times. For me: 4 times it will cycle before settling down and showing the home screen.
  3. Repeat with the Google Apps Play Store file (gapps-5.1-xxxxx) file –  drag and drop, confirm install, adb reboot.  This should only cycle off/on your emulator once, unlike the ARM Translation APK you installed earlier.
  4. Click on the Home button (center white circle with dots at bottom) and find Google Play Store.  Click it to start Google Play.  It may ask to update.  If so, update it.
    genymotion-playstore-installed
  5. Open Google Play. It will ask for a Google account.  Feel free to create a new one or use one you already have.  Supply its password.  Accept Terms of Service and Privacy Policy.  Google Play Services may pop up a dialog saying it has stopped.  Google Play setup will continue though.  Setup payment info if you like.  I didn’t and chose Remind me later.  After that you’ll be shown the Play Store home page.
    google_play_setup_account
  6. Google Play Store in Genymotion crashes repeatedly after install on a Genymotion emulator.  To fix the Google Play Store crashes on Genymotion emulator, open the Settings application in Android (gear icon), scroll down and click on Apps.  Click on Google Play Store.  Click on Clear Cache and Clear Data.  Click OK on any dialogs that come up.
    genymotion_settings
    settings_apps_downloaded
    settings_apps_clear_cache_clear_data

    1. Restart Google Play
    2. Accept Terms and Conditions again
    3. You may or may not have to log in again.  If you get an error regarding you must be logged in when trying to search for Maps, click on the three horizontal bars at the top left of the Google Play home screen to slide out the left tray and click My Account.  Log in if asked to do so.  For me it auto-logged me in from cached credentials.
      google_play_my_account
    4. Back in Google Play, text search for maps.  Google Play should not crash.
  7. Install Google Maps.
    google_play_store_google_maps
  8. In Google Play, find and install a mock location provider such as Mock Locations or Fake GPS.  Mock Locations (fake GPS path), which allows you to draw out a path which you can “drive” or “walk” at your desired speed between locations of your choice.  I find this quite useful when using GoogleApiClient fused provider where requestLocationUpdates is returning Location objects repeatedly over a period.  Very helpful when you’re testing Entering and Exiting GeoFences
    mock_location
  9. Don’t forgot to turn on High Accuracy mode in Settings -> Location -> Mode -> High accuracy, which allows GoogleAPIClient Location Services fused location provider to work properly.  I didn’t have much luck running Google Play Services Location Provider with GPS (Device) only Location setting.
    location_mode
  10. And finally Dev Tools (application on Genymotion) -> Developer Options -> Allow Mock Locations needs to be On.
    dev_options_app_icon_highlighted
    dev_options_mock_locations

 

Spring Boot SSL with Android Retrofit

To setup Spring Boot SSL with Android Retrofit connecting on HTTPS 443:

In Spring Boot

  1. <your project>/src/main/resources/application.properties – add the following values (not the “1.” which is just WordPress ordered list numbering)
    1. security.require-ssl=true
      server.port=8443
      server.ssl.key-store=src/main/resources/private/keystore
      server.ssl.key-store-password=changeit
      server.ssl.key-password=changeit
  2. create and add an SSL key to the location specified by server.ssl.key-store. Note: the SSL certificate file is actually named “keystore”.  That’s not a directory.
  3. Restart your Spring server and you should be able to make httpS connections on port 8443

In Android Retrofit

  • Add an OkHttp compile dependency to build.gradle

compile 'com.squareup.okhttp:okhttp:2.5.0'

  • Create a class that returns an OkHttp client that doesn’t validate trust certificates (written by Jules White of Vanderbuilt)


public class UnsafeHttpsClient {

public static OkHttpClient getUnsafeOkHttpClient() {
try {
// Create a trust manager that does not validate certificate chains
final TrustManager[] trustAllCerts = new TrustManager[] {
new X509TrustManager() {

@Override
public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException {
}

@Override
public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException {
}

@Override
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
}
};

// Install the all-trusting trust manager
final SSLContext sslContext = SSLContext.getInstance(“SSL”);
sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
// Create an ssl socket factory with our all-trusting manager
final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();

OkHttpClient okHttpClient = new OkHttpClient();
okHttpClient.setSslSocketFactory(sslSocketFactory);
okHttpClient.setHostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
return true;
}
});

return okHttpClient;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}

  • Update your Retrofit rest adapter with this unsafe OkHttp client

return new RestAdapter
.Builder()
.setEndpoint(server)
.setLogLevel(logLevel)
.setLog(new AndroidLog(debugTag))
.setConverter(new GsonConverter(myGsonDateAdapter()))
.setClient(new OkClient(UnsafeHttpsClient.getUnsafeOkHttpClient()))
.build()
.create(WebProxy.class);

In the above example,

  • WebProxy is the API interface class.
  • server is obviously the Spring server, should be https and at whatever port specified in Spring’s application.properties
  • debugTag is optional text to prefix Retrofit calls in Android’s console log
  • Converter is not required

Other tools that might help debug

  • Postman – if you can make an https/8443 GET request to your Spring server, you know Spring is setup properly