Java – How to resolve unknown classes and fail to parse symbols in Android Studio java library modules

How to resolve unknown classes and fail to parse symbols in Android Studio java library modules… here is a solution to the problem.

How to resolve unknown classes and fail to parse symbols in Android Studio java library modules

Using Android Studio and creating a java library module as part of a subproject, I get an error in the following java statement:

javaFile.writeTo(System.out);

It says that the symbol “writeTo” and the unknown class “System.out” cannot be resolved.

This is the gist of the source code class

import com.squareup.javapoet.JavaFile;
import com.squareup.javapoet.MethodSpec;
import com.squareup.javapoet.TypeSpec;

import javax.lang.model.element.Modifier;

public class MyClass {

...

JavaFile javaFile = JavaFile.builder("com.foobar.helloworld", helloWorld)
        .build();

javaFile.writeTo(System.out);
}

Solution

It’s hard to say if you don’t know exactly what your exception is, but when I test the submodule annotation processor, I pop something similar. When linking the processor to my main module, I get a java.lang.NoClassDefFoundError: com/squareup/javapoet/MethodSpec. I’m using android-apt to get the annotation processor to work, so in my build.config my dependencies have apt config definitions:

dependencies {
    ...

apt 'com.squareup:javapoet:1.7.0' // <- Note that since my Processor used javapoet, I had to include Javapoet in the main module's apt dependency as well as my processor jar
    apt files('.. /processor/build/libs/processor.jar')
}

Once I included the dependencies mentioned above, then everything works fine. Not sure if this applies to your case without more details, but that got me back on track.

Related Problems and Solutions