Share POJO Entity Data Classes between Android and Spring projects - java

How can I reference, add, link, depend on java classes defined in a 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 with Gradle Files
Allows Android Studio project to reference an external project (at the same directory level as AndroidProjectRoot/, without making a copy of the Java Library inside your Android Project.
Library Project / Module
You'll need a basic build.gradle for the Library/Module. The following will suffice. The dependencies are just for example (only use as appropriate), in case your module is a bunch of objects being 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 "Modules"
(Middle Column, top left) Click + button
Import Module
Select top-level directory that contains Java Library, e.g. common (see note below)
Click OK
Add Dependency so you can use import statements
(Middle Column, list of Modules) Select your main module (not the one you just added)
(Right rectangular area, bottom left) Click + button
Select 3 Module Dependency...
Select your added module (e.g. :common)
Click OK
Click OK (closes Project Structure)
IDEA should now rebuild gradle and add your library as a module to the project structure with a folder icon (with tiny blue square) beside the name
Note:
If your Library is 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
Further Reading
Mathias Hauser - Spring Boot JPA Multiple Projects

You need to import the pojo project as a module for the android project:
All you need to do is open one project, and then "Import Module", so you select the other "Project", this way they will be able to see each other resources and codes.
From there it would be as easy as using an import as long as the access modifiers allow you to do such import (public for example)

Related

Unable to add library to Android Studio project

I'm trying to add this library to Android Studio locally using this tutorial on YouTube.
However, I get errors that I can't post here because they're too many (99+).
Can someone tell me step by step (in detail) how to succesfully add this library to my project and be able to edit it afterwards?
This is the main error I get when I try to import this library: `Unable to determine constructor argument #1: missing parameter of type Factory, or no service of type Factory.
My build.gradle:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath "com.android.tools.build:gradle:4.0.0"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.72"
classpath 'com.github.dcendents:android-maven-plugin:1.2'
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.8.4'
classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
Here is your step-by-step guide:
Create an empty project or open your existing project where you want to add this library.
Clone the library project from the git or download the zip and extract it in some other directory.
Now, in Android Studio, go to File -> New -> Import Module.... Select the cloned/extracted library directory. Make sure the :placepicker module is selected for import. Then click Finish.
Now copy two files bintray.gradle and install.gradle from cloned/extracted project root directory to your own project root directory. Add this point your project structure should look like this:
Now open your project level build.gradle. Add ext.kotlin_version = '1.3.72' inside buildscript { and also add following dependencies:
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.8.5'
classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1'
Overall, this will look like:
Finally, go to File -> Sync Project with Gradle Files. It will take some time to download the missing dependencies and you have now successfully integrated the library within your project which is fully editable.
Now to make this library work in your own app module, in the build.gradle file of your app module, add this inside dependencies section:
api project(':placepicker')
Or edit your gradle.build like in the picture:
Step by step description:
Then ceck out the git project to your machine.
Modify and build it with a new Version.
Add the dipendency as Jar from Your local filesystem in Android Studio.
Try this:
File > Project Structure > Dependencies Tab > Add module dependency (scope = compile)
Where the module dependency is the project library Android folder.
In build.gradle(Module.app) file add this.
implementation 'com.google.android.gms:play-services-places:17.0.0'
let me knew if it's work.

Create single AAR file from multiple modules using Gradle in Android Studio [duplicate]

I am building android library project, which has a dependency on another internal library project.
I am wondering if there is a way to package a single AAR library, which already contains internal library inside it. I would like to share only 1 AAR library package to my application developers.
This is how my build.gradle files look currently, but currently they produce separate AAR files and both needs to be included in Application's build.gradle. As application is being built by another company, we need to share the final AAR file with them and not the complete library projects.
----- internalLib -------->>>>>>>>>>
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.7.+'
}
}
apply plugin: 'android-library'
repositories {
mavenCentral()
}
android {
compileSdkVersion 18
buildToolsVersion '18.1.1'
}
dependencies {
compile 'com.android.support:support-v4:18.0.0'
}
----- externalLib --------
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.7.+'
}
}
apply plugin: 'android-library'
repositories {
mavenCentral()
}
android {
compileSdkVersion 18
buildToolsVersion '18.1.1'
}
dependencies {
compile 'com.android.support:support-v4:18.0.0'
compile project(':internalLib')
}
There is no mechanism to combine library. It's a bit complicated as you probably want to control which dependencies get merged (for instance you probably don't want to include support-v4 in there). Also you'd need to merge the resources and Android manifest.
At this time there's no way to easily hack something, unless you are sure the resources have no conflicts between the two res folders (for instance you could have strings_a.xml in one lib and strings_b.xml in the other lib). This way you can just "merge" the two res folders by copying them both into the same location (as opposed to do a merge at the android res level).
For the Manifest it'd be more complicated, but doable with some custom code.
Providing a built-in mechanism for this is very low on our priority so don't expect it anytime soon.
For the sake you have to upload each library as separately on maven and use its implementation in parent library modules till the main library module. Only then when you publish your main library on maven will include your all child dependencies.
As far as we have only one option add aar as api dependency inside the module.
For that we have to generate aar file and publish it to Maven and make it accessible by another module and consume it in app.
https://developer.android.com/studio/projects/android-library
As mentioned above android developer document.
The library module with source code is copied to your project, so you can actually edit the library code. If you want to maintain a single version of the library code, then this is probably not what you want and you should instead add the compiled AAR file as described above.
If there anything else we can do, please let us know by jot down in the command section.
It is not supported
It is not recommended to include one library into another because it leads to a serious issues with managing versions and complexity of creating and supporting such solution.
You should stick to native approaches like dependency manager or rearchitect your codebase
[iOS Umbrella framework]

How can I use an application from another project as a library?

How can I use an application from another project as a library, when that application depends on a library that I am already using?
Here’s what I have:
MyProject
app
libraries
ExoPlayer
demo
library
Here's my current configuration as it pertains to this.
demo/build.gradle:
apply plugin: 'com.android.application'
...
dependencies {
compile project(':library')
}
library/build.gradle:
apply plugin: 'com.android.library'
MyProject/settings.gradle:
include ':libraries:ExoPlayer:library'
MyProject/app/build.gradle:
dependencies {
compile project(':libraries:ExoPlayer:library')
}
As you can see I’m using ExoPlayer library as a library (go figure) but I want to use demo application as a library as well (specifically for the DemoPlayer, I don't want the activities). But demo also has a dependency on library. What do I have to put in my gradle files to achieve this?
I tried to follow the library setup and apply it to demo but it broke the demo build:
Project with path ':library' could not be found in project ':libraries:ExoPlayer:demo'
I have tried following other similar threads such as this one but with no success.
I appreciate any help.
A long time ago I've use Exoplayer as a library project but it's not needed anymore : you can use gradle dependencies :
compile 'com.google.android.exoplayer:exoplayer:r1.4.2'
If you want to persist with library project, you will have to check your settings.gradle where all Android Studio referenced module are in. This file contain the name of module as you will use it after.
Example : (based on AndroidVuMeter)
settings.gradle
include ':app', ':vumeterlibrary'
project structure
Project/
app/
vumeterlibrary/
To use vumeterlibrary you will have to use compile project(':vumeterlibrary') in your app build.gradle

Android Studio reuse module library at other project

I have a module with some POJO classes that is marked at gradle as apply plugin: 'java' .Is there a way to reuse it at another project ? Everything i tried failed . (I dont want to copy pasta it)
I was facing the same issue recently.
This is how I resolved the code redundancy problem:
Create a new Android Studio project 'libs' and add all my APIs in a 'library' module.
In build.gradle of your library module, add code to upload the artifact to local Maven repo.
`
apply plugin: 'maven'
group = 'com.<example>'
version = '1.0'
uploadArchives {
repositories {
mavenDeployer {
repository(url: "file:///Users/<myuser>/.m2/repository")
}
}
}
`
The archive is uploaded in your maven repo as aar file.
Use this aar file in any other project as a dependency.
Hope this helps.
Here are two other questions on the same matter.
How do I add a library project to Android Studio?
How to create a library project in Android Studio and an application project that uses the library project
Personally I didn't use any of the two provided methods.
I built my project as a JAR, added it to the 'libs' folder, right clicked on it and clicked 'Add as library' and then finally added the dependency in the gradle file like so:
dependencies {
compile files('libs/MyJAR.jar')
}

How can I add a linked source folder in Android Studio?

In Eclipse I can add a source folder to my Android project as a "linked source folder". How do I achieve the same thing in Android Studio?
Or is it possible to add an external folder to build in Gradle?
In your build.gradle file, add the following to the end of the Android node:
android {
....
....
sourceSets {
main.java.srcDirs += 'src/main/<YOUR DIRECTORY>'
}
}
The right answer is:
android {
....
....
sourceSets {
main.java.srcDirs += 'src/main/<YOUR DIRECTORY>'
}
}
Furthermore, if your external source directory is not under src/main, you could use a relative path like this:
sourceSets {
main.java.srcDirs += 'src/main/../../../<YOUR DIRECTORY>'
}
You can add a source folder to the build script and then sync. Look for sourceSets in the documentation here: http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Basic-Project
I haven't found a good way of adding test source folders. I have manually added the source to the .iml file. Of course this means it will go away everytime the build script is synched.
While sourceSets allows you to include entire directory structures, there's no way to exclude parts of it in Android Studio (as of version 1.2), as described in Exclude a class from the build in Android Studio.
Until Android Studio gets updated to support include/exclude directives for Android sources, symbolic links work quite well. If you're using Windows, native tools such as junction or mklink can accomplish the equivalent of symbolic links on Unix-like systems. Cygwin can also create these with a little coercion. See: Git symbolic links in Windows and How to make a symbolic link with Cygwin in Windows 7.
Here’s a complete Java module Gradle file that correctly generates and references the built artefacts within an Android multi-module application:
buildscript {
repositories {
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath "net.ltgt.gradle:gradle-apt-plugin:0.15"
}
}
apply plugin: "net.ltgt.apt"
apply plugin: "java-library"
apply plugin: "idea"
idea {
module {
sourceDirs += file("$buildDir/generated/source/apt/main")
testSourceDirs += file("$buildDir/generated/source/apt/test")
}
}
dependencies {
// Dagger 2 and Compiler
compile "com.google.dagger:dagger:2.15"
apt "com.google.dagger:dagger-compiler:2.15"
compile "com.google.guava:guava:24.1-jre"
}
sourceCompatibility = "1.8"
targetCompatibility = "1.8"
This is for Kotlin DSL (build.gradle.kts):
android {
sourceSets["main"].java.srcDirs("src/main/myDirectory/code/")
sourceSets["main"].resources.srcDirs("src/main/myDirectory/resources/")
// Another notation:
// sourceSets {
// getByName("main") {
// java.srcDirs("src/main/myDirectory/code/")
// resources.srcDirs("src/main/myDirectory/resources/")
// }
// }
}
If you're not using Gradle (creating a project from an APK, for instance), this can be done through the Android Studio UI (as of version 3.3.2):
Right-click the project root directory and pick Open Module Settings
Hit the + Add Content Root button (center right)
Add your path and hit OK
In my experience (with native code), as long as your .so files are built with debug symbols and from the same absolute paths, breakpoints added in source files will be automatically recognized.

Categories

Resources