java.lang.ExceptionInitializerError when using Mockito on Android

So you’re being a good Android developer, using JUnit 3 tests [Sidenote: JUnit 4 does not play well with the JUnit runner in Eclipse for Android currently] and using Mockito to create mocks to make sure you’re focusing on the class that you’re testing. However, you run into a java.lang.ExceptionInitializerError when running your tests. This is pictured below. What should you do?

tl;dr –

To use Mockito on a device or emulator, you’ll need to add three .jar files to your test project’s libs directory: mockito-all-1.9.5.jar, dexmaker-1.0.jar, and dexmaker-mockito-1.0.jar.

“Mockito on Android” from the Square Engineering Blog

Put those 3 JARs linked above in a “libs” directory in your test project.

The root cause of this is that the classes that Mockito makes needs to interact with dex, which is what Android uses to create its classes. This can be done with Dexmaker and the Dexmaker/Mockito integration. Once you add in those 3 JARs into your libs directory, just do the refresh/clean project dance and then you should be good to go.

Special thanks to Jesse Wilson (@jessewilson) for making DexMaker and integrating Mockito with Android!

Parcelabler helps you create Parcelable implementations for your Android classes

If you have ever had to implement the Parcelable interface for one of your Android classes, you will find that it is tedious. It is especially tedious the more fields your class has. So usually you don’t do it unless you have to pass it in an Intent to an Activity or as a Bundle of arguments to a Fragment.

Enter Parcelabler, which is a web-based tool that creates Parcelable implementations for you. Just copy the portion of your class which has the fields into the Code textbox and then press the Build button. I’ve had good results with it, although it is best at primitive fields. I usually edit the class in a text editor like Sublime Text to make it so that only the class and fields are entered.

Thanks to Dallas Gutauckis for creating this helpful tool!

Nexus 7 Won’t Boot Up

I’ve recently been having problems with my Nexus 7 not booting up after it completely runs out of battery. Specifically – even if I have charged it overnight, if the power is off and I click the power button, then it does not turn on!

I’m not sure if this is related to 4.2.1, but this problem didn’t seem to occur with 4.1.1. I’ve got the 8GB version of the Nexus 7.

To solve it, press and held on to the power button until it starts up (for approximately 30 seconds.)

Note that Asus has a support page which mentions this titled Nexus 7 won’t start up. I agree with their recommendations, but I’d add that you’ll probably need to press and hold it for longer than what they recommend (around 30 seconds vs their recommended 15).

Update: after a quick search, a thread on Android Enthusiasts titled Nexus 7 wont boot after complete discharge corroborates my findings.

AnDevCon III Review

I attended AnDevCon III in May 2012 as my first Android-related conference, about a year and a half into my Android experience. I have attended many developer conferences before so I thought it’d be interesting to compare it to those. I’ll be attending AnDevCon IV later this week, so obviously I was pleased with what I experienced, but I wanted to note down what my thoughts were from that first experience.

I’ve been to a variety of conferences:

  • JavaOne, one of the biggest but also one of the most “corporate”
  • WWDC, the best place to interact with Apple engineers and other Mac and iOS developers
  • EclipseCon, which was focused on a single open source project, Eclipse
  • No Fluff Just Stuff, sort of an anti-JavaOne, where the speakers are all practitioners
  • C4, sort of an anti-WWDC, where the speakers were mostly Mac indies

AnDevCon feels a lot like EclipseCon. Partly because this is because it was held at the Hyatt Regency in Burlingame, where I once attended EclipseCon 2005. More substantively, a lot of companies participate in the conference, which gives it an interesting and varied vibe. This is compared to single-vendor conferences like WWDC or Google I/O, where you get the perspective of only one company mainly.

Google engineers and evangelists do play well with AnDevCon, however. They present some of the sessions, which are prominently marked on the scheduled as “Google CLASS”. Kirill Grouchnikov‘s Responsive Mobile Design in Practice was particularly insightful in the project that I was working on at the time. I had seen his slides, but they did not make as much sense as when I had heard him speak live about responsive design. Now that we can nest fragments within fragments as of the 4.2 SDK, I wonder what he would say about that.

There are a large amount of sponsors. You might think that this is a bad thing if you have attended a conference that had lots of sponsored talks. However, the organizers seem to be aware of this and have helpfully marked all the talks that as “Sponsored by XYZ”. I actually liked some of those, particularly the ones sponsored by Intel, where I learned more about the HAXM-accelerated emulator and Sony, where they taught us about the Sony SmartWatch SDK.

I normally don’t like Exhibit Halls at conferences. Sure, you can get a lot of free goodies. OK, I admit it – half my wardrobe is conference t-shirts. But the booths usually are kind of boring. However, at AnDevCon, there were a lot of interesting exhibits. I’m not sure if it is because the industry is expanding so rapidly or because I just like gadgets, but there were some things I hadn’t seen before there, like the Epson Android-powered glasses (sort of a bulky early release of Google Glass) or the Qualcomm developer boards, which are entire Android systems on a large PCB.

I met a lot of interesting people, especially at Square’s Android Dessert Bash. Everyone was fired up about Android and there were a lot of different perspectives there. There were folks from the phone manufacturers like HTC, independent consultants, authors, trainers, regular developers and also platforms like Nook. It was pretty friendly atmosphere but I didn’t see as many night parties as I have seen at other events, but maybe it’s because I didn’t know as many people.

I honestly did not know what to expect and only knew one person who was going to the event, Dave Smith, who gives talks on Android accessory development. I was pleasantly surprised with my overall experience. The only con is that the official conference t-shirt was sponsored – in my opinion, it’d be nicer if it just had AnDevCon on it. However, Square gave away one that was more subtle and had all the different dessert icons on it, which was my favorite.

I’m looking forward to AnDevCon IV, happening later this week, which promises to be even bigger and better, featuring keynotes from Amazon, Google and Facebook.

Fixing Copy in Eclipse

I use Eclipse for Android development and one of the issues I have come across is that sometimes Command-C on Mac (Control-C on Windows) doesn’t work properly. I will copy, but paste will paste what was previously in the buffer, not what the latest I wanted.
My previous workaround was to hit Command-C twice which always seems to work. This is a pain, however.
Fortunately I came across this blog post: Ctrl+C to copy does not always work in Eclipse which describes a simple fix: Unbind the copy command in Eclipse. Note that this also applies to the Android Developer Tools distribution, since it is built on Eclipse.

The workaround is to unbind the Copy command in Eclipse. The reason this works is that the native Copy will be used instead.

To do so, go into the menu:
ADT or Eclipse
> General
>> Keys

Search for Copy
Select Copy
Press the button “Unbind Command”

Here are some screenshots:

Before (Copy is bound to a shortcut key):
Copy command bound to a shortcut key

After (Copy command no longer bound to a shortcut key):
Copy command no longer bound to a shortcut key

LG Optimus V Black Screen Fix

I have an LG Optimus V that I use for testing. Its battery had run down and when I plugged it back in, the buttons would light up but the screen was black. So what to do with a blank screen?
Unplug from wall. Remove the back cover. Remove the battery. Put the battery back. Replace cover. Plug it back in. The screen is back!

Official Android SDK

There’s a new official Android SDK available that was released this past week. It is basically Eclipse 3.8 + Android Developer Tools (ADT) 21, pre-installed in a single downloadable package. Previously, Android programmers had to go through a multi-step process to locate the ADT update site, accept licenses, etc. This should save time for new installs. If you’ve got a working setup, I’d stick with that for now, but just update your ADT to version 21.

Why you should participate in Stack Overflow

If you’re reading this blog, you’re probably a programmer and you probably use Stack Overflow daily. It launched back in 2008 and I’ve been using it, sometimes without knowing it for these past few years. This year, I decided to consciously contribute back to Stack Overflow and also try to rack up 1000 reputation points, aka rep. As of this writing, I’m up to 2,106 rep and very happy about my experience contributing. So… why should you participate on Stack Overflow and what tips can I offer on how to do that?

Save the programmer, save the world (or Why you should participate)

Stack Overflow (SO) shows up frequently in Google searches. Google any Android exception or iOS error message and you’ll get at least one SO hit, if not several. It is the programming world’s equivalent to Wikipedia, being somewhat of a canonical resource for programming questions and answers.

Programmers seem to turn to SO first nowadays, even before asking other programmers or consulting documentation. Why is that? Well my theory is that we don’t want to interrupt other people and documentation can be hard to parse. On the other hand, a lot of times you can find either working code or good pseudo-code in an SO answer. If you think this is just my opinion, consider that SO has 15,000,000+ software developers visiting every month.

SO is also a great case study in gamification. Most programmers I know are avid game players (both board and video), so incentivizing us to contribute by rewarding us with points is a natural fit with our brains. SO has tuned this game mechanic over the years to encourage good questions, great answers and discourage spam, which is the natural enemy of any online community. I confess that some nights I would stay up later than I would so I could try to answer just one more question, which might lead to a few more rep.

Stack Overflow icon

Those are the obvious reasons, but there are deeper, more meaningful ones to contribute to Stack Overflow:

SO is good for your resume. Before I interview a potential programmer to join my team, I always check their SO profile, not just to see how much rep, which is a rough measure of how much they participate and how valued their contributions are, but to see how well they communicate. Programmers don’t just write code, but they also have to discuss potential problems, solutions, relay their progress, etc. Reading someone’s set of SO answers is a proxy for that. In past years, I would have said that I would go to read their technical blog, but those are sadly in decline due to a combination of SO, Twitter, Google+, App.net, etc.

You will learn as a consequence of answering questions. Even if you already know it, having to communicate an answer clearly to someone else will ingrain that knowledge into your brain further. Sometimes it is good to find a question that has interested you before that you can do a bit of research into and you’ll satisfy your curiosity while answering someone’s question.

“I play the Stack Exchange game happily alongside everyone else, collecting reputation and badges and rank and upvotes, and I am proud to do so, because I believe it ultimately helps me become more knowledgeable and a better communicator while also improving the very fabric of the web for everyone. I hope you feel the same way.”
– Jeff Atwood in The Gamification

Best of all, you will be helping people. We exist in a larger community of programmers and I think it is a good policy to help others when you can. Whether you subscribe to The Golden Rule, you should consider that if you use open source, if you rely on the answers in Stack Overflow, that it took someone willing to help to write those things that you use when you write your own apps.

Now the hard part… (or Tips on participating)

When you first register with Stack Overflow, you’ll start out with 1 rep. Which means that the only thing you can really do is ask a question or answer one. You can suggest edits too. I would suggest starting out by asking a good question, including what you have tried and any relevant code. That’s probably the easiest way to get started and you’ll get 5 points every time someone votes your question up. Voting a question up means that it is well worded and usually meaningful to the voter.

If you ask a question, make sure to check back and accept the best answer that you get. You will reward the answerer and you’ll get an additional 2 points yourself. Note that your accept rate is tracked and if you ask too many questions without accepting, people may stop answering your questions, since that is a sign that you’re not being a good member of the community.

Another way to participate is to share your knowledge by answering questions. That’s what I have been doing, since I’ve learned a thing or two over my 18 year programming career. (I feel old now.) In any case, I think everyone has some expertise to share and that’s how we all learn as a community: I share what I know, you share what you know, we both get better.

Make sure you answer questions thoroughly. Don’t just do a quick link to a blog post. Code is probably the best. If you do include code, make sure it compiles and works correctly. Include screenshots if it is relevant, for example if someone is asking how to change the background of an action bar, show a screenshot with a multi-colored action bar. Learn the right markup, which is in Markdown or use the toolbar to format your answer to properly insert hyperlinks, quote the question, quote documentation, etc.

Multi-color action bar

Once you get up to 15 rep, which shouldn’t take too long, then start to vote up questions and answers. This will help others figure out which answer is better than the others if there are multiple answers to a question. It also helps figure out which questions are popular.

Fill out your profile and include an avatar. I greatly prefer answering people’s questions that have a face to them, not just some randomly generated artwork. Include your qualifications and this might also help people judge whether you are credible or not, especially when you are first starting out.

Be persistent. Ultimately, you’re dealing with other people who have their own schedules. Just because you ask a question, it doesn’t mean that people are going to answer it right away. If you really want it answered and haven’t had any takers yet, then share the link to it on App.Net or Google+ or Twitter. Conversely, just because you answer a question, it does not mean that the system will automatically recognize your contribution and award you 15 points. The person asking the question has to do that and they may have some follow-up comments. Sometimes you just have to wait. Sometimes you will never get rewarded and that’s alright – you still have contributed to the community. And who knows – weeks, months or years later, someone might come across your question because they have it themselves or appreciate the answer that you gave and upvote you, sending a few more rep your way.