36

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

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

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

这是我的 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')
    }
}

然后我运行命令:

gradle clean build cleanEclipse eclipse

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

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

这是 Gradle 的优先功能吗?

- 更新 -

我相信这是一个问题。因此,我已将其发布到 Android 工具团队问题 #57668。请给它加星标,让他们优先考虑这个问题:)

- 更新 -

Android 工具团队似乎没有调查此问题。所以现在我已经转换到Android Studio,我可以通过 IDE 导入我的 gradle 项目和依赖项。

4

6 回答 6

9

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

所有功能都使用插件启用。传统上,构建插件不会生成项目结构。这是项目特定工具的工作。Gradle 的 Android 插件遵循这一点。

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

在 Maven 等工具中有一个原型概念,它允许使用项目模板。Gradle 尚不支持该功能(已为此功能提出请求)。

参考这个线程: http: //issues.gradle.org/browse/GRADLE-1289,一些用户提供了脚本来生成结构。

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

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

更新

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

于 2013-07-12T06:30:08.880 回答
3

Gradle 插件 'com.android.application' 和 'eclipse' 的组合存在四个问题: 首先,配置类路径没有添加到 Eclipse 的类路径中,但这很容易修复。其次,必须对 .AAR 依赖项采取一些措施。这有点棘手。第三,我们需要为 R.java 之类的东西包含生成的源代码。最后,我们需要包含 Android.jar 本身。

我能够破解一个 gradle 配置,该配置.classpath将从 Android Studio 为 Eclipse 生成适当的文件build.config。效果让我的 CPU 粉丝非常满意,一直在 Android Studio 上运行。Eclipse 将生成的项目视为功能齐全的 Java 项目,但仅此而已。

我最终将以下内容直接放在我的应用程序项目的 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']
    }
}

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

我从 Android Studio 中错过的唯一功能是调试(但谁有时间处理错误!)和可视化编辑资源。

于 2016-03-21T10:45:31.850 回答
2

正如 Android 团队在问题 57668中回答的那样(由@arcone 提出)

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

eclipse插件与android插件不兼容。

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

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

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

现在使用 Eclipse 可以做到的是

.1。将项目作为一般项目导入

.project

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

导入-android-gradle-as-general-project

.2. 放 2 日食。“点”文件到模块/OpenSpritz-Android/app/src/main/OpenSpritz-Android/lib/src/main

添加日食文件

.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

<?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. 作为现有 Android 代码导入 Workspace

结果

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

.4.

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

开始

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

于 2014-04-15T06:29:42.660 回答
2

以下对我有用

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

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

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

于 2014-05-10T11:42:35.623 回答
2

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

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

于 2017-04-23T00:41:37.280 回答
1

也许我遗漏了一些非常明显的东西,但是你正在寻找的不是eclipse 插件吗?这会生成一个 .project 文件和一个 .classpath 文件,并在后续运行时根据需要更新这些文件。

还有一个IDEA 插件,虽然我没有使用过这个插件,也不能和它说话。

希望有帮助。

于 2013-07-13T08:06:30.287 回答