Include another project using Gradle? [duplicate] - java

I created a library project in Android Studio (currently 0.5.2) by choosing File > New Project... > "Mark this project as a library".
I have two other non-library projects that I would like to add a dependency to this library project.
-My Library
-Project 1 (depends on My Library)
-Project 2 (depends on My Library)
My goal is to keep each project independent and avoid duplicating modules/code. How can this be done without copying the library module into the other projects?
Update:
Android Studio 0.6.0 allows you to Import a module, though, this simply copies the module source into the Project.

You can also refer to a library outside of your project folder using the project().projectDir property. If your external library is relative to your project like so
- MyLibrary
- library
- MyProject
- app
in MyProject/settings.gradle
include ':library'
project(':library').projectDir = new File(settingsDir, '../MyLibrary/library')
in MyProject/app/build.gradle
dependencies {
compile project(':library')
}

This is very similar to this question:
Sharing an Android library between multiple Android apps using Gradle
Instead of pushing to maven central you can push to your local maven repository (mavenLocal() in build.gradle)

Another route (if you don't want to deploy the library somewhere) is to use your VCS and check out the library within your project. Git has submodules for that, Mercurial has subrepos and SVN has external to name a few examples.
Then add it to your Gradle build using a project dependency.

Related

Intellij : import project as library for another project

I am developing a project A (Java Maven) which uses a library B (a JAR file)
I have the source code of library B, and I want to change code in project B, while it's used as library for project A.
Is that possible in Intellij to replace a JAR library by its source code ?
I'd configure a multi-module Maven project with the parent aggregate pom.xml and 2 sub-modules: for the app and for the library with their own pom.xml files.
Define a dependency on the lib module for the app module. IDE will resolve such dependencies via the sources and you will see the changes in the project instantly without the need to compile. Refactorings will also work across the modules so that you can rename a method from the usage in the app and it will automatically rename it in the lib.

IntelliJ project with sub projects

Is there a way to create a gradle java project in IntelliJ with a client and server side that share a class library? Something similar to how Visual Studio allows multiple sub projects that can have their own .exe build for each and a .dll for the library.
Example:
+ Project
- Client (Separate .jar built)
- Server (Separate .jar built)
- ClassLibrary (used by Client and Server)
Yes, use File | New... | Module action and select Gradle type for the new module.
By default IDE will add this Gradle module to your project as a child via include '<project-name>' directive in settings.gradle file.
Then in your main/parent project build.gradle script you can add this Gradle project as a dependency: implementation project(':<project-name>'). See Gradle documentation for Project lib dependencies for more information.

How to make .jar file from custom java class in android-studio for use other project

I create my android application in android-studio it's work well.
In my project has my custom 3 class "ClassA.java", "ClassB.java" and "ClassC.java"
I want to convert 3 class to 1 java libary file(.jar) for use in another project.
How can i do it's? Please let me know step by step.
You can create a Java only gradle project.
Create a new directory elsewhere.
Create a build.gradle file there and don't apply the android plugin, just apply "java".
Create src/main/java directory structure and move your code there.
Add any needed dependencies to the build file.
"./gradle build" will create your jar,
You can publish the jar to an artifact repo (locally or to someplace like jCenter) using a plugin such as gradle-mvn-push
Then any project that needs your 3 classes can add a maven or gradle dependency.
If your classes depend on Android you will want to make an "aar" instead of a jar.

How do I add a java gradle submodule to my Android module?

Suppose I have an Android application project in IntelliJ Idea with Gradle. Call it MyApplication.
What I want to do is to add a plain java library module (not Android library project) as a submodule to MyApplication. Lets call this module a testlib.
And then when whole project is built, I want this module compiled to jar and included to /libs folder of MyApplication.
What I tried: I've created a submodule testlib and included it as a dependency to MyApplication, but I get a following warning:
Warning:Gradle: module 'testlib' won't be compiled. Unfortunately you
can't have non-Gradle Java module and Android-Gradle module in one
project.
Is this even possible?
If you've created your project in IntelliJ as an Android-gradle project, then when you go to the "Add new module" window you should see an "Gradle: Java library" option like this:
IntelliJ IDEA does not support Gradle and Non-Gradle modules in one single project. A comment from JetBrains in their forum states this clearly.
The solution is to either convert the Maven modules into Gradle
modules or split the project in two. With the latter approach, if you
build the plain java library module with Maven before building the
Gradle project, you will still be able to use it as a dependency in
the Android application project.
Beware of using different Java language source levels in multi-module
Gradle projects. IntelliJ IDEA does not handle them correctly due to a
bug in either IntelliJ IDEA or Gradle. (There is a pending discussion
whether this should be fixed in the IDE or Gradle.)

Gradle 2 android projects using same library

I have the following structure
MainProjectRoot
Android Project 1:
...
Android Project 2:
...
Shared Library:
...
The library project is shared between both of the Android projects. I am trying to convert things to work with the new build system.
This is what export from eclipse generated for the build.gradle for Project1.
compile project(':D::workspace:MainProjectRoot:shared-library')
How do i fix this reference?
i tried this but it expects the library to be inside the Project1 Folder if i leave it out as
compile project(':shared-library')
The export from Eclipse is broken on Windows when dealing with multi-module projects. We are fixing this.
In the meantime ensure you have the settings.gradle file under MainProjectRoot/
It should contain:
include 'project1'
include 'project2'
include 'shared-library'
(or whatever those folder names are).
Then you change the dependency line to be
compile project(':shared-library')

Categories