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

时间:2013-07-04 12:52:10

标签: android eclipse ide intellij-idea gradle

是否可以使用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工具团队issue #57668。请为他们加注星标以确定问题的优先顺序:)

- 更新 -

看起来Android工具团队并未考虑此问题。所以现在我已经转换为Android Studio,我可以通过IDE导入我的gradle项目和依赖项。

6 个答案:

答案 0 :(得分:9)

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

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

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

Maven等工具中有archetypes概念,允许使用项目模板。 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。

答案 1 :(得分:3)

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

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

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

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

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

答案 2 :(得分:2)

由Android团队Issue 57668回答(由@arcone提出)

  

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

     

eclipse插件与android插件不兼容。

     

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

     

要使它在Eclipse中运行,我们将不得不更改Eclipse的Gradle插件,就像我们在IntelliJ中修改Gradle支持一样

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

现在Eclipse的可能性是

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

.project

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

import-android-gradle-as-general-project

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

add-eclipse-files

.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>

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

results

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

0.4。

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

start

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

讨论

答案 3 :(得分: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

答案 4 :(得分:2)

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

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

答案 5 :(得分:1)

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

还有一个IDEA plugin,虽然我没有使用过这个但不能说话。

希望有所帮助。