ZenBalance
ZenBalance

Reputation: 10317

Android library assembles with Gradle but "package R does not exist" error raise on connectedCheck

I have an android library with the file structure below.

Android_Library
├── libs
└── src
    ├── instrumentTest
    │   ├── assets
    │   └── java
    └── main
        ├── aidl
        ├── gen
        ├── java
        └── res

When I assemble the library with Gradle, the task runs successfully. However, when I try running a connectedCheck I get the following errors about the R file not being found:

:validateDebugSigning
:packageDebug
:assembleDebug
:prepareTestDependencies
:compileTestAidl
:processTestTestManifest
:generateTestBuildConfig
:mergeTestAssets
:compileTestRenderscript
:mergeTestResources
:processTestResources
:compileTest


 Android_Library/src/instrumentTest/java/com/example/library/util/UtilTests.java:514: error: package R does not exist
                        case R.plurals.time_days_ago: {
                              ^
        ...

Note: Recompile with -Xlint:unchecked for details.
18 errors
:compileTest FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':compileTest'.
> Compilation failed; see the compiler error output for details.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

I originally thought that it was a problem in the R resource generation, but then I would expect it to also fail when assembling.

Edit:

I have tried both having and R import statement (which I know is incorrect) and not having and R import statement. Both result in the same error.

SOLVED:

Thanks to @Xav's tip (below): I'm extracting a library from a larger project and attempting to build it on its own for modular testing.

Changing the

import com.example.app.R;

statements in the library files to

com.example.library.R

allows the connected check to run.

(apparently the res files for both the main app and the library are the same).

Additional notes:

Since the switch statement takes in R id's the switch statement works when it compiles. However, I am still unsure of why gradlew assemble works with the incorrect import statements while gradlew connectedCheck fails.

Upvotes: 4

Views: 2675

Answers (1)

Xavier Ducrohet
Xavier Ducrohet

Reputation: 28549

While this seem unrelated to the error, I see you are doing a switch statement using IDs coming from the R class, but you are in a library.

This is not possible because those IDs are not final.

I'm wondering if that's the error but the compiler is confused and outputs a different error message.

Upvotes: 2

Related Questions