如何使用Gradle为Android项目生成Eclipse和Intellij项目文件

时间:2021-11-11 11:19:03

Is it possible to generate Eclipse and Intellij project files for Android projects using Gradle?

是否可以使用Gradle为Android项目生成Eclipse和Intellij项目文件?

In maven we would do mvn eclipse:eclipse and in PlayFramework we would do play eclipsify. Does Gradle have this feature for Android projects?

在maven中我们会做mvn eclipse:eclipse,在PlayFramework中我们会玩eclipsify。 Gradle是否为Android项目提供此功能?

I have installed Gradle (1.6) and Android SDK Manager (22.0.1) explained here

我已经安装了Gradle(1.6)和Android SDK Manager(22.0.1)

This is my build.gradle file:

这是我的build.gradle文件:

buildscript {
    repositories {
      mavenCentral()
    }
    dependencies {
      classpath 'com.android.tools.build:gradle:0.5.0'
    }
}

apply plugin: 'android'
apply plugin: 'eclipse'

sourceCompatibility = 1.7
version = '1.0.2'

repositories {
    mavenCentral()
}

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

android {
    buildToolsVersion "17" 
    compileSdkVersion 8
    defaultConfig {
        versionCode 1
        versionName "1.0"
        minSdkVersion 7
        targetSdkVersion 8
    }
    sourceSets {
        main {
              manifest.srcFile 'AndroidManifest.xml'
              java.srcDirs = ['src']
              resources.srcDirs = ['src']
              aidl.srcDirs = ['src']
              renderscript.srcDirs = ['src']
              res.srcDirs = ['res']
              assets.srcDirs = ['assets']
        }
        instrumentTest.setRoot('tests')
    }
}

And then I run the command:

然后我运行命令:

gradle clean build cleanEclipse eclipse

It builds just fine, but when I import the project into Eclipse it looks like a normal java project.

它构建得很好,但是当我将项目导入Eclipse时,它看起来像一个普通的java项目。

Anyone know how to get Gradle to create Android specific Eclipse project files?

任何人都知道如何让Gradle创建Android特定的Eclipse项目文件?

Is this a prioritized feature by Gradle?

这是Gradle的优先功能吗?

-- UPDATE --

- 更新 -

I do believe this is an issue. So I have posted it to the Android Tools team issue #57668. Please star it for them to prioritize the issue :)

我相信这是一个问题。所以我已将其发布到Android Tools团队问题#57668。请明星为他们优先解决问题:)

-- UPDATE --

- 更新 -

It does not look like the Android Tools team are looking into this issue. So for now I have converted to Android Studio where I'm able to import my gradle project with dependencies via the IDE.

它看起来不像Android工具团队正在调查此问题。所以现在我已经转换到Android Studio,我可以通过IDE导入我的gradle项目与依赖项。

6 个解决方案

#1


9  

Gradle itself is a generic build tool, it is not created specifically for Android.

Gradle本身是一个通用的构建工具,它不是专门为Android创建的。

All the functionality is enabled using plug-ins. Traditionally, build plug-ins don't generate project structure. That's the job of project specific tools. The android plug-in for Gradle follows this.

使用插件启用所有功能。传统上,构建插件不会生成项目结构。这是项目特定工具的工作。 Gradle的android插件就是这样的。

The problem is that current android tool in SDK generates old type of project structure (ant builds). The new project structure can only be generated via Android Studio.

问题是SDK中的当前android工具生成旧类型的项目结构(ant构建)。新项目结构只能通过Android Studio生成。

There is a concept of archetypes in tools like Maven, which allow using project templates. Gradle does not support that yet (requests have been made for this feature).

在像Maven这样的工具中有一个原型概念,允许使用项目模板。 Gradle尚不支持(已针对此功能提出请求)。

Refer to this thread: http://issues.gradle.org/browse/GRADLE-1289 , some users have provided scripts to generate structure.

请参阅此主题:http://issues.gradle.org/browse/GRADLE-1289,一些用户提供了生成结构的脚本。

Build Initialization feature is in progress: https://github.com/gradle/gradle/blob/master/design-docs/build-initialisation.md

构建初始化功能正在进行中:https://github.com/gradle/gradle/blob/master/design-docs/build-initialisation.md

Hopefully some one can write a script to do that, refer to this guide for new project structure: http://tools.android.com/tech-docs/new-build-system/user-guide

希望有人可以编写一个脚本来执行此操作,请参阅本指南以了解新的项目结构:http://tools.android.com/tech-docs/new-build-system/user-guide

Update

更新

There is now an official android IDE : Android Studio . It is based on Intellij and the new build system is Gradle based.

现在有一个官方的Android IDE:Android Studio。它基于Intellij,新的构建系统基于Gradle。

#2


2  

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插件也需要更新。

What is possible with Eclipse now is

现在Eclipse的可能性是什么

.1. import the project as general project

0.1。将项目导入为一般项目

.project

。项目

<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
    <name>OpenSpritz-Android</name>
    <comment></comment>
    <projects>
    </projects>
    <buildSpec>
    </buildSpec>
    <natures>
    </natures>
</projectDescription>

如何使用Gradle为Android项目生成Eclipse和Intellij项目文件

.2. Put 2 Eclipse . "dot" files into modules into /OpenSpritz-Android/app/src/main and /OpenSpritz-Android/lib/src/main

0.2。放2个Eclipse。将“dot”文件分成模块放入/ OpenSpritz -Android / app / src / main和/ OpenSpritz -Android / lib / src / main

如何使用Gradle为Android项目生成Eclipse和Intellij项目文件

.project

。项目

<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
    <name>OpenSpritz-Android-app</name>
    <comment></comment>
    <projects>
    </projects>
    <buildSpec>
        <buildCommand>
            <name>com.android.ide.eclipse.adt.ResourceManagerBuilder</name>
            <arguments>
            </arguments>
        </buildCommand>
        <buildCommand>
            <name>com.android.ide.eclipse.adt.PreCompilerBuilder</name>
            <arguments>
            </arguments>
        </buildCommand>
        <buildCommand>
            <name>org.eclipse.jdt.core.javabuilder</name>
            <arguments>
            </arguments>
        </buildCommand>
        <buildCommand>
            <name>com.android.ide.eclipse.adt.ApkBuilder</name>
            <arguments>
            </arguments>
        </buildCommand>
    </buildSpec>
    <natures>
        <nature>com.android.ide.eclipse.adt.AndroidNature</nature>
        <nature>org.eclipse.jdt.core.javanature</nature>
    </natures>
</projectDescription>

.classpath

的.classpath

<?xml version="1.0" encoding="UTF-8"?>
<classpath>
    <classpathentry kind="src" path="java"/>
    <classpathentry kind="src" path="gen"/>
    <classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>
    <classpathentry exported="true" kind="con" path="com.android.ide.eclipse.adt.LIBRARIES"/>
    <classpathentry exported="true" kind="con" path="com.android.ide.eclipse.adt.DEPENDENCIES"/>
    <classpathentry kind="output" path="bin/classes"/>
</classpath>

.3. Import as Existing Android Code into Workspace

0.3。作为现有Android代码导入Workspace

如何使用Gradle为Android项目生成Eclipse和Intellij项目文件

you can then browse code in familiar way, but even after that you won't be able to run with Eclipse ADT.

然后,您可以以熟悉的方式浏览代码,但即使在此之后,您也无法使用Eclipse ADT运行。

.4.

0.4。

Now you can run build and tasks with gradle CLI or Nodeclipse/Enide Gradle for Eclipse (marketplace)

现在,您可以使用gradle CLI或Nodeclipse / Enide Gradle for Eclipse(市场)运行构建和任务

如何使用Gradle为Android项目生成Eclipse和Intellij项目文件

discuss at https://github.com/Nodeclipse/nodeclipse-1/issues/148

在https://github.com/Nodeclipse/nodeclipse-1/issues/148讨论

#3


2  

The following worked for me

以下对我有用

eclipse.classpath.plusConfigurations += configurations.compile

eclipse.classpath.file {
    beforeMerged { classpath ->
    classpath.entries.removeAll() { c -> 
        c.kind == 'src'
    }
}

withXml {
    def node = it.asNode()

    node.appendNode('classpathentry kind="src" path="src/main/java"')
    node.appendNode('classpathentry kind="src" path="src/debug/java"')
    node.appendNode('classpathentry kind="src" path="gen"')

    node.children().removeAll() { c ->
        def path = c.attribute('path')
        path != null && (
                path.contains('/com.android.support/support-v4')
                )
    }
  }
}

eclipse.project {
   name = 'AndroidGradleBasic'

   natures 'com.android.ide.eclipse.adt.AndroidNature'
   buildCommand 'com.android.ide.eclipse.adt.ResourceManagerBuilder'
   buildCommand 'com.android.ide.eclipse.adt.PreCompilerBuilder'
   buildCommand 'com.android.ide.eclipse.adt.ApkBuilder'
}

Source - http://blog.gouline.net/2013/11/02/new-build-system-for-android-with-eclipse/

来源 - http://blog.gouline.net/2013/11/02/new-build-system-for-android-with-eclipse/

Sample - https://github.com/krishnaraj/oneclipboard

示例 - https://github.com/krishnaraj/oneclipboard

#4


2  

There are four issues with the combination of the Gradle plugins 'com.android.application' and 'eclipse': First, the configuration classpaths are not added to Eclipse's classpath, but this is easy to fix. Second, something must be done about the .AAR-dependencies. This was a bit trickier. Third, we need to include the generated sources for things like R.java. Finally, we need to include Android.jar itself.

Gradle插件'com.android.application'和'eclipse'的组合有四个问题:首先,配置类路径没有添加到Eclipse的类路径中,但这很容易修复。其次,必须对.AAR依赖关系做些什么。这有点棘手。第三,我们需要包含像R.java这样的生成源。最后,我们需要包含Android.jar本身。

I was able to hack together a gradle configuration that would generate proper .classpath files for Eclipse from an Android Studio build.config. The effects were very satisfying to my CPU fan, which had been running constantly with Android Studio. Eclipse sees the resulting project as a fully functional Java project, but only that.

我能够破解一个gradle配置,它可以从Android Studio build.config为Eclipse生成正确的.classpath文件。我的CPU风扇效果非常令人满意,它一直在Android Studio上运行。 Eclipse将生成的项目视为一个功能齐全的Java项目,但仅限于此。

I ended up putting the following directly in build.gradle in my app-project:

我最终将以下内容直接放在我的app-project中的build.gradle中:

apply plugin: 'eclipse'
eclipse {
    pathVariables 'GRADLE_HOME': gradle.gradleUserHomeDir, "ANDROID_HOME": android.sdkDirectory 
    classpath {
        plusConfigurations += [ configurations.compile, configurations.testCompile ]

        file {
            beforeMerged { classpath ->
                classpath.entries.add(new org.gradle.plugins.ide.eclipse.model.SourceFolder("src/main/java", "bin"))
                // Hardcoded to use debug configuration
                classpath.entries.add(new org.gradle.plugins.ide.eclipse.model.SourceFolder("build/generated/source/r/debug", "bin"))
                classpath.entries.add(new org.gradle.plugins.ide.eclipse.model.SourceFolder("build/generated/source/buildConfig/debug", "bin"))
            }

            whenMerged { classpath ->
                def aars = []
                classpath.entries.each { dep ->
                    if (dep.path.toString().endsWith(".aar")) {
                        def explodedDir = new File(projectDir, "build/intermediates/exploded-aar/" + dep.moduleVersion.group + "/" + dep.moduleVersion.name + "/" + dep.moduleVersion.version + "/jars/")
                        if (explodedDir.exists()) {
                            explodedDir.eachFileRecurse(groovy.io.FileType.FILES) {
                                if (it.getName().endsWith("jar")) {
                                    def aarJar = new org.gradle.plugins.ide.eclipse.model.Library(fileReferenceFactory.fromFile(it))
                                    aarJar.sourcePath = dep.sourcePath
                                    aars.add(aarJar)
                                }
                            }
                        } else {
                            println "Warning: Missing " + explodedDir
                        }
                    }
                }
                classpath.entries.removeAll { it.path.endsWith(".aar") }
                classpath.entries.addAll(aars)

                def androidJar = new org.gradle.plugins.ide.eclipse.model.Variable(
                    fileReferenceFactory.fromPath("ANDROID_HOME/platforms/" + android.compileSdkVersion + "/android.jar"))
                androidJar.sourcePath = fileReferenceFactory.fromPath("ANDROID_HOME/sources/" + android.compileSdkVersion) 
                classpath.entries.add(androidJar)
            }
        }
    }
}

// We need build/generated/source/{r,buildConfig}/debug to be present before generating classpath
//  This also ensures that AARs are exploded 
eclipseClasspath.dependsOn "generateDebugSources"


// Bonus: start the app directly on the device with "gradle startDebug"
task startDebug(dependsOn: "installDebug") << {
    exec {
        executable = new File(android.sdkDirectory, 'platform-tools/adb')
        args = ['shell', 'am', 'start', '-n', android.defaultConfig.applicationId + '/.MainActivity']
    }
}

Run gradle eclipse and you will have an Eclipse-project that can be imported and compiled. However, this project acts as a normal Java-project. In order to build the apk, I have to drop back to gradle command line and execute gradle installDebug. gradle processDebugResources picks up changes in Android XML files and regenerates the files under build/generated/source. I use the "monitor" program with Android SDK to view the app logs. I have so far not found any way to debug without Android Studio.

运行gradle eclipse,你将拥有一个可以导入和编译的Eclipse项目。但是,此项目充当普通的Java项目。为了构建apk,我必须回退到gradle命令行并执行gradle installDebug。 gradle processDebugResources获取Android XML文件中的更改并重新生成build / generated / source下的文件。我使用Android SDK的“监视器”程序来查看应用程序日志。到目前为止,我没有找到任何方法来调试没有Android Studio。

The only features I miss from Android Studio are debugging (but who has time for bugs!) and editing resources visually.

我想念Android Studio的唯一功能是调试(但是谁有时间查看错误!)并直观地编辑资源。

#5


1  

Maybe I'm missing something very obvious, but isn't the eclipse plugin what you're looking for? This generates a .project file as well as a .classpath file and updates these as needed when run subsequent times.

也许我错过了一些非常明显的东西,但不是你想要的eclipse插件吗?这将生成.project文件以及.classpath文件,并在后续运行时根据需要更新这些文件。

There is also an IDEA plugin, though I haven't used this one and can't speak to it.

还有一个IDEA插件,虽然我还没有使用过这个插件而且无法说话。

Hope that helps.

希望有所帮助。

#6


0  

I've created a new Gradle plugin that generates the appropriate Eclipse .project and .classpath files, based on the answer provided by Johannes Brodwall on this stack overflow.

我已经创建了一个新的Gradle插件,它根据Johannes Brodwall在此堆栈溢出时提供的答案生成相应的Eclipse .project和.classpath文件。

See https://github.com/greensopinion/gradle-android-eclipse for details.

有关详细信息,请参阅https://github.com/greensopinion/gradle-android-eclipse。

#1


9  

Gradle itself is a generic build tool, it is not created specifically for Android.

Gradle本身是一个通用的构建工具,它不是专门为Android创建的。

All the functionality is enabled using plug-ins. Traditionally, build plug-ins don't generate project structure. That's the job of project specific tools. The android plug-in for Gradle follows this.

使用插件启用所有功能。传统上,构建插件不会生成项目结构。这是项目特定工具的工作。 Gradle的android插件就是这样的。

The problem is that current android tool in SDK generates old type of project structure (ant builds). The new project structure can only be generated via Android Studio.

问题是SDK中的当前android工具生成旧类型的项目结构(ant构建)。新项目结构只能通过Android Studio生成。

There is a concept of archetypes in tools like Maven, which allow using project templates. Gradle does not support that yet (requests have been made for this feature).

在像Maven这样的工具中有一个原型概念,允许使用项目模板。 Gradle尚不支持(已针对此功能提出请求)。

Refer to this thread: http://issues.gradle.org/browse/GRADLE-1289 , some users have provided scripts to generate structure.

请参阅此主题:http://issues.gradle.org/browse/GRADLE-1289,一些用户提供了生成结构的脚本。

Build Initialization feature is in progress: https://github.com/gradle/gradle/blob/master/design-docs/build-initialisation.md

构建初始化功能正在进行中:https://github.com/gradle/gradle/blob/master/design-docs/build-initialisation.md

Hopefully some one can write a script to do that, refer to this guide for new project structure: http://tools.android.com/tech-docs/new-build-system/user-guide

希望有人可以编写一个脚本来执行此操作,请参阅本指南以了解新的项目结构:http://tools.android.com/tech-docs/new-build-system/user-guide

Update

更新

There is now an official android IDE : Android Studio . It is based on Intellij and the new build system is Gradle based.

现在有一个官方的Android IDE:Android Studio。它基于Intellij,新的构建系统基于Gradle。

#2


2  

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插件也需要更新。

What is possible with Eclipse now is

现在Eclipse的可能性是什么

.1. import the project as general project

0.1。将项目导入为一般项目

.project

。项目

<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
    <name>OpenSpritz-Android</name>
    <comment></comment>
    <projects>
    </projects>
    <buildSpec>
    </buildSpec>
    <natures>
    </natures>
</projectDescription>

如何使用Gradle为Android项目生成Eclipse和Intellij项目文件

.2. Put 2 Eclipse . "dot" files into modules into /OpenSpritz-Android/app/src/main and /OpenSpritz-Android/lib/src/main

0.2。放2个Eclipse。将“dot”文件分成模块放入/ OpenSpritz -Android / app / src / main和/ OpenSpritz -Android / lib / src / main

如何使用Gradle为Android项目生成Eclipse和Intellij项目文件

.project

。项目

<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
    <name>OpenSpritz-Android-app</name>
    <comment></comment>
    <projects>
    </projects>
    <buildSpec>
        <buildCommand>
            <name>com.android.ide.eclipse.adt.ResourceManagerBuilder</name>
            <arguments>
            </arguments>
        </buildCommand>
        <buildCommand>
            <name>com.android.ide.eclipse.adt.PreCompilerBuilder</name>
            <arguments>
            </arguments>
        </buildCommand>
        <buildCommand>
            <name>org.eclipse.jdt.core.javabuilder</name>
            <arguments>
            </arguments>
        </buildCommand>
        <buildCommand>
            <name>com.android.ide.eclipse.adt.ApkBuilder</name>
            <arguments>
            </arguments>
        </buildCommand>
    </buildSpec>
    <natures>
        <nature>com.android.ide.eclipse.adt.AndroidNature</nature>
        <nature>org.eclipse.jdt.core.javanature</nature>
    </natures>
</projectDescription>

.classpath

的.classpath

<?xml version="1.0" encoding="UTF-8"?>
<classpath>
    <classpathentry kind="src" path="java"/>
    <classpathentry kind="src" path="gen"/>
    <classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>
    <classpathentry exported="true" kind="con" path="com.android.ide.eclipse.adt.LIBRARIES"/>
    <classpathentry exported="true" kind="con" path="com.android.ide.eclipse.adt.DEPENDENCIES"/>
    <classpathentry kind="output" path="bin/classes"/>
</classpath>

.3. Import as Existing Android Code into Workspace

0.3。作为现有Android代码导入Workspace

如何使用Gradle为Android项目生成Eclipse和Intellij项目文件

you can then browse code in familiar way, but even after that you won't be able to run with Eclipse ADT.

然后,您可以以熟悉的方式浏览代码,但即使在此之后,您也无法使用Eclipse ADT运行。

.4.

0.4。

Now you can run build and tasks with gradle CLI or Nodeclipse/Enide Gradle for Eclipse (marketplace)

现在,您可以使用gradle CLI或Nodeclipse / Enide Gradle for Eclipse(市场)运行构建和任务

如何使用Gradle为Android项目生成Eclipse和Intellij项目文件

discuss at https://github.com/Nodeclipse/nodeclipse-1/issues/148

在https://github.com/Nodeclipse/nodeclipse-1/issues/148讨论

#3


2  

The following worked for me

以下对我有用

eclipse.classpath.plusConfigurations += configurations.compile

eclipse.classpath.file {
    beforeMerged { classpath ->
    classpath.entries.removeAll() { c -> 
        c.kind == 'src'
    }
}

withXml {
    def node = it.asNode()

    node.appendNode('classpathentry kind="src" path="src/main/java"')
    node.appendNode('classpathentry kind="src" path="src/debug/java"')
    node.appendNode('classpathentry kind="src" path="gen"')

    node.children().removeAll() { c ->
        def path = c.attribute('path')
        path != null && (
                path.contains('/com.android.support/support-v4')
                )
    }
  }
}

eclipse.project {
   name = 'AndroidGradleBasic'

   natures 'com.android.ide.eclipse.adt.AndroidNature'
   buildCommand 'com.android.ide.eclipse.adt.ResourceManagerBuilder'
   buildCommand 'com.android.ide.eclipse.adt.PreCompilerBuilder'
   buildCommand 'com.android.ide.eclipse.adt.ApkBuilder'
}

Source - http://blog.gouline.net/2013/11/02/new-build-system-for-android-with-eclipse/

来源 - http://blog.gouline.net/2013/11/02/new-build-system-for-android-with-eclipse/

Sample - https://github.com/krishnaraj/oneclipboard

示例 - https://github.com/krishnaraj/oneclipboard

#4


2  

There are four issues with the combination of the Gradle plugins 'com.android.application' and 'eclipse': First, the configuration classpaths are not added to Eclipse's classpath, but this is easy to fix. Second, something must be done about the .AAR-dependencies. This was a bit trickier. Third, we need to include the generated sources for things like R.java. Finally, we need to include Android.jar itself.

Gradle插件'com.android.application'和'eclipse'的组合有四个问题:首先,配置类路径没有添加到Eclipse的类路径中,但这很容易修复。其次,必须对.AAR依赖关系做些什么。这有点棘手。第三,我们需要包含像R.java这样的生成源。最后,我们需要包含Android.jar本身。

I was able to hack together a gradle configuration that would generate proper .classpath files for Eclipse from an Android Studio build.config. The effects were very satisfying to my CPU fan, which had been running constantly with Android Studio. Eclipse sees the resulting project as a fully functional Java project, but only that.

我能够破解一个gradle配置,它可以从Android Studio build.config为Eclipse生成正确的.classpath文件。我的CPU风扇效果非常令人满意,它一直在Android Studio上运行。 Eclipse将生成的项目视为一个功能齐全的Java项目,但仅限于此。

I ended up putting the following directly in build.gradle in my app-project:

我最终将以下内容直接放在我的app-project中的build.gradle中:

apply plugin: 'eclipse'
eclipse {
    pathVariables 'GRADLE_HOME': gradle.gradleUserHomeDir, "ANDROID_HOME": android.sdkDirectory 
    classpath {
        plusConfigurations += [ configurations.compile, configurations.testCompile ]

        file {
            beforeMerged { classpath ->
                classpath.entries.add(new org.gradle.plugins.ide.eclipse.model.SourceFolder("src/main/java", "bin"))
                // Hardcoded to use debug configuration
                classpath.entries.add(new org.gradle.plugins.ide.eclipse.model.SourceFolder("build/generated/source/r/debug", "bin"))
                classpath.entries.add(new org.gradle.plugins.ide.eclipse.model.SourceFolder("build/generated/source/buildConfig/debug", "bin"))
            }

            whenMerged { classpath ->
                def aars = []
                classpath.entries.each { dep ->
                    if (dep.path.toString().endsWith(".aar")) {
                        def explodedDir = new File(projectDir, "build/intermediates/exploded-aar/" + dep.moduleVersion.group + "/" + dep.moduleVersion.name + "/" + dep.moduleVersion.version + "/jars/")
                        if (explodedDir.exists()) {
                            explodedDir.eachFileRecurse(groovy.io.FileType.FILES) {
                                if (it.getName().endsWith("jar")) {
                                    def aarJar = new org.gradle.plugins.ide.eclipse.model.Library(fileReferenceFactory.fromFile(it))
                                    aarJar.sourcePath = dep.sourcePath
                                    aars.add(aarJar)
                                }
                            }
                        } else {
                            println "Warning: Missing " + explodedDir
                        }
                    }
                }
                classpath.entries.removeAll { it.path.endsWith(".aar") }
                classpath.entries.addAll(aars)

                def androidJar = new org.gradle.plugins.ide.eclipse.model.Variable(
                    fileReferenceFactory.fromPath("ANDROID_HOME/platforms/" + android.compileSdkVersion + "/android.jar"))
                androidJar.sourcePath = fileReferenceFactory.fromPath("ANDROID_HOME/sources/" + android.compileSdkVersion) 
                classpath.entries.add(androidJar)
            }
        }
    }
}

// We need build/generated/source/{r,buildConfig}/debug to be present before generating classpath
//  This also ensures that AARs are exploded 
eclipseClasspath.dependsOn "generateDebugSources"


// Bonus: start the app directly on the device with "gradle startDebug"
task startDebug(dependsOn: "installDebug") << {
    exec {
        executable = new File(android.sdkDirectory, 'platform-tools/adb')
        args = ['shell', 'am', 'start', '-n', android.defaultConfig.applicationId + '/.MainActivity']
    }
}

Run gradle eclipse and you will have an Eclipse-project that can be imported and compiled. However, this project acts as a normal Java-project. In order to build the apk, I have to drop back to gradle command line and execute gradle installDebug. gradle processDebugResources picks up changes in Android XML files and regenerates the files under build/generated/source. I use the "monitor" program with Android SDK to view the app logs. I have so far not found any way to debug without Android Studio.

运行gradle eclipse,你将拥有一个可以导入和编译的Eclipse项目。但是,此项目充当普通的Java项目。为了构建apk,我必须回退到gradle命令行并执行gradle installDebug。 gradle processDebugResources获取Android XML文件中的更改并重新生成build / generated / source下的文件。我使用Android SDK的“监视器”程序来查看应用程序日志。到目前为止,我没有找到任何方法来调试没有Android Studio。

The only features I miss from Android Studio are debugging (but who has time for bugs!) and editing resources visually.

我想念Android Studio的唯一功能是调试(但是谁有时间查看错误!)并直观地编辑资源。

#5


1  

Maybe I'm missing something very obvious, but isn't the eclipse plugin what you're looking for? This generates a .project file as well as a .classpath file and updates these as needed when run subsequent times.

也许我错过了一些非常明显的东西,但不是你想要的eclipse插件吗?这将生成.project文件以及.classpath文件,并在后续运行时根据需要更新这些文件。

There is also an IDEA plugin, though I haven't used this one and can't speak to it.

还有一个IDEA插件,虽然我还没有使用过这个插件而且无法说话。

Hope that helps.

希望有所帮助。

#6


0  

I've created a new Gradle plugin that generates the appropriate Eclipse .project and .classpath files, based on the answer provided by Johannes Brodwall on this stack overflow.

我已经创建了一个新的Gradle插件,它根据Johannes Brodwall在此堆栈溢出时提供的答案生成相应的Eclipse .project和.classpath文件。

See https://github.com/greensopinion/gradle-android-eclipse for details.

有关详细信息,请参阅https://github.com/greensopinion/gradle-android-eclipse。