Apollo Android Client – Cannot access generated classes on classpath

Apollo Android Client – Cannot access generated classes on classpath


4

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

Apollo Android Client - Cannot access generated classes on classpath

6

  • Why are you trying to edit or reference build/generated source code?

    – OneCricketeer

    May 23, 2017 at 17:58


  • I'm not trying to edit it, I'm trying to access the generated class. As in import the class.

    – Michael.

    May 23, 2017 at 17:59

  • Your app/build.gradle isn't compiling that as a dependency. I don't think you can import plugins within your code (at least I've never tried)

    – OneCricketeer

    May 23, 2017 at 18:01


  • Um, that doesn't look right. There should be package directories inside of build/generated/source/apollo/. What is the contents of your src/main/graphql/ directory? If it is just the GraphQL document file directly, create a set of subdirectories matching your desired Java package for the generated code, and move the GraphQL file into there.

    – CommonsWare

    May 23, 2017 at 18:02


  • @CommonsWare BTW, github.com/michaeljq/graphQlMCVE/tree/master/app/src/main/…

    – OneCricketeer

    May 23, 2017 at 18:06

3 Answers
3


16

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).

4

  • I did this but I still don't see the generated .java files in my project. I do see them in the build/generated/source/apollo/mypackage directory though. I still can't reference them from IDEA. What can be the problem?

    – Adam Arold

    Mar 17, 2018 at 15:08


  • @AdamArold: I don't use IDEA. Somehow, you need to teach it that build/generated/source/apollo/ has sources to be included in your project.

    – CommonsWare

    Mar 17, 2018 at 15:22

  • I think it is better to copy them over to the src/main/java folder, or not?

    – Adam Arold

    Mar 17, 2018 at 16:44


  • Well, they will get regenerated when you change your GraphQL schema, your GraphQL queries, or upgrade Apollo-Android. So, IMHO, it is better to teach IDEA to pull from the generated source directory. Otherwise, you will forget to manually copy them again.

    – CommonsWare

    Mar 17, 2018 at 16:47


0

Here is a screenshot to understand the solution faster:

Apollo Android Client - Cannot access generated classes on classpath

2

  • this is an incorrect diagram and didn't worked for me. what commonsware said was to create subfolders similar to our current architecture hierarchy . this folder system worked for me

    – ansh sachdeva

    Oct 11, 2020 at 9:22

  • @Andrii-Kovalchuk : what if internal module(app in above img) is ant project and external prj(GraphQlAndroidInFull) is in gradle. How we should generate build folder inside internal module in this case. I am using apollo client 3.1.0

    – ridersingh92

    Feb 16, 2022 at 11:41


0

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.



Leave a Reply

Your email address will not be published. Required fields are marked *