Gradle: Shared java project with an android project - android

I've got this shared common classes that I extracted into a separate "common" module with gradle (which has dependencies on some jars). I defined it as a java project because I want to also use it outside of the android context. But when I include it in my android project with:
compile project(':my_common')
It doesn't seem to work.

Related

Use Android library module as dependency to pure Java module

I have an android library module in Android Studio, named :androidtestlib, and a "pure" Java module, named :javatest. I want to share code between them.
I can set :javatest as a dependancy to :androidtestlib.
However, since I have already written many classes in :androidtestlib, I want to set this one as a dependency to :javatest. When using implementation project(':androidtestlib'), build fails with:
Could not determine the dependencies of task ':androidtestlib:MyClass.main()'.
> Could not resolve all task dependencies for configuration ':androidtestlib:runtimeClasspath'.
> Could not resolve project :javatest.
Required by:
project :androidtestlib
> The consumer was configured to find a runtime of a library compatible with Java 7, packaged as a jar, and its dependencies declared externally. However we cannot choose between the following variants of project :javatest:
- debugAndroidTestCompile
- debugAndroidTestRuntime
...
If I use the deprecated compile project(path: ':androidtestlib', configuration: 'default'), Gradle syncs successfully and even code completion works. Building however fails since it can't find the package from the android library module:
error: package com.test.some_package does not exist
import com.test.some_pacakge.MyClass;
^
Is there a way to make this work and use the Android Library module as a dependency to the pure Java module?

Packing run time dependecies into aar library

I build an Android Application which contains a module as library. Most of the logic is inside the LibraryModule. The library module has several other dependencies which are included as gradle dependencies
Application
* LibraryModule
*implementation ('dependency1')
*implementation ('dependency2')
The library module is include in the application's build.gradle like
implementation project(":LibraryModule")
The application works fine then.
But when I first build the Library module as an aar file using
gradle :LibraryModule:assemble
and then using the applications build.gradle to include the aar
implementation(':LibraryModule#aar') The application compiles. But several of the classes of the dependencies (dependency1, dependency2) which are required at run time are missing from the aar.
Is there some way to include all the contents of the dependency1 and dependency2 in the aar file so that run time dependecies is also packed together in the aar file.
I have done some googling and found out that fat aar is an option. Is there some way to include all the class file from the dependeices also into the aar file which are also needed at run time ?
Building a fat-aar is not officially supported by Android. There are some similar discussions at Android gradle multiproject dependency resolution

Does an Android .aar built with Gradle contain dependencies?

Not sure why I can't find any answers on this. If I convert my library project into an .aar using Gradle in Android Studio, does it retain all the dependencies of that module?
I'm asking because I'm trying to use a Gradle generated .aar locally, but it looks like only some of the original dependencies have been packaged. Namely, it complains that I'm missing 'OkHttp', but if I add it to the main project I get duplicate class errors.
Usually a library does not directly contain its dependencies. This does not matter whether it is an aar or a jar. Instead, the library declares its dependencies in the Gradle build file and they are resolved when someone uses the library.

`rootProject` in a Gradle module that's imported to another project

In my current setup I have two top-level Gradle projects:
A library project VideoPresenter with modules
videopresenter-common
videopresenter-exoplayer
videopresenter-visualon
where both videopresenter-exoplayer and videopresenter-visualon depend on videopresenter-common.
All three of the modules depend on OkHttp so I defined a version variable in my top-level build.gradle:
ext {
okhttp = 'com.squareup.okhttp3:okhttp:3.0.0-RC1'
}
which I use in the three other build.gradles:
dependencies {
compile rootProject.ext.okhttp
}
So far, this is completely analogous to the way, for example, RxBinding is set up. And it seems to work as long as I compile the modules from within this project.
However, I also have an application project that uses one or more of these modules. Let's say the settings.gradle of that project includes the following:
include ':videopresenter-common'
include ':videopresenter-exoplayer'
project(':videopresenter-common').projectDir = new File('../VideoPresenterAndroid/videopresenter-common')
project(':videopresenter-exoplayer').projectDir = new File('../VideoPresenterAndroid/videopresenter-exoplayer')
Now, when I try to compile the application project Gradle complains because it
Cannot get property 'okhttp' on extra properties extension as it does
not exist
presumably because rootProject now points to the top-level build.gradle of my application project.
If I add the property there the project compiles. However, I don't want to have to "inject" the correct version number from the main project into the library project. Is there a way to centrally declare the property in the library project so that it also applies when the module is imported into another project?
If the goal is to get the value from the library then you could just get the library project's rootProject then reference the ext like before. Being specific about what project we are looking for should provide the expected result.
dependencies {
compile project(':videopresenter-common').rootProject.ext.okhttp
}
As long as the project is in the settings.gradle you should be able to reference it's extension.

Gradle Android project dependency not working

I'm trying to migrate from maven to Gradle for an Android project and I am facing the following issue:
I have three projects i.e
root
- projlib
build.gradle
- projA
build.gradle
- projB
build.gradle
build.gradle
settings.gradle
Basically what I want to achieve is that projB depends on projA and projlib. projlib is a lib folder that compiles and generates a lib(jar) file. projA is an Android Application and projB is another Android Application that needs to reference code in projA. Right now what I have added in the projB build.gradle file is
dependencies {
compile project(':projlib')
compile project(':projA')
}
So say if there's a class
FooProjLib in projlib and
FooProjA in projA
Then In projB I can do
FooProjLib foo = new FooProjLib
which works fine
but when I do
FooProjA foo = new FooProjA
Gradle gives me package projA does not exist, what I have observed is that both dependency is resolved but only the lib can be reference and not the apk.
Does anyone have an idea how to solve this?
You can't do exactly what you want. projA can't build an application (i.e. an APK) and also have other things depend on it. projB can only depend on projA if projA is an Android library, meaning in its build file you have this declaration:
apply plugin: 'android-library'
instead of
apply plugin: 'android'
Of course, this means that projA won't build an APK, but will build an AAR instead.
If projA needs to also be an APK, you'll have to restructure things such that the common code that's in projA that projB also needs is moved out to a shared library. If both projects are similar, perhaps you could have just one module for them and use project flavors to differentiate them; it's hard to say whether this is a good approach without a lot more information.

Categories

Resources