简体   繁体   中英

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

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);
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM