Issue with using .AAR file in other project

2019-05-10 17:21发布

问题:

I have to use my android library project (which has resources), in another project. Here is what I did so far:

  1. Created a library project in Android Studio. Build that project, so .aar file was generated like: ..\MyApplication\mylibrary\build\outputs\aar\mylibrary-debug.aar

  2. Created a new Android project in Android Studio. Then, I followed this:

    File >> New Module >> Import .JAR or .AAR package

This gives me following structure in my project:

There was no any error in gradle sync or while I make the project.

But now, if I try to use class name of my Library project, it doesn't get shown in auto suggest. There is one class MyClass.java in my library project. In IDE, if I try to write MyC, I am not able to see MyClass. It shows No suggestions instead.

Am I missing some configuration?

Let me know in case you want other information related to folder structure or build.gradle file.

回答1:

This alternative (credits go to Dwayne) seems to work fine for me. Summarizing:

  • Declare your flat file repository. This repository declaration goes in the gradle.build file in your project module and not the top level gradle.build file in the parent directory:

    repositories{
       flatDir {
         dirs 'libs'
       }
    }
    
  • Copy your mylibrary-release.aar or mylibrary-debug.aar from ..\MyLibraryProject\mylibrary\build\outputs\aar\ to the libs folder in your application module ..\MyApplicationProject\app\libs.

  • Declare your dependency on the library.

    dependencies {
       compile 'com.example.library:mylibrary:release@aar'
    }
    

Note the namespace section (com.example.library) of the dependency declaration is ignored by the ‘flat file’ repository, while having an incorrect namespace in the dependency declaration doesn’t effect anything you should ensure the namespace is correct incase you move to a library repository at a later date.


Another alternative may be creating a local Maven repository as explained here, but I haven't tried this option.