How to use common source code across multiple IntelliJ projects?

Viewed 1161

I'm currently looking to start two new projects using IntelliJ - one which is an Android project and another that is just a standard Java project.

I want to use a lot of the same code within both projects, but don't want to copy files into projects. At a later stage, it is likely that this code will be used in other projects too.

At the moment I'm having difficulty working out how to include the common code within both projects - I have created new modules but then it seems that the import does not recognize code from the module.

Therefore the structure of directories would be something like:

|- Android Project
|- Java Project
|- Android Library
|- Java Library
|- Common Library

So you would have:

  • "Android Project" uses code from "Android Library" and "Common Library" directories
  • "Java Project" uses code from "Java Library" and "Common Library" directories
  • "Android Library" uses code from "Common Library" directory
  • "Java Library" uses code from "Common Library" directory

Could someone please advise as to how this is to be configured in IntelliJ?

In the case of the Android project, I am playing around with the module settings and dependencies and can get it to work briefly, but then these changes get lost whenever there is a Gradle sync.

Thanks.

1 Answers
Related