How to Integrate Eclipse with Gradle in Android Project

Setup Standard Android Project using Gradle in Eclipse

You can't import Gradle-based Android projects into Eclipse using Eclipse's Gradle support. Android Gradle projects use a lot of custom infrastructure in the Android plugin that Eclipse's Gradle support doesn't understand. Most importantly, Android doesn't use Java sourceSets.

There's no simple answer for this. To be able to use Gradle-based Android projects in Eclipse, someone will have to write an Eclipse plugin for it, which won't be a trivial task.

If you want to use the same project in both Android Studio and Eclipse, you'll need to set up a normal Eclipse project for it and bypass Gradle altogether on the Eclipse side. You'll have to keep the project in Eclipse-style directory structure, which it looks like you've already done based on how you set up your sourceSets.

As for your second question, you've crossed Gradle versions with Android-Gradle-plugin versions. The latest version of Gradle is 1.12; the latest version of the plugin is 0.10.2.

Adding Gradle support to Eclipse

Do not use Eclipse for android developing, it's slow and lack of tools.
Also Eclipse support has ended by Google officially.

With the release of Android Studio 2.2, the time has now come to say goodbye to the Eclipse Android Developer Tools. We have formally ended their support and development. There's never been a better time to switch to Android Studio and experience the improvements we've made to the Android development workflow.

Source

This documentation is very helpful to migrate Android Studio from Eclipse. We cannot help without which migrating problems did you encountered while the process.

Also there are useful links for you:

Gradle Integration for Eclipse

Gradle Plugin Documentation for Eclipse

Useful Gradle Tutorial for Eclipse - This one has the instructions needed in section 5. After completing section 5 with running gradle init, you should right click the project -> Gradle -> Refresh Gradle Project. Then the new Gradle files be added to your project.

Using gradle project in both Eclipse and IDEA

You'll have to maintain the build separately in both Gradle and Eclipse; Eclipse can't use the Android Gradle builder natively. It's on our list of things to implement but we don't have a roadmap for it yet.

Things will go easiest if you use an Eclipse-like directory structure and adapt the Gradle build file to work with it. See Maintaining directory structure during Android Studio import for advice.

Add dependencies via gradle to eclipse in android project

My solution is based off Rafael's in that it copies dependencies to the libs directory which is only used by Android. However I go further to completely explode the referenced AAR's for use in Eclipse.

Gradle Build File

Add the following to the end of your Android projects build.gradle :

task copyJarDependencies(type: Copy) {
description = 'Used for Eclipse. Copies all dependencies to the libs directory. If there are any AAR files it will extract the classes.jar and rename it the same as the AAR file but with a .jar on the end.'
libDir = new File(project.projectDir, '/libs')
println libDir
println 'Adding dependencies from compile configuration'
configurations.compile.filter {it.name.endsWith 'jar'}.each { File file -> moveJarIntoLibs(file)}
println 'Adding dependencies from releaseCompile configuration'
configurations.releaseCompile.filter {it.name.endsWith 'jar'}.each { File file -> moveJarIntoLibs(file)}
println 'Adding dependencies from debugCompile configuration'
configurations.debugCompile.filter {it.name.endsWith 'jar'}.each { File file -> moveJarIntoLibs(file)}
println 'Adding dependencies from instrumentTestCompile configuration'
configurations.instrumentTestCompile.filter {it.name.endsWith 'jar'}.each { File file -> moveJarIntoLibs(file)}
println 'Extracting dependencies from compile configuration'
configurations.compile.filter {it.name.endsWith 'aar'}.each { File file -> moveAndRenameAar(file) }
println 'Extracting dependencies from releaseCompile configuration'
configurations.releaseCompile.filter {it.name.endsWith 'aar'}.each { File file -> moveAndRenameAar(file) }
println 'Extracting dependencies from debugCompile configuration'
configurations.debugCompile.filter {it.name.endsWith 'aar'}.each { File file -> moveAndRenameAar(file) }
println 'Extracting AAR dependencies from instrumentTestCompile configuration'
configurations.instrumentTestCompile.filter {it.name.endsWith 'aar'}.each { File file -> moveAndRenameAar(file) }
}

void moveJarIntoLibs(File file){
println 'Added jar ' + file
copy{
from file
into 'libs'
}
}

void moveAndRenameAar(File file){
println 'Added aar ' + file
def baseFilename = file.name.lastIndexOf('.').with {it != -1 ? file.name[0..<it] : file.name}

// directory excluding the classes.jar
copy{
from zipTree(file)
exclude 'classes.jar'
into 'libs/'+baseFilename
}

// Copies the classes.jar into the libs directory of the expoded AAR.
// In Eclipse you can then import this exploded ar as an Android project
// and then reference not only the classes but also the android resources :D
copy{
from zipTree(file)
include 'classes.jar'
into 'libs/' + baseFilename +'/libs'
rename { String fileName ->
fileName.replace('classes.jar', baseFilename + '.jar')
}
}
}

Building with Gradle

Run :

"gradle clean build"

You should find all dependencies and exploded AARs in your libs directory. This is all Eclipse should need.

Importing in Eclipse

Now this is where the real benefit begins. After you've generated the libs directory from the gradle step above you'll notice there are folders in there too. Those new folders are the exploded AAR dependencies from your build.gradle file.

Now the cool part is that when you import your existing Android project into Eclipse it will also detect the exploded AAR folders as projects it can import too!

1. Import those folders under your project's libs directory, don't import any 'build' folders, they're generated by Gradle

2. Ensure you perform a Project -> Clean on all AAR projects you've added. In your workspace check that each AAR exploded project has the following in the project.properties :

target=android-<YOUR INSTALLED SKD VERSION GOES HERE>
android.library=true

3. Now in your main Android project you can just add the library references with either ADT or you can just edit the project.properties file and add

android.libraries.reference.1=libs/someExplodedAAR/

4. Now you can right-click on your main Android project and Run as -> Android Application.

But what does this even mean?

  1. Well it means you don't need the source code for any of your Android AAR Gradle dependencies in order to reference both it's classes and resources in Eclipse.

  2. The gradle build script above takes the AAR file and prepares it for use in Eclipse. Once you add it to your workspace you're ready to just focus on your actual main Android project.

  3. You can now debug and develop using Eclipse and deploy using ADT with AAR dependencies being properly bundled in the APK. When you need to make some specific builds then you can use gradle.



Related Topics



Leave a reply



Submit