I have to add .aar files as a library in a sub project in Android studio. It gives me an error.

徘徊边缘 提交于 2019-12-09 12:56:02

问题


I have added the .aar files to libs folder in the sub-project. And have given the repositories as:

repositories {
 mavenCentral()
    mavenLocal()
    flatDir {
        dirs 'libs'
    } 

in the build.gradle of this sub-project. Do I need to add dependencies in Main project's build.gradle also? If yes, how should that be done?


回答1:


classpath 'com.android.tools.build:gradle:1.3.0'
  1. File -> New Module -> Import JAR/AAR Package

  2. "File name" select .aar file, example:

d:/myproject/mylib/build/outputs/aar/mylib-release.aar
  1. "Subproject name",example:

    mylib-release
  2. /app/build.gradle

    dependencies{

    compile fileTree(include: ['*.jar'], dir: 'libs')

    compile project(':mylib-release')

    }




回答2:


You need to set the dependencies too:

compile fileTree(dir: 'libs', include: ['*.jar','*.aar'])

This can be done in the subprojects build.gradle.


Edit: You might need to specify the file itself:

compile(name:'name-of-file',ext:'aar')



回答3:


For filename.aar

repositories {
   flatDir {
        dirs 'libs'
    }
}

dependencies {
    compile(name:'filename', ext:'aar')
}



回答4:


In the repositories of your module: app

repositories {

    flatDir {
        dirs 'libs'
    }
}

Add the following in the dependencies of your module: app In the case that your have both a JAR and an AAR file, do the following.

dependencies 
{
  implementation (name: '***library name***', ext: 'aar')
  implementation fileTree(dir: 'libs', include: ['*.jar', '*.aar'], )

}



回答5:


You are adding a aar file in libs folder.
The aar file doesn't contain the dependencies, then you have to add these dependencies also in the main project.

In your module1/build.gradle you should have something like:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile('com.android.support:appcompat-v7:22.2.1') //for example
    //..
 }

In your mainModule/build.gradle you have to add all the dependencies used by your module1.

dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])
        compile(name:'fileName',ext:'aar')
        compile('com.android.support:appcompat-v7:22.2.1') //for example
        //...
     }


来源:https://stackoverflow.com/questions/31958139/i-have-to-add-aar-files-as-a-library-in-a-sub-project-in-android-studio-it-giv

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!