Android 将本地 .aar 文件添加到我的 gradle 构建中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21882804/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:15:23  来源:igfitidea点击:

Adding local .aar files to my gradle build

androidmavengradleandroid-studioaar

提问by sn0ep

So i have created a Android-Library and succesfully compiled it into a .aar file i called this aar file: "projectx-sdk-1.0.0.aar"now i want my new project to depend on this aar so what i have did is follow this post: http://life.nimbco.us/referencing-local-aar-files-with-android-studios-new-gradle-based-build-system/

所以我创建了一个 Android-Library 并成功地将它编译成一个 .aar 文件,我称之为 aar 文件:"projectx-sdk-1.0.0.aar"现在我希望我的新项目依赖于这个 aar,所以我所做的就是关注这篇文章:http://life。 nimbco.us/referencing-local-aar-files-with-android-studios-new-gradle-based-build-system/

But the post confuses me since i do not get the desired result:

但是这篇文章让我感到困惑,因为我没有得到想要的结果:

The package name of the aar is : com.projectx.photosdkand the module inside is called sdk

aar的包名是:com.projectx.photosdk,里面的模块叫sdk

here is my current project structure:

这是我目前的项目结构:

|-SuperAwesomeApp
|--.idea
|--gradle
|--App
|---aars
|----projectx-sdk-1.0.0.aar
|---build
|---jars
|---src
|---build.gradle

And he is my gradle build file:

他是我的 gradle 构建文件:

apply plugin: 'android'

buildscript {
    repositories {
        mavenCentral()
        flatDir {
            dirs 'aars'
        }
    }
}

android {
    compileSdkVersion 19
    buildToolsVersion "19.0.1"

    defaultConfig {
        minSdkVersion 11
        targetSdkVersion 19
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
}

dependencies {
    compile 'com.android.support:gridlayout-v7:19.0.1'
    compile 'com.android.support:support-v4:19.0.1'
    compile 'com.android.support:appcompat-v7:19.0.1'


    compile 'com.projectx.photosdk:sdk:1.0.0@aar'
//    compile files( 'aars/sdk-1.0.0.aar' ) // Does not work either
}

// EDIT

// 编辑

The errors i am getting:

我得到的错误:

Failed to refresh Gradle project 'SuperAwesomeApp'
     Could not find com.projectx.photosdk:sdk:1.0.0.
     Required by:
     SuperAwesomeApp:App:unspecified

回答by Scott Barta

You put your flatDirblock in the wrong repostoriesblock. The repositoriesblock inside buildscripttells Gradle where to find the Android-Gradle plugin, but not the rest of the dependencies. You need to have another top-level repositoriesblock like this:

你把你的flatDir块放在错误的repostories块中。repositories里面的块buildscript告诉 Gradle 在哪里可以找到 Android-Gradle 插件,而不是其余的依赖项。您需要有另一个repositories像这样的顶级块:

repositories {
    mavenCentral()
    flatDir {
        dirs 'aars'
    }
}

I tested this and it works okay on my setup.

我对此进行了测试,它在我的设置上运行正常。

回答by pelotasplus

With recent versions of Android Studio, tested with 1.3, to use local .AAR file and not one fetched from maven/jcenter repository, just go to File > New > New moduleand choose Import .JAR/.AAR Package.

使用最新版本的 Android Studio,使用 1.3 进行测试,要使用本地 .AAR 文件而不是从 maven/jcenter 存储库中获取的文件,只需转到File > New > New module并选择Import .JAR/.AAR Package

What you will end up with is a new module in your project that contains very simple build.gradlefile that looks more or less like this:

你最终会在你的项目中得到一个新模块,它包含非常简单的build.gradle文件,看起来或多或少是这样的:

configurations.create("default")
artifacts.add("default", file('this-is-yours-package-in-aar-format.aar'))

Of course, other projects have to reference this new module with regular compile projectdirective. So in a project that uses this new module which is simple a local .aar file has this in it's build.gradle

当然,其他项目必须使用常规编译项目指令引用这个新模块 。所以在一个使用这个新模块的项目中,这个新模块很简单,一个本地 .aar 文件在它的build.gradle 中有这个

[...]
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    ompile 'com.android.support:appcompat-v7:23.1.0'
    compile 'com.android.support:design:23.1.0'
    [...]

    compile project(':name-of-module-created-via-new-module-option-described-above')
}
[...]

回答by Yen

In Android Studio 3.1.3 with gradle 3.0.1.
Simply adding implementation fileTree(dir: 'libs', include: ['*.aar'])or implementation files('libs/app-release.aar')without any other flatdirworks.

在带有 gradle 3.0.1 的 Android Studio 3.1.3 中。
只需添加implementation fileTree(dir: 'libs', include: ['*.aar'])implementation files('libs/app-release.aar')不添加任何其他flatdir作品。

回答by Stan Kurdziel

These days (over 1 year after this question) with Android Studio >1.0, local dependency does work properly:

这些天(在这个问题之后超过 1 年)使用 Android Studio > 1.0,本地依赖确实正常工作:

  • The android sdk looks for dependencies in a default local repo of: $ANDROID_HOME/extras/android/m2repository/
  • In a local library project you can publish the aar to this directory. Here's a snippet that can be added to your module's build.gradlefile (ex: sdk/build.gradle)

    apply plugin: 'maven'
    
    uploadArchives {
        repositories {
            mavenDeployer {
                repository(url: "file://localhost" + System.getenv("ANDROID_HOME")
                    + "/extras/android/m2repository/")
                pom.version = '1.0-SNAPSHOT'
                pom.groupId = 'your.package'
                pom.artifactId = 'sdk-name'
            }
        }
    }
    
  • In your library project, run ./gradlew uploadArchivesto publish the aar to that directory
  • In the application project you want to use the library in, add the dependency to your project/app/build.gradle. compile 'your.package:sdk-name:1.0-SNAPSHOT'
  • android sdk 在默认本地存储库中查找依赖项: $ANDROID_HOME/extras/android/m2repository/
  • 在本地库项目中,您可以将 aar 发布到此目录。这是可以添加到模块build.gradle文件中的代码段(例如:sdk/build.gradle)

    apply plugin: 'maven'
    
    uploadArchives {
        repositories {
            mavenDeployer {
                repository(url: "file://localhost" + System.getenv("ANDROID_HOME")
                    + "/extras/android/m2repository/")
                pom.version = '1.0-SNAPSHOT'
                pom.groupId = 'your.package'
                pom.artifactId = 'sdk-name'
            }
        }
    }
    
  • 在您的库项目中,运行./gradlew uploadArchives以将 aar 发布到该目录
  • 在您要使用该库的应用程序项目中,将依赖项添加到您的项目/app/build.gradle。 compile 'your.package:sdk-name:1.0-SNAPSHOT'

For local dependency, the next gradle build should find the previously deployed archive and that's it!

对于本地依赖,下一个 gradle 构建应该找到之前部署的存档,就是这样!



In my case, I use the above for local dev, but also have a Bamboo continuous integration server for the Library that publishes each build to a shared Nexus artifact repository. The full library code to deploy the artifact then becomes:

就我而言,我将上述内容用于本地开发,但也有一个 Bamboo 持续集成服务器用于将每个构建发布到共享 Nexus 工件存储库的库。用于部署工件的完整库代码变为:

uploadArchives {
    repositories {
        mavenDeployer {
            if (System.getenv("BAMBOO_BUILDNUMBER") != null) {
                // Deploy to shared repository
                repository(url: "http://internal-nexus.url/path/") {
                    authentication(userName: "user", password: "****")
                }
                pom.version = System.getenv("BAMBOO_BUILDNUMBER")
            } else {
                // Deploy to local Android sdk m2repository
                repository(url: "file://localhost" + System.getenv("ANDROID_HOME")
                        + "/extras/android/m2repository/")
                pom.version = '1.0-SNAPSHOT'
            }

            pom.groupId = 'your.package'
            pom.artifactId = 'sdk-name'
        }
    }
}

In order to tell applications to download from my internal Nexus repository, I added the internal Nexus maven repository just above jcenter() in both "repositories" blocks in the project/build.gradle

为了告诉应用程序从我的内部 Nexus 存储库下载,我在项目/build.gradle 的两个“存储库”块中的 jcenter() 上方添加了内部 Nexus maven 存储库

repositories {
    maven {
        url "http://internal-nexus.url/path/"
    }
    jcenter()
}

And application dependency then looks like compile 'your.package:sdk-name:45'When I update the 45 version to 46 is when my project will grab the new artifact from the Nexus server.

然后应用程序依赖性看起来像compile 'your.package:sdk-name:45'当我将 45 版本更新为 46 时,我的项目将从 Nexus 服务器获取新工件。

回答by mir

With the newest Gradle version there is now a slightly updated way of doing what Stan suggested (see maving publishing)

使用最新的 Gradle 版本,现在有一种稍微更新的方式来执行 Stan 建议的操作(请参阅maving 发布

apply plugin: 'maven-publish'

publishing {
    publications {
        aar(MavenPublication) {
            groupId 'org.your-group-id'
            artifactId 'your-artifact-id'
            version 'x.x.x'

            // Tell maven to prepare the generated "*.aar" file for publishing
            artifact("$buildDir/outputs/aar/${project.getName()}-release.aar")
        }
    }
    repositories {
        maven {
            url("file:" + System.getenv("HOME") + "/.m2/repository")
        }
    }
}

回答by pyus13

It seems adding .aar files as local dependency is not yet supported(Planned to be supported in 0.5.0 Beta)

似乎添加 .aar 文件,因为尚不支持本地依赖项(计划在 0.5.0 Beta 中支持)

https://code.google.com/p/android/issues/detail?id=55863

https://code.google.com/p/android/issues/detail?id=55863

But the way you are using your library in dependency will only work if your library is on central maven repository or in the local maven repository.

但是,您依赖库使用的方式只有在您的库位于中央 Maven 存储库或本地 Maven 存储库中时才有效。

Refer this for How to use local maven repository to use .aar in module dependencies.

有关如何使用本地 maven 存储库以在模块依赖项中使用 .aar 的信息,请参阅此内容。

http://www.flexlabs.org/2013/06/using-local-aar-android-library-packages-in-gradle-builds

http://www.flexlabs.org/2013/06/using-local-aar-android-library-packages-in-gradle-builds