<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Android &#8211; Wiebe Elsinga</title>
	<atom:link href="http://wiebe-elsinga.com/blog/tag/android/feed/" rel="self" type="application/rss+xml" />
	<link>http://wiebe-elsinga.com/blog</link>
	<description>Blog</description>
	<lastBuildDate>Fri, 05 Jun 2015 08:58:31 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=5.8</generator>
	<item>
		<title>Design/Prototyping tools for Android</title>
		<link>http://wiebe-elsinga.com/blog/designprototyping-tools-for-android/</link>
					<comments>http://wiebe-elsinga.com/blog/designprototyping-tools-for-android/#respond</comments>
		
		<dc:creator><![CDATA[W.Elsinga]]></dc:creator>
		<pubDate>Fri, 05 Jun 2015 01:58:31 +0000</pubDate>
				<category><![CDATA[AndroidUX Tips]]></category>
		<category><![CDATA[Android]]></category>
		<category><![CDATA[Design]]></category>
		<category><![CDATA[Tool]]></category>
		<guid isPermaLink="false">http://wiebe-elsinga.com/blog/?p=1826</guid>

					<description><![CDATA[For developers getting the right Android developing tool is easy, it&#8217;s either Android Studio or Eclipse. For designing/prototyping Android applications it&#8217;s a bit more tricky. There are a lot of products/tools you can use, all with different features, prices and usability. So let us look at some Design/Prototyping tools: Sketch 3 (Bohemian Coding) Sketch 3 is a Vector based design application. It&#8217;s easy to use, and has a low learning curve. Think of a user friendly mix of Photoshop and Illustrator You have the possibility to use a ton of]]></description>
										<content:encoded><![CDATA[<p>For developers getting the right Android developing tool is easy, it&#8217;s either Android Studio or Eclipse.<br />
For designing/prototyping Android applications it&#8217;s a bit more tricky. There are a lot of products/tools you can use, all with different features, prices and usability.</p>
<p>So let us look at some Design/Prototyping tools:<br />
<span id="more-1826"></span></p>
<h3>Sketch 3 (Bohemian Coding)</h3>
<p><img loading="lazy" src="http://wiebe-elsinga.com/blog/wp-content/uploads/2015/06/Sketch3.png" alt="Sketch3" width="640" height="378" class="aligncenter size-full wp-image-1834" data-wp-pid="1834" srcset="http://wiebe-elsinga.com/blog/wp-content/uploads/2015/06/Sketch3.png 640w, http://wiebe-elsinga.com/blog/wp-content/uploads/2015/06/Sketch3-300x177.png 300w" sizes="(max-width: 640px) 100vw, 640px" /><br />
<a href="http://bohemiancoding.com/sketch/" target="_blank">Sketch 3</a> is a Vector based design application. It&#8217;s easy to use, and has a low learning curve. Think of a user friendly mix of Photoshop and Illustrator<br />
You have the possibility to use a ton of <a href"http:>plugins</a> to make the design experience even greater.<br />
And using the <a href="http://material-design.storage.googleapis.com/publish/material_v_4/material_ext_publish/0Bzhp5Z4wHba3M25veVdZRHNHT0k/stickersheet_general.sketch" target="_blank">template components for Material Design</a> should make designing great applications fun.</p>
<p><strong>Price:</strong> $99<br />
<strong>Makes me happy because:</strong></p>
<ul>
<li>Easy to use, no high learning curve</li>
<li>Support plugins</li>
<li>Many templates are available</li>
</ul>
<p>
<strong>Makes me sad because:</strong></p>
<ul>
<li>Updates can break created designs</li>
<li>So support for live viewing on an actual Android device</li>
</ul>
<h3>Illustrator (Adobe)</h3>
<p><img loading="lazy" src="http://wiebe-elsinga.com/blog/wp-content/uploads/2015/06/Illustrator.png" alt="Illustrator" width="640" height="378" class="aligncenter size-full wp-image-1837" data-wp-pid="1837" srcset="http://wiebe-elsinga.com/blog/wp-content/uploads/2015/06/Illustrator.png 640w, http://wiebe-elsinga.com/blog/wp-content/uploads/2015/06/Illustrator-300x177.png 300w" sizes="(max-width: 640px) 100vw, 640px" /><br />
<a href="http://www.adobe.com/products/illustrator.html" target="_blank">Adobe Illustrator</a> is also a Vector based design application. It contains a lot of features for the skilled designer. But if you are a Illustrator newbie, you can be overwhelmed by all the possibilities and options available.<br />
As with Sketch 3 they also have a <a href="https://material-design.storage.googleapis.com/publish/material_v_4/material_ext_publish/0Bzhp5Z4wHba3YlIzblpPcnlFb0E/stickersheet_general.ai" target="_blank">template components for Material Design</a> which you can use.</p>
<p><strong>Price:</strong> €24.19 per month for just Illustrator (or €69.49 per month for entire Creative Cloud suite)<br />
<strong>Makes me happy because:</strong></p>
<ul>
<li>Focus on screen design</li>
<li>Many templates are available</li>
</ul>
<p>
<strong>Makes me sad because:</strong></p>
<ul>
<li>Learning curve is to high</li>
<li>So expensive</li>
</ul>
<h3>Flinto</h3>
<p><img loading="lazy" src="http://wiebe-elsinga.com/blog/wp-content/uploads/2015/06/mobile_prototyping_tool_flinto.jpg" alt="mobile_prototyping_tool_flinto" width="640" height="400" class="aligncenter size-full wp-image-1839" data-wp-pid="1839" srcset="http://wiebe-elsinga.com/blog/wp-content/uploads/2015/06/mobile_prototyping_tool_flinto.jpg 640w, http://wiebe-elsinga.com/blog/wp-content/uploads/2015/06/mobile_prototyping_tool_flinto-300x188.jpg 300w" sizes="(max-width: 640px) 100vw, 640px" /><br />
<a href="https://www.flinto.com" target="_blank">Flinto</a> is designed as a tool for creating interactive prototypes based on PNG’s. Just link your PNG&#8217;s that you have created to screens and set transitions for that app-like feeling.</p>
<p><strong>Price:</strong> €20 per month<br />
<strong>Makes me happy because:</strong></p>
<ul>
<li>Ease of use for prototyping</li>
<li>Great transitions</li>
</ul>
<p>
<strong>Makes me sad because:</strong></p>
<ul>
<li>You can only use it for prototyping</li>
<li>Kind of pricey</li>
</ul>
<h3>Marvelapp</h3>
<p><img loading="lazy" src="http://wiebe-elsinga.com/blog/wp-content/uploads/2015/06/mobile_prototyping_tool_marvelapp.jpg" alt="mobile_prototyping_tool_marvelapp" width="640" height="400" class="aligncenter size-full wp-image-1841" data-wp-pid="1841" srcset="http://wiebe-elsinga.com/blog/wp-content/uploads/2015/06/mobile_prototyping_tool_marvelapp.jpg 640w, http://wiebe-elsinga.com/blog/wp-content/uploads/2015/06/mobile_prototyping_tool_marvelapp-300x188.jpg 300w" sizes="(max-width: 640px) 100vw, 640px" /><br />
<a href="https://marvelapp.com" target="_blank">Marvelapp</a> links your prototype with Dropbox to create an interactive prototype on your mobile device or desktop. No need to code or save PNG&#8217;s as images.<br />
It&#8217;s easy to use, and can be used with different Vector based design applications. You can even export your prototype to a runnable APK.</p>
<p><strong>Price:</strong> starts from €0 per month to €100 per month.<br />
<strong>Makes me happy because:</strong></p>
<ul>
<li>Ease of use for prototyping</li>
<li>Great UI</li>
<li>Exportable APK&#8217;s</li>
</ul>
<p>
<strong>Makes me sad because:</strong></p>
<ul>
<li>No unlimited exportable APK&#8217;s</li>
</ul>
<h3>Keynotopia</h3>
<p>If you know how to use Keynote or Powerpoint, you may want to use <a href="http://keynotopia.com" target="_blank">Keynotopia</a>. Keynotopia is a library that contains UI components.<br />
So you don&#8217;t need to know how to use a different design tool, just drag en drop your UI components to make your design.</p>
<p><strong>Price:</strong> €49 if you are just interested in Android design.<br />
<strong>Makes me happy because:</strong></p>
<ul>
<li>No need to learn a new tool</li>
<li>Free life time updates</li>
</ul>
<p>
<strong>Makes me sad because:</strong></p>
<ul>
<li>UI components aren&#8217;t updated regularly</li>
<li>Still no Android preview app available</li>
</ul>
<h3>Pen and paper</h3>
<p>Last but not least, you can just you a pen and a piece of paper to do your design. It&#8217;s doesn&#8217;t need to be a paid app or service to start designing awesome designs.</p>
<p><strong>Price:</strong> €1.<br />
<strong>Makes me happy because:</strong></p>
<ul>
<li>No limitations</li>
</ul>
<p>Also available:<br />
<a href="http://www.invisionapp.com/" target="_blank">InVision</a>, <a href="https://proto.io/" target="_blank">Proto.io</a>, <a href="https://popapp.in/" target="_blank">POP</a>, <a href="https://www.uxpin.com/" target="_blank">UXPin</a>, <a href="http://www.fieldtest.it/" target="_blank">FieldTest</a></p>
<p><center><img width="320" src="http://i.giphy.com/5fBH6zoy1szfjORd14s.gif"/></center></p>
<p>finally let me give you one advice, if you have no previous graphic design experience then I would suggest that you mastering the basics of graphic design first.<br />
Once you are comfortable with the basics then you can start to look into designing with a specific tool.</p>
]]></content:encoded>
					
					<wfw:commentRss>http://wiebe-elsinga.com/blog/designprototyping-tools-for-android/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Tips on how to improve your ‪Android‬ ‪UX</title>
		<link>http://wiebe-elsinga.com/blog/improve-your-android-ux/</link>
		
		<dc:creator><![CDATA[W.Elsinga]]></dc:creator>
		<pubDate>Fri, 08 May 2015 03:59:54 +0000</pubDate>
				<category><![CDATA[AndroidUX Tips]]></category>
		<category><![CDATA[Business]]></category>
		<category><![CDATA[Android]]></category>
		<category><![CDATA[Design]]></category>
		<category><![CDATA[UX]]></category>
		<guid isPermaLink="false">http://wiebe-elsinga.com/blog/?p=1786</guid>

					<description><![CDATA[During recent Android app clinics I hosted and/or attended, I started noticing developers/designers implement some common UX flaws and found myself questioning if they know how to do proper UX. Some of those Android applications I&#8217;ve seen are really well thought out from a technical point of view, but lack good UX. So maybe I should share some techniques/tips on how to improve the UX. 1) Analyze Do some analysis on your current (or future) UX design. As designer and/or developer we tend to think we know everything, but we don&#8217;t. Your application may be used differently than you might think. Knowing how users are using or will use your application is valid information in making a good UX design, as the focus is, and always should be, the user. Do usability tests to discover problems or bottle-necks in your design. This can be done by actually sitting down with a user and observe his/her behavior while he/she is using your application or prototype. Use analyzing tools like Google analytics to find how user are using your application, so you know the user focus (or lack of) inside your application. Or just look around how people are using there device to know basic human-device interaction. A good example might be when you application is using the camera as feature. You&#8217;ve probably seen that &#8220;selfiesticks&#8221; are becoming more populair, so your application might be needing front-camera support as well. And finally have a look at user reviews in Google Play. They might supply you with vital information on how to make your users happy (and getting good ratings at the same time). 2) (Re)Think It might be a good idea to have a second (or third) look at your current UX design and re-think. Try to look for these bad UX omens: Additional texts to explain how it elements work. You can&#8217;t document your way out of confusing UX. Novel paradigms. Stick to known patterns, so users feel quickly at ease. FAQ section. The information presented to the user should be obvious and clear. &#8220;Pop-ups&#8221;, like a confirm dialog. Don&#8217;t block the UX. Don&#8217;t say that something is dangerous if you can&#8217;t fix the danger. Do support &#8220;undo&#8221;. Too much information. Keep it simple (is always the hardest to do). So how can you test if your new UX is wel thought out you may ask, well: You might want to prototype your new UX design first in stead of rewriting your app. This is quick and easy to do and can test if your redesign has been improved. Try doing A/B testing so you can compare the new design against your current one. Testing eliminates guesswork and turns &#8220;I think&#8221; into &#8220;I know&#8221;. 3) Adapt As described in the previous section it&#8217;s important to stick to known patterns, so the user can quickly become acquainted with your application features. Adapting platform standards is the way to go. And Google has done a great job with the introduction of Material Design, by suppling us with well thought out UX design patterns. Learn these pattern and apply them to your UX design. One question I get asked a lot is when an application is adapting Material design will it still contain its uniqueness and will my branding survive. My answer is yes. Branding can by maintained by: The application or product icon. Use of colors. And the actual application itself, meaning your brand should be reflected in the application. Let me explain this by giving you an example giving to me by Marie Schweiz about her redesign of the Top Gear application. The succes of Top Gear (brand) is not the cars, but the three guys hosting it and the fun they have with cars. This should be reflected in the UX design, so make if playful. 4) Add It might sounds strange, adding features to transform UX design, but it&#8217;s not. Let me be clear, I&#8217;m not talking about adding more content but adding features like feedback, value and user aid. So you can add: Interaction feedback, which gives the user confidence that he/she is using the content correctly Error feedback, which warns the user and a possible solution. But be careful not to block the user while giving this type of feedback. Loading feedback, which gives the user options, should I wait or just continue. Again don&#8217;t block the user. Action aid, which gives the the user the option to &#8220;undo stupidity&#8221; If you want to know more, you may want to visit: Google Material design introduction User testing explained, part 1 User testing explained, part 2 Android UI/UX website by Taylor Ling Android UI patterns website by Juhani Lehtimäki Udacity course: UX Design for Mobile Developers Google design Youtube channel I really want to thank my friend and colleague Ali Derbane for reviewing this post]]></description>
										<content:encoded><![CDATA[<p>During recent Android app clinics I hosted and/or attended, I started noticing developers/designers implement some common UX flaws and found myself questioning if they know how to do proper UX.<br />
Some of those Android applications I&#8217;ve seen are really well thought out from a technical point of view, but lack good UX.</p>
<p>So maybe I should share some techniques/tips on how to improve the UX.<br />
<span id="more-1786"></span><br />
<center><img width="320" src="http://i.giphy.com/rV4guAJUoU6Vq.gif"/></center></p>
<h3>1) Analyze</h3>
<p>Do some analysis on your current (or future) UX design. As designer and/or developer we tend to think we know everything, but we don&#8217;t. Your application may be used differently than you might think.<br />
Knowing how users are using or will use your application is valid information in making a good UX design, as the focus is, and always should be, the user.  </p>
<ul>
<li><strong>Do usability tests</strong> to discover problems or bottle-necks in your design. This can be done by actually sitting down with a user and observe his/her behavior while he/she is using your application or prototype. </li>
<li><strong>Use analyzing tools</strong> like Google analytics to find how user are using your application, so you know the user focus (or lack of) inside your application.</li>
<li>Or just <strong>look around</strong> how people are using there device to know basic human-device interaction. A good example might be when you application is using the camera as feature. You&#8217;ve probably seen that &#8220;selfiesticks&#8221; are becoming more populair, so your application might be needing front-camera support as well.</li>
<li>And finally have a <strong>look at user reviews</strong> in Google Play. They might supply you with vital information on how to make your users happy (and getting good ratings at the same time).</li>
</ul>
<p></p>
<h3>2) (Re)Think</h3>
<p>It might be a good idea to have a second (or third) look at your current UX design and re-think.<br />
Try to look for these bad UX omens:</p>
<ul>
<li><em>Additional texts</em> to explain how it elements work. You can&#8217;t document your way out of confusing UX.</li>
<li><em>Novel paradigms</em>. Stick to known patterns, so users feel quickly at ease.</li>
<li><em>FAQ section</em>. The information presented to the user should be obvious and clear.</li>
<li><em>&#8220;Pop-ups&#8221;</em>, like a confirm dialog. Don&#8217;t block the UX. Don&#8217;t say that something is dangerous if you can&#8217;t fix the danger. Do support &#8220;undo&#8221;.</li>
<li><em>Too much information</em>. Keep it simple (is always the hardest to do).</li>
</ul>
<p>
So how can you test if your new UX is wel thought out you may ask, well:</p>
<ul>
<li>You might want to <strong>prototype</strong> your new UX design first in stead of rewriting your app. This is quick and easy to do and can test if your redesign has been improved. </li>
<li>Try doing <strong>A/B testing</strong> so you can compare the new design against your current one. Testing eliminates guesswork and turns &#8220;I think&#8221; into &#8220;I know&#8221;.</li>
</ul>
<p></p>
<h3>3) Adapt</h3>
<p>As described in the previous section it&#8217;s important to stick to known patterns, so the user can quickly become acquainted with your application features.<br />
Adapting platform standards is the way to go. And Google has done a great job with the introduction of Material Design, by suppling us with well thought out UX design patterns.<br />
Learn these pattern and apply them to your UX design.<br />
One question I get asked a lot is when an application is adapting Material design will it still contain its uniqueness and will my branding survive. My answer is yes.<br />
<center><img width="320" src="http://i.giphy.com/12nuDeDV4xU3yE.gif"/></center><br />
Branding can by maintained by: </p>
<ul>
<li>The application or product icon.</li>
<li>Use of colors. </li>
<li>And the actual application itself, meaning your brand should be reflected in the application. </li>
</ul>
<p>
Let me explain this by giving you an example giving to me by <a href="https://plus.google.com/+MarieSchweiz" target="_blank">Marie Schweiz</a> about her redesign of the Top Gear application.<br />
The succes of <a href="http://www.topgear.com/">Top Gear</a> (brand) is not the cars, but the three guys hosting it and the fun they have with cars. This should be reflected in the UX design, so make if playful.<br />
</p>
<h3>4) Add</h3>
<p>It might sounds strange, adding features to transform UX design, but it&#8217;s not. Let me be clear, I&#8217;m not talking about adding more content but adding features like feedback, value and user aid.<br />
So you can add:</p>
<table>
<tr>
<td style="vertical-align:middle">
			<video src="http://wiebe-elsinga.com/blog/wp-content/uploads/2015/05/shamuLMY47DWiebe05082015095543.mp4" id="id0" width="200" onMouseOver="id0.play()" onMouseOut="id0.pause()" onclick="window.location='http://wiebe-elsinga.com/blog/wp-content/uploads/2015/05/shamuLMY47DWiebe05082015095543.mp4';id0.pause()" loop></video>
		</td>
<td style="vertical-align:middle">
			<strong>Interaction feedback</strong>, which gives the user confidence that he/she is using the content correctly
		</td>
</tr>
<tr>
<td style="vertical-align:middle">
			<a href="http://wiebe-elsinga.com/blog/wp-content/uploads/2015/05/shamuLMY47DWiebe05082015102326.png" rel="lightbox[1786]" title="Tips on how to improve your ‪Android‬ ‪UX"><br />
				<img src="http://wiebe-elsinga.com/blog/wp-content/uploads/2015/05/shamuLMY47DWiebe05082015102326.png" width="300" class="aligncenter" /><br />
			</a>
		</td>
<td style="vertical-align:middle">
			<strong>Error feedback</strong>, which warns the user and a possible solution. But be careful not to block the user while giving this type of feedback.
		</td>
</tr>
<tr>
<td style="vertical-align:middle">
			<video src="http://wiebe-elsinga.com/blog/wp-content/uploads/2015/05/shamuLMY47DWiebe05082015102824.mp4" id="id3" width="200" onMouseOver="id3.play()" onMouseOut="id3.pause()" onclick="window.location='http://wiebe-elsinga.com/blog/wp-content/uploads/2015/05/shamuLMY47DWiebe05082015102824.mp4';id3.pause()" loop></video>
		</td>
<td style="vertical-align:middle">
			<strong>Loading feedback</strong>, which gives the user options, should I wait or just continue. Again don&#8217;t block the user.
		</td>
</tr>
<tr>
<td style="vertical-align:middle">
			<video src="http://wiebe-elsinga.com/blog/wp-content/uploads/2015/05/shamuLMY47DWiebe05082015104350.mp4" id="id4" width="200" onMouseOver="id4.play()" onMouseOut="id4.pause()" onclick="window.location='http://wiebe-elsinga.com/blog/wp-content/uploads/2015/05/shamuLMY47DWiebe05082015104350.mp4';id4.pause()" loop></video>
		</td>
<td style="vertical-align:middle">
			<strong>Action aid</strong>, which gives the the user the option to &#8220;undo stupidity&#8221;
		</td>
</tr>
</table>
<p>If you want to know more, you may want to visit:</p>
<ul>
<li><a href="http://www.google.com/design/spec/material-design/introduction.html" target="_blank">Google Material design introduction</a></li>
<li><a href="http://thenextweb.com/creativity/2015/04/27/user-testing-explained/" target="_blank">User testing explained, part 1</a></li>
<li><a href="http://thenextweb.com/dd/2015/04/28/user-testing-explained-part-two/" target="_blank">User testing explained, part 2</a></li>
<li><a href="http://androiduiux.com/" target="_blank">Android UI/UX</a> website by <a href="https://plus.google.com/+TaylorLing" target="_blank">Taylor Ling</a></li>
<li><a href="http://www.androiduipatterns.com/ http://www.androiduipatterns.com/" target="_blank">Android UI patterns</a> website by <a href="https://plus.google.com/+JuhaniLehtimäki" target="_blank">Juhani Lehtimäki</a></li>
<li>Udacity course: <a href="https://www.udacity.com/wiki/ud849" target="_blank">UX Design for Mobile Developers</a></li>
<li><a href="https://www.youtube.com/channel/UClKO7be7O9cUGL94PHnAeOA" target="_blank">Google design Youtube channel</a></li>
</ul>
<p>
I really want to thank my friend and colleague <a href="https://plus.google.com/+AliDerbane" target="_blank">Ali Derbane</a> for reviewing this post </p>
]]></content:encoded>
					
		
		<enclosure url="http://wiebe-elsinga.com/blog/wp-content/uploads/2015/05/shamuLMY47DWiebe05082015095543.mp4" length="1161987" type="video/mp4" />
<enclosure url="http://wiebe-elsinga.com/blog/wp-content/uploads/2015/05/shamuLMY47DWiebe05082015102824.mp4" length="603187" type="video/mp4" />
<enclosure url="http://wiebe-elsinga.com/blog/wp-content/uploads/2015/05/shamuLMY47DWiebe05082015104350.mp4" length="1593388" type="video/mp4" />

			</item>
		<item>
		<title>What’s new in Android Testing</title>
		<link>http://wiebe-elsinga.com/blog/whats-new-in-android-testing/</link>
		
		<dc:creator><![CDATA[W.Elsinga]]></dc:creator>
		<pubDate>Thu, 09 Apr 2015 02:20:09 +0000</pubDate>
				<category><![CDATA[AndroidDev Tips]]></category>
		<category><![CDATA[Business]]></category>
		<category><![CDATA[Android]]></category>
		<category><![CDATA[espresso]]></category>
		<category><![CDATA[testing]]></category>
		<guid isPermaLink="false">http://wiebe-elsinga.com/blog/?p=1755</guid>

					<description><![CDATA[During Droidcon Italy Stephan Linzner from Google explained upcoming features in the Android Testing Support Library. Lets have a closer look at some: Test Filtering If you want to suppress a test to run on certain target Api levels, just add the @SdkSuppress annotation. @SdkSuppress&#40;minSdkVersion=15&#41; @Test public void featureWithMinSdk15&#40;&#41; &#123; &#160; ... &#125; You can also filter tests to only run on a (physical) device by adding the @RequiresDevice annotation. @RequiresDevice @Test public void SomeDeviceSpecificFeature&#40;&#41; &#123; &#160; ... &#125; ActivityTestRule Because ActivityInstrumentationTestCase2 will become deprecated you need to define a @Rule. Rules allow very flexible addition or redefinition of the behavior of each test method in a test class https://github.com/junit-team/junit/wiki/Rules So in your test add a @Rule annotation, and create an ActivityTestRule for your Activity @RunWith&#40;AndroidJUnit4.class&#41; @LargeTest public class ChangeTextBehaviorTest &#123; &#160; &#160; ... &#160; &#160; @Rule &#160; &#160; public ActivityTestRule&#60;MainActivity&#62; mActivityRule = new ActivityTestRule&#60;&#62;&#40;MainActivity.class&#41;; &#160; &#160; @Test &#160; &#160; public void changeText_sameActivity&#40;&#41; &#123; &#160; &#160; &#160; &#160; // Type text and then press the button. &#160; &#160; &#160; &#160; onView&#40;withId&#40;R.id.editTextUserInput&#41;&#41;.perform&#40;typeText&#40;STRING_TO_BE_TYPED&#41;, closeSoftKeyboard&#40;&#41;&#41;; &#160; &#160; &#160; &#160; onView&#40;withId&#40;R.id.changeTextBt&#41;&#41;.perform&#40;click&#40;&#41;&#41;; &#160; &#160; &#160; &#160; // Check that the text was changed. &#160; &#160; &#160; &#160; onView&#40;withId&#40;R.id.textToBeChanged&#41;&#41;.check&#40;matches&#40;withText&#40;STRING_TO_BE_TYPED&#41;&#41;&#41;; &#160; &#160; &#125; &#125; Now create your ActivityTestRule public class ActivityTestRule&#60;T extends Activity&#62; &#123; &#160; &#160; public T getActivity&#40;&#41; &#123;&#125; &#160; &#160; protected Intent getActivityIntent&#40;&#41; &#123;&#125; &#160; &#160; protected void beforeActivityLaunched&#40;&#41; &#123;&#125; &#160; &#160; protected void afterActivityFinished&#40;&#41; &#123;&#125; &#125; Espresso-Intents Espresso-Intents is like Mockito but for Intent, basically hermetic inter-app testing. To give you an example, lets say in your application when a user presses a &#8220;call&#8221; button, you want to know if the correct data will be send to the Intent.ACTION_CALL. So first create an IntentsTestRule and then verify that the Intent was sent using Espresso-Intents @RunWith&#40;AndroidJUnit4.class&#41; @LargeTest public class DialerActivityTest &#123; &#160; &#160; ... &#160; &#160; @Rule &#160; &#160; public IntentsTestRule&#60;DialerActivity&#62; mRule = new IntentsTestRule&#60;&#62;&#40;DialerActivity.class&#41;; &#160; &#160; @Test &#160; &#160; public void typeNumber_ValidInput_InitiatesCall&#40;&#41; &#123; &#160; &#160; &#160; &#160; onView&#40;withId&#40;R.id.edit_text_caller_number&#41;&#41;.perform&#40;typeText&#40;VALID_PHONE_NUMBER&#41;, &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; closeSoftKeyboard&#40;&#41;&#41;; &#160; &#160; &#160; &#160; onView&#40;withId&#40;R.id.button_call_number&#41;&#41;.perform&#40;click&#40;&#41;&#41;; &#160; &#160; &#160; &#160; intended&#40;allOf&#40; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; hasAction&#40;is&#40;equalTo&#40;Intent.ACTION_CALL&#41;&#41;&#41;, &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; hasData&#40;equalTo&#40;INTENT_DATA_PHONE_NUMBER&#41;&#41;, &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; toPackage&#40;PACKAGE_ANDROID_DIALER&#41;&#41;&#41;; &#160; &#160; &#125; &#125; So now you know&#8230; All code can be found in android-testing github: ActivityTestRule Sample IntentsBasicSample General Espresso Github samples by Google Thanks Stephan Linzner for the resources. #HappyTesting]]></description>
										<content:encoded><![CDATA[<p><img loading="lazy" src="http://wiebe-elsinga.com/blog/wp-content/uploads/2015/04/DroidconIT.jpg" width="4096" height="3040" class="aligncenter size-full wp-image-1779" data-wp-pid="1779" srcset="http://wiebe-elsinga.com/blog/wp-content/uploads/2015/04/DroidconIT.jpg 4096w, http://wiebe-elsinga.com/blog/wp-content/uploads/2015/04/DroidconIT-300x223.jpg 300w, http://wiebe-elsinga.com/blog/wp-content/uploads/2015/04/DroidconIT-1024x760.jpg 1024w, http://wiebe-elsinga.com/blog/wp-content/uploads/2015/04/DroidconIT-800x594.jpg 800w" sizes="(max-width: 4096px) 100vw, 4096px" /></p>
<p>During Droidcon Italy <a href="https://plus.google.com/+StephanLinzner">Stephan Linzner</a> from Google explained upcoming features in the <a href="https://developer.android.com/tools/support-library/index.html">Android Testing Support Library</a>. Lets have a closer look at some:<br />
<span id="more-1755"></span><br />
</p>
<h3>Test Filtering</h3>
<p>If you want to suppress a test to run on certain target Api levels, just add the <code class="codecolorer bash default"><span class="bash"><span style="color: #000000; font-weight: bold;">@</span>SdkSuppress</span></code> annotation.</p>
<div class="codecolorer-container java blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">@SdkSuppress<span style="color: #009900;">&#40;</span>minSdkVersion<span style="color: #339933;">=</span><span style="color: #cc66cc;">15</span><span style="color: #009900;">&#41;</span><br />
@Test<br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> featureWithMinSdk15<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; ...<br />
<span style="color: #009900;">&#125;</span></div></div>
<p>You can also filter tests to only run on a (physical) device by adding the <code class="codecolorer bash default"><span class="bash"><span style="color: #000000; font-weight: bold;">@</span>RequiresDevice</span></code> annotation.</p>
<div class="codecolorer-container java blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">@RequiresDevice<br />
@Test<br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> SomeDeviceSpecificFeature<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; ...<br />
<span style="color: #009900;">&#125;</span></div></div>
<p></p>
<h3>ActivityTestRule</h3>
<p>Because <code class="codecolorer bash default"><span class="bash">ActivityInstrumentationTestCase2</span></code> will become deprecated you need to define a <code class="codecolorer bash default"><span class="bash"><span style="color: #000000; font-weight: bold;">@</span>Rule.</span></code></p>
<blockquote><p>Rules allow very flexible addition or redefinition of the behavior of each test method in a test class</p></blockquote>
<div align="right" ><small>https://github.com/junit-team/junit/wiki/Rules</small></div>
<p>
So in your test add a <code class="codecolorer bash default"><span class="bash"><span style="color: #000000; font-weight: bold;">@</span>Rule</span></code> annotation, and create an <code class="codecolorer bash default"><span class="bash">ActivityTestRule</span></code> for your Activity</p>
<div class="codecolorer-container java blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">@RunWith<span style="color: #009900;">&#40;</span>AndroidJUnit4.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><br />
@LargeTest<br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ChangeTextBehaviorTest <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; ...<br />
<br />
&nbsp; &nbsp; @Rule<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> ActivityTestRule<span style="color: #339933;">&lt;</span>MainActivity<span style="color: #339933;">&gt;</span> mActivityRule <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ActivityTestRule<span style="color: #339933;">&lt;&gt;</span><span style="color: #009900;">&#40;</span>MainActivity.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; @Test<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> changeText_sameActivity<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Type text and then press the button.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; onView<span style="color: #009900;">&#40;</span>withId<span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">id</span>.<span style="color: #006633;">editTextUserInput</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">perform</span><span style="color: #009900;">&#40;</span>typeText<span style="color: #009900;">&#40;</span>STRING_TO_BE_TYPED<span style="color: #009900;">&#41;</span>, closeSoftKeyboard<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; onView<span style="color: #009900;">&#40;</span>withId<span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">id</span>.<span style="color: #006633;">changeTextBt</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">perform</span><span style="color: #009900;">&#40;</span>click<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Check that the text was changed.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; onView<span style="color: #009900;">&#40;</span>withId<span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">id</span>.<span style="color: #006633;">textToBeChanged</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">check</span><span style="color: #009900;">&#40;</span>matches<span style="color: #009900;">&#40;</span>withText<span style="color: #009900;">&#40;</span>STRING_TO_BE_TYPED<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>Now create your <code class="codecolorer bash default"><span class="bash">ActivityTestRule</span></code></p>
<div class="codecolorer-container java blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ActivityTestRule<span style="color: #339933;">&lt;</span>T <span style="color: #000000; font-weight: bold;">extends</span> Activity<span style="color: #339933;">&gt;</span> <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> T getActivity<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">protected</span> Intent getActivityIntent<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> beforeActivityLaunched<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> afterActivityFinished<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p></p>
<h3>Espresso-Intents</h3>
<p>Espresso-Intents is like <a href="http://site.mockito.org/">Mockito</a> but for Intent, basically hermetic inter-app testing.<br />
To give you an example, lets say in your application when a user presses a &#8220;call&#8221; button, you want to know if the correct data will be send to the <code class="codecolorer bash default"><span class="bash">Intent.ACTION_CALL</span></code>. </p>
<p><img loading="lazy" src="http://wiebe-elsinga.com/blog/wp-content/uploads/2015/04/EspressoIntent.png" alt="EspressoIntent" width="1067" height="573" class="aligncenter size-full wp-image-1768" data-wp-pid="1768" srcset="http://wiebe-elsinga.com/blog/wp-content/uploads/2015/04/EspressoIntent.png 1067w, http://wiebe-elsinga.com/blog/wp-content/uploads/2015/04/EspressoIntent-300x161.png 300w, http://wiebe-elsinga.com/blog/wp-content/uploads/2015/04/EspressoIntent-1024x550.png 1024w, http://wiebe-elsinga.com/blog/wp-content/uploads/2015/04/EspressoIntent-800x430.png 800w" sizes="(max-width: 1067px) 100vw, 1067px" /></p>
<p>So first create an <code class="codecolorer bash default"><span class="bash">IntentsTestRule</span></code> and then verify that the Intent was sent using Espresso-Intents</p>
<div class="codecolorer-container java blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">@RunWith<span style="color: #009900;">&#40;</span>AndroidJUnit4.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><br />
@LargeTest<br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> DialerActivityTest <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; ...<br />
&nbsp; &nbsp; @Rule<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> IntentsTestRule<span style="color: #339933;">&lt;</span>DialerActivity<span style="color: #339933;">&gt;</span> mRule <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> IntentsTestRule<span style="color: #339933;">&lt;&gt;</span><span style="color: #009900;">&#40;</span>DialerActivity.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; @Test<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> typeNumber_ValidInput_InitiatesCall<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; onView<span style="color: #009900;">&#40;</span>withId<span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">id</span>.<span style="color: #006633;">edit_text_caller_number</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">perform</span><span style="color: #009900;">&#40;</span>typeText<span style="color: #009900;">&#40;</span>VALID_PHONE_NUMBER<span style="color: #009900;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; closeSoftKeyboard<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; onView<span style="color: #009900;">&#40;</span>withId<span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">id</span>.<span style="color: #006633;">button_call_number</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">perform</span><span style="color: #009900;">&#40;</span>click<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; intended<span style="color: #009900;">&#40;</span>allOf<span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; hasAction<span style="color: #009900;">&#40;</span>is<span style="color: #009900;">&#40;</span>equalTo<span style="color: #009900;">&#40;</span>Intent.<span style="color: #006633;">ACTION_CALL</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; hasData<span style="color: #009900;">&#40;</span>equalTo<span style="color: #009900;">&#40;</span>INTENT_DATA_PHONE_NUMBER<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; toPackage<span style="color: #009900;">&#40;</span>PACKAGE_ANDROID_DIALER<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p></br><br />
So now you know&#8230;<br />
<br />
All code can be found in <a href="https://github.com/googlesamples/android-testing">android-testing github</a>:</p>
<ul>
<li><a href="https://github.com/googlesamples/android-testing/tree/master/espresso/BasicSample">ActivityTestRule Sample</a></li>
<li><a href="https://github.com/googlesamples/android-testing/tree/master/espresso/IntentsBasicSample">IntentsBasicSample</a></li>
<li><a href="">General Espresso Github samples by Google</a></li>
</ul>
<p>
Thanks <a href="https://plus.google.com/+StephanLinzner">Stephan Linzner</a> for the resources. #HappyTesting</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>The Hitchhiker&#8217;s guide to Android Testing, part 2: Espresso</title>
		<link>http://wiebe-elsinga.com/blog/the-hitchhikers-guide-to-android-testing-part-2-espresso/</link>
		
		<dc:creator><![CDATA[W.Elsinga]]></dc:creator>
		<pubDate>Fri, 03 Apr 2015 02:04:02 +0000</pubDate>
				<category><![CDATA[AndroidDev Tips]]></category>
		<category><![CDATA[Business]]></category>
		<category><![CDATA[Android]]></category>
		<category><![CDATA[AndroidDev]]></category>
		<category><![CDATA[espresso]]></category>
		<category><![CDATA[testing]]></category>
		<guid isPermaLink="false">http://wiebe-elsinga.com/blog/?p=1711</guid>

					<description><![CDATA[As mentioned in the first article Ali Derbane and me wrote about Android Functional Testing, there are a lot of frameworks you can use. In this second part of the journey I will be explaining the functional testing framework called Espresso. So now you know a bit more about Espresso. If you want to know more, you may want to visit: Espresso website Github repo corresponding to this article General Espresso Github samples by Google Updated (08-04) When running the tests with gradle you can just use gradle connectedAndroidTest]]></description>
										<content:encoded><![CDATA[<p>As mentioned in the <a href="http://www.technotalkative.com/part-10-the-hitchhikers-guide-to-android-testing/">first article</a> <a href="https://plus.google.com/+AliDerbane">Ali Derbane</a> and me wrote about Android Functional Testing, there are a lot of frameworks you can use. In this second part of the journey I will be explaining the functional testing framework called <a href="https://code.google.com/p/android-test-kit/">Espresso</a>.<br />
<span id="more-1711"></span><br />
<img src="http://i.giphy.com/VtFd68Pr19fYk.gif" width=100%" /></p>
<h3>Introduction</h3>
<p>Introduced at the GTAC in 2013, Espresso is designed to be used in environments where the developers write their own tests, and makes it possible to write concise, beautiful, and reliable Android UI tests quickly.<br />
Espresso has several general components:</p>
<ul>
<li>The <code class="codecolorer bash default"><span class="bash">Espresso</span></code> class offers the <code class="codecolorer bash default"><span class="bash">onView</span></code> and <code class="codecolorer bash default"><span class="bash">onData</span></code> methods which, alone, can be used for a good numbers of possible tests on a given interface.</li>
<li><code class="codecolorer bash default"><span class="bash">ViewMatchers</span></code> contains a collection of objects that implements the interface <code class="codecolorer bash default"><span class="bash">Matcher <span style="color: #000000; font-weight: bold;">&lt;</span>? super View<span style="color: #000000; font-weight: bold;">&gt;</span></span></code>. Using this Class you can collect and check View elements. For example, getting a View element (Button) with text &#8220;7&#8221;.</li>
<li><code class="codecolorer bash default"><span class="bash">ViewActions</span></code> contains a collection of <code class="codecolorer bash default"><span class="bash">viewAction</span></code> objects to perform actions on a view. These actions are passed to the method <code class="codecolorer bash default"><span class="bash">ViewInteraction.perform</span></code> and may contain multiple actions. For example, clicking on a View element (Button).</li>
<li><code class="codecolorer bash default"><span class="bash">ViewAssertions</span></code> contains a collection of <code class="codecolorer bash default"><span class="bash">ViewAssertion</span></code> to conduct checks on views. </li>
</ul>
<p>
To illustrate these components a test can look like this:</p>
<div class="codecolorer-container java blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">&nbsp; Espresso.<span style="color: #006633;">onView</span><span style="color: #009900;">&#40;</span>ViewMatchers.<span style="color: #006633;">withText</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;7&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">perform</span><span style="color: #009900;">&#40;</span>ViewActions.<span style="color: #006633;">click</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; Espresso.<span style="color: #006633;">onView</span><span style="color: #009900;">&#40;</span>withId<span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">id</span>.<span style="color: #006633;">result</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">check</span><span style="color: #009900;">&#40;</span>ViewAssertions.<span style="color: #006633;">matches</span><span style="color: #009900;">&#40;</span>ViewMatchers.<span style="color: #006633;">withText</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;42&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>And the good news, as of last year Google has introduced a <a href="https://developer.android.com/tools/support-library/index.html">Testing Support Library</a> containing Espresso. So lets start by implementing Espresso.</p>
<div style="background: lightgray; padding: 12pt;">To illustrate, we are going to write some tests that tests agains a <a href="https://github.com/welsinga/sample_espresso/app">Android calculator application</a>. The common test scenario we will be implementing is testing if <em>‘6’ x ‘7’ equals ‘42’</em>.</div>
<p>&nbsp;&nbsp;</p>
<h3>Define the test runner</h3>
<p>To use Espresso we first need to define who is running the tests. Espresso uses a new runner named <code class="codecolorer bash default"><span class="bash">AndroidJUnitRunner</span></code>. This runner, based on <code class="codecolorer bash default"><span class="bash">InstrumentationTestRunner</span></code> and <code class="codecolorer bash default"><span class="bash">GoogleInstrumentationTestRunner</span></code>, runs <em>JUnit3</em> and <em>JUnit4</em> tests against your Android application.</p>
<p>First add the dependencies to your <code class="codecolorer bash blackboard"><span class="bash">build.gradle</span></code>, assuming you have installed the <a href="https://developer.android.com/tools/support-library/index.html">Testing Support Library</a>.</p>
<div class="codecolorer-container groovy blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="groovy codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">dependencies <span style="color: #66cc66;">&#123;</span><br />
&nbsp; androidTestCompile <span style="color: #ff0000;">'com.android.support.test:testing-support-lib:0.1'</span><br />
<span style="color: #66cc66;">&#125;</span></div></div>
<p>Then add the runner in your build.gradle<code class="codecolorer groovy default"><span class="groovy">android.<span style="color: #006600;">defaultConfig</span></span></code> configuration:</p>
<div class="codecolorer-container groovy blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="groovy codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">defaultConfig <span style="color: #66cc66;">&#123;</span><br />
&nbsp; ...<br />
&nbsp; <span style="color: #006600;">testInstrumentationRunner</span> <span style="color: #ff0000;">&quot;android.support.test.runner.AndroidJUnitRunner&quot;</span><br />
<span style="color: #66cc66;">&#125;</span></div></div>
<p>&nbsp;&nbsp;</p>
<h3>Writing the test</h3>
<p>As you may know test classes must be in <code class="codecolorer bash default"><span class="bash">src\androidTest\com.example.package.tests</span></code>, com.example.package being the package specified in the package attribute of the manifest element in the <code class="codecolorer bash default"><span class="bash">AndroidManifest</span></code> file.&nbsp;<br />
Also each test class must extend the abstract class <code class="codecolorer bash default"><span class="bash">ActivityInstrumentationTestCase2</span></code> and supply the Test Activity as generic type that will be used by default for testing.<br />
It must also be passed to the superclass via the <code class="codecolorer bash default"><span class="bash">super<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span></span></code>. To make the Test Activity being called by the test framework, simply define a setup which calls the synchronous method <code class="codecolorer bash default"><span class="bash">getActivity<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>.</span></code></p>
<div class="codecolorer-container java blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> FunctionalInstrumentationTest <span style="color: #000000; font-weight: bold;">extends</span> ActivityInstrumentationTestCase2<span style="color: #339933;">&lt;</span>ActivityToTest<span style="color: #339933;">&gt;</span> <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> FunctionalInstrumentationTest<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">super</span><span style="color: #009900;">&#40;</span>ActivityToTest.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; @Override<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> setUp<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+exception"><span style="color: #003399;">Exception</span></a> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">setUp</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; getActivity<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>As mentioned we want to check if <em>‘6’ x ‘7’ equals ‘42’</em>.</p>
<div class="codecolorer-container java blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> testAnswer_to_the_Ultimate_Question_of_Life_the_Universe_and_Everything<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; onView<span style="color: #009900;">&#40;</span>withText<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;7&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">perform</span><span style="color: #009900;">&#40;</span>click<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; onView<span style="color: #009900;">&#40;</span>withText<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;×&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">perform</span><span style="color: #009900;">&#40;</span>click<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; onView<span style="color: #009900;">&#40;</span>withText<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;6&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">perform</span><span style="color: #009900;">&#40;</span>click<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; onView<span style="color: #009900;">&#40;</span>withText<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;=&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">perform</span><span style="color: #009900;">&#40;</span>click<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; onView<span style="color: #009900;">&#40;</span>withId<span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">id</span>.<span style="color: #006633;">resText</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">check</span><span style="color: #009900;">&#40;</span>matches<span style="color: #009900;">&#40;</span>withText<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;42&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>You may have noticed, this example is using static imports. This is solely done to make the code more readable.</p>
<p>Other actions you want to use are:</p>
<ul>
<li><code class="codecolorer java default"><span class="java">pressBack<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></span></code> to simulate the use of the &#8220;back&#8221; button,</li>
<li><code class="codecolorer java default"><span class="java">isDisplayed<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></span></code> to check if an element is being shown and </li>
<li><code class="codecolorer java default"><span class="java">scrollTo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></span></code> to scroll to an element.</li>
</ul>
<p>&nbsp;&nbsp;</p>
<h3>Running the test</h3>
<p>Now lets do the fun part, lets run the test. This can be done from the command line with he command line with <code class="codecolorer bash blackboard"><span class="bash">gradle connectedAndroidTest</span></code> or using Android Studio by:</p>
<ol>
<li><em>Open Run menu | Edit Configurations</em></li>
<li><em>Add a new Android Tests configuration</em></li>
<li><em>Choose the module you are testing</em></li>
<li><em>Define our test runner: <code class="codecolorer bash default"><span class="bash">android.support.test.runner.AndroidJUnitRunner</span></code></em></li>
</ol>
<p>	<br />
<img src="http://i.giphy.com/10OjZwNlstPj9e.gif" width=100%"/><br />
<br />	<br />
So now you know a bit more about Espresso. If you want to know more, you may want to visit:</p>
<ul>
<li><a href="https://code.google.com/p/android-test-kit/">Espresso website</a></li>
<li><a href="https://github.com/welsinga/sample_espresso">Github repo corresponding to this article</a></li>
<li><a href="https://github.com/googlesamples/android-testing">General Espresso Github samples by Google</a></li>
</ul>
<p>
<strong>Updated</strong> (08-04) When running the tests with gradle you can just use <code class="codecolorer bash blackboard"><span class="bash">gradle connectedAndroidTest</span></code></p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Archive addition Android artifacts with Gradle.</title>
		<link>http://wiebe-elsinga.com/blog/archive-addition-android-artifacts-with-gradle/</link>
		
		<dc:creator><![CDATA[W.Elsinga]]></dc:creator>
		<pubDate>Fri, 20 Mar 2015 06:42:19 +0000</pubDate>
				<category><![CDATA[AndroidDev Tips]]></category>
		<category><![CDATA[Business]]></category>
		<category><![CDATA[Android]]></category>
		<category><![CDATA[gradle]]></category>
		<category><![CDATA[javadoc]]></category>
		<category><![CDATA[Proguard]]></category>
		<guid isPermaLink="false">http://wiebe-elsinga.com/blog/?p=1651</guid>

					<description><![CDATA[When building Android applications or libraries common practice is to save your artifacts to a local file storage or repo. Beside your APK there are some additional artifacts you want/need to save, and you want gradle to do this. Most common ones are Javadoc, your source files and perhaps your proguard generated files like the mapping file. I naturally wanted a Gradle task to handle this. So lets have a look as some tasks you may want to use. Adding Javadoc archive tasks The following will add tasks to generate Javadocs for each build type and assemble it into a jar archive . 123456789101112131415161718192021222324252627282930313233343536android.applicationVariants.all &#123; variant -&#62; &#160; &#160; project.task&#40;&#34;${variant.name.capitalize()}Javadoc&#34;, type: Javadoc&#41; &#123; &#160; &#160; &#160; &#160; destinationDir = new File&#40;&#34;$project.buildDir/javadoc/$variant.name&#34;&#41; &#160; &#160; &#160; &#160; source = variant.javaCompile.source &#160; &#160; &#160; &#160; ext.androidJar = &#34;${project.android.sdkDirectory}/platforms/${project.android.compileSdkVersion}/android.jar&#34; &#160; &#160; &#160; &#160; classpath = project.files&#40;variant.javaCompile.classpath.files&#41; + project.files&#40;ext.androidJar&#41; &#160; &#160; &#160; &#160; options &#123; &#160; &#160; &#160; &#160; &#160; &#160; linksOffline&#40;&#34;http://d.android.com/reference&#34;, &#34;${project.android.sdkDirectory}/docs/reference&#34;&#41; &#160; &#160; &#160; &#160; &#160; &#160; links&#40;&#34;http://docs.oracle.com/javase/7/docs/api/&#34;&#41;; &#160; &#160; &#160; &#160; &#160; &#160; setMemberLevel&#40;JavadocMemberLevel.PACKAGE&#41; &#160; &#160; &#160; &#160; &#160; &#160; docEncoding = 'UTF-8' &#160; &#160; &#160; &#160; &#160; &#160; encoding = 'UTF-8' &#160; &#160; &#160; &#160; &#160; &#160; charSet = 'UTF-8' &#160; &#160; &#160; &#160; &#125; &#160; &#160; &#160; &#160; exclude '**/BuildConfig.java' &#160; &#160; &#160; &#160; exclude '**/R.java' &#160; &#160; &#125; &#160; &#160; project.task&#40;&#34;generate${variant.name.capitalize()}JavadocJar&#34;, type: Jar, dependsOn: &#34;${variant.name.capitalize()}Javadoc&#34;&#41; &#123; &#160; &#160; &#160; &#160; classifier 'javadoc' &#160; &#160; &#160; &#160; description = 'Assembles a jar archive containing the generated Javadoc API documentation of $variant.name.' &#160; &#160; &#160; &#160; destinationDir = new File&#40;&#34;$project.buildDir/libs/&#34;&#41; &#160; &#160; &#160; &#160; exclude '**/BuildConfig.class' &#160; &#160; &#160; &#160; exclude '**/R.class' &#160; &#160; &#160; &#160; from &#34;$project.buildDir/javadoc/$variant.name&#34; &#160; &#160; &#125; &#125; When added to your build.gradle file, generate your Javadoc jar archive from the command line with gradle generateReleaseJavadocJar. Adding Source archive tasks The following will add tasks to assemble a jar archive containing the Java sources. 123456789101112131415android.applicationVariants.all &#123; variant -&#62; &#160; &#160; project.task&#40;&#34;generate${variant.name.capitalize()}SourcesJar&#34;, type: Jar&#41; &#123; &#160; &#160; &#160; &#160; classifier = 'sources' &#160; &#160; &#160; &#160; description = 'Assembles a jar archive containing the main sources of $variant.name..' &#160; &#160; &#160; &#160; destinationDir = new File&#40;&#34;$project.buildDir/libs/&#34;&#41; &#160; &#160; &#160; &#160; // exclude generated files &#160; &#160; &#160; &#160; exclude '**/BuildConfig.java' &#160; &#160; &#160; &#160; exclude '**/R.java' &#160; &#160; &#160; &#160; from variant.javaCompile.source &#160; &#160; &#125; &#125; When added to your build.gradle file, generate your Sources jar archive from the command line with gradle generateReleaseSourcesJar. Adding Proguard archive tasks The following will add tasks to assemble a zip archive containing the generated proguard files. 12345678910android.applicationVariants.all &#123; variant -&#62; &#160; &#160; project.task&#40;&#34;generate${variant.name.capitalize()}ProguardFilesJar&#34;, type: Zip&#41; &#123; &#160; &#160; &#160; &#160; classifier 'proguard' &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; description = 'Assembles a jar archive containing the Proguard files of $variant.name..' &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; destinationDir = new File&#40;&#34;$project.buildDir/libs/&#34;&#41; &#160; &#160; &#160; &#160; from &#34;$project.buildDir/outputs/mapping&#34; &#160; &#160; &#125; &#160; &#160; &#125; When added to your build.gradle file, generate your Proguard zip archive from the command line with gradle generateReleaseProguardFilesJar.]]></description>
										<content:encoded><![CDATA[<p>When building Android applications or libraries common practice is to save your artifacts to a local file storage or repo.</p>
<p>Beside your APK there are some additional artifacts you want/need to save, and you want gradle to do this. Most common ones are Javadoc, your source files and perhaps your <a href="http://proguard.sourceforge.net/">proguard</a> generated files like the mapping file.</p>
<p>I naturally wanted a Gradle task to handle this. So lets have a look as some tasks you may want to use.<br />
</p>
<h3>Adding Javadoc archive tasks</h3>
<p>The following will add tasks to generate Javadocs for each build type and assemble it into a jar archive .</p>
<div class="codecolorer-container groovy blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:100%;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br /></div></td><td><div class="groovy codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">android.<span style="color: #006600;">applicationVariants</span>.<span style="color: #006600;">all</span> <span style="color: #66cc66;">&#123;</span> variant <span style="color: #66cc66;">-&gt;</span><br />
&nbsp; &nbsp; project.<span style="color: #006600;">task</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;${variant.name.capitalize()}Javadoc&quot;</span>, type: Javadoc<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; destinationDir <span style="color: #66cc66;">=</span> <a href="http://www.google.de/search?q=site%3Agroovy.codehaus.org/%20new"><span style="color: #000000; font-weight: bold;">new</span></a> <a href="http://www.google.de/search?as_q=File&amp;num=100&amp;hl=en&amp;as_occt=url&amp;as_sitesearch=java.sun.com%2Fj2se%2F1%2E5%2E0%2Fdocs%2Fapi%2F"><span style="color: #aaaadd; font-weight: bold;">File</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;$project.buildDir/javadoc/$variant.name&quot;</span><span style="color: #66cc66;">&#41;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; source <span style="color: #66cc66;">=</span> variant.<span style="color: #006600;">javaCompile</span>.<span style="color: #006600;">source</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; ext.<span style="color: #006600;">androidJar</span> <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;${project.android.sdkDirectory}/platforms/${project.android.compileSdkVersion}/android.jar&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; classpath <span style="color: #66cc66;">=</span> project.<span style="color: #006600;">files</span><span style="color: #66cc66;">&#40;</span>variant.<span style="color: #006600;">javaCompile</span>.<span style="color: #006600;">classpath</span>.<span style="color: #006600;">files</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">+</span> project.<span style="color: #006600;">files</span><span style="color: #66cc66;">&#40;</span>ext.<span style="color: #006600;">androidJar</span><span style="color: #66cc66;">&#41;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; options <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; linksOffline<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;http://d.android.com/reference&quot;</span>, <span style="color: #ff0000;">&quot;${project.android.sdkDirectory}/docs/reference&quot;</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; links<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;http://docs.oracle.com/javase/7/docs/api/&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setMemberLevel<span style="color: #66cc66;">&#40;</span>JavadocMemberLevel.<a href="http://www.google.de/search?q=site%3Agroovy.codehaus.org/%20package"><span style="color: #000000; font-weight: bold;">PACKAGE</span></a><span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; docEncoding <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'UTF-8'</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; encoding <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'UTF-8'</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; charSet <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'UTF-8'</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; exclude <span style="color: #ff0000;">'**/BuildConfig.java'</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; exclude <span style="color: #ff0000;">'**/R.java'</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
<br />
&nbsp; &nbsp; project.<span style="color: #006600;">task</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;generate${variant.name.capitalize()}JavadocJar&quot;</span>, type: Jar, dependsOn: <span style="color: #ff0000;">&quot;${variant.name.capitalize()}Javadoc&quot;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; classifier <span style="color: #ff0000;">'javadoc'</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; description <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'Assembles a jar archive containing the generated Javadoc API documentation of $variant.name.'</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; destinationDir <span style="color: #66cc66;">=</span> <a href="http://www.google.de/search?q=site%3Agroovy.codehaus.org/%20new"><span style="color: #000000; font-weight: bold;">new</span></a> <a href="http://www.google.de/search?as_q=File&amp;num=100&amp;hl=en&amp;as_occt=url&amp;as_sitesearch=java.sun.com%2Fj2se%2F1%2E5%2E0%2Fdocs%2Fapi%2F"><span style="color: #aaaadd; font-weight: bold;">File</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;$project.buildDir/libs/&quot;</span><span style="color: #66cc66;">&#41;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; exclude <span style="color: #ff0000;">'**/BuildConfig.class'</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; exclude <span style="color: #ff0000;">'**/R.class'</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; from <span style="color: #ff0000;">&quot;$project.buildDir/javadoc/$variant.name&quot;</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
<span style="color: #66cc66;">&#125;</span></div></td></tr></tbody></table></div>
<p>When added to your <code class="codecolorer bash blackboard"><span class="bash">build.gradle</span></code> file, generate your Javadoc jar archive from the command line with <code class="codecolorer bash blackboard"><span class="bash">gradle generateReleaseJavadocJar</span></code>.<br />
</p>
<h3>Adding Source archive tasks</h3>
<p>The following will add tasks to assemble a jar archive containing the Java sources.</p>
<div class="codecolorer-container groovy blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br /></div></td><td><div class="groovy codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">android.<span style="color: #006600;">applicationVariants</span>.<span style="color: #006600;">all</span> <span style="color: #66cc66;">&#123;</span> variant <span style="color: #66cc66;">-&gt;</span><br />
&nbsp; &nbsp; project.<span style="color: #006600;">task</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;generate${variant.name.capitalize()}SourcesJar&quot;</span>, type: Jar<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; classifier <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'sources'</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; description <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'Assembles a jar archive containing the main sources of $variant.name..'</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; destinationDir <span style="color: #66cc66;">=</span> <a href="http://www.google.de/search?q=site%3Agroovy.codehaus.org/%20new"><span style="color: #000000; font-weight: bold;">new</span></a> <a href="http://www.google.de/search?as_q=File&amp;num=100&amp;hl=en&amp;as_occt=url&amp;as_sitesearch=java.sun.com%2Fj2se%2F1%2E5%2E0%2Fdocs%2Fapi%2F"><span style="color: #aaaadd; font-weight: bold;">File</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;$project.buildDir/libs/&quot;</span><span style="color: #66cc66;">&#41;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// exclude generated files</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; exclude <span style="color: #ff0000;">'**/BuildConfig.java'</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; exclude <span style="color: #ff0000;">'**/R.java'</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; from variant.<span style="color: #006600;">javaCompile</span>.<span style="color: #006600;">source</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
<span style="color: #66cc66;">&#125;</span></div></td></tr></tbody></table></div>
<p>When added to your <code class="codecolorer bash blackboard"><span class="bash">build.gradle</span></code> file, generate your Sources jar archive from the command line with <code class="codecolorer bash blackboard"><span class="bash">gradle generateReleaseSourcesJar</span></code>.<br />
</p>
<h3>Adding Proguard archive tasks</h3>
<p>The following will add tasks to assemble a zip archive containing the generated proguard files.</p>
<div class="codecolorer-container groovy blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br /></div></td><td><div class="groovy codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">android.<span style="color: #006600;">applicationVariants</span>.<span style="color: #006600;">all</span> <span style="color: #66cc66;">&#123;</span> variant <span style="color: #66cc66;">-&gt;</span><br />
&nbsp; &nbsp; project.<span style="color: #006600;">task</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;generate${variant.name.capitalize()}ProguardFilesJar&quot;</span>, type: Zip<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; classifier <span style="color: #ff0000;">'proguard'</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; description <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'Assembles a jar archive containing the Proguard files of $variant.name..'</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; destinationDir <span style="color: #66cc66;">=</span> <a href="http://www.google.de/search?q=site%3Agroovy.codehaus.org/%20new"><span style="color: #000000; font-weight: bold;">new</span></a> <a href="http://www.google.de/search?as_q=File&amp;num=100&amp;hl=en&amp;as_occt=url&amp;as_sitesearch=java.sun.com%2Fj2se%2F1%2E5%2E0%2Fdocs%2Fapi%2F"><span style="color: #aaaadd; font-weight: bold;">File</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;$project.buildDir/libs/&quot;</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; from <span style="color: #ff0000;">&quot;$project.buildDir/outputs/mapping&quot;</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span> &nbsp; &nbsp;<br />
<span style="color: #66cc66;">&#125;</span></div></td></tr></tbody></table></div>
<p>When added to your <code class="codecolorer bash blackboard"><span class="bash">build.gradle</span></code> file, generate your Proguard zip archive from the command line with <code class="codecolorer bash blackboard"><span class="bash">gradle generateReleaseProguardFilesJar</span></code>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Android Testing Support Library announced</title>
		<link>http://wiebe-elsinga.com/blog/android-testing-support-library-announced/</link>
					<comments>http://wiebe-elsinga.com/blog/android-testing-support-library-announced/#comments</comments>
		
		<dc:creator><![CDATA[W.Elsinga]]></dc:creator>
		<pubDate>Mon, 03 Nov 2014 03:39:42 +0000</pubDate>
				<category><![CDATA[Business]]></category>
		<category><![CDATA[Events]]></category>
		<category><![CDATA[Android]]></category>
		<category><![CDATA[Functional testing]]></category>
		<category><![CDATA[Support library]]></category>
		<guid isPermaLink="false">http://wiebe-elsinga.com/blog/?p=1608</guid>

					<description><![CDATA[During the Droidcon UK 2014, Stephan Linzner of Google announced the upcoming Android Testing Support Library. This means Google is (finally) taking testing Android apps seriously. The library is a unbundled static testing library for Android that containers all Android testing frameworks by Google. It will become a available through the Android Support repository and as open source in AOSP. So what does it contain? AndroidJUnitRunner This Runner, based on android.test.InstrumentationTestRunner, runs JUnit3 and JUnit4 tests against an Android package (application). Let me give an example: @RunWith &#40;JUnit4.class&#41; public class MyJUnit4Test &#123; &#123; &#160; Droidcon mDroidconUK; &#160; &#160; @Before &#160; public void setUp&#40;&#41; &#123; &#160; &#160; mDroidconUK = new Droidcons.get&#40;Droidcon.UK&#41;; &#160; &#160; mDroidconUK.open&#40;&#41;; &#160; &#125; &#160; @Test &#160; public void checkPreconditions&#40;&#41; &#123; &#160; &#160; assertNotNull&#40;&#34;mDroidconUK cannot be null&#34;, mDroidconUK&#41;; &#160; &#125; &#160; @After &#160; public void tearDown&#40;&#41; &#123; &#160; &#160; mDroidconUK.close&#40;&#41;; &#160; &#125; &#125; Espresso Introduced at the GTAC in 2013, Espresso makes it possible to write concise, beautiful, and reliable Android UI tests quickly. Lets look at some examples: // Find view using a Matcher and type text using a ViewAction onView&#40;withId&#40;R.id.message_edit_text&#41;&#41;.perform&#40;typeText&#40;TEXT_MESSAGE&#41;, closeSoftKeyboard&#40;&#41;&#41;; // Perform a click ViewAction onView&#40;withId&#40;R.id.send_button&#41;&#41;.perform&#40;click&#40;&#41;&#41;; // Verify using a ViewAssertion onView&#40;withId&#40;R.id.received_message_text_view&#41;&#41;.check&#40;matches&#40;withText&#40;&#40;TEXT_MESSAGE&#41;&#41;&#41;&#41;; Intento Intento is like Mockito but for Intents. Basically a mock framework that allows you to create and configure mock objects. Let me give you an example: public void testDailerInput_typeNumberAndCall&#40;&#41; &#123; &#160; &#160; //Type phonenumber in dialer &#160; onView&#40;withId&#40;R.id.send_data_to_call_edit_text&#41;&#41;.perform&#40;scrollTo&#40;&#41;, typeText&#40;&#34;123-345-6789&#34;&#41;, closeSoftKeyboard&#40;&#41;&#41;; &#160; &#160; // Click the call button &#160; onView&#40;withId&#40;R.id.send_to_call_button&#41;&#41;.perform&#40;scrollTo&#40;&#41;, click&#40;&#41;&#41;; &#160; &#160; //Validate Intent has been send &#160; intended&#40;allOf&#40; &#160; &#160; &#160; hasAction&#40;Intent.ACTION_CALL&#41;, &#160; &#160; &#160; toPackage&#40;&#34;com.android.phone&#34;&#41;, &#160; &#160; &#160; hasData&#40;allOf&#40;hasSchemaSpecificPart&#40;&#34;tel&#34;, &#34;123-345-6789&#34;&#41;&#41;&#41;&#41;, &#160; &#160; &#160; times&#40;1&#41;&#41;; &#125; In conclusion: Testing with Android is finally becoming a more prominent part of Android Development cycle with the introduction of the Android Testing Support Library]]></description>
										<content:encoded><![CDATA[<p>During the <a href="http://uk.droidcon.com/2014/" title="Droidcon UK 2014" target="_blank">Droidcon UK 2014</a>, <a href="https://plus.google.com/+StephanLinzner" title="StephanLinzner G+ Profile" target="_blank">Stephan Linzner</a> of Google announced the upcoming Android Testing Support Library. This means Google is (finally) taking testing Android apps seriously. </p>
<p>The library is a unbundled static testing library for Android that containers all Android testing frameworks by Google. It will become a available through the Android Support repository and as open source in AOSP.</p>
<p>So what does it contain?</p>
<ul>
<li><em><strong>AndroidJUnitRunner</strong></em><br />
This Runner, based on <em>android.test.InstrumentationTestRunner</em>, runs <em>JUnit3</em> and <em>JUnit4</em> tests against an Android package (application). Let me give an example:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:100%;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">@RunWith <span style="color: #009900;">&#40;</span>JUnit4.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MyJUnit4Test <span style="color: #009900;">&#123;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; Droidcon mDroidconUK<span style="color: #339933;">;</span><br />
&nbsp;<br />
&nbsp; @Before <br />
&nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setUp<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; mDroidconUK <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Droidcons.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>Droidcon.<span style="color: #006633;">UK</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; mDroidconUK.<span style="color: #006633;">open</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; @Test <br />
&nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> checkPreconditions<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; assertNotNull<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;mDroidconUK cannot be null&quot;</span>, mDroidconUK<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; @After <br />
&nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> tearDown<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; mDroidconUK.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>
</li>
<li><em><strong>Espresso</strong></em><br />
Introduced at the GTAC in 2013, Espresso makes it possible to write concise, beautiful, and reliable Android UI tests quickly. Lets look at some examples:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #666666; font-style: italic;">// Find view using a Matcher and type text using a ViewAction</span><br />
onView<span style="color: #009900;">&#40;</span>withId<span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">id</span>.<span style="color: #006633;">message_edit_text</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">perform</span><span style="color: #009900;">&#40;</span>typeText<span style="color: #009900;">&#40;</span>TEXT_MESSAGE<span style="color: #009900;">&#41;</span>, closeSoftKeyboard<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #666666; font-style: italic;">// Perform a click ViewAction</span><br />
onView<span style="color: #009900;">&#40;</span>withId<span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">id</span>.<span style="color: #006633;">send_button</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">perform</span><span style="color: #009900;">&#40;</span>click<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #666666; font-style: italic;">// Verify using a ViewAssertion</span><br />
onView<span style="color: #009900;">&#40;</span>withId<span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">id</span>.<span style="color: #006633;">received_message_text_view</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">check</span><span style="color: #009900;">&#40;</span>matches<span style="color: #009900;">&#40;</span>withText<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>TEXT_MESSAGE<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>
</li>
<li><em><strong>Intento</strong></em><br />
Intento is like Mockito but for Intents. Basically a mock framework that allows you to create and configure mock objects. Let me give you an example:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> testDailerInput_typeNumberAndCall<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; <br />
&nbsp; <span style="color: #666666; font-style: italic;">//Type phonenumber in dialer</span><br />
&nbsp; onView<span style="color: #009900;">&#40;</span>withId<span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">id</span>.<span style="color: #006633;">send_data_to_call_edit_text</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">perform</span><span style="color: #009900;">&#40;</span>scrollTo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, typeText<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;123-345-6789&quot;</span><span style="color: #009900;">&#41;</span>, closeSoftKeyboard<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <br />
&nbsp; <span style="color: #666666; font-style: italic;">// Click the call button</span><br />
&nbsp; onView<span style="color: #009900;">&#40;</span>withId<span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">id</span>.<span style="color: #006633;">send_to_call_button</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">perform</span><span style="color: #009900;">&#40;</span>scrollTo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, click<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <br />
&nbsp; <span style="color: #666666; font-style: italic;">//Validate Intent has been send</span><br />
&nbsp; intended<span style="color: #009900;">&#40;</span>allOf<span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; hasAction<span style="color: #009900;">&#40;</span>Intent.<span style="color: #006633;">ACTION_CALL</span><span style="color: #009900;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; toPackage<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;com.android.phone&quot;</span><span style="color: #009900;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; hasData<span style="color: #009900;">&#40;</span>allOf<span style="color: #009900;">&#40;</span>hasSchemaSpecificPart<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;tel&quot;</span>, <span style="color: #0000ff;">&quot;123-345-6789&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; times<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
</li>
</ul>
<p>
<strong>In conclusion</strong>: Testing with Android is finally becoming a more prominent part of Android Development cycle with the introduction of the Android Testing Support Library</p>
]]></content:encoded>
					
					<wfw:commentRss>http://wiebe-elsinga.com/blog/android-testing-support-library-announced/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
			</item>
		<item>
		<title>Functional testing for Android</title>
		<link>http://wiebe-elsinga.com/blog/functional-testing-for-android/</link>
					<comments>http://wiebe-elsinga.com/blog/functional-testing-for-android/#comments</comments>
		
		<dc:creator><![CDATA[W.Elsinga]]></dc:creator>
		<pubDate>Tue, 26 Nov 2013 02:18:52 +0000</pubDate>
				<category><![CDATA[General]]></category>
		<category><![CDATA[Android]]></category>
		<category><![CDATA[Functional testing]]></category>
		<guid isPermaLink="false">http://wiebe-elsinga.com/blog/?p=1480</guid>

					<description><![CDATA[Applying functional testing is not a new technique during the development of Java software. Writing functional tests gives you a good understanding about the stability of an application or system, during code modifications or if new features are added. But what about using functional testing when developing an Android application, it is also easy to achieve? &#8220;And, is the application still works?&#8221; or &#8220;Is the application broken?&#8221;, these are questions that developers should be able to answer if a code change has been made or an improvement has been added. This is no different when developing an Android application. Also, the use of TDD (Test Driven Development), in which first the test is written, after which the code follows, is good practice. Automated functional testing has become part of software development. Using Selenium and Fitnesse for testing Java application, combined with Jenkins has become avery day pratice to most Java developers. So you would expect that this would differ during the development of Android app. Unfortunately this is not (yet) the case. Because of the relatively rapid growth of Android, these testing methods aren&#8217;t yet mature enough. Lets look at four testing framework that can be used during Android app development: How to install the specific framework is not part of this article. With the use of code examples pros and cons will be explained for the specific framework. &#160; The Android application that is being tested is a simple calculator. The common test scenario we will be implementing is testing if ‘1’ + ‘3’ gives a result of ‘4’. UIAutomator The Google test framework UIAutomator makes it possible to write simple Java (and JUnit) functional tests. The framework is not tied to the application that needs to be tested, but also has access to the Android platform itself. This makes it possible to adjust your Android emulator or physical phone settings while running your test(s). This means that you can test the behavior of the test application if, for example, the internet connection is being switched off. UiObject allAppsButton = new UiObject&#40;new UiSelector&#40;&#41;.description&#40;&#34;Apps&#34;&#41;&#41;; allAppsButton.clickAndWaitForNewWindow&#40;&#41;; UiObject appsTab = new UiObject&#40;new UiSelector&#40;&#41;.text&#40;&#34;Apps&#34;&#41;&#41;; appsTab.click&#40;&#41;; UiScrollable appViews = new UiScrollable&#40;new UiSelector&#40;&#41;.scrollable&#40;true&#41;&#41;; appViews.setAsHorizontalList&#40;&#41;; UiObject calculatorApp = appViews.getChildByText&#40;new UiSelector&#40;&#41;.className&#40;android.widget.TextView.class.getName&#40;&#41;&#41;, &#34;Calculator app&#34;&#41;; calculatorApp.clickAndWaitForNewWindow&#40;&#41;; UiObject oneButton = new UiObject&#40;new UiSelector&#40;&#41;.text&#40;&#34;1&#34;&#41;&#41;; oneButton.click&#40;&#41;; UiObject plusButton = new UiObject&#40;new UiSelector&#40;&#41;.text&#40;&#34;+&#34;&#41;&#41;; plusButton.click&#40;&#41;; UiObject threeButton = new UiObject&#40;new UiSelector&#40;&#41;.text&#40;&#34;3&#34;&#41;&#41;; threeButton.click&#40;&#41;; UiObject eqButton = new UiObject&#40;new UiSelector&#40;&#41;.text&#40;&#34;=&#34;&#41;&#41;; eqButton.click&#40;&#41;; UiObject text = new UiObject&#40;new UiSelector&#40;&#41;.className&#40;&#34;android.widget.EditText&#34;&#41;.text&#40;&#34;4&#34;&#41;&#41;; assertTrue&#40;text.exists&#40;&#41;&#41;; Unfortunately there is little documentation available. Also the framework only supports Android 4.1 or higher. In short: Fully control device Code written in Java Extends JUnit framework Easy to read Only for Android 4.1 and higher Little documentation available &#160; Robolectric The Java-based testing framework Robolectric is the best of the tested frameworks in terms of performance. This is because it is not running on a device or on the Android emulator, but uses his own visual UI simulator. As a result, the delaying factor of starting an emulator or installing the app on a device has no influence on running the test(s). This makes Robolectric a good choice for TDD. @RunWith&#40;RobolectricTestRunner.class&#41; public class MainActivityTest &#123; private MainActivity _activity; @Before public void setUp&#40;&#41; throws Exception &#123; _activity = Robolectric.buildActivity&#40;MainActivity.class&#41;.create&#40;&#41;.get&#40;&#41;; &#125; @Test public void testAdding&#40;&#41; throws Exception &#123; Button btnOne = &#40;Button&#41; _activity.findViewById&#40;R.id.num_1&#41;; Button btnAdd = &#40;Button&#41; _activity.findViewById&#40;R.id.op_add&#41;; Button btnThree = &#40;Button&#41; _activity.findViewById&#40;R.id.num_3&#41;; Button btnEquals = &#40;Button&#41; _activity.findViewById&#40;R.id.op_equ&#41;; btnOne.performClick&#40;&#41;; btnAdd.performClick&#40;&#41;; btnThree.performClick&#40;&#41;; btnEquals.performClick&#40;&#41;; EditText resultText = &#40;EditText&#41; _activity.findViewById&#40;R.id.editText1&#41;; assertEquals&#40;&#34;4&#34;, resultText.getText&#40;&#41;.toString&#40;&#41;&#41;; &#125; &#125; Because Robolectric uses a non-visual UI simulator its not possible to test a problem which only occurs on say a Samsung device. In short: Does not require emulator or device Code written in Java Extends JUnit framework Fastest in terms of performance Does not run on actual device &#160; Robotium The idea behind Robotium is that a functional test should description what the user sees and can do. This framework is a good choice if you are just &#8220;black box&#8221; testing your application. Besides extensive documentation, there are some well described examples available and the API is easy to use. public class MainActivityTest extends ActivityInstrumentationTestCase2 &#123; private Solo _solo; public MainActivityTest&#40;&#41; &#123; super&#40;MainActivity.class&#41;; &#125; @Override protected void setUp&#40;&#41; throws Exception &#123; super.setUp&#40;&#41;; _solo = new Solo&#40;getInstrumentation&#40;&#41;, getActivity&#40;&#41;&#41;; &#125; public void testAdding&#40;&#41; &#123; _solo.assertCurrentActivity&#40;&#34;Is app started&#34;, MainActivity.class&#41;; _solo.clickOnButton&#40;&#34;CLR&#34;&#41;; _solo.clickOnButton&#40;&#34;1&#34;&#41;; _solo.clickOnButton&#40;&#34;+&#34;&#41;; _solo.clickOnButton&#40;&#34;3&#34;&#41;; _solo.clickOnButton&#40;&#34;=&#34;&#41;; EditText editText = &#40;EditText&#41; getActivity&#40;&#41;.findViewById&#40;com.elsinga.calculator.R.id.editText1&#41;; assertEquals&#40;&#34;4&#34;, editText.getText&#40;&#41;.toString&#40;&#41;&#41;; &#125; &#125; In short: APIs are easy to use Tests are text-based Good for &#8220;black box&#8221; testing &#160; Calabash-Android Like Robotium, the functional tests described in Calabash Android is written as &#8220;What you see you test&#8221;. The only difference is that the tests are not written in Java, but in Cucumber (a text-based structure). This makes it possible that tests can be written/used by non developers. A drawback, because test(s) aren&#8217;t written in Java, is that &#8220;debugging&#8221; a failing test is not possible. Feature: Adding Scenario: add I should see &#34;1&#34; Then I press &#34;1&#34; And I press &#34;+&#34; And I press &#34;3&#34; And I press &#34;=&#34; Then I see &#34;4&#34; In short: Well documented Test(s) can be writen/used bij non developers What you see, you test ‘Debuggen’ of tests isn&#8217;t possible &#160; Conclusion Functional testing of Android applications is possible. Which test framework you want to use, dependents on the what you want to do with it. To choice with one fits best, ask yourself who is going to make the tests, what needs to be tested and/or on which device the tests are running on.In any cast, there is enough choice to start writing functional tests. Source code can be found here]]></description>
										<content:encoded><![CDATA[<p>Applying functional testing is not a new technique during the development of Java software. Writing functional tests gives you a good understanding about the stability of an application or system, during code modifications or if new features are added. But what about using functional testing when developing an Android application, it is also easy to achieve?</p>
<p>&#8220;And, is the application still works?&#8221; or &#8220;Is the application broken?&#8221;, these are questions that developers should be able to answer if a code change has been made or an improvement has been added. This is no different when developing an Android application. Also, the use of TDD (Test Driven Development), in which first the test is written, after which the code follows, is good practice. Automated functional testing has become part of software development. Using Selenium and Fitnesse for testing Java application, combined with Jenkins has become avery day pratice to most Java developers. So you would expect that this would differ during the development of Android app. Unfortunately this is not (yet) the case. Because of the relatively rapid growth of Android, these testing methods aren&#8217;t yet mature enough.</p>
<p>Lets look at four testing framework that can be used during Android app development:</p>
<div style="background: lightgray; padding: 12pt;">How to install the specific framework is not part of this article. With the use of code examples pros and cons will be explained for the specific framework.</div>
<p>&nbsp;<br />
The <a href="https://github.com/welsinga/sample_testsing" target="_blank">Android application</a> that is being tested is a simple calculator. The common test scenario we will be implementing is testing if ‘1’ + ‘3’ gives a result of ‘4’.</p>
<h2>UIAutomator</h2>
<p>The Google test framework <a href="http://developer.android.com/tools/help/uiautomator/index.html">UIAutomator</a> makes it possible to write simple Java (and JUnit) functional tests. The framework is not tied to the application that needs to be tested, but also has access to the Android platform itself.<br />
This makes it possible to adjust your Android emulator or physical phone settings while running your test(s). This means that you can test the behavior of the test application if, for example, the internet connection is being switched off.</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:100%;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">UiObject allAppsButton <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> UiObject<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> UiSelector<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">description</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Apps&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
allAppsButton.<span style="color: #006633;">clickAndWaitForNewWindow</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
UiObject appsTab <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> UiObject<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> UiSelector<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">text</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Apps&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
appsTab.<span style="color: #006633;">click</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
UiScrollable appViews <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> UiScrollable<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> UiSelector<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">scrollable</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
appViews.<span style="color: #006633;">setAsHorizontalList</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
UiObject calculatorApp <span style="color: #339933;">=</span> appViews.<span style="color: #006633;">getChildByText</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> UiSelector<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">className</span><span style="color: #009900;">&#40;</span>android.<span style="color: #006633;">widget</span>.<span style="color: #006633;">TextView</span>.<span style="color: #000000; font-weight: bold;">class</span>.<span style="color: #006633;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>, <span style="color: #0000ff;">&quot;Calculator app&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
calculatorApp.<span style="color: #006633;">clickAndWaitForNewWindow</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
UiObject oneButton <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> UiObject<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> UiSelector<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">text</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;1&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
oneButton.<span style="color: #006633;">click</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
UiObject plusButton <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> UiObject<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> UiSelector<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">text</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;+&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
plusButton.<span style="color: #006633;">click</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
UiObject threeButton <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> UiObject<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> UiSelector<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">text</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;3&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
threeButton.<span style="color: #006633;">click</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
UiObject eqButton <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> UiObject<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> UiSelector<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">text</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;=&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
eqButton.<span style="color: #006633;">click</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
UiObject text <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> UiObject<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> UiSelector<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">className</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;android.widget.EditText&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">text</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;4&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
assertTrue<span style="color: #009900;">&#40;</span>text.<span style="color: #006633;">exists</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>Unfortunately there is little documentation available. Also the framework only supports Android 4.1 or higher.<br />
In short:</p>
<ul>
<li><span style="color: #339966;">Fully control device</span></li>
<li><span style="color: #339966;">Code written in Java</span></li>
<li><span style="color: #339966;">Extends JUnit framework</span></li>
<li><span style="color: #339966;">Easy to read</span></li>
<li><span style="color: #ff0000;">Only for Android 4.1 and higher</span></li>
<li><span style="color: #ff0000;">Little documentation available</span></li>
</ul>
<p>&nbsp;</p>
<h2>Robolectric</h2>
<p>The Java-based testing framework <a href="http://pivotal.github.io/robolectric/index.html" target="_blank">Robolectric</a> is the best of the tested frameworks in terms of performance. This is because it is not running on a device or on the Android emulator, but uses his own visual UI simulator. As a result, the delaying factor of starting an emulator or installing the app on a device has no influence on running the test(s). This makes Robolectric a good choice for TDD.</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:100%;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">@RunWith<span style="color: #009900;">&#40;</span>RobolectricTestRunner.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MainActivityTest<br />
<span style="color: #009900;">&#123;</span><br />
<span style="color: #000000; font-weight: bold;">private</span> MainActivity _activity<span style="color: #339933;">;</span><br />
<br />
@Before<br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setUp<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+exception"><span style="color: #003399;">Exception</span></a><br />
<span style="color: #009900;">&#123;</span><br />
_activity <span style="color: #339933;">=</span> Robolectric.<span style="color: #006633;">buildActivity</span><span style="color: #009900;">&#40;</span>MainActivity.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">create</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
@Test<br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> testAdding<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+exception"><span style="color: #003399;">Exception</span></a><br />
<span style="color: #009900;">&#123;</span><br />
<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+button"><span style="color: #003399;">Button</span></a> btnOne <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+button"><span style="color: #003399;">Button</span></a><span style="color: #009900;">&#41;</span> _activity.<span style="color: #006633;">findViewById</span><span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">id</span>.<span style="color: #006633;">num_1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+button"><span style="color: #003399;">Button</span></a> btnAdd <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+button"><span style="color: #003399;">Button</span></a><span style="color: #009900;">&#41;</span> _activity.<span style="color: #006633;">findViewById</span><span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">id</span>.<span style="color: #006633;">op_add</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+button"><span style="color: #003399;">Button</span></a> btnThree <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+button"><span style="color: #003399;">Button</span></a><span style="color: #009900;">&#41;</span> _activity.<span style="color: #006633;">findViewById</span><span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">id</span>.<span style="color: #006633;">num_3</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+button"><span style="color: #003399;">Button</span></a> btnEquals <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+button"><span style="color: #003399;">Button</span></a><span style="color: #009900;">&#41;</span> _activity.<span style="color: #006633;">findViewById</span><span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">id</span>.<span style="color: #006633;">op_equ</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
btnOne.<span style="color: #006633;">performClick</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
btnAdd.<span style="color: #006633;">performClick</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
btnThree.<span style="color: #006633;">performClick</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
btnEquals.<span style="color: #006633;">performClick</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
EditText resultText <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>EditText<span style="color: #009900;">&#41;</span> _activity.<span style="color: #006633;">findViewById</span><span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">id</span>.<span style="color: #006633;">editText1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
assertEquals<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;4&quot;</span>, resultText.<span style="color: #006633;">getText</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>Because Robolectric uses a non-visual UI simulator its not possible to test a problem which only occurs on say a Samsung device.<br />
In short:</p>
<ul>
<li><span style="color: #339966;">Does not require emulator or device</span></li>
<li><span style="color: #339966;">Code written in Java</span></li>
<li><span style="color: #339966;">Extends JUnit framework</span></li>
<li><span style="color: #339966;">Fastest in terms of performance</span></li>
<li><span style="color: #ff0000;">Does not run on actual device</span></li>
</ul>
<p>&nbsp;</p>
<h2>Robotium</h2>
<p>The idea behind <a href="https://code.google.com/p/robotium/" target="_blank">Robotium</a> is that a functional test should description what the user sees and can do. This framework is a good choice if you are just &#8220;black box&#8221; testing your application. Besides extensive documentation, there are some well described examples available and the API is easy to use.</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:100%;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MainActivityTest <span style="color: #000000; font-weight: bold;">extends</span> ActivityInstrumentationTestCase2<br />
<span style="color: #009900;">&#123;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">private</span> Solo _solo<span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">public</span> MainActivityTest<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
<span style="color: #000000; font-weight: bold;">super</span><span style="color: #009900;">&#40;</span>MainActivity.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
@Override<br />
<span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> setUp<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+exception"><span style="color: #003399;">Exception</span></a><br />
<span style="color: #009900;">&#123;</span><br />
<span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">setUp</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
_solo <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Solo<span style="color: #009900;">&#40;</span>getInstrumentation<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, getActivity<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> testAdding<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
_solo.<span style="color: #006633;">assertCurrentActivity</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Is app started&quot;</span>, MainActivity.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
_solo.<span style="color: #006633;">clickOnButton</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;CLR&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
_solo.<span style="color: #006633;">clickOnButton</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;1&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
_solo.<span style="color: #006633;">clickOnButton</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;+&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
_solo.<span style="color: #006633;">clickOnButton</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;3&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
_solo.<span style="color: #006633;">clickOnButton</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;=&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
EditText editText <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>EditText<span style="color: #009900;">&#41;</span> getActivity<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">findViewById</span><span style="color: #009900;">&#40;</span>com.<span style="color: #006633;">elsinga</span>.<span style="color: #006633;">calculator</span>.<span style="color: #006633;">R</span>.<span style="color: #006633;">id</span>.<span style="color: #006633;">editText1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
assertEquals<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;4&quot;</span>, editText.<span style="color: #006633;">getText</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>In short:</p>
<ul>
<li><span style="color: #339966;">APIs are easy to use</span></li>
<li><span style="color: #339966;">Tests are text-based</span></li>
<li><span style="color: #339966;">Good for &#8220;black box&#8221; testing</span></li>
</ul>
<p>&nbsp;</p>
<h2>Calabash-Android</h2>
<p>Like Robotium, the functional tests described in <a href="https://github.com/calabash/calabash-android" target="_blank">Calabash Android</a> is written as &#8220;What you see you test&#8221;. The only difference is that the tests are not written in Java, but in <a href="http://cukes.info/" target="_blank">Cucumber</a> (a text-based structure). This makes it possible that tests can be written/used by non developers. A drawback, because test(s) aren&#8217;t written in Java, is that &#8220;debugging&#8221; a failing test is not possible.</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">Feature: Adding<br />
<br />
Scenario: add<br />
I should see &quot;1&quot;<br />
Then I press &quot;1&quot;<br />
And I press &quot;+&quot;<br />
And I press &quot;3&quot;<br />
And I press &quot;=&quot;<br />
Then I see &quot;4&quot;</div></div>
<p>In short:</p>
<ul>
<li><span style="color: #339966;">Well documented</span></li>
<li><span style="color: #339966;">Test(s) can be writen/used bij non developers</span></li>
<li><span style="color: #339966;">What you see, you test</span></li>
<li><span style="color: #ff0000;">‘Debuggen’ of tests isn&#8217;t possible</span></li>
</ul>
<p>&nbsp;</p>
<h2>Conclusion</h2>
<p>Functional testing of Android applications is possible. Which test framework you want to use, dependents on the what you want to do with it. To choice with one fits best, ask yourself who is going to make the tests, what needs to be tested and/or on which device the tests are running on.In any cast, there is enough choice to start writing functional tests.</p>
<p>Source code can be found <a href="https://github.com/welsinga/sample_testsing" target="_blank">here</a></p>
]]></content:encoded>
					
					<wfw:commentRss>http://wiebe-elsinga.com/blog/functional-testing-for-android/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
			</item>
		<item>
		<title>Obfuscating for Android with ProGuard</title>
		<link>http://wiebe-elsinga.com/blog/obfuscating-for-android-with-proguard/</link>
					<comments>http://wiebe-elsinga.com/blog/obfuscating-for-android-with-proguard/#comments</comments>
		
		<dc:creator><![CDATA[W.Elsinga]]></dc:creator>
		<pubDate>Fri, 14 Dec 2012 05:56:23 +0000</pubDate>
				<category><![CDATA[Business]]></category>
		<category><![CDATA[How-To]]></category>
		<category><![CDATA[Android]]></category>
		<category><![CDATA[Proguard]]></category>
		<guid isPermaLink="false">http://wiebe-elsinga.com/blog/?p=1337</guid>

					<description><![CDATA[Obfu-what? Right, Obfuscation, in general, describes a practice that is used to intentionally make something more difficult to understand. The nature of Java (the programming language for Android apps) is that the code is not compiled down to machine code; it is compiled to an intermediate format that is ready to be run on a variety of hardware platforms. While this allows great portability, it also leaves the code for Android apps, as present in the APK (Application PacKage file), available for extraction. I&#8217;m going to describe a way for you to obfuscate your Android code to make it harder for others to reverse engineer. And also why not shrink the size of your Android applications and optimize them to make them run faster at the same time. Without a doubt, the simplest method to protect an app is to enable the obfuscation in ProGuard. This freely available tool is already built into the Android toolkit. Obfuscation By default, compiled bytecode still contains a lot of debugging information: source file names, line numbers, field names, method names, argument names, variable names, etc. This information makes it straightforward to decompile the bytecode and reverse-engineer entire apps. Sometimes, this is not desirable. Obfuscators, such as ProGuard, can remove the debugging information and replace all names by meaningless character sequences, making it much harder to reverse-engineer the code. It further compacts the code as a bonus. The app remains functionally equivalent, except for the class names, method names, and line numbers given in exception stack traces. Shrinking Java source code (.java files) is typically compiled to bytecode (.class files). Bytecode is more compact than Java source code, but it may still contain a lot of unused code, especially if it includes program libraries. Shrinking programs such as ProGuard can analyze bytecode and remove unused classes, fields, and methods. The app remains functionally equivalent, including the information given in exception stack traces. For a realistic example, take the following code: if&#40;Config.LOGD&#41;&#41; &#123; &#160;Log.d&#40;TAG, &#34;Some text&#34;&#41;; &#125; The code as shown above is a typical scenario during development. You create code like this to help debug and test your code. Before releasing the final product, though, you set Config.LOGD to false, so it doesn&#8217;t execute. The problem is, this code is still in your application. It makes it bigger, and may cause potential security issues by including code which should never be seen by a snooping hacker. Shrinking the code solves this problem beautifully. The code is completely removed from the final product, leaving the final package safer and smaller. Optimizing Apart from removing unused classes, fields, and methods in the shrinking step, ProGuard can also perform optimizations at the bytecode level, inside and across methods. Proguard So let&#8217;s have a look at the proguard.cfg file. I recommend you open up the sample code, as I will be highlighting the key parts on this page. Basic template -dontpreverify -repackageclasses '' -allowaccessmodification -optimizations !code/simplification/arithmetic -keepattributes *Annotation* -keep public class * extends android.app.Activity -keep public class * extends android.app.Application -keep public class * extends android.app.Service -keep public class * extends android.content.BroadcastReceiver -keep public class * extends android.content.ContentProvider -keep public class * extends android.view.View { &#160; &#160; public &#60;init&#62;(android.content.Context); &#160; &#160; public &#60;init&#62;(android.content.Context, android.util.AttributeSet); &#160; &#160; public &#60;init&#62;(android.content.Context, android.util.AttributeSet, int); &#160; &#160; public void set*(...); } -keepclasseswithmembers class * { &#160; &#160; public &#60;init&#62;(android.content.Context, android.util.AttributeSet); } -keepclasseswithmembers class * { &#160; &#160; public &#60;init&#62;(android.content.Context, android.util.AttributeSet, int); } -keepclassmembers class * implements android.os.Parcelable { &#160; &#160; static android.os.Parcelable$Creator CREATOR; } -keepclassmembers class **.R$* { &#160; &#160; public static &#60;fields&#62;; } Fragments -keep public class * extends android.support.v4.app.Fragment -keep public class * extends android.app.Fragment Serializables -keepnames class * implements java.io.Serializable -keepclassmembers class * implements java.io.Serializable { &#160; &#160; static final long serialVersionUID; &#160; &#160; private static final java.io.ObjectStreamField[] serialPersistentFields; &#160; &#160; !static !transient &#60;fields&#62;; &#160; &#160; !private &#60;fields&#62;; &#160; &#160; !private &#60;methods&#62;; &#160; &#160; private void writeObject(java.io.ObjectOutputStream); &#160; &#160; private void readObject(java.io.ObjectInputStream); &#160; &#160; java.lang.Object writeReplace(); &#160; &#160; java.lang.Object readResolve(); } Removing Logging -assumenosideeffects class android.util.Log { &#160; &#160; public static *** e(...); &#160; &#160; public static *** w(...); &#160; &#160; public static *** wtf(...); &#160; &#160; public static *** d(...); &#160; &#160; public static *** v(...); } Methods -keepclasseswithmembernames class * { &#160; &#160; native &#60;methods&#62;; } -keepclassmembers class * { &#160; &#160; public void *ButtonClicked(android.view.View); } Now that we have added all our Proguard settings, we need to run Proguard during the build fase. I will use Maven to illustrate this. Within your pom.xml the android-maven-plugin should be defined. To this definition add the proguard setting like: &#160; &#160; &#160; &#60;plugin&#62; &#160; &#160; &#160; &#160; &#60;groupId&#62;com.jayway.maven.plugins.android.generation2&#60;/groupId&#62; &#160; &#160; &#160; &#160; &#60;artifactId&#62;android-maven-plugin&#60;/artifactId&#62; &#160; &#160; &#160; &#160; &#60;version&#62;3.0.0&#60;/version&#62; &#160; &#160; &#160; &#160; &#60;extensions&#62;true&#60;/extensions&#62; &#160; &#160; &#160; &#160; &#60;configuration&#62; &#160; &#160; &#160; &#160; &#160; &#160; &#60;sdk&#62; &#160; &#160; &#160; &#160; &#160; &#160; &#60;platform&#62;15&#60;/platform&#62; &#160; &#160; &#160; &#160; &#160; &#60;/sdk&#62; &#160; &#160; &#160; &#160; &#160; &#60;dex&#62; &#160; &#160; &#160; &#160; &#160; &#160; &#60;jvmArguments&#62; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#60;argument&#62;-Xms256m&#60;/argument&#62; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#60;argument&#62;-Xmx512m&#60;/argument&#62; &#160; &#160; &#160; &#160; &#160; &#160; &#60;/jvmArguments&#62; &#160; &#160; &#160; &#160; &#160; &#60;/dex&#62; &#160; &#160; &#160; &#160; &#160; &#60;run&#62; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#60;debug&#62;true&#60;/debug&#62; &#160; &#160; &#160; &#160; &#160; &#60;/run&#62; &#160; &#160; &#160; &#160; &#160; &#60;proguard&#62; &#160; &#160; &#160; &#160; &#160; &#160; &#60;skip&#62;false&#60;/skip&#62; &#160; &#160; &#160; &#160; &#160; &#160; &#60;config&#62;proguard.cfg&#60;/config&#62; &#160; &#160; &#160; &#160; &#160; &#60;/proguard&#62; &#160; &#160; &#160; &#160; &#60;/configuration&#62; &#160; &#160; &#160; &#60;/plugin&#62; All that is left is to run Maven with mvn clean install To check if the app has been obfuscated, install the apk on you Android device and when you run it, you shouldn&#8217;t see any logging in logcat.]]></description>
										<content:encoded><![CDATA[<p>Obfu-what? Right, Obfuscation, in general, describes a practice that is used to intentionally make something more difficult to understand.<br />
The nature of Java (the programming language for Android apps) is that the code is not compiled down to machine code; it is compiled to an intermediate format that is ready to be run on a variety of hardware platforms. While this allows great portability, it also leaves the code for Android apps, as present in the APK (Application PacKage file), available for extraction.<br />
I&#8217;m going to describe a way for you to obfuscate your Android code to make it harder for others to reverse engineer. And also why not shrink the size of your Android applications and optimize them to make them run faster at the same time.</p>
<p>Without a doubt, the simplest method to protect an app is to enable the obfuscation in <a href="http://proguard.sourceforge.net/" title="Proguard" target="_blank">ProGuard</a>. This freely available tool is already built into the Android toolkit.</p>
<h3>Obfuscation</h3>
<p>By default, compiled bytecode still contains a lot of debugging information: source file names, line numbers, field names, method names, argument names, variable names, etc. This information makes it straightforward to decompile the bytecode and reverse-engineer entire apps. Sometimes, this is not desirable. Obfuscators, such as ProGuard, can remove the debugging information and replace all names by meaningless character sequences, making it much harder to reverse-engineer the code. It further compacts the code as a bonus. The app remains functionally equivalent, except for the class names, method names, and line numbers given in exception stack traces.</p>
<h3>Shrinking</h3>
<p>Java source code (.java files) is typically compiled to bytecode (.class files). Bytecode is more compact than Java source code, but it may still contain a lot of unused code, especially if it includes program libraries. Shrinking programs such as ProGuard can analyze bytecode and remove unused classes, fields, and methods. The app remains functionally equivalent, including the information given in exception stack traces. </p>
<p>For a realistic example, take the following code:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>Config.<span style="color: #006633;">LOGD</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp;Log.<span style="color: #006633;">d</span><span style="color: #009900;">&#40;</span>TAG, <span style="color: #0000ff;">&quot;Some text&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>The code as shown above is a typical scenario during development. You create code like this to help debug and test your code. Before releasing the final product, though, you set Config.LOGD to false, so it doesn&#8217;t execute. The problem is, this code is still in your application. It makes it bigger, and may cause potential security issues by including code which should never be seen by a snooping hacker.</p>
<p>Shrinking the code solves this problem beautifully. The code is completely removed from the final product, leaving the final package safer and smaller.</p>
<h3>Optimizing</h3>
<p>Apart from removing unused classes, fields, and methods in the shrinking step, ProGuard can also perform optimizations at the bytecode level, inside and across methods.</p>
<h3>Proguard</h3>
<p>So let&#8217;s have a look at the <i>proguard.cfg</i> file. I recommend you open up the <a href="https://github.com/welsinga/sample_proguard" title="Sample code" target="_blank">sample code</a>, as I will be highlighting the key parts on this page.</p>
<h4>Basic template</h4>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:100%;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">-dontpreverify<br />
-repackageclasses ''<br />
-allowaccessmodification<br />
-optimizations !code/simplification/arithmetic<br />
-keepattributes *Annotation*<br />
<br />
-keep public class * extends android.app.Activity<br />
-keep public class * extends android.app.Application<br />
-keep public class * extends android.app.Service<br />
-keep public class * extends android.content.BroadcastReceiver<br />
-keep public class * extends android.content.ContentProvider<br />
<br />
-keep public class * extends android.view.View {<br />
&nbsp; &nbsp; public &lt;init&gt;(android.content.Context);<br />
&nbsp; &nbsp; public &lt;init&gt;(android.content.Context, android.util.AttributeSet);<br />
&nbsp; &nbsp; public &lt;init&gt;(android.content.Context, android.util.AttributeSet, int);<br />
&nbsp; &nbsp; public void set*(...);<br />
}<br />
<br />
-keepclasseswithmembers class * {<br />
&nbsp; &nbsp; public &lt;init&gt;(android.content.Context, android.util.AttributeSet);<br />
}<br />
<br />
-keepclasseswithmembers class * {<br />
&nbsp; &nbsp; public &lt;init&gt;(android.content.Context, android.util.AttributeSet, int);<br />
}<br />
<br />
-keepclassmembers class * implements android.os.Parcelable {<br />
&nbsp; &nbsp; static android.os.Parcelable$Creator CREATOR;<br />
}<br />
<br />
-keepclassmembers class **.R$* {<br />
&nbsp; &nbsp; public static &lt;fields&gt;;<br />
}</div></div>
<h4>Fragments</h4>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">-keep public class * extends android.support.v4.app.Fragment<br />
-keep public class * extends android.app.Fragment</div></div>
<h4>Serializables</h4>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">-keepnames class * implements java.io.Serializable<br />
<br />
-keepclassmembers class * implements java.io.Serializable {<br />
&nbsp; &nbsp; static final long serialVersionUID;<br />
&nbsp; &nbsp; private static final java.io.ObjectStreamField[] serialPersistentFields;<br />
&nbsp; &nbsp; !static !transient &lt;fields&gt;;<br />
&nbsp; &nbsp; !private &lt;fields&gt;;<br />
&nbsp; &nbsp; !private &lt;methods&gt;;<br />
&nbsp; &nbsp; private void writeObject(java.io.ObjectOutputStream);<br />
&nbsp; &nbsp; private void readObject(java.io.ObjectInputStream);<br />
&nbsp; &nbsp; java.lang.Object writeReplace();<br />
&nbsp; &nbsp; java.lang.Object readResolve();<br />
}</div></div>
<h4>Removing Logging</h4>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">-assumenosideeffects class android.util.Log {<br />
&nbsp; &nbsp; public static *** e(...);<br />
&nbsp; &nbsp; public static *** w(...);<br />
&nbsp; &nbsp; public static *** wtf(...);<br />
&nbsp; &nbsp; public static *** d(...);<br />
&nbsp; &nbsp; public static *** v(...);<br />
}</div></div>
<h4>Methods</h4>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">-keepclasseswithmembernames class * {<br />
&nbsp; &nbsp; native &lt;methods&gt;;<br />
}<br />
-keepclassmembers class * {<br />
&nbsp; &nbsp; public void *ButtonClicked(android.view.View);<br />
}</div></div>
<p>Now that we have added all our Proguard settings, we need to run Proguard during the build fase. I will use <i>Maven</i> to illustrate this.</p>
<p>Within your <i>pom.xml</i> the <i>android-maven-plugin</i> should be defined. To this definition add the <i>proguard</i> setting like:</p>
<div class="codecolorer-container xml default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:100%;"><div class="xml codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">&nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;plugin<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>com.jayway.maven.plugins.android.generation2<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>android-maven-plugin<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>3.0.0<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;extensions<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>true<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/extensions<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;configuration<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;sdk<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;platform<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>15<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/platform<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/sdk<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dex<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;jvmArguments<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;argument<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>-Xms256m<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/argument<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;argument<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>-Xmx512m<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/argument<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/jvmArguments<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dex<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;run<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;debug<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>true<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/debug<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/run<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;proguard<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;skip<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>false<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/skip<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;config<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>proguard.cfg<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/config<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/proguard<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/configuration<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/plugin<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></div>
<p>All that is left is to run Maven with</p>
<div class="codecolorer-container dos default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="dos codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">mvn clean install</div></div>
<p>To check if the app has been obfuscated, install the apk on you Android device and when you run it, you shouldn&#8217;t see any logging in logcat.</p>
]]></content:encoded>
					
					<wfw:commentRss>http://wiebe-elsinga.com/blog/obfuscating-for-android-with-proguard/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
		<item>
		<title>Automatically starting an Android application on boot</title>
		<link>http://wiebe-elsinga.com/blog/automatically-starting-an-android-application-on-boot/</link>
					<comments>http://wiebe-elsinga.com/blog/automatically-starting-an-android-application-on-boot/#respond</comments>
		
		<dc:creator><![CDATA[W.Elsinga]]></dc:creator>
		<pubDate>Tue, 04 Dec 2012 06:04:03 +0000</pubDate>
				<category><![CDATA[Business]]></category>
		<category><![CDATA[How-To]]></category>
		<category><![CDATA[Android]]></category>
		<category><![CDATA[Broadcast]]></category>
		<guid isPermaLink="false">http://wiebe-elsinga.com/blog/?p=1311</guid>

					<description><![CDATA[There can be multiple reasons for you to want to automatically start (autostart) an Android application or service after booting your device. Lets have a look at what a colleague of mine wrote. To autostart an Android application there are a couple of steps you need to take. However before you do this you should consider this: Background service awareness When manually or even automatically starting an application, users will see something being started. If you (auto)start a background service it won’t be visible to the user. Users might not be aware of the fact that a service was started. Keep this in mind when using a service. Battery life As we know by now people might not be aware that your automatically started background service is running. Keep the battery life of the phone in mind when performing tasks in the background. To create a battery aware service use these things in moderation: Getting a GPS or network location Connecting to your network and sending/receiving data Read/write files (IO) Intensive tasks Let’s autostart our service First of all we need to make sure your application is allowed to receive and handle the broadcast that tells the application that your phone was booted. To be able to receive the broadcast you should add this permission as a child of the &#60;manifest&#62; element to your Android Manifest: &#60;uses-permission android:name=&#34;android.permission.RECEIVE_BOOT_COMPLETED&#34; /&#62; So, right now our application has the permission to receive a broadcasted message to tell us the phone was booted. In order to do anything with this broadcast we need to have a BroadcastReceiver. An example would look like this: public class ExampleBroadcastReceiver extends BroadcastReceiver &#123; &#160; @Override &#160; public void onReceive&#40;Context context, Intent intent&#41; &#160; &#123; &#160; &#160; if &#40;Intent.ACTION_BOOT_COMPLETED.equals&#40;intent.getAction&#40;&#41;&#41;&#41; &#160; &#160; &#123; &#160; &#160; &#160; /* Only perform this code if the BroadcastReceiver received the ACTION_BOOT_COMPLETED action.*/ &#160; &#160; &#125; &#160; &#125; &#125; What we see here is a class that extends the BroadcastReceiver and overrides the onReceive method. We could add our code to start our background service inside the if statement. This way we make sure that only the ACTION_BOOT_COMPLETED action is being processed. This gives us the opportunity to implement code for different Intent actions if necessary. Read the onReceive documentation for some do&#8217;s and dont&#8217;s. An important thing to know is that the onReceive method will be called whenever the class is declared in your manifest and wishes to receive the specified Intent. Let&#8217;s let our application know that our BroadcastReceiver exists and wishes to receive the android.intent.action.BOOT_COMPLETED intent. Add this to your &#60;application&#62; element of your Android Manifest: &#60;receiver android:enabled=”true” android:name=”com.itude.mobile.broadcast.ItudeMobileBroadcastReceiver” android:permission=”android.permission.RECEIVE_BOOT_COMPLETED”&#62; &#160; &#60;intent-filter&#62; &#160; &#160; &#60;action android:name=”android.intent.action.BOOT_COMPLETED” /&#62; &#160; &#60;/intent-filter&#62; &#60;/receiver&#62; Check and double check So to make sure you got everything covered check if you did this: Add the android.permission.RECEIVE_BOOT_COMPLETED permission as a child of your &#60;manifest&#62; element in your Android Manifest Create a class that extends BroadcastReceiver and add your code to the onReceive method Add this BroadcastReceiver class to your Android Manifest as a child of the &#60;application&#62; element so Android knows of it&#8217;s existence and it can trigger the onReceive method if the class matches the intent-filter Enjoy autostarting your application If all went well you can turn off your phone and turn it back on. After it has been booted your application should receive a broadcast from android that triggers your code. Automatically starting an Android application on boot, done!]]></description>
										<content:encoded><![CDATA[<p>There can be multiple reasons for you to want to automatically start (autostart) an Android application or service after booting your device. Lets have a look at what a <a href="https://plus.google.com/105121117776590013398/posts" title="Ali Derbane" target="_blank">colleague of mine</a> wrote. </p>
<p>To autostart an Android application there are a couple of steps you need to take. However before you do this you should consider this:</p>
<h3>Background service awareness</h3>
<p>When manually or even automatically starting an application, users will see something being started.<br />
If you (auto)start a background service it won’t be visible to the user. Users might not be aware of the fact that a service was started. Keep this in mind when using a service.</p>
<h3>Battery life</h3>
<p>As we know by now people might not be aware that your automatically started background service is running. Keep the battery life of the phone in mind when performing tasks in the background.</p>
<p>To create a battery aware service use these things in moderation:</p>
<ul>
<li>Getting a GPS or network location</li>
<li>Connecting to your network and sending/receiving data</li>
<li>Read/write files (IO)</li>
<li>Intensive tasks</li>
</ul>
<p></br></p>
<h3>Let’s autostart our service</h3>
<p>First of all we need to make sure your application is allowed to receive and handle the broadcast that tells the application that your phone was booted.<br />
To be able to receive the broadcast you should add this permission as a child of the <i>&lt;manifest&gt;</> element to your Android Manifest:</p>
<div class="codecolorer-container xml default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="xml codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #000000; font-weight: bold;">&lt;uses-permission</span> <span style="color: #000066;">android:name</span>=<span style="color: #ff0000;">&quot;android.permission.RECEIVE_BOOT_COMPLETED&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></div></div>
<p>So, right now our application has the permission to receive a broadcasted message to tell us the phone was booted. In order to do anything with this broadcast we need to have a <a title="BroadcastReceiver" href="http://developer.android.com/reference/android/content/BroadcastReceiver.html" target="_blank"><b>BroadcastReceiver</b></a>. An example would look like this:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ExampleBroadcastReceiver <span style="color: #000000; font-weight: bold;">extends</span> BroadcastReceiver<br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; @Override<br />
&nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onReceive<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+context"><span style="color: #003399;">Context</span></a> context, Intent intent<span style="color: #009900;">&#41;</span><br />
&nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>Intent.<span style="color: #006633;">ACTION_BOOT_COMPLETED</span>.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>intent.<span style="color: #006633;">getAction</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">/* Only perform this code if the BroadcastReceiver received the ACTION_BOOT_COMPLETED action.*/</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>What we see here is a class that extends the <strong>BroadcastReceiver</strong> and overrides the <strong>onReceive</strong> method. We could add our code to start our background service inside the if statement. This way we make sure that only the ACTION_BOOT_COMPLETED action is being processed. This gives us the opportunity to implement code for different Intent actions if necessary.<br />
Read the <a title="onReceive" href="http://developer.android.com/reference/android/content/BroadcastReceiver.html#onReceive%28android.content.Context,%20android.content.Intent%29" target="_blank">onReceive</a> documentation for some do&#8217;s and dont&#8217;s.</p>
<p>An important thing to know is that the onReceive method will be called whenever the class is declared in your manifest and wishes to receive the specified Intent. Let&#8217;s let our application know that our BroadcastReceiver exists and wishes to receive the <i>android.intent.action.BOOT_COMPLETED</i> intent. Add this to your <i>&lt;application&gt;</i> element of your Android Manifest:</p>
<div class="codecolorer-container xml default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="xml codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;receiver</span> <span style="color: #000066;">android:enabled</span>=”true” <span style="color: #000066;">android:name</span>=”com.itude.mobile.broadcast.ItudeMobileBroadcastReceiver” <span style="color: #000066;">android:permission</span>=”android.permission.RECEIVE_BOOT_COMPLETED”<span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
&nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;intent-filter<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;action</span> <span style="color: #000066;">android:name</span>=”android.intent.action.BOOT_COMPLETED” <span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/intent-filter<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/receiver<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></div>
<p></p>
<h3>Check and double check</h3>
<p>So to make sure you got everything covered check if you did this:</p>
<ol>
<li>Add the <i><strong>android.permission.RECEIVE_BOOT_COMPLETED</strong></i> permission as a child of your <i><strong>&lt;manifest&gt;</strong></i> element in your Android Manifest</li>
<li>Create a class that extends <strong>BroadcastReceiver</strong> and add your code to the <strong>onReceive</strong> method</li>
<li>Add this BroadcastReceiver class to your Android Manifest as a child of the <i><strong>&lt;application&gt;</strong></i> element so Android knows of it&#8217;s existence and it can trigger the onReceive method if the class matches the intent-filter</li>
</ol>
<p></p>
<h3>Enjoy autostarting your application</h3>
<p>If all went well you can turn off your phone and turn it back on. After it has been booted your application should receive a broadcast from android that triggers your code. Automatically starting an Android application on boot, done!</p>
]]></content:encoded>
					
					<wfw:commentRss>http://wiebe-elsinga.com/blog/automatically-starting-an-android-application-on-boot/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Fun with Android and NFC</title>
		<link>http://wiebe-elsinga.com/blog/fun-with-android-and-nfc/</link>
					<comments>http://wiebe-elsinga.com/blog/fun-with-android-and-nfc/#comments</comments>
		
		<dc:creator><![CDATA[W.Elsinga]]></dc:creator>
		<pubDate>Thu, 29 Nov 2012 03:25:48 +0000</pubDate>
				<category><![CDATA[Business]]></category>
		<category><![CDATA[How-To]]></category>
		<category><![CDATA[Android]]></category>
		<category><![CDATA[NFC]]></category>
		<guid isPermaLink="false">http://wiebe-elsinga.com/blog/?p=1269</guid>

					<description><![CDATA[So, you probably heard of something called NFC (Near Field Communication), and maybe you have seen the video (as shown below) from the 2012 Google I/O showing you a developers perspective on NFC. But what you want is a short &#8220;How to&#8221; on creating an Android NFC application. In this tutorial we’re going to build an app that reads and writes to an NFC tag. In order to follow along you will need an NFC enabled Android device, such as the Samsung Galaxy Nexus or Samsung Nexus 7. Also, but not mandatory, what you will need is an NFC tag or sticker. Be sure the NFC tag is pre-formatted because a bug in Android 4.0.2 may prevent you from encoding completely blank tags. Getting started Before we can make use of NFC we need to add a few new tags to our manifest XML. I recommend you open up the sample code, as I will be highlighting the key parts on this page. When you have the project imported, open up AndroidManifest.xml in the XML view. &#60;uses-feature android:name=&#34;android.hardware.nfc&#34; android:required=&#34;true&#34; /&#62; &#160; &#60;uses-permission android:name=&#34;android.permission.NFC&#34; /&#62; &#60;activity android:name=&#34;.ReadTagActivity&#34; &#160; &#160; &#160; &#160; &#160; android:screenOrientation=&#34;portrait&#34; &#160; &#160; &#160; &#160; &#160; android:label=&#34;@string/title_activity_readtag&#34; &#160; &#160; &#160; &#160; &#160; android:parentActivityName=&#34;.MainActivity&#34;&#62; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#60;intent-filter&#62; &#160; &#160; &#160; &#160; &#160; &#160; &#60;action android:name=&#34;android.nfc.action.NDEF_DISCOVERED&#34; /&#62; &#160; &#160; &#160; &#160; &#160; &#160; &#60;category android:name=&#34;android.intent.category.DEFAULT&#34; /&#62; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#60;data android:mimeType=&#34;application/com.elsinga.sample.nfc&#34; /&#62; &#160; &#160; &#160; &#160; &#160; &#160; &#60;/intent-filter&#62; &#160; &#160; &#160; &#160; &#160; &#60;/activity&#62; The uses-permission tag will allow us to access the NFC chip in the device, and the uses-hardware tag will make it required, which will mean our app only appears to NFC enabled devices in Google Play. For our ReadTagActivity tag we add a new intent-filter, this will launch the activity when a tag is scanned that contains data in our format. We do this by specifying a custom mime-type, in this case I’ve chosen “application/com.elsinga.sample.nfc”. Android handles NFC tag scans by trying to find the best match for the data on the tag, providing several fallback mechanisms to make sure the tag is handled by the right app and allowing the user to pick from a chooser dialog when there is no clear target. The first thing we need to do is write some data to a blank NFC tag, so open up WriteTagActivity.java. This activity is launched when the user clicks the &#8220;Write button&#8221; in the app’s main activity. It displays a button that, when pressed, waits for a tag to write to. In our onCreate() we grab a reference to the NFC adapter for later use: _nfcAdapter = NfcAdapter.getDefaultAdapter&#40;this&#41;; The NfcAdapter allows us to begin listening to a tag being scanned, before any other app gets a chance. This is called “foreground dispatch”. When the user presses the button we call enableTagWriteMode() to begin this process. In this method we set up a PendingIntent to run when a tag gets scanned, in this case we simply want the Intent to launch the current Activity when any tag gets scanned, so set up no special filters. When the system dispatches ACTION_TAG_DISCOVERED, this Activity will launch and onNewIntent() will get called with the tag details. &#160; @Override &#160; protected void onNewIntent&#40;Intent intent&#41; &#160; &#123; &#160; &#160; if &#40;_writeMode&#41; &#160; &#160; &#123; &#160; &#160; &#160; if &#40;intent.getAction&#40;&#41;.equals&#40;NfcAdapter.ACTION_TAG_DISCOVERED&#41;&#41; &#160; &#160; &#160; &#123; &#160; &#160; &#160; &#160; Tag detectedTag = intent.getParcelableExtra&#40;NfcAdapter.EXTRA_TAG&#41;; &#160; &#160; &#160; &#160; writeTag&#40;buildNdefMessage&#40;&#41;, detectedTag&#41;; &#160; &#160; &#160; &#160; _imageViewImage.setImageDrawable&#40;getResources&#40;&#41;.getDrawable&#40;R.drawable.android_blue_logo&#41;&#41;; &#160; &#160; &#160; &#160; _editTextData.setEnabled&#40;true&#41;; &#160; &#160; &#160; &#125; &#160; &#160; &#125; &#160; &#125; In onNewIntent() we pull out the NFC Tag and begin writing to it in writeTag(). NDEF The data we store is organised into NDEF Records inside a single NDEF Message. The first thing we do is to determine whether the tag is already NDEF formatted by calling Ndef.get(tag). If it’s already NDEF formatted we check if it’s writeable and that the data is not too large. With all being well we write the data with ndef.writeNdefMessage(). That’s it, we’ve written to an NFC tag! If you go to the home screen, or exit the app, and re-scan the tag, it should now launch the ReadTagActivity and show the text. Open up ReadTagActivity.java to see how we read the information from the Intent: &#160; NdefMessage&#91;&#93; getNdefMessagesFromIntent&#40;Intent intent&#41; &#160; &#123; &#160; &#160; // Parse the intent &#160; &#160; NdefMessage&#91;&#93; msgs = null; &#160; &#160; String action = intent.getAction&#40;&#41;; &#160; &#160; if &#40;action.equals&#40;NfcAdapter.ACTION_TAG_DISCOVERED&#41; &#124;&#124; action.equals&#40;NfcAdapter.ACTION_NDEF_DISCOVERED&#41;&#41; &#160; &#160; &#123; &#160; &#160; &#160; Parcelable&#91;&#93; rawMsgs = intent.getParcelableArrayExtra&#40;NfcAdapter.EXTRA_NDEF_MESSAGES&#41;; &#160; &#160; &#160; if &#40;rawMsgs != null&#41; &#160; &#160; &#160; &#123; &#160; &#160; &#160; &#160; msgs = new NdefMessage&#91;rawMsgs.length&#93;; &#160; &#160; &#160; &#160; for &#40;int i = 0; i &#60; rawMsgs.length; i++&#41; &#160; &#160; &#160; &#160; &#123; &#160; &#160; &#160; &#160; &#160; msgs&#91;i&#93; = &#40;NdefMessage&#41; rawMsgs&#91;i&#93;; &#160; &#160; &#160; &#160; &#125; &#160; &#160; &#160; &#125; &#160; &#160; &#160; else &#160; &#160; &#160; &#123; &#160; &#160; &#160; &#160; // Unknown tag type &#160; &#160; &#160; &#160; byte&#91;&#93; empty = new byte&#91;&#93;&#123;&#125;; &#160; &#160; &#160; &#160; NdefRecord record = new NdefRecord&#40;NdefRecord.TNF_UNKNOWN, empty, empty, empty&#41;; &#160; &#160; &#160; &#160; NdefMessage msg = new NdefMessage&#40;new NdefRecord&#91;&#93;&#123;record&#125;&#41;; &#160; &#160; &#160; &#160; msgs = new NdefMessage&#91;&#93;&#123;msg&#125;; &#160; &#160; &#160; &#125; &#160; &#160; &#125; &#160; &#160; else &#160; &#160; &#123; &#160; &#160; &#160; Log.e&#40;TAG, &#34;Unknown intent.&#34;&#41;; &#160; &#160; &#160; finish&#40;&#41;; &#160; &#160; &#125; &#160; &#160; return msgs; &#160; &#125; With that determined we can go ahead and grab the NdefMessage from the Intent’s extras. In our case we know the first NdefRecord is the record we want, containing our written text in the payload: &#160; &#160; if &#40;getIntent&#40;&#41;.getAction&#40;&#41; != null&#41; &#160; &#160; &#123; &#160; &#160; &#160; if &#40;getIntent&#40;&#41;.getAction&#40;&#41;.equals&#40;NfcAdapter.ACTION_NDEF_DISCOVERED&#41;&#41; &#160; &#160; &#160; &#123; &#160; &#160; &#160; &#160; NdefMessage&#91;&#93; msgs = getNdefMessagesFromIntent&#40;getIntent&#40;&#41;&#41;; &#160; &#160; &#160; &#160; NdefRecord record = msgs&#91;0&#93;.getRecords&#40;&#41;&#91;0&#93;; &#160; &#160; &#160; &#160; byte&#91;&#93; payload = record.getPayload&#40;&#41;; &#160; &#160; &#160; &#160; String payloadString = new String&#40;payload&#41;; &#160; &#160; &#160; &#160; _textViewData.setText&#40;payloadString&#41;; &#160; &#160; &#160; &#125; &#160; &#160; &#125; [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>So, you probably heard of something called NFC (Near Field Communication), and maybe you have seen the video (as shown below) from the 2012 Google I/O showing you a developers perspective on NFC.</p>
<p><center></p>
<div style="max-width:320px;" ><iframe src="http://www.youtube.com/embed/HkzPc8ZvCco?wmode=transparent" width="320" height="180" ></iframe></div>
<p></center>But what you want is a short &#8220;How to&#8221; on creating an Android NFC application. In this tutorial we’re going to build an app that reads and writes to an NFC tag.<br />
In order to follow along you will need an NFC enabled Android device, such as the Samsung Galaxy Nexus or Samsung Nexus 7. Also, but not mandatory, what you will need is an NFC tag or sticker. Be sure the NFC tag is pre-formatted because a bug in Android 4.0.2 may prevent you from encoding completely blank tags.</p>
<h2>Getting started</h2>
<p>Before we can make use of NFC we need to add a few new tags to our manifest XML. I recommend you open up the <a title="Github Sample code" href="https://github.com/welsinga/sample_nfc" target="_blank">sample code</a>, as I will be highlighting the key parts on this page. When you have the project imported, open up <i>AndroidManifest.xml</i> in the XML view.</p>
<div class="codecolorer-container xml default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="xml codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;uses-feature</span> <span style="color: #000066;">android:name</span>=<span style="color: #ff0000;">&quot;android.hardware.nfc&quot;</span> <span style="color: #000066;">android:required</span>=<span style="color: #ff0000;">&quot;true&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; <br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;uses-permission</span> <span style="color: #000066;">android:name</span>=<span style="color: #ff0000;">&quot;android.permission.NFC&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
<br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;activity</span> <span style="color: #000066;">android:name</span>=<span style="color: #ff0000;">&quot;.ReadTagActivity&quot;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000066;">android:screenOrientation</span>=<span style="color: #ff0000;">&quot;portrait&quot;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000066;">android:label</span>=<span style="color: #ff0000;">&quot;@string/title_activity_readtag&quot;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000066;">android:parentActivityName</span>=<span style="color: #ff0000;">&quot;.MainActivity&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;intent-filter<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;action</span> <span style="color: #000066;">android:name</span>=<span style="color: #ff0000;">&quot;android.nfc.action.NDEF_DISCOVERED&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;category</span> <span style="color: #000066;">android:name</span>=<span style="color: #ff0000;">&quot;android.intent.category.DEFAULT&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;data</span> <span style="color: #000066;">android:mimeType</span>=<span style="color: #ff0000;">&quot;application/com.elsinga.sample.nfc&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/intent-filter<span style="color: #000000; font-weight: bold;">&gt;</span></span></span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/activity<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></div>
<p>The <b>uses-permission</b> tag will allow us to access the NFC chip in the device, and the <b>uses-hardware</b> tag will make it required, which will mean our app only appears to NFC enabled devices in Google Play.<br />
For our <strong>ReadTagActivity</strong> tag we add a new <strong>intent-filter</strong>, this will launch the activity when a tag is scanned that contains data in our format. We do this by specifying a custom <strong>mime-type</strong>, in this case I’ve chosen “<strong>application/com.elsinga.sample.nfc</strong>”. Android handles NFC tag scans by trying to find the best match for the data on the tag, providing several fallback mechanisms to make sure the tag is handled by the right app and allowing the user to pick from a chooser dialog when there is no clear target.</p>
<p>The first thing we need to do is write some data to a blank NFC tag, so open up <i>WriteTagActivity.java</i>. This activity is launched when the user clicks the &#8220;Write button&#8221; in the app’s main activity. It displays a button that, when pressed, waits for a tag to write to.</p>
<p>In our <strong>onCreate()</strong> we grab a reference to the NFC adapter for later use:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">_nfcAdapter <span style="color: #339933;">=</span> NfcAdapter.<span style="color: #006633;">getDefaultAdapter</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>The <strong>NfcAdapter</strong> allows us to begin listening to a tag being scanned, before any other app gets a chance. This is called “foreground dispatch”. When the user presses the button we call <strong>enableTagWriteMode()</strong> to begin this process. In this method we set up a <strong>PendingIntent</strong> to run when a tag gets scanned, in this case we simply want the <strong>Intent</strong> to launch the current Activity when any tag gets scanned, so set up no special filters. When the system dispatches <strong>ACTION_TAG_DISCOVERED</strong>, this Activity will launch and <strong>onNewIntent()</strong> will get called with the tag details.</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">&nbsp; @Override<br />
&nbsp; <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> onNewIntent<span style="color: #009900;">&#40;</span>Intent intent<span style="color: #009900;">&#41;</span><br />
&nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>_writeMode<span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>intent.<span style="color: #006633;">getAction</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>NfcAdapter.<span style="color: #006633;">ACTION_TAG_DISCOVERED</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Tag detectedTag <span style="color: #339933;">=</span> intent.<span style="color: #006633;">getParcelableExtra</span><span style="color: #009900;">&#40;</span>NfcAdapter.<span style="color: #006633;">EXTRA_TAG</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; writeTag<span style="color: #009900;">&#40;</span>buildNdefMessage<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, detectedTag<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; _imageViewImage.<span style="color: #006633;">setImageDrawable</span><span style="color: #009900;">&#40;</span>getResources<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getDrawable</span><span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">drawable</span>.<span style="color: #006633;">android_blue_logo</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; _editTextData.<span style="color: #006633;">setEnabled</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span></div></div>
<p>In <strong>onNewIntent()</strong> we pull out the NFC Tag and begin writing to it in <strong>writeTag()</strong>.</p>
<h2>NDEF</h2>
<p>The data we store is organised into NDEF Records inside a single NDEF Message. The first thing we do is to determine whether the tag is already NDEF formatted by calling <strong>Ndef.get(tag)</strong>. If it’s already NDEF formatted we check if it’s writeable and that the data is not too large. With all being well we write the data with <strong>ndef.writeNdefMessage()</strong>. That’s it, we’ve written to an NFC tag!</p>
<p>If you go to the home screen, or exit the app, and re-scan the tag, it should now launch the <strong>ReadTagActivity</strong> and show the text.<br />
Open up <strong>ReadTagActivity.java</strong> to see how we read the information from the Intent:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:100%;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">&nbsp; NdefMessage<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> getNdefMessagesFromIntent<span style="color: #009900;">&#40;</span>Intent intent<span style="color: #009900;">&#41;</span><br />
&nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Parse the intent</span><br />
&nbsp; &nbsp; NdefMessage<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> msgs <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+string"><span style="color: #003399;">String</span></a> action <span style="color: #339933;">=</span> intent.<span style="color: #006633;">getAction</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>action.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>NfcAdapter.<span style="color: #006633;">ACTION_TAG_DISCOVERED</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> action.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>NfcAdapter.<span style="color: #006633;">ACTION_NDEF_DISCOVERED</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; Parcelable<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> rawMsgs <span style="color: #339933;">=</span> intent.<span style="color: #006633;">getParcelableArrayExtra</span><span style="color: #009900;">&#40;</span>NfcAdapter.<span style="color: #006633;">EXTRA_NDEF_MESSAGES</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>rawMsgs <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; msgs <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> NdefMessage<span style="color: #009900;">&#91;</span>rawMsgs.<span style="color: #006633;">length</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> rawMsgs.<span style="color: #006633;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; msgs<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>NdefMessage<span style="color: #009900;">&#41;</span> rawMsgs<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">else</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Unknown tag type</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> empty <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; NdefRecord record <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> NdefRecord<span style="color: #009900;">&#40;</span>NdefRecord.<span style="color: #006633;">TNF_UNKNOWN</span>, empty, empty, empty<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; NdefMessage msg <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> NdefMessage<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> NdefRecord<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span>record<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; msgs <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> NdefMessage<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span>msg<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">else</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; Log.<span style="color: #006633;">e</span><span style="color: #009900;">&#40;</span>TAG, <span style="color: #0000ff;">&quot;Unknown intent.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; finish<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> msgs<span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span></div></div>
<p>With that determined we can go ahead and grab the NdefMessage from the Intent’s extras. In our case we know the first <strong>NdefRecord</strong> is the record we want, containing our written text in the payload:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>getIntent<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getAction</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>getIntent<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getAction</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>NfcAdapter.<span style="color: #006633;">ACTION_NDEF_DISCOVERED</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; NdefMessage<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> msgs <span style="color: #339933;">=</span> getNdefMessagesFromIntent<span style="color: #009900;">&#40;</span>getIntent<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; NdefRecord record <span style="color: #339933;">=</span> msgs<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span>.<span style="color: #006633;">getRecords</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> payload <span style="color: #339933;">=</span> record.<span style="color: #006633;">getPayload</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+string"><span style="color: #003399;">String</span></a> payloadString <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+string"><span style="color: #003399;">String</span></a><span style="color: #009900;">&#40;</span>payload<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; _textViewData.<span style="color: #006633;">setText</span><span style="color: #009900;">&#40;</span>payloadString<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; _nfcAdapter.<span style="color: #006633;">enableForegroundDispatch</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>, _nfcPendingIntent, _readTagFilters, <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>That&#8217;s it. We have managed to write custom data to an NFC tag, and read back that custom data.<br />
Have a look at the <a title="Github Sample code" href="https://github.com/welsinga/sample_nfc" target="_blank">sample code</a> and also discover how to beam data from one NFC-enabled device to another.</p>
]]></content:encoded>
					
					<wfw:commentRss>http://wiebe-elsinga.com/blog/fun-with-android-and-nfc/feed/</wfw:commentRss>
			<slash:comments>3</slash:comments>
		
		
			</item>
	</channel>
</rss>
