java - Share POJO Entity Data Classes between Android and Spring projects -
how can reference, add, link, depend on java classes defined in different project or library without copy / paste?
for:
- android studio
- intellij idea
android studio
androidprojectroot/settings.gradle
before
include ':app'
after
include ':app', ':common' project(':common').projectdir = new file('../common')
androidprojectroot/app/build.gradle
before
apply plugin: 'com.android.application' android { ... } dependencies { }
after
apply plugin: 'com.android.application' android { ... } dependencies { compile project(':common') }
then...
- tools -> android -> sync project gradle files
allows android studio project reference external project (at same directory level androidprojectroot/, without making copy of java library inside android project.
library project / module
you'll need basic build.gradle library/module. following suffice. dependencies example (only use appropriate), in case module bunch of objects being handled dao 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 module
- file -> project structure
- (left column) select "modules"
- (middle column, top left) click + button
- import module
- select top-level directory contains java library, e.g. common (see note below)
- click ok
add dependency can use import statements
- (middle column, list of modules) select main module (not 1 added)
- (right rectangular area, bottom left) click + button
- select 3 module dependency...
- select added module (e.g. :common)
- click ok
- click ok (closes project structure)
- idea should rebuild gradle , add library module project structure folder icon (with tiny blue square) beside name
note:
if library structured like:
common/
common/build.gradle
common/src
common/src/java
common/src/java/main
common/src/java/main/com
common/src/java/main/com/your
common/src/java/main/com/your/package
common/src/java/main/com/your/package/yourclass.java
Comments
Post a Comment