如何从头开始设置的Andr​​oid Studio项目,允许我使用Groovy [英] How to set up Android Studio project from scratch that allows me to use groovy

查看:444
本文介绍了如何从头开始设置的Andr​​oid Studio项目,允许我使用Groovy的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Groovy的2.4版本附带了Android应用开发的官方支持 HTTP://docs.$c$chaus.org/display/GROOVY/Groovy+2.4+release+notes

Groovy's 2.4 release comes with official support for android app development http://docs.codehaus.org/display/GROOVY/Groovy+2.4+release+notes.

有previously使用Eclipse进行Android开发,没有任何经验可言了Android Studio和与摇篮,为与Android工作室建立常规的Andr​​oid项目(如的http://docs.groovy-lang.org/docs/next/html/documentation/tools-groovyc.html#section-android, https://github.com/groovy/groovy-android-gradle-plugin 或<一href=\"http://hosain.net/2015/02/07/getting-started-with-android-development-using-groovy-2.4-and-android-studio.html\" rel=\"nofollow\">http://hosain.net/2015/02/07/getting-started-with-android-development-using-groovy-2.4-and-android-studio.html)过于含糊,没有为我工作,以及他们使用的版本号不同的是混乱的。

Having previously used eclipse for Android development, with no experience at all with Android Studio nor with Gradle, the existing instructions for setting up groovy android projects with Android Studio (e.g. http://docs.groovy-lang.org/docs/next/html/documentation/tools-groovyc.html#section-android, https://github.com/groovy/groovy-android-gradle-plugin, or http://hosain.net/2015/02/07/getting-started-with-android-development-using-groovy-2.4-and-android-studio.html) are too vague, do not work for me, and the difference in version numbers used by them is confusing.

我想看到Android的工作室1.2.1.1为Linux工作详细的一步一步的指示,并解释如何在版本号决定。

I'd like to see detailed step-by-step instructions that work for Android Studio 1.2.1.1 for Linux, and explain how to decide on the version numbers.

推荐答案

我自己学习如何做到这一点后,这是一步一步的说明,我也喜欢看,当我开始。我已经从许多来源使用的信息。这些资源没有工作对我来说,他们在版本号不同,在哪里插入东西放到构建脚本。这些指令是尝试各种组合,在一些填空的结果。

After learning by myself how to do it, this is the step-by-step description that I would have liked to read when I started. I have used information from a number of sources. None of these sources worked for me, and they differed in version numbers and where to insert things into build scripts. These instructions are the result of trying out various combinations and filling in some blanks.

从欢迎屏幕中,选择开始一个新的Andr​​oid Studio项目。

From the welcome screen, select "Start a new Android Studio project".

填写应用程序名称和公司域。

Fill in the app name and a "company domain".

此页面上有必要进行任何更改。

No changes necessary on this page.

本例使用空白活动。

此示例使用名称GroovyActivity为Android的主屏幕。新项目现在被创建。当机器人工作室完成创建项目时,它显示了这样的两个开放的文件:

This example uses the name GroovyActivity for the main android screen. The new project now gets created. When android studio has finished creating the project, it shows two open files like this:

忽略渲染问题了。这将是在编译期间后自行解决。通过点击其标签的十字关闭打开的文件。

Ignore the "rendering problem" for now. It will be resolve itself later during compilation. Close the two open files by clicking on the crosses of their tabs.

打开此的build.gradle(模块:应用程序)文件:

Open this build.gradle (Module: app) file:

在第一行之前,插入下面的code:

Insert the following code before the first line:

    buildscript {
        repositories {
            jcenter()
        }
        dependencies {
            classpath 'com.android.tools.build:gradle:1.1.0'
            classpath 'org.codehaus.groovy:gradle-groovy-android-plugin:0.3.6'
        }
    }

让我们暂停了一段时间,以反映有关的版本号。该行

Let's pause for a while to reflect about the version numbers. The lines

            classpath 'com.android.tools.build:gradle:1.1.0'

            classpath 'org.codehaus.groovy:gradle-groovy-android-plugin:0.3.6'

包含版本号。你怎么知道这里插入的版本号?在您阅读这些说明时,可能有过的新版本,除非您有特定的版本要求,你将要使用什么它是用在这里的脚本的Gradle的最新版本。

contain version numbers. How do you know which version number to insert here? At the time you read these instructions, there might have been new releases and unless you have specific version requirements, you will want to use the latest version of whatever it is that is used here in the gradle script.

您如何了解最新的可用版本?这取决于:

How can you learn about the latest available versions? It depends:

请注意,该编辑器突出com.android.tools.build:gradle:1.1.0黄色背景颜色。当你将鼠标指针移到该文本,会出现一个提示:

Note that the editor has highlighted com.android.tools.build:gradle:1.1.0 with a yellow background color. When you hover the mouse pointer over this text, a tooltip appears:

如此看来,Android的工作室都知道了这件事情的最新版本,并建议您使用它。听取意见和更新此版本号为你的工作室的Andr​​oid版本的指示。

So it seems that android studio knows about the latest version for this thing and advises you to use it. Heed the advice and update this version number to what your version of android studio indicates.

但是,没有这样的提示出现的gradle此,Groovy的Andr​​oid的插件的事。

However, no such tooltip appears for this gradle-groovy-android-plugin thing.

什么是发生在这里啊?路上,我的理解是,这些名称和版本号确定用于您的应用程序或建立您的应用程序二进制组件的名字,但也不是他们的Andr​​oid工作室的一部分,也不是你希望找到并安装或自己编译它们。相反,此构建脚本指定的的位置,这些二进制组件可以从希望下载。该功能jcenter()似乎返回仓库的位置。目前,该库是bintray.com。

What is happening here, anyway? The way I understand it, these names and version numbers identify names of binary components that are used for your app or for building your app, but neither are they part of android studio, nor are you expected to find and install or compile them yourself. Instead, this build script specifies a repository location where these binary components can hopefully be downloaded from. The function jcenter() seems to return the location of the repository. Currently, this repository is bintray.com.

要了解这个常规的gradle Android插件的最新版本,请访问bintray.com您的网页浏览器,并使用它的搜索功能。搜索Groovy的Andr​​oid的插件。结果的beginnig看起来像这样在我写这篇文章的时候:

To learn about the latest version of this gradle groovy android plugin, visit bintray.com with your webbrowser and use its search function. Search for groovy-android-plugin. The beginnig of the result looks like this at the time I write this:

列出的结果的数量之多,是有点失望。我希望他们有一定的相关性在里面分拣。通过第一页搜索,我只看到2相关的匹配,以及最新的版本号为0.3.6。

The sheer number of listed results is a bit discouraging. I hope they have some relevance sorting in there. Searching through the first page, I only see 2 relevant matches, and the latest version number is 0.3.6.

在了解最新的版本号(在写这篇文章的时间),脚本的gradle的正确的开始是这样的code:

Having learned about the latest version numbers (at the time of this writing), the correct start of the gradle script is this code:

    buildscript {
        repositories {
            jcenter()
        }
        dependencies {
            classpath 'com.android.tools.build:gradle:1.2.3'
            classpath 'org.codehaus.groovy:gradle-groovy-android-plugin:0.3.6'
        }
    }

返回修改剧本的gradle,插入下面的code应用插件:'com.android.application'之后行:

Back to modifying the gradle script, insert the following code after the "apply plugin: 'com.android.application'" line:

    apply plugin: 'groovyx.grooid.groovy-android'

最后,在附近的gradle脚本的末尾,插入一行

Finally, near the end of the gradle script, insert a line

    compile 'org.codehaus.groovy:groovy:2.4.1:grooid'

,这样它看起来是这样的:

so that it looks like this:

此外,还有一个版本号在这里,您可以通过搜索* grooid.jar(是的,与明星作为第一个字符)找到bintray最新版本。结果列表跨越两页,第二页上我发现2.4.3的最新版本是:

Again, there is a version number here, you can find the latest version on bintray by searching for *grooid.jar (yes, with the star as first character). The result list spans two pages, on the second page I find that 2.4.3 is the latest version:

改变gradle这个文件后,gradle这个需要同步主编。如果你完全退出机器人工作室,并重新启动它会的gradle同步,所以这是我在这一点上做的。此图为重启后同步:

After changing the gradle file, gradle needs to be "sync"ed. The gradle will sync if you exit android studio completely and restart it, so this is what I've done at this point. This picture shows the syncing after the restart:

后来我发现,这个图标在工具栏

Later I found that this icon in the toolbar

触发同步。

后同步完成,在左窗格中,从Android的看法改变与下拉菜单中的项目视图:

After the syncing finishes, change the left pane from the "Android" view to the "Project" view with the pull-down menu:

现在构建并至少执行一次仿真器中的项目。我不知道这是什么变化,但如果不建立,现在执行的项目,而一切仍是唯一的,那么你的时髦人士将不会被发现的Java源代码之后,我们改变Groovy的。您可以构建和使用工具栏或键盘快捷键Shift-F10运行图标运行项目。

Now build and execute your project in the emulator at least once. I'm not sure what this changes, but if you do not build and execute your project now, while everything is still java sources only, then your groovy sources will not be found after we change to groovy. You can build and run your project using the run icon in the toolbar or with the keyboard shortcut shift-F10.

在模拟器中执行你的项目后,回头左侧窗格中,仍然显示项目视图。这种观点认为,导航到应用程序/ src目录/主目录,该目录目前包含子目录java和资源,以及AndroidManifest.xml文件。

After executing your project in the emulator, turn back to the left pane, which still shows the "Project" view. In this view, navigate to the app/src/main directory, which currently contains the subdirectories java and res, and the AndroidManifest.xml file.

您现在将创建以下主要额外的Java文件夹,通过上下文菜单的帮助:

You will now create an additional "Java Folder" below main, with the help of the context menu:

请务必使用菜单项新建 - >文件夹 - > Java的文件夹,然后的的新建 - >目录,这将在以后导致问题。

Be sure to use the menu item New->Folder->Java Folder, and not New->Directory, which would cause problems later on.

在下一屏幕上,

勾选复选框更改文件夹位置,并确保在输入字段的名称为src /主/ Groovy的。

tick the check box "Change Folder Location" and make sure the name in the entry field is src/main/groovy.

如果您仍然有脚本的gradle打开,你会看到,它已经适应,并包含在一条线上的新目录的名称开头为sourceSets。良好的措施,点击同步的gradle图标,以确保一切正常进行同步。

If you still have that gradle script open, you will see that it has been adapted and contains the name of the new directory in a line starting with "sourceSets". For good measure, click on the gradle sync icon to make sure everything is properly synced.

接下来,你要实现这一新的常规文件夹包含您的应用程序的包名合适的包装路径。我发现能够正常实现这一目标的唯一途径,就是要创造一个新的,临时的,java类的常规文件夹的右键菜单,

Next you want to achieve that the new groovy folder contains the proper package path for the package name of your app. The only way that I have found to achieve this properly, is to create a new, temporary, java class with the context menu of the groovy folder,

和进入这个类的名包名完全合格的:

and enter the name of this class fully qualified with the package name:

确认包名字的拼写是正确的!

Make sure the spelling of the package name is correct!

新的方案将出现在常规文件夹,并在里面,临时Java类。

The new package will appear in the groovy folder, and inside it, the temporary java class.

所有的Groovy类和所有的Java类,使以某种方式使用Groovy类都有住的src / main / Groovy的目录,而不是在src / main / java目录下面。现在,我们可以拖动从Java目录树中的Activity类同一包的常规目录如下:

All groovy classes and also all java classes that make in some way use of groovy classes have to live below the src/main/groovy directory instead of the src/main/java directory. We can now drag the Activity class from the java directory tree to the same package below the groovy directory:

临时类现在可以删除(通过上下文菜单)和活动类可以从的.java迁移选择Refactor-到.groovy作为>从上下文菜单中重命名文件。

The temporary class can now be deleted (with the context menu), and the Activity class can be migrated from .java to .groovy by choosing Refactor->Rename File from the context menu.

文件扩展名为.java只是用.groovy作为扩展名代替。以同样的方式,以后可以创建新的Java类和当你真正想要创建Groovy类重命名其文件到.groovy作为。
现在,您可以更改一些code的活性以验证您其实可以有常规code在一个Android应用程序。建议:

The file extension .java is simply replaced with a .groovy extension. In the same way, you can later create new java classes and rename their file to .groovy when you actually want to create groovy classes. You can now change some code in the activity to verify that you can actually have groovy code in an android app. Suggestion:

带来的Hello World文本在res /布局/ activity_groovy.xml文件查看ID,如:

Give the "Hello World" text view an id in the res/layout/activity_groovy.xml file, like this:

然后,以编程方式使用Groovy的串插更改此视图中显示的文本:

Then, programmatically change the text shown by this view using groovy's string interpolation:

建立和执行仿真器:

这篇关于如何从头开始设置的Andr​​oid Studio项目,允许我使用Groovy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆