1

I have added a library by going to File-> New ->Import Module, the library added successfully, there are around 8 jar files added in this imported module, but in my app module i am unable to access these files.

Although i can easily access files that are available in library src folder.

These lines are added in library gradle file.

compile fileTree(dir: 'libs', include: '*.jar')
compile fileTree(dir: "$buildDir/native", include: 'native.jar')

build.gradle(app)

compile fileTree(dir: 'libs', include: '*.jar')
compile project(':myLibrary')

settings.gradle

include ':myLibrary'

If i perform these steps in a new project, i can access classes inside jar files from my app module, but in my current project they are not accessible.

I also tried deleting android studio preference, invalidate cache and restart, but nothing sees to be working here.

Martin Zeitler
  • 1
  • 19
  • 155
  • 216
dev90
  • 7,187
  • 15
  • 80
  • 153

1 Answers1

1

in my current project they are not accessible.

The are not supposed to be accessible, because those dependency jars are not part of your app project.

If you want to have access to those jars depended by your library module myLibrary, you need to specify them inside your app project using

compile fileTree(dir: '../myLibrary/libs', include: '*.jar')
compile fileTree(dir: "../myLibrary/native", include: 'native.jar')

and change your myLibrary settings to be

provided fileTree(dir: 'libs', include: '*.jar')
provided fileTree(dir: "$buildDir/native", include: 'native.jar')

By doing above, you should be able to access your jars from your app module and there won't be any duplicate classes causing conflict.

Maybe it will be better to upgrade your Android Gradle Plugin to 3.0+.

Additional information about android gradle plugin versions:

  1. https://stackoverflow.com/a/51392464/8034839
  2. https://developer.android.com/studio/build/dependencies

Just quote the key explanations here for your information about provided and compileOnly:

Gradle adds the dependency to the compile classpath only (that is, it is not added to the build output). This is useful when you're creating an Android module and you need the dependency during compilation, but it's optional to have it present at runtime.

For your library module, itself actually only requires its dependencies be in compile classpath if you don't package them into your library .jar or .aar. In this case, the application that uses this library needs to add those external jars depended by your library module because your library module did not package them (by using compileOnly) . And your application need those external jars be runtime available by using compile.

shizhen
  • 12,251
  • 9
  • 52
  • 88
  • Thanks @shizhen, When i use `compileOnly` it returns following error message, `Could not find method compileOnly()` I am using gradle `2.3.3` , but when i am using `compile` it works fine, and loads jar files :) – dev90 Oct 28 '18 at 08:22
  • Oh, if you are using **older** version of gradle plugin, then change `compileOnly` to `provided`. But, i suggest you upgrade your gradle plugin version to latest version, 3.2.1 to take its a number of advantages. – shizhen Oct 28 '18 at 08:28
  • Thanks shizhen, this fixed my problem, but i am still unable to understand why external jar in a module are working fine in a new separate project without using `provided ` or `CompileOnly` keyword. – dev90 Oct 28 '18 at 10:08
  • @Kirmani88, see my updated answer about the `provided` and `compileOnly`. – shizhen Oct 29 '18 at 00:58
  • In the library I change from implementation to compile it started picking .jars file. – Ganesh Jogam Apr 14 '20 at 04:24