Apollo Android Client - Cannot access generated classes on classpath

4.6k views Asked by At

I've generated the Apollo classes successfully and can see them in the build directory, however they're not available on the classpath. Strangely the Enum that is generated is available but the classes themselves aren't.

Running the sample project provided on Apollo's Github does work but I cant see the difference between the configurations.

mcve below.

https://github.com/michaeljq/graphQlMCVE

enter image description here

3

There are 3 answers

4
CommonsWare On BEST ANSWER

Right now, you have src/main/graphql/ containing GraphQL documents. That means that your Java classes will be generated with no Java package, which probably is not what you want.

Instead, create a package tree under src/main/graphql/ (e.g., src/main/graphql/apollotest/mq/apollotest/api/). Move the GraphQL documents and schema.json there. Clean the project, and you should find that your Apollo-generated classes are available to you in whatever Java package you chose (e.g., apollotest.mq.apollotest.api in the above example).

0
Binish Manandhar On

In my case, I had forgotten to include the package name in app level build.gradle file

apollo {
    // instruct the compiler to generate Kotlin models
    generateKotlinModels.set(true)
    packageNamesFromFilePaths("com.example.your_package_name")
}

After this I didn't need to create the subfolders inside graphql folder.

2
Andrii Kovalchuk On

Here is a screenshot to understand the solution faster: Project structure