How to resolve unknown class and can not resolve symbol in Android Studio java library module

3.2k Views Asked by At

Using Android Studio and creating a java library module as part of a sub project I get an error on the following java statement:

javaFile.writeTo(System.out);

and it complains of can not resolve symbol 'writeTo' and unknown class 'System.out'.

Here's 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);
}
1

There are 1 best solutions below

0
On

A bit hard to say without knowing exactly what your exception was, but I had something similar pop up when I was testing out a sub-module annotation processor. When linking the processor to my main module, I was getting a java.lang.NoClassDefFoundError: com/squareup/javapoet/MethodSpec. I was using android-apt to get the annotation processor working, so in my build.config my dependencies had the apt configuration definition:

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 above noted dependency, then all worked fine. Not sure if this applies to your situation without more details, but this got me back on track.