是否可以在 Eclipse 中使用 Android 的 Gradle 构建系统?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17107014/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 20:43:58  来源:igfitidea点击:

Is it possible to use the Gradle build system for Android with Eclipse?

androideclipsegradle

提问by Chris

I've an app that needs to be build multiple times with different resources for different customers (branding, configuration, and pre-loaded data change between customers).

我有一个应用程序需要为不同的客户使用不同的资源多次构建(品牌、配置和客户之间的预加载数据更改)。

In the wake of this year's Google I/O I've heard about the new Gradle-based Android build-system. So I thought, it would be a good idea, to realize this one-source/multiple-apks scenario using a Gradle build script.

在今年的 Google I/O 之后,我听说了新的基于 Gradle 的 Android 构建系统。所以我想,使用 Gradle 构建脚本来实现这种单源/多 apk 场景是个好主意。

Now here comes my question: How can I get started using Gradle for building while sticking to Eclipse? All the reads I found online point to converting the project to the still immature Android Studio. I'd like to put off migrating to Android Studio until that's declared "safe" for production use by Google.

现在我的问题来了:如何在坚持使用 Eclipse 的同时开始使用 Gradle 进行构建?我在网上找到的所有内容都指向将项目转换为仍然不成熟的 Android Studio。我想推迟迁移到 Android Studio,直到 Google 宣布它对生产使用“安全”。

Ideally, I'd like to be able to hook the build script(s) to the Debug and Run Configurations of Eclipse, very much the same way as I can pick and choose different build targets for debugging and archiving in XCode. If that's possible, what steps are required to make that work?

理想情况下,我希望能够将构建脚本挂钩到 Eclipse 的调试和运行配置,这与我可以选择不同的构建目标以在 XCode 中进行调试和存档非常相似。如果可能的话,需要采取哪些步骤才能使其发挥作用?

I apologize for the noobish quality of these questions, but for me this is actually quite an undiscovered country. Any help is appreciated.

我为这些问题的愚蠢而道歉,但对我来说,这实际上是一个未被发现的国家。任何帮助表示赞赏。

Edit:Our team has migrated to Android Studio in late October 2013 and since version 0.4 were are encountering fewer and fewer bugs. If your organization is not super-conservative about adopting pre-1.0 environments for development, I'd encourage you to jump into the cold water and try working with Android Studio and its Gradle build system. The only important thing missing IMHO is decent support for unit-testing.

编辑:我们的团队已于 2013 年 10 月下旬迁移到 Android Studio,自 0.4 版以来,遇到的错误越来越少。如果您的组织对于采用 1.0 之前的环境进行开发不是非常保守,我鼓励您跳入冷水并尝试使用 Android Studio 及其 Gradle 构建系统。恕我直言,唯一缺少的是对单元测试的体面支持。

回答by Paul Verest

It is possible to use 2 build systems (Eclipse + gradle based). Just make sure output folders are different (binfor ADT, buildfor gradle). (Update for TL;DR : check Nodeclipse/Enide Gradle for Eclipse(marketplace) )

可以使用 2 个构建系统(基于 Eclipse + gradle)。只要确保输出文件夹不同(bin对于 ADT,build对于 gradle)。(TL 更新;DR:检查Nodeclipse/Enide Gradle for Eclipse市场))

File -> Export -> Generate Gradle build fileswill just add build.gradlewith content below (but check versions). No existing files are changed.

File -> Export -> Generate Gradle build files将只添加build.gradle下面的内容(但检查版本)。不更改现有文件。

com.android.tools.build:gradleversion should be the latest. For gradle type gradle buildas said in http://tools.android.com/tech-docs/new-build-system/user-guide. Try gradle tasksfor more. (On my slow Internet connection it took 1 hour! for gradle to download all needed dependencies)

com.android.tools.build:gradle版本应该是最新的。对于http://tools.android.com/tech-docs/new-build-system/user-guide 中gradle build所说的gradle 类型。尝试更多。(在我缓慢的 Internet 连接上花了 1 小时!gradle 下载所有需要的依赖项)gradle tasks

Vogella tutorial http://www.vogella.com/articles/AndroidBuild/article.htmlis not yet ready. Other online tutorials are not really finished http://www.jayway.com/2013/02/26/using-gradle-for-building-android-applications/

Vogella 教程http://www.vogella.com/articles/AndroidBuild/article.html还没有准备好。其他网上教程都没有真正写完http://www.jayway.com/2013/02/26/using-gradle-for-building-android-applications/

Eclipse ADT is not yet using gradle, I think it will be polished within Android Studio first. It would be not so wise to start using evolving technology in both IDEs at the same time.

Eclipse ADT 还没有使用 gradle,我认为它会先在 Android Studio 中完善。同时开始在两个 IDE 中使用不断发展的技术是不明智的。

See build.gradleexample below. If you already mastered gradle, then maybe Wizards is not needed at all. For the latest build.gradletemplate for classic Android project check gh.c/N/n-1/b/m/o.n.e.e.g/docs/android/build.gradle.

请参阅build.gradle下面的示例。如果您已经掌握了 gradle,那么可能根本不需要 Wizards。有关build.gradle经典 Android 项目的最新模板,请查看gh.c/N/n-1/b/m/oneeg/docs/android/build.gradle

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.5.+'
    }
}
apply plugin: 'android'

dependencies {
    compile fileTree(dir: 'libs', include: '*.jar')
}

android {
    compileSdkVersion 8
    buildToolsVersion "19.0.0"

    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src']
            resources.srcDirs = ['src']
            aidl.srcDirs = ['src']
            renderscript.srcDirs = ['src']
            res.srcDirs = ['res']
            assets.srcDirs = ['assets']
        }

        // Move the tests to tests/java, tests/res, etc...
        instrumentTest.setRoot('tests')

        // Move the build types to build-types/<type>
        // For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ...
        // This moves them out of them default location under src/<type>/... which would
        // conflict with src/ being used by the main source set.
        // Adding new build types or product flavors should be accompanied
        // by a similar customization.
        debug.setRoot('build-types/debug')
        release.setRoot('build-types/release')
    }
}

ADT-Bundle does not come with Eclipse Marketplace, so update site could be used.

ADT-Bundle 不附带 Eclipse Marketplace,因此可以使用更新站点。

Update p2 repository for Gradle Integration for Eclipseis

用于 Eclipse 的 Gradle 集成的更新 p2 存储库是

http://dist.springsource.com/release/TOOLS/gradle

http://dist.springsource.com/release/TOOLS/gradle

But as of version 3.4.0 it does not provide Editor for .gradle files. So there is no sense of having it for Android development.

但是从 3.4.0 版本开始,它不为 .gradle 文件提供编辑器。所以对于Android开发来说没有任何意义。

I would go with default ADT build, having gradle as secondary build for experimentations and keeping an eye when flow of bugs on http://tools.android.com/tech-docs/new-build-systembecomes rare. (That should be around formal 1.0 version)

我会使用默认的 ADT 构建,将 gradle 作为实验的辅助构建,并在http://tools.android.com/tech-docs/new-build-system上的错误流变得罕见时密切关注。(那应该是正式的 1.0 版本左右)

UPDATE: 2014-04-15

更新:2014-04-15

Alex Ruiz's (from Android team) Blog about Android, Gradle & ADT

Alex Ruiz(来自 Android 团队)关于 Android、Gradle 和 ADT 的博客

Android's Gradle Model

Instead of creating IDE-specific Android/Gradle models, we decided to have an IDE-agnostic representation of a Gradle project. This way, we have a single source of information that is easier to maintain. IDE integration will be implemented as a plug-in for each supported IDE (in our case, Eclipse and IDEA.) The main benefit of this approach is that we can release the Gradle plug-in independently of the IDE integration plug-ins. For example, we can ship a new version of the Eclipse plug-in that has several bug fixes, without affecting the Gradle side of Android.

Android 的 Gradle 模型

我们决定使用与 IDE 无关的 Gradle 项目表示,而不是创建特定于 IDE 的 Android/Gradle 模型。这样,我们就有了更易于维护的单一信息源。IDE 集成将作为每个受支持 IDE(在我们的示例中为 Eclipse 和 IDEA)的插件来实现。这种方法的主要好处是我们可以独立于 IDE 集成插件发布 Gradle 插件。例如,我们可以发布一个新版本的 Eclipse 插件,该插件修复了几个错误,而不会影响 Android 的 Gradle 端。

As of April 2014 eclipse-gradle plugin is not compatible with android-gradle plugin:

截至 2014 年 4 月,eclipse-gradle 插件与 android-gradle 插件不兼容:

As answered in Issue 57668by Android team (raised by @arcone)

如Android 团队在Issue 57668 中的回答(由 @arcone 提出)

Project Member #2 [email protected]

The eclipse plugin is not compatible with the android plugin.

You will not be able to import an Android gradle project into Eclipse using the default Gradle support in Eclipse.

To make it work in Eclipse we will have to change the Gradle plugin for Eclipse, the same way we are modifying the Gradle support in IntelliJ

项目成员 #2 [email protected]

eclipse 插件与android 插件不兼容。

您将无法使用 Eclipse 中的默认 Gradle 支持将 Android gradle 项目导入 Eclipse。

为了让它在 Eclipse 中工作,我们必须为 Eclipse 更改 Gradle 插件,就像我们在 IntelliJ 中修改 Gradle 支持一样

That is Android team is working on gradle plugin for IntelliJ and gradle plugin for Eclipse needs to be updated too.

那是 Android 团队正在开发 IntelliJ 的 gradle 插件,Eclipse 的 gradle 插件也需要更新。

There is effort at Nodeclipseto smooth the transition times. And continue to develop in Eclipse while still experimenting or fully using gradle.

Nodeclipse正在努力平滑过渡时间。并继续在 Eclipse 中开发,同时仍在试验或完全使用 gradle。

Nodeclipse/Enide Gradle for Eclipse(marketplace)

Eclipse 的 Nodeclipse/Enide Gradle市场

Some screenshots for Gradle for Eclipse:

Gradle for Eclipse 的一些屏幕截图:

回答by rajkabbur

  1. Install Gradle:
    a. http://gradle.org/gradle-download/b. Choose 2.1 from previous releases section. c. Unzip at convenient folder.(Ex : D:\Graddle\gradle-2.1) d. Set system environment variables. i. GRADLE_HOME as D:\Graddle\gradle-2.1) ii. GRADLE_OPTS as -XX:MaxPermSize=512m iii. %GRADLE_HOME%\bin to Path iv. Set ANDROID_HOME ( Ex: D:\android-sdk) v. Append “%ANDROID %\platform-tools” to path.
  1. 安装摇篮:
    一。 http://gradle.org/gradle-download/b. 从以前的版本部分选择 2.1。C。在方便的文件夹中解压。(例如:D:\Graddle\gradle-2.1) d. 设置系统环境变量。一世。GRADLE_HOME 为 D:\Grdle\gradle-2.1) ii. GRADLE_OPTS 为 -XX:MaxPermSize=512m iii. %GRADLE_HOME%\bin 到路径 iv。设置 ANDROID_HOME(例如:D:\android-sdk) v. 将“%ANDROID %\platform-tools”附加到路径。

e. Open command prompt and check gradle is set. May use gradle -version to check.

e. 打开命令提示符并检查 gradle 已设置。可以使用 gradle -version 来检查。

  1. Install Gradle eclipse PlugIn: a. Launch Eclipse b. Help > Eclipse Market Place c. Search “gradle” d. In that choose “Nodeeclipse/enide” e. Select all listed, accept & install. f. Restart eclipse once installed.

  2. Set Gradle & Java Homes : a. Launch eclipse. b. Window > Preferences > Gradle EnIDE c. Set these if not set : i. Gradle home to use is set ( Ex: D:\Graddle\gradle-2.1) ii. Alternate JAVA_HOME to use is set ( Ex : C:\Program Files (x86)\Java\jdk1.7.0_60) iii. JVM options for GRADLE_OPTS is set to “-XX:MaxPermSize=512m”

  3. Build the Project: a. Expand APK in eclipse Java explorer. b. Right click on build.gradle c. Run As > Gradle GUI d. Comand Line : gradle clean build e. Wait for build to complete : First time build will take several minutes. f. If Build dex error or Java heap space error : i. Open build.gradle in editor. ii. For multi dex builds- Set appropriate javaMaxHeapSize based on your java (javaMaxHeapSize=1024M for 32bit Java,2048M for 64bit Java) iii. May comment signing (//apply from: "$rootProject.projectDir/jenkins_config/gradle/signing.gradle";) to avoid signing for debug build. iv. Build again after these fixes.

  4. Install Build On device: a. Connect the device to m/c. b. Right click on build.gradle c. Run As > gradle installDebug Gradle Android start d. Wait for install to complete

  5. Debug Build: a. Launch the app b. Attach the debugger (DDMS>Devices > App). c. We able to debug on few devices checked.

  1. 安装 Gradle eclipse 插件:启动 Eclipse B. 帮助 > Eclipse Market Place c. 搜索“gradle” d. 在那选择“Nodeeclipse/enide”e。选择所有列出的,接受并安装。F。安装后重启eclipse。

  2. 设置 Gradle 和 Java 主目录:启动日食。湾 窗口 > 首选项 > Gradle EnIDE c. 如果未设置,请设置这些: i. 设置要使用的 Gradle 主页(例如:D:\Grdle\gradle-2.1) ii. 设置要使用的备用 JAVA_HOME(例如:C:\Program Files (x86)\Java\jdk1.7.0_60) iii. GRADLE_OPTS 的 JVM 选项设置为“-XX:MaxPermSize=512m”

  3. 构建项目:在 Eclipse Java 资源管理器中展开 APK。湾 右键单击 build.gradle c。运行方式 > Gradle GUI d. 命令行:gradle clean build e。等待构建完成:第一次构建需要几分钟。F。如果构建 dex 错误或 Java 堆空间错误: i. 在编辑器中打开 build.gradle。ii. 对于多 dex 构建-根据您的 java 设置适当的 javaMaxHeapSize(javaMaxHeapSize=1024M 用于 32 位 Java,2048M 用于 64 位 Java) iii. 可以评论签名(//apply from: "$rootProject.projectDir/jenkins_config/gradle/signing.gradle";)以避免为调试版本签名。四、在这些修复之后再次构建。

  4. 安装 Build On 设备:将设备连接到 m/cb 右键单击​​ build.gradle c。Run As > gradle installDebug Gradle Android start d. 等待安装完成

  5. 调试构建:启动应用程序 B. 连接调试器(DDMS>Devices>App)。C。我们能够在检查的少数设备上进行调试。