Gradle 2 android projects using same library - java

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')

Related

Include plain java projects as dependency at gradle project

I have two projects
one plain java project and one gradle spring project created from jhipster
I would like to use the java project alongside with the gradle one.
After search on the net i have found how to do it link1 but this works only if all my projects are gradles.
Also when i try to add module dependency from Project Structure does not work.
Project tree
workspace
-project1
--build.gradle
-project2
What is the right way of doin this?
Also why i cannot make changes from IDE's project structure and automatically update build.gradle and settings.gradle with the new dependencies?
Change the plain java project into a gradle project, change the two projects into a gradle multi-project.
It's not hard to do, the plain java project probably just needs an almost empty build.gradle file and the settings.gradle file just needs to include it.
An IDE cannot help you much in these tasks, you will have to do most of it manually.

Include another project using Gradle? [duplicate]

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.

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.)

Why does Gradle only include classes from my library with project dependency

I'm building an Android app that has a dependency on a custom library, and Gradle is only willing to include my custom library when I use a project dependency, not when I use a files dependency to include the library's jar file. I'm building both my app and the library with the API levee 19 SDK.
failing dependencies section from build.gradle:
dependencies {
compile 'com.android.support:appcompat-v7:+'
compile files('libs/MyLibrary.jar')
}
If I use the above dependencies section, none of the class in MyLibrary.jar are included in the build apk file, as verified by extracting its classes.dex and running dexdump. I have also verified that all of the classes are present in the jar file I'm using.
If I use the following dependencies section, then all of the classes in MyLibrary are included in the apk file:
dependencies {
compile 'com.android.support:appcompat-v7:+'
compile project(':MyLibrary')
}
I'm using Android Studio 0.4.0, Gradle 1.9, and I think the Gradle plugin 0.7.1.
What is going on here? I'd really like to build my app with the API level 18 sdk to test compatibility, but I can't get that working unless I'm able to just use the jar file for my library.
Okay, this was my fault. My library's build.gradle was configured to only include the source files in the jar output file. The following is incorrect Gradle code and will give you the same problems as I've had.
task jar(type: Jar) {
from android.sourceSets.main.java
}
This answer shows how to fix the jar file creation. It's ugly, but it seems to work.
Jar task does not include dependencies in the final jar artifact.
From Gradle documentation on jar task:
The jar task creates a JAR file containing the class files and
resources of the project.
It assumes that since you are building jar for your project, all dependencies will be provided during runtime. As opposed to war, where all dependencies are usually included in the final artifact.
If you need to create "fat jar", which will include the dependencies, then look into specific plugins, for example gradle-fatjar-plugin.
It's a little bit of a longshot, but if you're not using Android Studio 0.4.0 and you've just added the jar file, try cleaning your project and rebuilding from scratch. We've seen this bug: https://code.google.com/p/android/issues/detail?id=63366 where libraries don't get included without cleaning the project, though this bug refers to a dependency downloaded from Maven and not a local jar file (which may or may not be an important difference). This was fixed in Android Studio 0.4.0 (more specifically, in the Gradle plugin 0.7.0).

Categories