Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get Gradle for Android to find a library's test code from within other test code?

I have an Android application with two library layers underneath it. Each library has its own test code. I would like the upper library’s test code to extend the lower library’s test code, but I can’t figure out how to get Gradle to find the lower library’s test code (it only finds the production code).

My file structure looks like this:

myproject/
 + myapp/
   + src/
     + main/
       + java
         + com/myapp
 + lib1/
   + src/
     + main/
       + java
         + com/myapp/lib1
     + androidTest
       + java
         + com/myapp/lib1/test
 + lib2/
   + src/
     + main/
       + java
         + com/myapp/lib2
     + androidTest
       + java
         + com/myapp/lib2/test

The gradle file for the app looks like:

apply plugin: 'com.android.application'
android { ... }
dependencies {
  compile project(':lib2')
} 

The gradle file for lib2 looks like:

apply plugin: 'com.android.library'
android { ... }
dependencies {
  compile project(':lib1')
} 

The gradle file for lib1 looks like:

apply plugin: 'com.android.library'
android { ... }

When I run “gradle connectedCheck” I get this:

...
:lib2:generateDebugTestSources UP-TO-DATE
:lib2:compileDebugTestJava
myproject/lib2/src/androidTest/java/com/myapp/lib2/test/SubClass.java:10: error: package com.myapp.lib1.test does not exist
import com.myapp.lib1.test.BaseClass;

I have tried adding various androidTestCompile lines to the dependencies section of lib2’s gradle file, but none of them help.

Anybody know how to make this work?

EDIT: Sorry, I didn't mention that I also have a settings.gradle in the myproject folder that looks like this:

include ':lib1'
include ':lib2'
include ':myapp'
like image 215
Bruce Avatar asked Nov 11 '14 22:11

Bruce


People also ask

How do I run a Gradle test from the command line?

Use the command ./gradlew test to run all tests.

Where is the AAR file in Android Studio?

Add your AAR or JAR as a dependency Navigate to File > Project Structure > Dependencies. In the Declared Dependencies tab, click and select Jar Dependency in the dropdown. In the Add Jar/Aar Dependency dialog, first enter the path to your .aar or .jar file, then select the configuration to which the dependency applies.

How do I add a dependency in Gradle?

To add a dependency to your project, specify a dependency configuration such as implementation in the dependencies block of your module's build.gradle file.


1 Answers

The issue is that your gradle does not know connections of myapp, lib1 and lib2 modules.

Try to add some settings.gradle in modules that are using another one. For your structure i recommend to make additional gradle files in root:

for example in myproject/

settings.gradle:

rootProject.name = 'myproject'
include 'myapp'
include 'lib1'
include 'lib2'

and then write gradle.build for all module where you clarify type of all project for example ear etc.

You can also connect modules separately.

like image 174
SuperAndrew Avatar answered Oct 21 '22 15:10

SuperAndrew