{"id":23420,"date":"2015-07-23T00:11:40","date_gmt":"2015-07-22T18:41:40","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=23420"},"modified":"2015-07-23T10:45:04","modified_gmt":"2015-07-23T05:15:04","slug":"creating-android-application-with-groovy","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/creating-android-application-with-groovy\/","title":{"rendered":"Creating Android Application with Groovy"},"content":{"rendered":"<p>Groovy 2.4 was released in January 2015, with native support for <a title=\"Android Application Development Services\" href=\"http:\/\/www.tothenew.com\/mobile-android-application-development-services\">Android development<\/a>. It allows to write Android applications fully using Groovy.<\/p>\n<p><strong>Running Groovy on Android<\/strong><\/p>\n<p><strong>Step 1:<\/strong> Create a new Android project in Android Studio<\/p>\n<p><strong>Step 2:<\/strong> Open this <strong>build.gradle (Module: app)<\/strong> file:<br \/>\nInsert the following code before the first line:<\/p>\n<p>[java]buildscript {<br \/>\n        repositories {<br \/>\n            jcenter()<br \/>\n        }<br \/>\n        dependencies {<br \/>\n            classpath &#8216;com.android.tools.build:gradle:1.1.0&#8217;<br \/>\n            classpath &#8216;org.codehaus.groovy:gradle-groovy-android-plugin:0.3.6&#8217;<br \/>\n        }<br \/>\n    }[\/java]<\/p>\n<p>Insert the following code after the <strong>&#8220;apply plugin:&#8217;com.android.application'&#8221; <\/strong>line:<\/p>\n<p>[java]apply plugin: &#8216;groovyx.grooid.groovy-android'[\/java]<\/p>\n<p>Finally, near the end of the gradle script, insert a line in dependencies:<\/p>\n<p>[java]compile &#8216;org.codehaus.groovy:groovy:2.4.1:grooid'[\/java]<\/p>\n<p>After changing the gradle file, gradle needs to be &#8220;sync&#8221;ed.<\/p>\n<p><strong>Step 3:<\/strong> You will now create an additional &#8220;Java Folder&#8221; below <strong>main<\/strong>. Use New-&gt;Folder-&gt;Java Folder instead of New-&gt;Directory<br \/>\nThe name in the entry field is <strong>src\/main\/groovy<\/strong>.<br \/>\n<strong>Note:<\/strong> Groovy code files need to be placed in src\/main\/groovy instead of src\/main\/java. Adding Groovy files to src\/main\/java will not work!<\/p>\n<p><strong>Step 4:<\/strong> Create a new, <strong>MainActivity<\/strong>, with the context menu of the groovy folder, and enter the name of this class fully qualified with the package name.<\/p>\n<p><strong>Step 5:<\/strong> MainActivity class can be migrated from .java to .groovy by choosing <strong>Refactor-&gt;Rename File<\/strong> from the context menu. The file extension .java is simply replaced with a .groovy extension.<\/p>\n<p><strong>Step 6:<\/strong> Give the &#8220;Hello World&#8221; text view an id in the <strong>res\/layout\/activity_main.xml<\/strong> file, like this:<\/p>\n<p>[java]&lt;TextView<br \/>\nandroid:id=&quot;@+id\/textview_hello&quot;<br \/>\nandroid:layout_width=&quot;wrap_content&quot;<br \/>\nandroid:layout_height=&quot;wrap_content&quot;<br \/>\nandroid:text=&quot;@string\/hello_world&quot;\/&gt;<br \/>\n[\/java]<\/p>\n<p><strong>Step 7:<\/strong> In MainActivity.groovy, programmatically change the text shown by this view using groovy&#8217;s string interpolation:<\/p>\n<p>[java]public class MainActivity extends Activity {<\/p>\n<p>    @Override<br \/>\n    protected void onCreate(Bundle savedInstanceState) {<br \/>\n        super.onCreate(savedInstanceState);<br \/>\n        setContentView(R.layout.activity_main)<br \/>\n        int id = R.id.textview_hello<br \/>\n        findViewById(id).text = &quot;Hello Groovy&quot;<br \/>\n    }<br \/>\n}[\/java]<\/p>\n<p><strong>Step 8:<\/strong> Build and execute the application.<\/p>\n<p>It is very easy to run Groovy on Android. Because of Groovy&#8217;s interoperability with Java, it is also very easy to mix Groovy and Java in the same application.<\/p>\n<p><strong>Happy groovy&#8217;ing!<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Groovy 2.4 was released in January 2015, with native support for Android development. It allows to write Android applications fully using Groovy. Running Groovy on Android Step 1: Create a new Android project in Android Studio Step 2: Open this build.gradle (Module: app) file: Insert the following code before the first line: [java]buildscript { repositories [&hellip;]<\/p>\n","protected":false},"author":167,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":32},"categories":[518,1772,1],"tags":[4845,1503,9],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/23420"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/167"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=23420"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/23420\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=23420"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=23420"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=23420"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}