Enide Studio

Gradle for Eclipse - Android

Back to Android page

Make Android Eclipse project ready for Android Studio, yet keeping it accessible for Eclipse use too

In short: use src/main/java.

Welcome to raise issue on GitHub.

  1. Create project in Eclipse
  2. In .classpath change src value into src/main/java, that is
    <classpathentry kind="src" path="src/main/java"/>
    ( Your project has red ! by now)
  3. Create folder main/java under src
  4. Move source under src/main/java, e.g. move com from src into src/main/java

    Now project compiles well.

  5. Way 1: add build.gradle, e.g. with such minimal content:

     buildscript {
         repositories {
             mavenCentral()
         }
         dependencies {
             classpath 'com.android.tools.build:gradle:1.1.+'
         }
     }
     apply plugin: 'com.android.application'
    	
     dependencies {
         compile fileTree(dir: 'libs', include: '*.jar', exclude: 'android-support-*.jar')
     }
    	
     android {
         compileSdkVersion 22
         buildToolsVersion "22"
    	
         sourceSets {
             main {
                 manifest.srcFile 'AndroidManifest.xml'
                 res.srcDirs = ['res']
                 assets.srcDirs = ['assets']
             }
    	
             androidTest.setRoot('tests')
         }
     }
    

    Note that with gradle you are to specify buildToolsVersion. If you want to specify for Eclipse ADT (e.g. to make them similar) in project.properties add line like sdk.buildtools=19.1.0. ref

  6. Way 2: Right-click on the project Export… -> Android / Generate Gradle build files.

    In wizard select only project (and its dependencies except for Android support libraries)

    This will add build.gradle build file and wrapper files: gradlew, gradlew.bat and gradle/wrapper folder.

    Check/correct that your sourceSets section is like below:

     sourceSets {
         main {
             manifest.srcFile 'AndroidManifest.xml'
             res.srcDirs = ['res']
             assets.srcDirs = ['assets']
         }
     }
    

    Hint 1: when you Gradle build file becomes long, define some modules and put them into gradle folder, then inside build.gradle use
    apply from: 'gradle/feature-module.gradle'

    Hint 2: For Eclipse project with dependencies build.gradle will be added in every of them, and main project will get settings.gradle. Instead of Android support library add compile 'com.android.support:appcompat-v7:19.0.+'

  7. (optional) If you are using git, add .gitignore with content:

     /gen/
     /bin/
     /.gradle/
     /build/
     /gradle-app.setting	
     .*.md.html	
     .DS_Store
     .idea/workspace.xml
     /local.properties
    
  8. In Android Studio File -> Import Project… and select locate build.gradle from the project.

Now you can apply power of Gradle in Eclipse with ADT and/or Android Studio

Refs: 1, 2

comments powered by Disqus