Java – sharing POJO entity data classes between Android and spring projects
How to reference, add and link without copy / paste, depending on Java classes defined in different projects or libraries?
For:
> Android Studio > IntelliJ IDEA
resolvent:
Android Studio
AndroidProjectRoot / settings.gradle
before
include ':app'
after
include ':app', ':common'
project(':common').projectDir = new File('../common')
Android projectroot / build.gradle
before
apply plugin: 'com.android.application'
android {
...
}
dependencies {
}
after
apply plugin: 'com.android.application'
android {
...
}
dependencies {
compile project(':common')
}
And then
>Tools – > Android – > synchronize projects using gradle files
Allow Android studio projects to reference external projects (at the same directory level as Android projectroot /) without copying Java libraries in Android projects
Library plan / module
You need a basic build.gradle for a library / module. The following is enough. Dependencies are only for example (only used when appropriate) in case your module is a pile of objects handled by a Dao like ormlite
Library project root / build.gradle
apply plugin: 'java'
sourceCompatibility = JavaVersion.VERSION_1_7
targetCompatibility = JavaVersion.VERSION_1_7
repositories {
mavenCentral()
}
dependencies {
compile 'com.j256.ormlite:ormlite-core:4.48'
compile 'com.j256.ormlite:ormlite-android:4.48'
compile 'com.j256.ormlite:ormlite-jdbc:4.48'
}
IntelliJ IDEA
Reference library as module
>File – > project structure > (left column) select "module" > (middle column, upper left corner) click button > import module > select the top-level directory containing Java libraries, such as common (see note below) > click "OK"
Add dependencies so that you can use the import statement
>(middle column, module list) select the main module (not the module you just added) > (rectangular area on the right, lower left corner) click the button > select 3 module dependencies... > select the module you added (e.g. common) > click OK > click OK (close the project structure) > idea should now rebuild gradle and add your library as a module to the project structure, Next to the name is a folder icon (with a small blue square)
be careful:
If your library structure is as follows:
Common / common / build.gradle common / SRC directory public / SRC / Java public / SRC / Java / main common / SRC directory / Java / main / COM common / SRC directory / Java / main / COM / your common / SRC directory / Java / main / COM / you / package general / SRC directory / Java / main / COM / you / package / yourclass.java
Further reading
Mathias Hauser – Spring Boot JPA Multiple Projects