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

前端 未结 6 1776
没有蜡笔的小新
没有蜡笔的小新 2020-12-02 13:30

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 PlayFra

6条回答
  •  -上瘾入骨i
    2020-12-02 14:07

    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.

提交回复
热议问题