Thursday, April 20, 2017

Android NFC radio control using instrumentation

I have always worked a lot with NFC on Android. For this reason, I tend to favor real devices over emulators, since missing an NFC radio means there's no way to truly test the intricacies of radio communication. Unfortunately, one can not power cycle the NFC radio using any official API unless going through hoops and using rooted devices, so ensuring NFC radio power state during testing is an uphill battle. For instrumented test scenarios however, there is actually a way forward.

UIAutomator to the rescue

While not as elegant as using an API, we can launch the settings screen for NFC and manipulate it through the use of instrumentation. This is NOT possible using modern Espresso which limits you to the app under test, but thankfully the UIAutomator framework is still available. The accompanying UIAutomator Viewer tool (which has now moved to sdk/tools/bin/uiautomatorviewer) is a great asset in this regard as it helps us identify the widget we need to manipulate.


What the NFC toggle button is named is not consistent across devices and versions of the operating system, so we have to get a bit heuristic here. In practice, looking through my some 10 devices with various versions of Android using various custom skinning, I have identified 3 unique resourceId's for the toggle button. These are com.android.settings:id/switch_widget, android:id/switchWidget and android:id/switch_widget. Unfortunately, on Android 7 (for Huawei devices anyway) it seems as if launching the ACTION_NFC_SETTINGS intent will not actually get you to where you want, but requires an additional navigational step. This complicates the code a bit but it's still possible to make it work.

To launch the Settings activity prior to any Activity under test, we need to pass along the Intent.FLAG_ACTIVITY_NEW_TASK flag. From there, we can write our logic to help us toggle NFC state.

    private void toggleNfc(final Context context) {

        final Intent intent = new Intent(Settings.ACTION_NFC_SETTINGS);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);

        final UiDevice device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());

        findAndToggleNfcInUI(device);
    }

    private void findAndToggleNfcInUI(final UiDevice device) {

        final UiObject toggleButton = device.findObject(new UiSelector()
                .resourceIdMatches("(com.android.settings:id/switch_widget|android:id/switchWidget|android:id/switch_widget)"));

        try{
            toggleButton.click();
            device.pressBack();
            return;
        }catch(UiObjectNotFoundException e){
            UiObject2 nfcMenuItem = device.findObject(By.textContains("NFC"));

            // Move up in the view hierachy until we're at a clickable item
            while(!nfcMenuItem.isClickable()){
                nfcMenuItem = nfcMenuItem.getParent();
            }

            // Issue click to navigate into menu
            nfcMenuItem.click();

            // Wait for any UI jitter to settle
            getInstrumentation().waitForIdleSync();

            // Try to toggle NFC button using this new child activity
            findAndToggleNfcInUI(device);
        }
    }


Composable test aspect using a JUnit rule

The code above is fine and dandy, but I'm a big proponent of composable and reusable aspects, so lets take advantage of the fact, that we can encapsulate the functionality nicely using JUnit's rule mechanism. If you're new to these you may read up on them here. The resulting NfcStateRule.class can be seen below.

/**
 * JUnit test rule for controlling NFC radio power state. Useful in order to ensure NFC is
 * enabled or disabled prior to executing a test.
 */
public class NfcStateRule implements TestRule {

    private static final String NFC_TOGGLE_WIDGET_RESOURCEIDS =
            "(com.android.settings:id/switch_widget|android:id/switchWidget|android:id/switch_widget)";

    private final boolean desiredState;

    public NfcStateRule(boolean desiredState) {
        this.desiredState = desiredState;
    }

    @Override
    public Statement apply(final Statement base, final Description description) {
        return new Statement() {
            public void evaluate() throws Throwable {

                try{
                    final Context context = InstrumentationRegistry.getTargetContext();
                    ensureNfcState(context, desiredState);

                }catch(final Throwable e){
                    e.printStackTrace();
                }
                base.evaluate();
            }
        };
    }

    private void ensureNfcState(final Context context, final boolean desiredState) {
        if(desiredState){
            ensureNfcIsEnabled(context);
        }else{
            ensureNfcIsDisabled(context);
        }
    }

    private void ensureNfcIsDisabled(final Context context) {
        if(isNfcEnabled(context)){
            toggleNfc(context);
        }
    }

    private void ensureNfcIsEnabled(final Context context) {
        if(!isNfcEnabled(context)){
            toggleNfc(context);
        }
    }

    private boolean isNfcEnabled(final Context context) {
        final NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(context);

        if (nfcAdapter == null) {
            return false;
        }

        return nfcAdapter.isEnabled();
    }

    private void toggleNfc(final Context context) {

        final Intent intent = new Intent(Settings.ACTION_NFC_SETTINGS);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);

        final UiDevice device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());

        findAndToggleNfcInUI(device);
    }

    private void findAndToggleNfcInUI(final UiDevice device) {

        final UiObject toggleButton = device.findObject(new UiSelector()
                .resourceIdMatches(NFC_TOGGLE_WIDGET_RESOURCEIDS));

        try{
            toggleButton.click();
            device.pressBack();
            return;
        }catch(UiObjectNotFoundException e){
            UiObject2 nfcMenuItem = device.findObject(By.textContains("NFC"));

            // Move up in the view hierachy until we're at a clickable item
            while(!nfcMenuItem.isClickable()){
                nfcMenuItem = nfcMenuItem.getParent();
            }

            // Issue click to navigate into menu
            nfcMenuItem.click();

            // Wait for any UI jitter to settle
            getInstrumentation().waitForIdleSync();

            // Try to toggle NFC button using this new child activity
            findAndToggleNfcInUI(device);
        }
    }
}

To use our NfcStateRule in an actual test, simply include it as a member and specify the desired NFC radio power state by passing a boolean with the constructor.

@LargeTest
@RunWith(AndroidJUnit4.class)
public class NfcDisabledTest {

    @ClassRule
    public static final NfcStateRule nfcStateRule = new NfcStateRule(false); // Make sure NFC is disabled

    ...actual test...
}

Voila, now it's possible to setup test scenarios correctly using the NFC radio. This is important for many of my Espresso tests to work consistently and reliably every time, as demonstrated by a screen below which tests the UI when the user has disabled NFC.

An example of an Activity/Fragment whos UI-state depends of the state of the NFC radio.

Conclusion

Where there is a will, there is a way! The above is not nearly as clean as having an API which we have available for WiFi, GPS etc. For acceptance testing however, I much prefer this kind of automated UI manipulation over mocking or polluting short-circuiting logic within the app itself.

By definition, the approach must be considered fragile since the NFC toggle button can be called something different on devices I have not yet had my hands on! If you run into this problem, the fix is easy - simply use the UIAutomator Viewer and expand the regular expression to work with this custom view. In a test scenario you usually have full control of the devices anyway so it's not really a practical concern since end-users will never be exposed to the code.

As usual, the code may be buggy, may not work on all versions of Android and is definitely not production safe. You may assume a Public Domain license of the code snippets above. Feel free to contribute back in the comments if you want to share your findings or experiences regarding the matter.

Thursday, January 5, 2017

BangBits Privacy Policy


Welcome to the BangBits Privacy Policy

When you use apps and other software developer by BangBits, you trust us with your information. This Privacy Policy is meant to help you understand what data we collect, why we collect it, and what we do with it. It is important to understand, that BangBits operate both as an owner of given software and as a proxy for work developer by Customers. App's published by BangBits but taking part of a specific Customer solution are treated separately in the "Specific Products" section below.


Information we collect and why we collect it

We collect information to provide better customer experience. This may happen through various forms of remote logging using Google Analytics, Firebase Analytics or similar tool. At no time is personal data directly mappable to an identifiable user being collected. What can be collected is:
  • Device identifiers (DeviceID, IMEI and handset identifiers) in order to black-list and/or white-list otherwise fraudulent and/or abusive users.
  • Stack traces and associated debugging data when app is behaving unexpectedly
  • Behavioral data to better understand how the user is using the software

Specific Products

The following notices explain specific privacy practices with respect to specific products offered by BangBits that you may use:

"Rejsekort Kontrol"

The software known as "Rejsekort Kontrol" is located on a closed business domain on Google Play, and as such, is only accessable to (invited) users of that Organization. The app under control of BangBits takes part of a bigger software system owned by Rejsekort A/S, the danish national transit ticketing authority. No personal identifiable data is collected or transmittet to/from the app. Recent travel data from a Travelcard is inspected and collected to be submitted for backend processing by Rejsekort A/S, but this is governed by Rejsekort A/S' own Privacy Policy at: 


https://www.rejsekort.dk/~/media/rejsekort/pdf/privatlivspolitik/privatlivspolitik---13-10-2014.pdf

Monday, September 19, 2016

Best-practice Android logging

A blog post about Android logging? Must be a slow news day you are probably already thinking, but hear me out on this one. This is a quick tour around my personal best-practices and you may likely not agree with all of them - if so, let me know in the comments. ;)

Android Studio and LogCat

On Android, logging has always been an integral part of the development experience, first through the standalone LogCat utility and since through the Android Studio integration. The build-in LogCat now even offers advanced filtering capabilities based on logging level, regular expressions for package names, logging tag and the actual log messages. However, all this power is really only useful if you play along nicely at the source level as well, or you will drown in log messages without really knowing how to specify what you want to see. This is a common problem as the Android OS and any libraries you may use will spit out an obscene amount of logging, especially at the verbose level.

Use correct log levels

This isn't really Android specific since it's a problem all over the Java space. Many projects don't classify log severity correctly and some can't even be bothered to use logging at all and pipes stuff to System.out and System.err. While there are a LOT of logging frameworks for Java and they differ in functionality, most of them agree on the logging levels. On Android, the levels are:
  • ERROR – Don't use; if something this severe has happened, it is the result of an exceptional state rendering the application context invalid! The application should crash gracefully and collect relevant info for submission as a crash report to Google, CrashLytics, TPA etc.
  • WARN – Something bad happened, but the application is still able to recover and continue executing albeit perhaps in a limited fashion. A developer can filter on the WARN level in order to learn about application states which are candidates for being handled better.
  • INFO – Important business process that changes the state of the application significantly (database update, external system request, network connectivity change etc.) has completed. A developer can filter on the INFO level in order to get the big picture of what is going on at the task and flow level, which a non-developer should also be able to understand.
  • DEBUG – Detailed developer insight into core decisions, states and events.
  • VERBOSE – Don't use; very detailed trace information, intended only for temporary development insight (debugging and troubleshooting), should not risk being left in code and potentially be checked into Git (you are using Git right?). Instead you'll want to use a logging breakpoint as explained below.
I think one of the problems with logging being used wrong it that it takes too much energy deciding on the appropriate level. By reducing the levels down to just 3, it becomes somewhat simpler to decide which one to use. Use only WARN, INFO and DEBUG and use them accordingly; WARN for things that should not happen but is recoverable, INFO for major changes and DEBUG for minor changes.

Logging breakpoint

I mentioned in the above section, that VERBOSE should not be used. This is because the stuff you'd put at the VERBOSE level is so detailed that it doesn't belong in source code (logging statements riddled everywhere makes code harder to read). Instead, Android Studio offers a powerful hybrid mechanism between debugging and logging - basically it allows you to set breakpoints and rather than have these suspend program execution as usually happens, they merely log an expression to LogCat.


This makes so-called logging breakpoints a perfect fit what you would otherwise have put in verbose logging statements at the source level. You can see this feature demoed here.

LogCat Filtering

As an alternative to viewing the complete system log, the "Show only selected application" context filter is not too hard to understand and use, but even this filter will result in a lot of irrelevant garbage from app and OS libraries being used from the app process. I would like to be able to recommend using custom logging filter configurations, but unfortunately I find them too unreliable to trust in practice. For instance, it seems like (with the present version of Android Studio anyway) there is no way to whitelist debug and verbose messages (anything more detailed than info messages) and that's just a no-go for me. However, it's Ok as we can filter at a line level instead and accomplish much the same. Unless I'm hunting for something specific, I always use a black-list approach, since you never want to exclude relevant messages you do not know about in advance! This means entering a rather long and complex regular expression, but it only has to be done once. The following filter clause is a common one I use, but it will vary a lot depending on the amount of noise being generated on the specific device you are using:
^((?!(?:art|ResourceType|System|GED|libEGL|
DynamiteModule|Timeline|OpenGLRenderer)).)*$
This way it becomes a joy to see what's going on and simply jump up or down in the severity filter depending on how fine grained you want to follow along.

Tweak the LogCat settings

To the left of the LogCat window is a vertical toolbar which you may also want to utilize a bit more - it took me a while before I started taking full advantage. Press the cog wheel to go to Settings to configure the LogCat headers and remove the checkbox in "Show date and time" as well as "Show process and thread IDs". This will give you 30 characters more on each line for actual valuable info. If you need to time something, there are better ways to do so using profiling tools like i.e. systrace. Sometimes I also remove the package name since the truly interesting part remains the TAG and associated message. While you are in the LogCat toolbar, familiarize yourself with the other options. Soft wraps can be very handy when you'd rather not miss something in a large message - typically serialized data. The restart button is also going to be needed from time to time, as adb or its integration with the device, is notoriously unstable and can play tricks on you.


Even with filtering in place, sometimes you just have groups of related log info you'd rather be collapsed somehow. This is easy to do by right-clicking on the line in question (typically first line of what you want hidden in the fold) and writing an expression to match what will get folded. It's also here you can shorten excessive stack-traces and hide irrelevant internal calls.



The above screenshot shows 3 similar lines for each Firebase remote config property loaded. The screenshot below shows how the 3 related yet different lines are now folded up under the parent "Successfully obtained Firebase remote config" log line.


Last but not least, the color scheme used by Android Studio for LogCat can be improved upon a bit by making level-indication a bit more obvious (this becomes even more important when you are running a dark theme). I like to make all levels different in color and style so its easy to identify.

Log application aspects rather than class names

While the vanilla approach to writing logging statements on Android is to define a TAG constant with the same name as the class, this is a stupid practice in my opinion. Sure it is easy to adhere to (no thinking required) and can work well for small projects with only a few activities or views. This however, does not scale to projects with 50+ views and/or apps with complex life-cycles - it is simply too difficult to troubleshoot a problem and sooner rather than later you'd have to resort to a slow and tedious debugging session. So what to do instead then?
During execution of an app, one is typically interested in certain course-grained aspects of an app, and probably not so much which file logic lives in. For this reason, I like to define global logging tags based on app aspects. It doesn't matter too much what you choose for these aspect names, but make them generic and/or something that makes sense to your app. I typically always have the following:

    /**
     * Logging tag used for common UI lifecycle events
     */
    public static final String LOG_UI = "UI";

    /**
     * Logging tag used for any kind of network I/O communication
     */
    public static final String LOG_NET = "NET";

    /**
     * Logging tag used for storage; local files, preferences and databases
     */
    public static final String LOG_DATA = "DATA";

    /**
     * Logging tag used for business logic and app related things not
     * already covered by the other log tags
     */
    public static final String LOG_APP = "APP";

Reading a log now becomes super easy and at the INFO level, even non-technical people should be able to follow along if need be:

Always log major UI flow events

Apps are, by definition, highly interactive and UI heavy. It thus makes a whole lot of sense, to trace UI aspects accordingly. The nice thing about UI events, is that it's surprisingly simple to get some broad standardized logging up and running which you won't have to worry about again when adding more views in the future.
To easily log UI lifecycle events for any and all activities, simply provide a custom application class and in its onCreate() method, register for lifecycle callbacks:

    @Override
    public void onCreate() {
        super.onCreate();
        Log.i(LOG_APP, "Creating Application");

        registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
            @Override
            public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
                Log.i(LOG_UI, activity.getClass().getSimpleName() + " created");
            }

            @Override
            public void onActivityStarted(Activity activity) {
                Log.i(LOG_UI, activity.getClass().getSimpleName() + " started");
            }

            @Override
            public void onActivityResumed(Activity activity) {
                Log.i(LOG_UI, activity.getClass().getSimpleName() + " resumed");
            }

            @Override
            public void onActivityPaused(Activity activity) {
                Log.i(LOG_UI, activity.getClass().getSimpleName() + " paused");
            }

            @Override
            public void onActivityStopped(Activity activity) {
                Log.i(LOG_UI, activity.getClass().getSimpleName() + " stopped");
            }

            @Override
            public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
                Log.i(LOG_UI, activity.getClass().getSimpleName() + " saved");
            }

            @Override
            public void onActivityDestroyed(Activity activity) {
                Log.i(LOG_UI, activity.getClass().getSimpleName() + " destroyed");
            }
        });
    }

Oh and while you are in there, do yourself the favor and enable StrictMode for your debugging builds so that you bad dynamic behaviors of your app are logged.

    if (BuildConfig.DEBUG) {
        StrictMode.enableDefaults();
    }


Modern apps are often revolving around just one activity but many fragments instead, so it's equally if not more important (due to their insane complex lifecycle) to be able to log fragment events. It can be done in a similar way, by overriding onAttachFragment() in a base class for your activities:

    @Override
    public void onAttachFragment(Fragment fragment){
        Log.i(LOG_UI, "Fragment " + fragment.getClass().getSimpleName() + " attached to " + this.getClass().getSimpleName()  );
    }

Now you have an easy way to follow the UI flow of your application without which should help interpret the context of surrounding log messages. I find these major UI events to be important enough to deserve an "info" level classification. In fact, I also add various other UI/INFO level logging lines for menus etc, as in the following:

    @Override
    public boolean onNavigationItemSelected(MenuItem menuItem) {
        Log.i(LOG_UI, "Option '" + menuItem.toString() + "' selected");
    }

Logging templates

Android Studio is based on one of the most powerful IDE's available in modern programming so it also comes with handy templates. The logging templates available out of the box are context aware and will save you much time once you get used to them.


Simply enter the template characters and expand with the tab key. The templates "logm", "logr" and "logi" remains my favorites.

Don't log in production!

Logging is for development purposes - there are plenty of other tools for analytics and crash-reporting, so respect these separation of concerns and avoid compiling logging into your app. Only error, warning and info logs are acted upon at runtime, but verbose and debug levels are still compiled in. Regardless, there simply is no reason to keep any kind of logging for a production app - it's just unnecessary noise. I like to use ProGuard to strip the logging, which is a simple matter of adding the following to your ProGuard file:

-assumenosideeffects class android.util.Log {
    public static boolean isLoggable(java.lang.String, int);
    public static int e(...);
    public static int w(...);
    public static int i(...);
    public static int d(...);
    public static int v(...);
}

Note that if you are using other logging frameworks or custom wrappers, you may also want to target these explicitly to remove the bad weed "at the root" so to speak.

In conclusion

The IDE and associated toolbox is our friend, and we do best to get to know it well since much time and frustration can be saved on that account. Hopefully there's a trick or two in the above you take away from this blog post, but let me just end with showing a matrix I use conceptually when narrowing down a problem in an app based on the proposed logging levels and application aspects above.


The first matrix demonstrates filter settings when just wanting to following along and learn how the UI is put together when i.e. getting to know a new application. The second matrix is how you would think (and thus configure the logging) when wanting to screen an app quickly after a test suite has run, whether something exceptional happened anywhere. The last example shows how one would configure the filter when looking for issues in some app logic doing network communication.

Wednesday, May 25, 2016

Rejsekort, NFC og smartphone kompatibilitet

Det sker ikke så sjældent, at jeg får en mail på noget lignende følgende form:
Min telefon har da NFC, hvorfor kan jeg så ikke downloade din app?
Hvorfor sker ikke noget når jeg scanner kortet med din app?
Jeg savner et sted hvor man kan se om hvilke telefoner der er kompatible med din app (Rejsekortscanner). Jeg kan finde nogle spredte informationer skrevet af dig. Nogle er 2 år gamle på din blog. Hvorfor vedligeholder du ikke bare en liste?
Det er jo gode spørgsmål som jeg har svaret på rigtig mange gange de seneste par år. Det siger sig selv, at som udvikler er jeg naturligvis interesseret i, at gøre det så nemt og smertefrit for potentielle brugere som muligt - men Rejsekort og NFC er og bliver bare et kompliceret emne. Herunder har jeg forsøgt at bryde emnet ned i let-fordøjlige bidder.
  • Hvad er NFC?
    NFC (Near Field Communication) udspringer af envejskommunikation RFID (Radio Frequency IDentification), og tillader kontaktløs tovejskommunikation med et smartcard. Det er altså muligt, over en meget kort afstand, at kommunikere elektronisk med en chip på f.eks. et adgangskort, Rejsekort eller Dankort. Dette sker på frekvensen 13.56MHz og med en båndbredde på 106 kbit/s. Det var firmaerne NXP (tidligere Philips) samt Sony, der pionerene NFC i sin tid og i dag er især NXP stadig en stor spiller inden for NFC, men Samsung, Broadcom mv. er også kommet til. NFC teknologien er nu (delvist) standardiseret af NFCForum under ISO/IEC 14443A. Men sådan har det ikke altid været!

  • Hvad er Mifare?
    Før NFC blev hvermandseje og indbygget i de fleste mobiltelefoner, var teknologien mere eksklusiv og blev primært brugt til elektroniske adgangskort, billetter mv. Til dette var der behov for et såkaldt krypteret "sikkert element", altså et område på kortet man ikke bare sådan uden videre kan læse og skrive til - af hensyn til sikkerhed og svindel. En af disse teknologier fra NXP hedder Mifare og det er dén teknologi rejsekortet også benytter. Selv om NXP var med til at standardisere NFC, så var de smarte nok til at sørge for en udvidelsesmekanisme, således at de stadig kunne bibeholde deres egen overbygning, i form af Mifare - uden at dette stred direkte imod standarden. Ligesom NXP har Mifare som overbygning, har Sony iøvrigt deres Felicia teknologi.

  • Burde alle NCF telefoner ikke kunne læse rejsekortet?
    I teorien jo, for protokol og frekvens er helt den samme og andre producenter end NXP kan stadig godt få lov til at kommunikere med Mifare typen, mod at betale en licens til NXP og implementere det i deres driver. Problemet er bare, at det gør ingen af de andre store spillere som Sony, Samsung og Broadcom. Istedet læner de sig, måske fornuftigt nok, op af den åbne standard - uden at tænke på evt. propriotære udvidelser. Det er derfor en smartphone skal have en NFC chip fra NXP, for overhovedet at have mulighed for at kunne læse rejsekortet!

  • Hvorfor vælger Rejsekort A/S at benytte sig af Mifare?
    Jo det gør de, fordi det i mange år har været de-facto standard inden for elektronisk rejsehjemmel og fordi teknologien er fra før mobiletelefoner med NFC blev hvermandseje. Rejsekort A/S har også haft krav om en pæn portion hukommelse på kortet, p.g.a. det danske system er et nationalt asynkront system, så der var ikke rigtig så mange alternativer. De har også haft krav til sikkerhed, p.g.a. risikoen for svindel med data på kortet. I praksis er der dog ikke megen sikkerhed, da krypteringen blev knækket tilbage i 2007 ligesom nøgler ligger spredt rundt på internettet og i diverse apps. Sikkerheden af systemet må derfor formodes primært at blive håndteret via overvågning af backend systemerne. Rejsekort A/S er sidenhen overgået fra Mifare Classic til Mifare Plus der er sikret bedre mod hacking. Her i 2016 findes der åbne alternativer ligesom, eftersom den fysiske sikkerhed er ikke-eksisterende, man burde kunne formattere Mifare kortet til kun at bruge standardiserede NDEF records - så ville alle NFC telefoner i princippet kunne aflæse kortet, og så ville krypteringen blot ligge i software.

  • Findes der ikke en liste over telefoner et sted?
    Der er faktisk ikke et sted på nettet med troværdig information omkring en telefon har Mifare support eller ej, det bedste der findes er en liste på shopnfc men denne har ofte fejl og mangler. Jeg forsøgte faktisk også i starten at vedligeholde en liste, men det skulle hurtigt vise sig at være en uholdbar praksis. Der kommer nye telefoner, samt nye versioner af eksisterende telefoner til, og det er ingen nem sag at finde ud af, om den har en NFC chip fra NXP eller ej - nogen gange kan man finde ud af dette på ifixit, men her lander kun de største og mest kendte telefoner. Den eneste sikre metode er derfor simpelthen at gå ned i din lokale butik, og prøve at installere og afprøve f.eks. Rejsekortscanner.

  • Hvorfor virker Google Play filtreringen ikke altid?
    Prøv f.eks. at se det screenshot herunder, fra Google Play. Som du måske kan se, er der 432 manuelt udelukkede enheder alene fra Samsung! Dette er altså telefoner med NFC, men som jeg (af den ene eller anden årsag) ikke mener har en NFC chip fra NXP og derfor ikke vil kunne bruge min app Rejsekortscanner. Bemærk også, at der er flere forskellige S6, S6 Edge og S6+ modeller - så man KAN faktisk slet ikke skrive "Virker ikke med S6" for det kommer an på hvilken af de 15 versioner det drejer sig om. Samsung S4 Mini (som min kone har) har aldrig kunne læse Mifare, men seneste model kan sjovt nok godt, så nu har jeg specifikt åbnet for denne. Sidst men ikke mindst, ser jeg også en del rapporter fra brugere der har enheder som Google Play simpelthen ikke kender endnu (f.eks. PHICOMM PASSION).


Det bedste jeg p.t. kan gøre, er at medsende så meget jeg kan omkring en brugers telefon, når de indsender en fejlrapport i app'en. Dette er nogengange nok til, at jeg kan identificere den inde i Google Play og åbne på eller lukke ned for den. Så når der kommer nye modeller til (og versioner af modeller), har disse jo som udgangspunkt lov til at installere Rejsekortscanner. Kun når jeg bliver bekendt med modeller/versioner der giver problemer (typisk brugere der sender vrede mails eller giver én stjerne som bedømmelse) skynder jeg mig ind på Google Play for at tilføje i filtret. Det er dog ikke altid jeg når det i god tid (jeg har job, familie osv. som så mange andre) og så er jeg da lidt ked af at få hivet gennemsnittet ned på ratingen af app'en når det nu ikke er min skyld, men sådan er det bare.

Sidst men ikke mindst, er der nogen gange driver problemer på telefoner på bestemte versioner af Android. Eksemplerne på dette er:
  • Visse Samsung telefoner med Android 5, returnerer ved en fejl 17 bytes for en Mifare blok hvor den kun skal returnere 16
  • På visse HTC One kørende med Android 5 har er NfcA null i TechExtras og skal trækkes ud via et hack
  • På Sony Xperia Z3 findes der 2 NfcA med forskellige SAK værker i TechExtras, som skal patches med et hack
  • Nexus X5 som ser ud til at virke 1/10 gange om som derfor er blokeret indtil jeg kan finde en løsning


På grund af desse problemer, forsøger jeg nu at logge (via Google Analytics) når folk bruger Rejsekortscanner og de IKKE kan læse kortet p.g.a. manglende Mifare. Dette samler jeg op i noget statistik og snart vil jeg formentlig kunne bruge dette til at lukke ned noget hurtigere. Det betyder at jeg på sigt nok VIL være i stand til at lave en form for liste, men den vil være meget stor, særdeles teknisk og vil aldrig være så brugervenlig at hr og fru Danmark bare lige kan se "S5, jo den er på listen".

Håber det var et klart nok svar - i en desværre noget uklar NFC/Mifare verden.

Monday, May 2, 2016

Android: Remove a dependency from a build variation

Android's build variations (flavor and buildType) feature is an awesome way of maintaining several cuts from the same source code. I've used it both for white-label branding solutions and for Free vs. Pro versions. However, sometimes you have different dependencies between the versions where you will usually have to live with the fact, that all variations of your app gets the superset of dependencies. There are ways around this however, and I used it to shave over 400Kb off from a Free variation to a Pro variation.

The problem

Java is a static language, so you can't just go ahead and rely on late-bound linking and have Proguard slice and dice. It's unclear to me, whether the Android variations mechanism extends to Java source code, but I don't think it does. It's also unclear to me, whether an SPI approach could be used, letting the build class-path inject different implementations of some API for use at compile-time. What IS clear to me however, is that we can avoid pulling in a dependency for a flavor its not needed for, which means we will have to use reflection for the flavor that actually needs the stuff.

Removing AdMob from the Pro variation

It doesn't really matter what this app does, the important aspect is that there's a Pro flavor which does not contain the Google Ads from the Free version. So the first thing we're going to need, is to tell Gradle that the AdMob dependency is only for the free flavor. With Gradle, this is as easy as prepending the flavor name in front of the scope, such that "compile" becomes "freeCompile":



Now the dependency is not included for the Pro version. The next thing we need to do, is fix the problem of the static linking with the Ads dependency. The culprit code looks like the following:

 
 
protected void installAd(){ final AdView adView = (AdView)findViewById(R.id.adView); // Only the "free" versions of layouts has an AdView if(adView != null){ final AdRequest adRequest = new AdRequest.Builder().build(); adView.loadAd(adRequest); } }

The above works great in the Free version, but since we modified the Gradle build script, the Pro version no longer compiles! The second thing we then need, is a way to only use the Google Ads for the Free flavor, and do so through reflection in order to not require compile-time linking.

 
 
protected void installAd(){ final View adViewInstance = findViewById(R.id.adView); // Only the "free" versions of layouts has an AdView if(adViewInstance != null){ try { final Class builderClass = Class.forName("com.google.android.gms.ads.AdRequest$Builder"); final Object builderInstance = builderClass.newInstance(); final Method buildMethod = builderClass.getDeclaredMethod ("build"); final Object adRequest = buildMethod.invoke(builderInstance); final Class adViewClass = Class.forName("com.google.android.gms.ads.AdView"); final Method loadAdMethod = adViewClass.getDeclaredMethod("loadAd", adRequest.getClass()); loadAdMethod.invoke(adViewInstance, adRequest); } catch (Exception e) { Log.w(TAG, "Ads could not be loaded due to: " + e.getMessage()); } } }


Note that in the code examples above there's actually no build flavor check to be seen. For this, I rely on the flavor mechanism again. Only the layout from the Free flavor has the R.id.adView view. If this view can not be found, as is the case for the Pro flavor, I skip over doing anything.



The third and final step is easy to forget. The powerful post-processing tool Proguard will remove all unused classes in an attempt to trim down the size of our shippable production build. However, we now rely on reflection in the code, which Proguard's static analysis of the source code won't be able to tell. In stead, we need to instruct Proguard NOT to optimize away the specific code that we rely on. If we don't do this, the reflection magic will have no effect and in my case that means no ads will load but it's also not uncommon to see the app simply crash! Edit your proguard-rules.pro and include the appropriate directives:

 
 
-keepclassmembers class com.google.android.gms.ads.* { 
   public *; 
} 
 

And while I'm in there, I remove all that logging which serves no purpose in a production build anyway. I think it's safe to assume that no end user is sitting with adb and logcat!

 
 
-assumenosideeffects class android.util.Log { 
    public static boolean isLoggable(java.lang.String, int); 
    public static int v(...); 
    public static int i(...); 
    public static int w(...); 
    public static int d(...); 
    public static int e(...); 
} 


There's a fourth and somewhat optional step to mention. The code that you removed, may also let you remove permissions from your application manifest. This is the case for me with the Google Ads, which requires "android.permission.INTERNET" and "android.permission.ACCESS_NETWORK_STATE". My Pro flavor no longer needs these, and no app should request more permissions than it needs, so lets remove this. Once again, the power of the Android flavor mechanism comes to the rescue. Previously I had just one AndroidManifest.xml located in /src/main folder. We can leave this one as the "master" and remove the AdMob specific stuff, which is not applicable to the Pro flavor. Then add a new AndroidManifest.xml under /src/free/ and include the stuff specific to the Free version, namely the requires permissions which will be merged into one.

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.bangbits.rejsekortscanner"> 
 
    <!-- This is needed for Google AdMob --> 
    <uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> 
 
</manifest> 


That's it! The Pro flavor is now trimmed down to about 400Kb lighter. This results in a smaller download, speedier load-time and lower memory pressure. Do you know of a better way then please let me know though the comments!