redhat-developer / vscode-java

Java Language Support for Visual Studio Code
Eclipse Public License 2.0
2.05k stars 420 forks source link

Android gradle project do not work - Eclipse build.gradle script #3682

Open hitabi opened 1 week ago

hitabi commented 1 week ago

Trying to get vscode to detect Android Project but it does not seem to work. With java.jdt.ls.androidSupport.enabled set to on it should automatically detect Android Project, add necessary files and classpath in Project and External Dependencies automatically but after many attempts none of the solutions seem to work. Android support should be available from here (https://github.com/eclipse-jdtls/eclipse.jdt.ls/pull/2197) . This introduces a init.gradle file that should automatically add the Android Sourcesets, add classpath, all other android dependencies but I do not think that ever runs.

Here are my steps and partial temporary solution I came up with.

Steps:

  1. Create a new Android Project using Android Studio using Java and Groovy Gradle.
  2. Move over to vscode. Install the plugin pack, enable vscode setting java.jdt.ls.androidSupport.enabled to on. (I have all other necessary setups such as having jdk installed.)
  3. After that by default vscode setting java.import.gradle.enabled is set to enable so it detects the gradle projects. So far the error in MainActivity.java is not on the classpath of project app.
  4. Using Add Folder to Java Source Path do not work as it says Unsupported operation. Please use build.gradle file to manage the source directories of gradle project. To resolve it I added this to app/build.gradle file:
    apply plugin: 'eclipse'
    eclipse {
    classpath {
        containers 'org.eclipse.buildship.core.gradleclasspathcontainer'
        file.whenMerged { cp ->
            def entries = cp.entries
            def src = new org.gradle.plugins.ide.eclipse.model.SourceFolder('src/main/java', null)
            entries.add(src)
        }
    }
    }
  5. Now it fails to get definitions for android and androidx. Errors: The import android cannot be resolved and The import androidx cannot be resolved. Still nothing inside Project and External Dependencies even after running gradle build.
  6. Add android definions manually inside eclipse build.gradle file
    def jarfile = new org.gradle.plugins.ide.eclipse.model.Library(fileReferenceFactory.fromPath("${android.sdkDirectory}/platforms/" + android.compileSdkVersion + "/android.jar"))
    jarfile.sourcePath = fileReferenceFactory.fromPath("${android.sdkDirectory}/sources/" + android.compileSdkVersion)
    entries.add(jarfile)

    It gives definions for android imports such as import android.os.Bundle; but now the problem is The import androidx.appcompat cannot be resolved .

I am looking for a solution that either gives me type definions for androidx.appcompat and other necessary dependencies. Or ways to get the plugin to detect and configure Android Project automatically. Or way to manually run the init.gradle file.