Open codewithtoucans opened 1 month ago
If someone could help me, I would be extremely grateful. 😃
Having exactly the same issue, seems that jdtls is not able to get the classpath from the gradle project if it is using the android gradle plugin or something. Manually adding jars from the android dependencies (extracting the .aar files to get the classes.jar file) to the .classpath file I managed to get some autocomplete, but not all. Also enabling the setting to enable android project support didn't have any effect either
@codewithtoucans I finally managed to get it to work, but as the gradle classpath resolver is not working properly we need to workaround it adding the necessary jars from the android sdk and dependencies to the .classpath file in the app module. Here is my raw setup, but beware that some tweaking is needed depending on the project (adjusting the sdk version, configured variants, sdk path...):
Add this task to the app build.gradle.kts, which is used to get the compile classpath for the declared dependencies:
tasks.register("printCompileClasspath") {
doLast {
println("---START---")
configurations.getByName("debugCompileClasspath").files.forEach { file ->
println(file.absolutePath)
}
println("---END---")
}
}
And then run this script to fill the .classpath file:
import subprocess
import os
result = subprocess.run(['./gradlew', 'app:printCompileClasspath'], stdout=subprocess.PIPE).stdout.decode("utf-8")
# Split by lines
lines = result.split('\n')
# Get the lines between ---START--- and ---END--- lines
start = lines.index("---START---") + 1
end = lines.index("---END---")
lines = lines[start:end]
print(lines)
final_lines = []
for line in lines:
if line.endswith('.aar'):
# Get the folder path
folder = os.path.dirname(line)
# Extract the aar in that folder
subprocess.run(['unzip', '-o', line, '-d', folder])
# Add the classes.jar
final_lines.append(os.path.join(folder, 'classes.jar'))
else:
final_lines.append(line)
with open('app/.classpath', 'w') as f:
f.write("""
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="output" path="bin/default"/>
<classpathentry kind="src" path="src/main/java"/>
<classpathentry kind="lib" path="/home/amg/Projects/JavaApp/app/build/intermediates/compile_and_runtime_not_namespaced_r_class_jar/debug/processDebugResources/R.jar"/>
<classpathentry kind="lib" path="/home/amg/SDK/android/platforms/android-34/android.jar"/>
<classpathentry kind="lib" path="/home/amg/SDK/android/platforms/android-34/core-for-system-modules.jar"/>
""")
for line in final_lines:
f.write(f' <classpathentry kind="lib" path="{line}"/>\n')
I haven't configured it yet to support some features (such as viewbinding or buildconfig), but it should be straightforward to add the support for these with this base
JDT-LS relies on Eclipse BuildShip to compute a proper Gradle classpath. Maybe the issue should also be reported at https://github.com/eclipse/buildship/issues ?
In Android project how can I get android code suggestions?
OS: windows 10 Gradle: 6.5 JDK: installed 8, 11 ,17 IDE: Neovim 0.10.1
I set JAVA_HOME and ANDROID_HOME in the system variables, but after building the project and install dependences, no errors, I only get basic JDK code suggestions and no Android code suggestions. Do I need to make any special settings in my lua file, or is there any other configuration needed? I'm a newbie in this area and would appreciate any help! Thanks!
my config