Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Gradle to generate Eclipse and Intellij project files for Android projects

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

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

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

This is my build.gradle file:

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.

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

Is this a prioritized feature by 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 :)

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

like image 970
Jan-Terje Sørensen Avatar asked Jul 04 '13 12:07

Jan-Terje Sørensen


People also ask

How do I sync a project with Gradle files in IntelliJ?

There is a useful shortcut Ctrl + Shift + A , after which a window appears and you can type some command that you would like IntelliJ to run. Just type "sync" in there and it will find that command Sync Project with Gradle Files, even if it's not visible in the menu.

How do I project a Gradle project in IntelliJ?

Open your project in IntelliJ IDEA. In the Project tool window, right-click the name of your project and select New | File. In the dialog that opens enter build. gradle and click OK.

Does Eclipse work with Gradle?

Buildship is an Eclipse plugin that allows you to build applications and libraries using Gradle through your IDE.


2 Answers

Gradle itself is a generic build tool, it is not created specifically for 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.

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.

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

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

Build Initialization feature is in progress: 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

Update

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

like image 168
S.D. Avatar answered Oct 10 '22 04:10

S.D.


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.

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.

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

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.

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

like image 23
Johannes Brodwall Avatar answered Oct 10 '22 05:10

Johannes Brodwall