简体   繁体   English

是否可以使用Eclipse的Gradle构建系统与Eclipse?

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

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. 在今年的Google I / O之后,我听说过基于Gradle的新Android构建系统。 So I thought, it would be a good idea, to realize this one-source/multiple-apks scenario using a Gradle build script. 所以我想,使用Gradle构建脚本实现这个单源/多apks场景是个好主意。

Now here comes my question: How can I get started using Gradle for building while sticking to Eclipse? 现在我的问题出现了:如何在坚持Eclipse的同时开始使用Gradle进行构建? All the reads I found online point to converting the project to the still immature Android Studio. 我在网上发现的所有读取都指向将项目转换为仍然不成熟的Android Studio。 I'd like to put off migrating to Android Studio until that's declared "safe" for production use by Google. 我想推迟迁移到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. 理想情况下,我希望能够将构建脚本挂钩到Eclipse的调试和运行配置,这与我可以选择的方式非常相似,并选择不同的构建目标来在XCode中进行调试和存档。 If that's possible, what steps are required to make that work? 如果可能的话,需要采取哪些步骤来完成这项工作?

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. 编辑:我们的团队已于2013年10月下旬迁移到Android Studio,因为版本0.4正在遇到越来越少的错误。 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. 如果您的组织对于采用1.0之前的环境进行开发并不过于保守,我建议您尽量避免使用Android Studio及其Gradle构建系统。 The only important thing missing IMHO is decent support for unit-testing. 缺少恕我直言的唯一重要的事情是对单元测试的体面支持。

It is possible to use 2 build systems (Eclipse + gradle based). 可以使用2个构建系统(基于Eclipse + gradle)。 Just make sure output folders are different ( bin for ADT, build for gradle). 只要确保输出文件夹是不同的( bin为ADT, build了gradle产出)。 (Update for TL;DR : check Nodeclipse/Enide Gradle for Eclipse ( marketplace ) ) (更新TL; DR:检查Nodeclipse / Enide Gradle for Eclipse市场 ))

File -> Export -> Generate Gradle build files will just add build.gradle with content below (but check versions). File -> Export -> Generate Gradle build files只会添加build.gradle其中包含以下内容(但检查版本)。 No existing files are changed. 没有更改现有文件。

com.android.tools.build:gradle version should be the latest . com.android.tools.build:gradle版本应该是最新版本。 For gradle type gradle build as said in http://tools.android.com/tech-docs/new-build-system/user-guide . 对于gradle类型的gradle buildhttp://tools.android.com/tech-docs/new-build-system/user-guide中所述。 Try gradle tasks for more. 尝试更多gradle tasks (On my slow Internet connection it took 1 hour! for gradle to download all needed dependencies) (在我缓慢的Internet连接上花了1个小时!为gradle下载所有需要的依赖项)

Vogella tutorial http://www.vogella.com/articles/AndroidBuild/article.html is not yet ready. Vogella教程http://www.vogella.com/articles/AndroidBuild/article.html尚未准备好。 Other online tutorials are not really finished http://www.jayway.com/2013/02/26/using-gradle-for-building-android-applications/ 其他在线教程还没有真正完成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. Eclipse ADT尚未使用gradle,我认为它将首先在Android Studio中完善。 It would be not so wise to start using evolving technology in both IDEs at the same time. 在两个IDE中同时开始使用不断发展的技术并不是那么明智。

See build.gradle example below. 请参阅下面的build.gradle示例。 If you already mastered gradle, then maybe Wizards is not needed at all. 如果你已经掌握了gradle,那么根本不需要巫师。 For the latest build.gradle template for classic Android project check gh.c/N/n-1/b/m/oneeg/docs/android/build.gradle . 对于经典Android项目的最新build.gradle模板,请检查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. Eclipse Marketplace不附带ADT-Bundle,因此可以使用更新站点。

Update p2 repository for Gradle Integration for Eclipse is 更新用于Eclipse的Gradle Integration的 p2存储库是

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

But as of version 3.4.0 it does not provide Editor for .gradle files. 但是从版本3.4.0开始,它不为.gradle文件提供编辑器。 So there is no sense of having it for Android development. 所以对于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-system becomes rare. 我会使用默认的ADT构建,将gradle作为实验的辅助构建,并在http://tools.android.com/tech-docs/new-build-system上的错误流量变得罕见时保持关注。 (That should be around formal 1.0 version) (那应该是正式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 Android的Gradle模型

Instead of creating IDE-specific Android/Gradle models, we decided to have an IDE-agnostic representation of a Gradle project. 我们决定使用与Gradle项目无关的IDE表示,而不是创建特定于IDE的Android / Gradle模型。 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. IDE集成将作为每个支持的IDE(在我们的例子中,Eclipse和IDEA)的插件实现。这种方法的主要好处是我们可以独立于IDE集成插件发布Gradle插件。 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. 例如,我们可以发布一个新版本的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 57668 by Android team (raised by @arcone) 正如Android团队在问题57668中所回答的那样(由@arcone提出)

Project Member #2 x...@android.com 项目成员#2 x ... @ android.com

The eclipse plugin is not compatible with the android plugin. eclipse插件与android插件不兼容。

You will not be able to import an Android gradle project into Eclipse using the default Gradle support in Eclipse. 您将无法使用Eclipse中的默认Gradle支持将Android gradle项目导入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 为了使它在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 Nodeclipse to smooth the transition times. 在Nodeclipse努力平滑过渡时间。 And continue to develop in Eclipse while still experimenting or fully using gradle. 并继续在Eclipse中开发,同时仍在试验或完全使用gradle。

Nodeclipse/Enide Gradle for Eclipse ( marketplace ) 用于Eclipse的Nodeclipse / Enide Gradle市场

Some screenshots for Gradle for Eclipse: Gradle for Eclipse的一些截图:

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

e. Open command prompt and check gradle is set. 打开命令提示符并设置检查gradle。 May use gradle -version to check. 可以使用gradle -version来检查。

  1. Install Gradle eclipse PlugIn: a. 安装Gradle eclipse PlugIn:a。 Launch Eclipse b. 启动Eclipse b。 Help > Eclipse Market Place c. 帮助> Eclipse Market Place c。 Search “gradle” d. 搜索“gradle”d。 In that choose “Nodeeclipse/enide” e. 在那里选择“Nodeeclipse / enide”e。 Select all listed, accept & install. 选择列出的所有内容,接受并安装。 f. F。 Restart eclipse once installed. 安装后重新启动eclipse。

  2. Set Gradle & Java Homes : a. 设置Gradle和Java Homes:a。 Launch eclipse. 发射日食。 b. Window > Preferences > Gradle EnIDE c. 窗口>首选项> Gradle EnIDE c。 Set these if not set : i. 设置这些如果没有设置:i。 Gradle home to use is set ( Ex: D:\\Graddle\\gradle-2.1) ii. 设置Gradle home(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. 设置要使用的备用JAVA_HOME(例如:C:\\ Program Files(x86)\\ Java \\ jdk1.7.0_60)iii。 JVM options for GRADLE_OPTS is set to “-XX:MaxPermSize=512m” GRADLE_OPTS的JVM选项设置为“-XX:MaxPermSize = 512m”

  3. Build the Project: a. 建设项目:a。 Expand APK in eclipse Java explorer. 在eclipse Java explorer中展开APK。 b. Right click on build.gradle c. 右键单击build.gradle c。 Run As > Gradle GUI d. 运行方式> Gradle GUI d。 Comand Line : gradle clean build e. Comand Line:gradle clean build e。 Wait for build to complete : First time build will take several minutes. 等待构建完成:第一次构建将需要几分钟。 f. F。 If Build dex error or Java heap space error : i. 如果Build dex错误或Java堆空间错误:i。 Open build.gradle in editor. 在编辑器中打开build.gradle。 ii. II。 For multi dex builds- Set appropriate javaMaxHeapSize based on your java (javaMaxHeapSize=1024M for 32bit Java,2048M for 64bit Java) iii. 对于多dex构建 - 根据您的java设置适当的javaMaxHeapSize(对于32位Java,javaMaxHeapSize = 1024M,对于64位Java,为2048M)。 May comment signing (//apply from: "$rootProject.projectDir/jenkins_config/gradle/signing.gradle";) to avoid signing for debug build. 可以评论签名(// apply from:“$ rootProject.projectDir / jenkins_config / gradle / signing.gradle”;)以避免签署调试版本。 iv. IV。 Build again after these fixes. 在这些修复之后再次构建。

  4. Install Build On device: a. 在设备上安装Build:a。 Connect the device to m/cb Right click on build.gradle c. 将设备连接到m / cb右键单击build.gradle c。 Run As > gradle installDebug Gradle Android start d. 运行方式> gradle installDebug Gradle Android start d。 Wait for install to complete 等待安装完成

  5. Debug Build: a. 调试版本:a。 Launch the app b. 启动应用程序b。 Attach the debugger (DDMS>Devices > App). 附加调试器(DDMS>设备>应用程序)。 c. C。 We able to debug on few devices checked. 我们能够在几个设备上进行调试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM