【问题标题】:How to use the downloaded maven aar file in my Android project?如何在我的Android项目中使用下载的maven aar文件?
【发布时间】:2017-11-02 07:10:24
【问题描述】:

我的Android项目从别人的个人maven下载maven aar文件,存在目录下:

C:\Users\username\.gradle\caches\modules-2\files-2.1

现在我想使用maven文件,如何在我的项目build.gradle,或我的模块build.gradle中配置。

我尝试了很多方法来解决这个问题,包括添加

repositories{
    flatDir{
        dirs 'libs'
    }
}
dependencies {
    compile(name: 'myaarfilename.aar', ext: 'arr')
}

在我的模块中build.gradle

并添加

buildscript{
    repositories{
        jcenter()
        mavencentral()
        mavenLocal() //to use my local maven aar file
    }
}

在我的项目中build.gradle

所有这些方法都不起作用,那么我该如何使用我的maven缓存aar文件,或者我该如何配置maven?希望有人能帮助我,非常感谢。

【问题讨论】:

  • mavenLocal 指 ~/.m2/ 文件夹
  • compile(name: 'myaarfilename', ext: 'arr')
  • 所以我不仅要在存储库中添加 mavenLocal() ,还要在其中添加 ~/.m2/folder ?但是我发现我要使用的文件在你的目录中不存在说,你能告诉我为什么
  • 还是不行,我想知道为什么Android Studio没有从我的本地缓存中读取maven aar文件?

标签: android maven build.gradle aar


【解决方案1】:

我的Android项目从别人的个人maven下载maven aar文件,存在目录下:

C:\Users\username\.gradle\caches\modules-2\files-2.1

请注意,因为 gradle 缓存文件夹不是 maven 存储库。

然后:

buildscript{
    repositories{
        jcenter()
        mavencentral()
        mavenLocal() //to use my local maven aar file
    }
}

您在buildscript 内使用repositories 块,它与dependencies 无关,如aar 文件。

如果你有 aar 文件,你可以将文件放在libs 文件夹中,然后使用:

dependencies {
   compile(name:'nameOfYourAARFileWithoutExtension', ext:'aar')
 }
repositories{
      flatDir{
              dirs 'libs'
       }
 }

请注意,因为 aar 文件不包含传递依赖项,并且没有描述库使用的依赖项的 pom 文件。 p>

这意味着,如果您使用 flatDir 存储库导入 aar 文件您还必须在项目中指定依赖项

否则,如果您有 maven 存储库,请使用:

dependencies {
    compile 'my_dependencies:X.X.X'
}

【讨论】:

  • 虽然没有解决我的问题,但我还是同意你的想法,我怀疑我的aar文件是不是错了。Android Studio没有从目录中读取maven文件让我很困惑。我依赖okhttp3,一旦我删除了okhttp3的缓存,Android Studio会从下一个建筑的jcenter下载okhttp3,如果okhttp3的缓存存在于文件夹中,Android Studio不会从jcenter下载,只是包很奇怪。 .....
【解决方案2】:

尝试添加项目的build.gradle:

allprojects {
    repositories {       
        maven { url 'file://' + new File('path/to/repository').canonicalPath }
    }
}

【讨论】:

    猜你喜欢
    • 2016-12-16
    • 2018-07-06
    • 2016-10-22
    • 1970-01-01
    • 2017-05-30
    • 1970-01-01
    • 2014-09-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多