Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Studio with Java Library Module Unable to load test resources

I have an Android Studio Project with a Java library inner module that has tests with test resources. Yet when i run the tests I am not able to retrieve the artifacts. This seems to work fine in a pure java gradle project (in eclipse at least).

meaning For java plugin:

src/main/java
src/main/test
src/test/java
src/test/resources

Under the resources directory i have a crt file that i want to load in my junit test. When using any command i have come across it returns null for the resource. Yet I have confirmed the resources are in the build folder.

Some things I Tried:

getClass().getClassLoader().getResourceAsStream("cert_format_der.crt").read(); // NPE
getClass().getClassLoader().getResourceAsStream("/cert_format_der.crt").read(); // NPE
getClass().getClassLoader().getSystemResourceAsStream("/cert_format_der.crt").read(); // NPE

Thanks

like image 982
nibuen Avatar asked Jul 21 '14 16:07

nibuen


People also ask

Can I use Java library in Android?

The Android platform contains a large number of shared Java libraries that can optionally be included in the classpath of apps with the <uses-library> tag in the app manifest. Apps link against these libraries, so treat them like the rest of the Android API in terms of compatibility, API review, and tooling support.

What is Java library plugin Gradle?

The Java Library plugin expands the capabilities of the Java plugin by providing specific knowledge about Java libraries. In particular, a Java library exposes an API to consumers (i.e., other projects using the Java or the Java Library plugin).

What is Library module in Android Studio?

An Android library is structurally the same as an Android app module. It can include everything needed to build an app, including source code, resource files, and an Android manifest.


1 Answers

Turns out this seems to be a bug with Intellij and how Gradle not setting the resource directory for the test sourcesets correctly.

Adding This to the build.gradle for the module Fixes it:

sourceSets {
   test {
      output.resourcesDir = output.classesDir
   }
}
like image 172
nibuen Avatar answered Oct 03 '22 00:10

nibuen