Use Android library module as dependency to pure Java module - android

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?

Related

Android Studio 3 and Gradle 4.3 -> Library modules no longer process local JARs -> how to handle this?

Upgrade to Android Studio 3.0.0 mentions this, and doesn't elaborate on how to handle it:
Library modules no longer process local JARs. This is to speed up incremental builds that are caused by changes to a library module's code.
So I have a project with a library project in it. In my library project's build.gradle file I have this:
compile files('libs/com.somelib.somepackage.jar')
I changed compile to implementation and when I tried to run my app, all my classes that tried to access the import com.somelib.somepackage.SomeClass import statement threw an error that this package didnt exist.
I changed back to compile and I was able to build and run my app.
I want to comply to the new rules since compile is deprecated and will be removed with the next Gradle release, so how do I go about doing that?
If you are trying to access classes from the .jar that is included in the library project from the app project, you will have to use api instead of implementation otherwise the classes will only be accessible in the library project:
implementation files('libs/com.somelib.somepackage.jar')
should be
api files('libs/com.somelib/somepackage.jar')
As said by the documentation:
... When a module includes an api dependency, it's letting Gradle know
that the module wants to transitively export that dependency to other
modules, so that it's available to them at both runtime and compile
time ...
Reference:
https://developer.android.com/studio/build/gradle-plugin-3-0-0-migration.html#new_configurations

dependencies issue when adding library AAR

I've created an Android library that I want to be able to use in other Android applications. I've created a small test app in the same project as the library and everything works.
To test integration with other apps, I created a new application, and followed this guide to import my library to that new application by creating a new module from the library's AAR file.
build.gradle file:
dependencies {
...
compile project(':my-sdk')
...
}
settings.gradle file:
include ':app', ':my-sdk'
Compilation succeeded and I can use the library's API inside the test app, but when running it I get a runtime exception
java.lang.NoClassDefFoundError: Failed resolution of: Lokhttp3/MediaType;
I assume it's a dependencies issue, since the library uses okhttp as a dependency, which is probably not being packed in the AAR file. I just couldn't find anywhere how to compile this dependency so it would work in other projects.
I fix it adding jar files instead of using maven into my module, then assemble release aar, and in the app project disabling "Instant Run".

'api' configuration does not expose dependency to the consumer of the module at compile time

We're trying to migrate to gradle-4.1-rc-1 and com.android.tools.build:gradle:3.0.0-alpha9 to start working on instant apps. However we have errors in our modules configuration. Using Studio Canary 9.
We have android library module util which declares dependency on dependencyA this way:
api dependencyA
And we have another library module lib using util module
api project(':util')
But code in lib module cannot see dependencyA and use it. I thought that implementation configuration behaves this way.
Also, sometimes we are required to specify configuration for our module (when there are more than build and release build types) doing it this way:
api project(path: ':util', configuration: 'default')
Does this have impact on the issue? But standard declaration is not working.

Gradle: Shared java project with an android project

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.

Why Android Studio can't reference a library if included in build.gradle only?

In my app I'm using Guava library. I have referenced it in my build.gradle file only as follows:
dependencies {
compile 'com.google.guava:guava:13.0.1'
}
My app rebuilds and runs fine. However when I'm trying to add a new something from Guava library then the compiler complains that it can't find the class.
For example: when I write
import com.google.common.base.Preconditions
in my new file the compiler says 'cannot resolve symbol common'.
But my old files are rebuilt fine. How so? Do I have to mess with Project|Structure? I specifically wanted to list any dependencies -only- in build.gradle.
It seems that doing a "sync project with gradle files" helps.
In Android Studio, select :
Tools > Android > Sync project with gradle files

Categories

Resources