RSS Feed
Jul 13

Android Contact Picker Library Update

Posted on Monday, July 13, 2015 in Coding

26 January 2013´- That is the date when the Android Contact Picker library first went live in the Maven central repository. The Android contact picker library enables you to support the workflow of opening the contact list, user picks a contact, selects a number and then have that number returned to your application through an Android Intent.

Since that time, several things have happened in the Android universe; new Android releases, migration to Gradle as the preferred build system for Android, introduction of Android Studio, Material Design and much more. acp_library_screenshot

Well, over the weekend, I updated and published v3.0.0 of the Android Contact Picker library to maven central. This update includes the following:

  • Update to Material design
  • New menu option for searching contacts
  • Menu option for adding a new contact
  • Added a new sample application for testing the app
  • Bug fixes

The new library is deployed as an .aar artifact to the Maven central repository. This is the library format currently preferred by gradle and eases integration with your current application. To use the library, simply add the dependency to your Gradle file as follows:

compile 'com.codinguser.android:contactpicker:3.0.0'

The minimum supported Android API level has also been increased from API level 7 to 8 and the library uses the AppCompat library for backwards compatibility support.

As always, your feedback is welcome! Tell me in the comments how you use the library, and what kinds of things you are missing, or just that you like it Winking smile

Jun 30

How to create Shadow classes in Robolectric 3

Posted on Tuesday, June 30, 2015 in Coding

Mobile application testing is one very important feature of app development. So is diagnostic logging for debugging purposes. Robolectric is one of the good frameworks out there for running very fast tests of your Android application inside the Java virtual machine.

Robolectric can be extended by use of shadow classes which modify the behaviour of certain classes in the Android OS. However, your application also depends on other libraries which are not included in the Android framework. Fortunately, Robolectric gives you the means to create shadow classes for external libraries which you use so you can modify or extend the behaviour of the library during testing.

Shadow classes for classes in the Android framework are automatically picked up and loaded by Robolectric during testing, but shadow classes for third-party libraries need a bit more work for Robolectric to use them. In Robolectric 3, the way to create shadow classes has changed from previous versions. There isn’t much documentation for the new release yet, so after some experimentation, I found a way to create shadow classes which I am documenting here for future reference.

In my case, I was creating a shadow class for the Crashlytics logging library so that I could disable all logging to the service during testing. I created my shadow class as follows:

@Implements(Crashlytics.class)
public class ShadowCrashlytics {

    @Implementation
    public static void start(Context context){
        System.out.println("Shadowing crashlytics start");
        //nothing to see here, move along
    }
}

The @Implements annotation allows you to specify which class you are shadowing. Also you need to implement the methods with the exact same signatures as in the original class. In this case, I only needed one method, the start() method which should do nothing.

Next thing to do is to implement a test runner which you can use to load the tests into the app. An example of such class looks like this (code adapted from this link):

public class GnucashTestRunner extends RobolectricGradleTestRunner {

    private static final List<String> CUSTOM_SHADOW_TARGETS =
            Collections.unmodifiableList(Arrays.asList(
                    "com.crashlytics.android.Crashlytics"
            ));
    public GnucashTestRunner(Class<?> klass) throws InitializationError {
        super(klass);
    }

    @Override
    protected ShadowMap createShadowMap() {
        return super.createShadowMap()
                .newBuilder().addShadowClass(ShadowCrashlytics.class).build();
    }

    @Override
    public InstrumentingClassLoaderConfig createSetup() {
        return new InstrumenterConfig();
    }

    private class InstrumenterConfig extends InstrumentingClassLoaderConfig {

        @Override
        public boolean shouldInstrument(ClassInfo classInfo) {
            return CUSTOM_SHADOW_TARGETS.contains(classInfo.getName())
                    || super.shouldInstrument(classInfo);
        }

    }

}

The test runner needs to be declared in your Gradle file in the defaultConfig section as follows:

testInstrumentationRunner "org.gnucash.android.test.ui.GnucashAndroidTestRunner"

Also, all the test classes should have the annotation:

@RunWith(GnucashTestRunner.class)
@Config(constants = BuildConfig.class, shadows = {ShadowCrashlytics.class})

Note that we declare our shadow class in the annotation so that it is loaded. If you have more than one shadow class, add it to the list separated by commas.

And that’s it! Now when you run your tests, your custom shadow classes should be loaded and executed.

 

Jun 22

Importing GnuCash XML in GnuCash Android

Posted on Monday, June 22, 2015 in Coding

GnuCash Android has for some time (since version 1.4.0) supported importing accounts from GnuCash desktop. However, the first iteration of this feature was not very easy to use. It was necessary to first export the accounts from GnuCash desktop as non-compressed XML and then feed that output file into GnuCash Android.

With recent updates, it is longer necessary to export accounts before importing into the Android app. Your GnuCash XML file from the desktop app can be directly imported by the GnuCash Android app without any modifications.  Also you do not need to worry about whether or not your XML file is compressed or not, both are supported. All you need to do is to make sure you save your GnuCash files as XML (and not SQLite database format).

GnuCash Android version 1.6.0 also supports backing up your transactions to GnuCash XML format, as well as limited support for parsing template transactions and scheduled transactions. This, combined with the scheduled exports feature, makes it possible to regularly backup your accounts and transactions as GnuCash XML and view these backups in the GnuCash desktop app.

We hope this simplification around file import improves the utility of the GnuCash Android app vis-á-vis GnuCash desktop. As always, feedback is always welcome.