<?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/"
	>

<channel>
	<title>XLV Labs blog</title>
	<atom:link href="http://blog.xlv-labs.com/?feed=rss2" rel="self" type="application/rss+xml" />
	<link>http://blog.xlv-labs.com</link>
	<description></description>
	<pubDate>Wed, 22 Apr 2009 04:41:01 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.7.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Using the PMD Eclipse plugin on Android projects</title>
		<link>http://blog.xlv-labs.com/?p=26</link>
		<comments>http://blog.xlv-labs.com/?p=26#comments</comments>
		<pubDate>Wed, 22 Apr 2009 02:18:40 +0000</pubDate>
		<dc:creator>xlv</dc:creator>
		
		<category><![CDATA[PMD]]></category>

		<category><![CDATA[android]]></category>

		<guid isPermaLink="false">http://blog.xlv-labs.com/?p=26</guid>
		<description><![CDATA[This post will explain how to configure the Eclipse project of an Android application to use PMD and create a custom ruleset crafted for the Android platform used to detect violations automatically.
Starting with version 4.2.5, PMD has some rules specific for Android development, defined  in the android.xml ruleset included in the PMD distribution. The newest [...]]]></description>
			<content:encoded><![CDATA[<p>This post will explain how to configure the Eclipse project of an Android application to use <a href="http://pmd.sourceforge.net/">PMD</a> and create a custom ruleset crafted for the Android platform used to detect violations automatically.</p>
<p>Starting with version 4.2.5, PMD has some rules specific for Android development, defined  in the android.xml ruleset included in the PMD distribution. The newest version of the PMD eclipse plugin, version 3.2.6,  includes some improvements so that the plugin can be used to check for violations on Android project.</p>
<p>First, you need to install the PMD Eclipse plugin by following the instructions on the <a href="http://pmd.sourceforge.net/integrations.html#eclipse">plugin page</a>.</p>
<p>Next, configure your project: in the project properties, select the PMD page. &#8220;Enable PMD&#8221; needs to be checked to run PMD automatically. Otherwise, you will need to manually start PMD each time you want to check your code using the PMD submenu and older violations will not be removed automatically once you fix your code, you will need to use the clear violations in the PMD submenu.</p>
<p>The &#8220;Handle high priority violations as errors&#8221; property needs to be unchecked. This is a new property in the 3.2.6 version of the PMD eclipse plugin. When that option is checked, the Android plugin will refuse to deploy your application if the project contains high level violations. So unless you have a development rule stating that there should be no high level violations in your project at any time, this option should be disabled.</p>
<p>With that setup, PMD will be enabled but it will be using the default ruleset shared by all projects in the workspace and that is not really adequate for Android development. To define a specific set of rules for your project, check the last box on the PMD page to create a file named &#8220;.ruleset&#8221; at the top level of your project. The configuration is now complete, press &#8220;OK&#8221; and then select &#8220;Yes&#8221; on the next popup.</p>
<p>A new &#8220;.pmd&#8221; file has now been created at the top level containing:</p>
<pre>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;pmd&gt;
  &lt;useProjectRuleSet&gt;true&lt;/useProjectRuleSet&gt;
  &lt;ruleSetFile&gt;.ruleset&lt;/ruleSetFile&gt;
  &lt;includeDerivedFiles&gt;false&lt;/includeDerivedFiles&gt;
  &lt;violationsAsErrors&gt;false&lt;/violationsAsErrors&gt;
&lt;/pmd&gt;</pre>
<p>The &#8220;.ruleset&#8221; file was also created and at this point references the workspace default rules. This file should be replaced to use rules more appropriate for Android development. For instance, you could replace its content with:</p>
<pre>&lt;?xml version="1.0"?&gt;
&lt;ruleset name="Android Application Rules"
    xmlns="http://pmd.sf.net/ruleset/1.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://pmd.sf.net/ruleset/1.0.0 http://pmd.sf.net/ruleset_xml_schema.xsd"
    xsi:noNamespaceSchemaLocation="http://pmd.sf.net/ruleset_xml_schema.xsd"&gt;

  &lt;description&gt;Ruleset for Android application&lt;/description&gt;

  &lt;exclude-pattern&gt;.*/R.java&lt;/exclude-pattern&gt;
  &lt;exclude-pattern&gt;.*/gen/.*&lt;/exclude-pattern&gt;

  &lt;rule ref="rulesets/android.xml"/&gt;

  &lt;rule name="DoNotHardCodeSDCard"
      message="Do not hardcode /sdcard."
      class="net.sourceforge.pmd.rules.XPathRule"&gt;
    &lt;description&gt;Use Environment.getExternalStorageDirectory() instead of "/sdcard"&lt;/description&gt;
    &lt;priority&gt;3&lt;/priority&gt;
    &lt;properties&gt;
      &lt;property name="xpath"&gt;
        &lt;value&gt;//Literal[starts-with(@Image,'"/sdcard')]&lt;/value&gt;
      &lt;/property&gt;
    &lt;/properties&gt;
  &lt;/rule&gt;

  &lt;rule ref="rulesets/clone.xml"/&gt;
  &lt;rule ref="rulesets/finalizers.xml"/&gt;
  &lt;rule ref="rulesets/imports.xml"/&gt;
  &lt;rule ref="rulesets/logging-java.xml"/&gt;
  &lt;rule ref="rulesets/naming.xml"&gt;
    &lt;exclude name="AbstractNaming"/&gt;
    &lt;exclude name="LongVariable"/&gt;
    &lt;exclude name="LongMethodName"/&gt;
    &lt;exclude name="ShortMethodName"/&gt;
    &lt;exclude name="ShortVariable"/&gt;
    &lt;exclude name="VariableNamingConventions"/&gt;
  &lt;/rule&gt;

&lt;/ruleset&gt;</pre>
<p>The customization starts with ignoring R.java and the &#8216;gen&#8217; directory introduced in the 1.5 SDK as it only contains automatically generated classes. Following that is the custom ruleset definition, starting with all the rules from the Android ruleset (android.xml), then a locally defined XPath based rule to check that &#8220;/sdcard&#8221; is not used (this rule will be part of the standard ruleset in future PMD versions) and finally some standard rulesets are included either entirely or as a subset of their rules.</p>
<p>Once you&#8217;ve modified &#8220;.ruleset&#8221; and refreshed the Eclipse workspace, the PMD plugin will use the new ruleset to flag violations and update the warnings on the fly in the Problems tab of the Java perspective. The PMD perspective can also be used to see an overview of the violations per package and to have access to detailed violation outlines for each warning.</p>
<p>Up to this point, the settings have only dealt with the Eclipse plugin but the same ruleset can also be used from the command line so that both Eclipse and ant will report the same violations. To do so, add the following target definition in the build.xml file:</p>
<pre>    &lt;target name="pmd"&gt;
        &lt;fail unless="pmd.home" message="pmd.home must be defined" /&gt;
        &lt;path id="pmd.path"&gt;
            &lt;fileset dir="${pmd.home}/lib" includes="*.jar"/&gt;
        &lt;/path&gt;
        &lt;taskdef name="pmd" classname="net.sourceforge.pmd.ant.PMDTask" classpathref="pmd.path" /&gt;

        &lt;pmd shortFilenames="true"&gt;
            &lt;auxclasspath&gt;
                &lt;pathelement location="${android-jar}"/&gt;
                &lt;pathelement location="${out-classes}" /&gt;
                &lt;fileset dir="libs"&gt;
                    &lt;include name="*.jar" /&gt;
                &lt;/fileset&gt;
            &lt;/auxclasspath&gt;
            &lt;formatter type="text" toConsole="true"/&gt;
            &lt;ruleset&gt;.ruleset&lt;/ruleset&gt;
            &lt;fileset dir="${source-folder}" includes="**/*.java" /&gt;
        &lt;/pmd&gt;
    &lt;/target&gt;</pre>
<p>The only additional ant configuration needed is to define pmd.home in build.properties to point to the PMD installation on your machine.</p>
<p>By following the steps above, you will be able to get started using PMD on Android projects. More ruleset customizations will be covered in a future post, based on the best practice documents on the Android site  (the <a href="http://developer.android.com/guide/practices/design/performance.html">design for performance</a> and <a href="http://source.android.com/submit-patches/code-style-guide">code style</a> documents) and the feedback from users. Please forward suggestions on improvements and possible new PMD rules to be added to future versions.</p>
<div style="float:left;margin: 12px;"><script type="text/javascript"><!--
google_ad_client = "pub-7988398006452697";
/* blog_half_size */
google_ad_slot = "9688625277";
google_ad_width = 234;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>]]></content:encoded>
			<wfw:commentRss>http://blog.xlv-labs.com/?feed=rss2&amp;p=26</wfw:commentRss>
		</item>
		<item>
		<title>Testing Android Localization with SKD 1.1</title>
		<link>http://blog.xlv-labs.com/?p=7</link>
		<comments>http://blog.xlv-labs.com/?p=7#comments</comments>
		<pubDate>Tue, 31 Mar 2009 22:58:33 +0000</pubDate>
		<dc:creator>xlv</dc:creator>
		
		<category><![CDATA[android]]></category>

		<guid isPermaLink="false">http://blog.xlv-labs.com/?p=7</guid>
		<description><![CDATA[Update (April 2nd 2009): the draft document has now been updated with similar instructions to the ones below.
The draft document for localization on the  Android platform has not been updated to support the 1.1 SDK yet and the method described there does not work.
It is possible to use the Settings -&#62; Locale &#38; Text-&#62; Select [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Update (April 2nd 2009):</strong> the draft document has now been updated with similar instructions to the ones below.</p>
<p>The <a href="http://groups.google.com/group/android-developers/web/localizing-android-apps-draft">draft document</a> for localization on the  Android platform has not been updated to support the 1.1 SDK yet and the method described there does not work.</p>
<p>It is possible to use the Settings -&gt; Locale &amp; Text-&gt; Select locale preference to change the locale but this only works for the predefined locales shipping with the emulator  (currently DE, US, GB).</p>
<p>To test a different locale, you need to modify /data/property/persist.sys.language and /data/property/persist.sys.country. The change will be reflected after an emulator restart. Note that the &#8220;stop; sleep 5; start&#8221; mentioned in the official document does not work, you need to exit from the emulator.</p>
<p>So for instance, to change the locale to fr-FR, once you have the emulator running, start <code>"adb -e shell"</code> and type:</p>
<p><code><br />
echo fr-FR &gt;/data/property/persist.sys.language</code></p>
<p>echo FR &gt;/data/property/persist.sys.country</p>
<p>Exit from the emulator and once you restart, the locale will be set correctly and be persistent across restarts. If the locale is not one shipping with the SDK, only your own application(s) will be translated, the default applications will still be using the default English values.</p>
<p>You may also want to use different images for testing so that you don&#8217;t have to redo the above steps each time as it&#8217;s easier to update the tested application for each image. To do that, use the &#8220;-data&#8221; option when starting the emulator, e.g.:</p>
<p>emulator -data /home/xlv/dev/android/emulator_files/fr.img</p>
<p>Using those steps, I was able to make sure that the <a href="http://xlv-labs.com/whocalled/">Who Called?</a> application was correctly translated in French (&#8221;Qui C&#8217;est?&#8221;):</p>
<p><a href="http://xlv-labs.com/whocalled/"><img class="aligncenter size-full wp-image-10" title="Qui C'Est?" src="http://blog.xlv-labs.com/wp-content/uploads/2009/03/quicest.png" alt="Qui C'Est?" width="320" height="480" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.xlv-labs.com/?feed=rss2&amp;p=7</wfw:commentRss>
		</item>
	</channel>
</rss>
