Pages

Wednesday, September 25, 2013

Maven Central's 'artifactid' is build.gradle's 'name' property

Recently I wanted to utilize a third party library in one of my Android applications. The library is available via Maven Central, and since I'm now using Android Studio for development I thought I'd use gradle to auto-import the library. I knew this was possible and relatively straightforward, but I ran into something that threw me for a loop and that I couldn't find documented officially anywhere. I figured I'd share what I found to hopefully save someone else the potential hassle.

At the bottom of each Android app's build.gradle file is the section where you reference third party libraries that will automatically come from Maven Central. For most Android apps the support library is included:
dependencies {
    compile 'com.android.support:support-v4:18.0.0'
    //This is where references to the third party libs go
}
To get a library to work in gradle from Maven, it needs 3 pieces of information. For every library, Maven central provides 3 primary pieces of information. Respectively, the fields for each are:
//Gradle Info:
group: 'com.example.library'  //The package name for the library
name: 'someuniquename'        //An identifier for the lib
version: 'X.Y.Z'              //lib version number

//Maven Info:
groupId: 'com.example.library'  //The package name for the library
artifactId: 'someuniquename'    //An identifier for the lib
version: 'X.Y.Z'
It seemed pretty obvious that "group" and "groupId" would line up in both places. But here was my gotcha point: when referencing the maven library, the "artifactId" value from Maven is what you use for gradle's "name" property.

Like I said, I couldn't find this documented anywhere. On a different project previously I attempted to bring in a Maven library and it didn't go so well. It could have been due to a buggier, earlier version of Android Studio, but not knowing this bit of info certainly didn't help.

When you bring it all together, this is what the build.gradle file will look like:
dependencies {
    compile 'com.android.support:support-v4:18.0.0'
    compile group: 'com.example.library', name: 'someuniquename', version: 'X.Y.Z'
}

Saturday, May 18, 2013

Quick Tip: Set socket.io log level in zappajs

I've been working on a zappajs based website that has a socket.io connection to the browser. By default socket.io likes to output a lot of debug text. I got tired of seeing it all and wanted to turn it off. Thankfully, it's easy.

At the top level of your zappajs instance definition, right alongside where you define your @use statements, define routes/templates, and so on, you can call into the @io object, which is a reference into the socket.io server-side instance. You just put:

@io.set 'log level', 1

-- or if you like closer to "regular" javascript syntax you can do: --

@io.set('log level', 1)

Log level 1 is the least chatty of them all. You can see the different levels to choose the one you want here.

Friday, March 8, 2013

"do" stuff in Coffeescript

I can't deny that I have an attraction to coffeescript. I can't even fully explain it. I think it might be the super-tight syntax, ability to not have to depend on parenthesis or curly brackets, and zippy server-side templating engines like CoffeeKup or Eco. Of course there are pros and cons to coffeescript (I'll abbreviate to CS), and I definitely don't use it in all cases. The point of this post isn't to love or hate on CS; I'll leave that to others more qualified than myself.

As I've been learning CS there are a couple of things I normally do in javascript that I couldn't quite figure out how to do:
  • The right way to call a function with no arguments
  • Immediately execute anonymous functionality, like in a switch statement
It turns out that they both hinge on using the same keyword: "do."

No-Argument Functions
CS is very sparse in its use of parenthesis; calling functions with arguments makes clear sense, but it's not immediately clear how functions without arguments get called. Check out the two lines of code:

The second line of code seems logical, but we can't forget this is still javascript. The processData function is an object after all, so that line of code merely assigns processData to returnData. Adding parenthesis works - that is, processData() - but I figured there was a more "coffeescript-y" way to do it. Enter "do:"

There is the CS way to do things! Calls the function perfectly. "do" Helps us elsewhere too:

Immediately Call Anonymous Functionality:
In a similar fashion, there are times when an anonymous function is immediately called, as opposed to in a callback fashion. I'll just show the code:

Again, "do" is the secret weapon here. When supplying callback function to CS, you can just specify inline without "do." But in the case of the switch statement it needs to called immediately, hence the keyword.

Those are the two cases that I was able to find, but perhaps there are more ways "do" is used in CS. Did I miss anything?

Tuesday, January 15, 2013

How to: Receive NFC data in an Adobe AIR Android app

Getting NFC data into an Adobe AIR for Android application is much easier than it might seem. At least, it's a lot easier than I thought it would be.

I'm currently working on an app that has just such a need. Specifically, users will run the AIR app and then swipe their device over a physical display that has some NFC tag hotspots. The app needs to receive the NFC data from each tag and react in a specific fashion. I didn't know much about NFC data when I began working on this current app, and since AIR doesn't have official support for NFC I figured I would need a native extension to properly handle things.

It turns out it is *much* simpler than needing to implement any sort of native functionality. All you have to do is add the proper intent handling to the "manifestAdditions" portion of the AIR application xml. I'll put a sample xml block here, and then explain it:


The key to making everything work is in effect "extending" the <application> node - which prior to discovering this I didn't know was possible. By adding the appropriate NFC intent filter information - the "default" way you handle NFC data in Android - the AIR application is handed the day the same way any other Android application would be.

In the xml sample above, I have  my AIR app responding to a URI. I've got placeholders for the scheme and host attribute values. Most commonly these will represent a web URL; in that case scheme would be "http" and host would be the domain - "yourdomain.com". But if you want to use proprietary data, you can do that too. Scheme and host can be anything. Scheme could be "my" and host could be "uri.value" which would represent "my://uri.value." It doesn't really matter, the data will make it to the AIR app. This is just one way of targeting NFC data into an app - there are a number of different ways to use intent filters in Android to target varied NFC data types. Virtually all of them should work as manifest extensions for an AIR app. The Android documentation has information about targeting NFC data here.

Extending the manifest only gets us so far; it doesn't tell us how the data gets into the AIR application. Thankfully, Adobe has facilitated that too. Anytime the app responds to intent filter data, an InvokeEvent.INVOKE event gets fired. Check out the below class:

To be clear, the invoke event will in fact be fired when the app starts up, but will *also* be fired whenever the NFC intent data qualifies and is passed to the app. This means that the app will be handed any and all relevant NFC data and can respond by reading the events.arguments data.

This also has the added effect of launching your AIR application whenever the relevant NFC data is swiped. That's actually what Android generally means to have happen with NFC data - the user swipes their device and the relevant specific application is opened. This will in fact do that. But as this post discusses, it will also pass in the NFC data while the application is running.

If you aren't wanting the app to be launched on NFC data but instead *only* respond to the NFC data while the application is open, that requires more complexity. It does, in fact require a native extension interacting with something called the "Foreground Dispatch". A very intelligent person has actually cracked the code and has made, well, the source code available for free! You can see it here:

http://code.google.com/p/ane-lab/source/browse/#svn%2Ftrunk%2Fmobile%2Fandroid%2Fjava%2Fnfc-foreground-dispatch

I hope this helps!