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

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

问题描述

Groovy 的 2.4 版本正式支持 Android 应用程序开发http://docs.codehaus.org/display/GROOVY/Groovy+2.4+release+notes.

之前使用过 eclipse 进行 Android 开发,完全没有使用 Android Studio 和 Gradle 的经验,使用 Android Studio 设置 groovy android 项目的现有说明(例如 http://docs.groovy-lang.org/docs/next/html/documentation/tools-groovyc.html#section-android, https://github.com/groovy/groovy-android-gradle-pluginhttp://hosain.net/2015/02/07/getting-started-with-android-development-using-groovy-2.4-and-android-studio.html) 太模糊了,不适合我,并且他们使用的版本号差异令人困惑.

我想查看适用于 Linux 版 Android Studio 1.2.1.1 的详细分步说明,并解释如何确定版本号.

解决方案

在 2019 年,在问题和这个答案之后将近 4 年,下面描述的方法不再适用于当前的 Android Studio 版本 (3.3.2).我找不到仍然可用的依赖项版本,而且 Gradle 抱怨无法下载某些依赖项.

4 年后,Groovy 维护者仍然没有可重现的设置指南这一事实只是表明 Groovy for Android 没有未来的一个迹象.我们早就应该看到关于 Groovy 的动态能力如何简化 Android 开发的模式的讨论,但这并没有发生.接下来我将尝试 Kotlin,它乍一看似乎与 Groovy 非常相似,除了必需的类型注释之外,但最重要的是:使用它不需要与 Android Studio 进行如此激烈的斗争.

原始答案如下,它是针对一个非常特定的 Android Studio 版本,扩展了猜测如何在未来的 Android Studio 版本中调整依赖项的版本.根据评论,这一直工作到 Android Studio 3.0.1.

<小时>

在我自己学习了如何做之后,这是我开始时很想阅读的分步说明.我使用了许多来源的信息.这些来源都不适合我,它们在版本号和将内容插入构建脚本的位置方面有所不同.这些说明是尝试各种组合并填空的结果.

从欢迎屏幕中,选择启动新的 Android Studio 项目".

填写应用名称和公司域名".

此页面无需更改.

此示例使用空白活动.

此示例使用名称 GroovyActivity 作为主 android 屏幕.新项目现已创建.当 android studio 创建完项目后,它会显示两个打开的文件,如下所示:

暂时忽略渲染问题".稍后将在编译期间自行解决.单击选项卡上的十字,关闭两个打开的文件.

打开这个 build.gradle (Module: app) 文件:

在第一行前插入以下代码:

 构建脚本 {存储库{jcenter()}依赖{类路径 'com.android.tools.build:gradle:1.1.0'类路径 'org.codehaus.groovy:gradle-groovy-android-plugin:0.3.6'}}

让我们暂停一段时间来考虑一下版本号.线条

 类路径 'com.android.tools.build:gradle:1.1.0'

 类路径 'org.codehaus.groovy:gradle-groovy-android-plugin:0.3.6'

包含版本号.你怎么知道在这里插入哪个版本号?在您阅读这些说明时,可能有新版本,除非您有特定的版本要求,否则您将希望使用 gradle 脚本中使用的最新版本.

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

请注意,编辑器已使用黄色背景高亮显示 com.android.tools.build:gradle:1.1.0.当您将鼠标指针悬停在此文本上时,会出现一个工具提示:

所以看来android studio 知道这个东西的最新版本,建议大家使用.听取建议并将此版本号更新为您的 android studio 版本指示.

然而,这个 gradle-groovy-android-plugin 的东西没有出现这样的工具提示.

这里到底发生了什么?根据我的理解,这些名称和版本号标识了用于您的应用程序或构建您的应用程序的二进制组件的名称,但它们既不是 android studio 的一部分,也不希望您自己查找和安装或编译它们.相反,此构建脚本指定了一个存储库 位置,希望可以从中下载这些二进制组件.函数 jcenter() 似乎返回存储库的位置.目前,这个仓库是 bintray.com.

要了解此 gradle groovy android 插件的最新版本,请使用您的网络浏览器访问 bintray.com 并使用其搜索功能.搜索 groovy-android-plugin.在我写这篇文章的时候,结果的开头是这样的:

列出的结果数量之多有点令人沮丧.我希望他们在那里有一些相关性排序.搜索第一页,只看到2个相关匹配,最新版本号为0.3.6.

了解最新版本号后(在撰写本文时),gradle 脚本的正确开头是以下代码:

 构建脚本 {存储库{jcenter()}依赖{类路径 'com.android.tools.build:gradle:1.2.3'类路径 'org.codehaus.groovy:gradle-groovy-android-plugin:0.3.6'}}

回到修改gradle脚本,在apply plugin:'com.android.application'"行后面插入如下代码:

 应用插件:'groovyx.grooid.groovy-android'

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

 编译 'org.codehaus.groovy:groovy:2.4.1:grooid'

使它看起来像这样:

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

更改gradle文件后,需要对gradle进行同步".如果您完全退出 android studio 并重新启动它,gradle 将同步,所以这就是我此时所做的.下图为重启后的同步:

后来我发现工具栏中有这个图标

触发同步.

同步完成后,使用下拉菜单将左窗格从Android"视图更改为项目"视图:

现在至少在模拟器中构建和执行您的项目一次.我不确定这会改变什么,但是如果您现在不构建和执行您的项目,而一切仍然只是 java 源代码,那么在我们更改为 groovy 后将找不到您的 groovy 源代码.您可以使用工具栏中的运行图标或键盘快捷键 shift-F10 来构建和运行您的项目.

在模拟器中执行您的项目后,返回左窗格,该窗格仍显示项目"视图.在此视图中,导航到 app/src/main 目录,该目录当前包含子目录 java 和 res 以及 AndroidManifest.xml 文件.

您现在将在上下文菜单的帮助下在主目录下创建一个额外的Java 文件夹":

一定要使用菜单项New->Folder->Java Folder,不要New->Directory,这会导致以后出现问题.

在下一个屏幕中,

勾选更改文件夹位置"复选框并确保输入字段中的名称为 src/main/groovy.

如果您仍然打开该 gradle 脚本,您将看到它已被修改,并在以sourceSets"开头的一行中包含新目录的名称.为了更好地衡量,请点击 gradle 同步图标以确保所有内容都正确同步.

接下来,您希望新的 groovy 文件夹包含应用程序包名称的正确包路径.我发现正确实现此目的的唯一方法是使用 groovy 文件夹的上下文菜单创建一个新的临时 java 类,

并输入该类的名称,并使用包名称进行完全限定:

确保包名拼写正确!

新包将出现在 groovy 文件夹中,其中包含临时 java 类.

所有 groovy 类以及以某种方式使用 groovy 类的所有 java 类都必须位于 src/main/groovy 目录下,而不是 src/main/java 目录下.我们现在可以将 Activity 类从 java 目录树拖到 groovy 目录下的同一个包中:

现在可以删除临时类(使用上下文菜单),并且可以通过从上下文菜单中选择 Refactor->Rename File 将 Activity 类从 .java 迁移到 .groovy.

文件扩展名 .java 被简单地替换为 .groovy 扩展名.以同样的方式,您可以稍后创建新的 java 类,并在您真正想要创建 groovy 类时将它们的文件重命名为 .groovy.您现在可以更改 Activity 中的一些代码,以验证您实际上可以在 android 应用程序中使用 groovy 代码.建议:

在 res/layout/activity_groovy.xml 文件中为Hello World"文本视图提供一个 id,如下所示:

然后,使用 groovy 的字符串插值以编程方式更改此视图显示的文本:

在模拟器中构建和执行:

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

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.

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.

解决方案

EDIT: In 2019, almost 4 years after the question and this answer, the method described below does not work anymore with the current Android Studio version (3.3.2). I could not find versions of the dependencies that still work, and Gradle complains about not being able to download some dependencies.

The fact that 4 years later there is still no reproducible setup guide by the Groovy maintainers is just one indicator that Groovy for Android has no future. We should have seen discussions on patterns regarding how Groovy's dynamic abilities simplify Android development long ago, but this has not happened. I'm going to try Kotlin next, which seems similar enough to Groovy on first glance, apart from the required type annotations, but most important: one does not have to fight so hard against Android Studio to use it.

The original answer follows, it was for a very specific Android Studio version, extended with guesses how to adapt versions of dependencies in future Android Studio versions. According to the comments, this has worked until Android Studio 3.0.1.


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.

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.

This example uses the Blank Activity.

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.

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

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'

and

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

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:

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:

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.

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

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.

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:

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.

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'
        }
    }

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

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

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:

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:

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

triggers the sync.

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

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.

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.

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

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

In the next screen,

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

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.

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!

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

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:

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.

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:

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

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

Build and execute in emulator:

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

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