简体   繁体   English

Android Studio如何使用具有远程依赖关系的gradle构建aar文件。

[英]Android Studio How to build an aar file with gradle that has remote dependencies.

I created a Android Library Module with Android Studio and I was able to use it in the apps and right now I need to use it for my other apps. 我使用Android Studio创建了一个Android库模块,我可以在应用程序中使用它,现在我需要将它用于我的其他应用程序。 So I was thinking about using the remote dependecies like Picasso https://github.com/square/picasso 所以我在考虑使用像Picasso这样的远程家属https://github.com/square/picasso

 compile 'com.squareup.picasso:picasso:2.5.2'

And I would like to know what are the steps I need to take? 我想知道我需要采取的步骤是什么? I read few article and website. 我读了几篇文章和网站。 It is very confusing. 这非常令人困惑。

To make your library available as a remote dependency there are only three steps you have to take: 要使您的库可用作远程依赖项,您只需要执行三个步骤:

  1. Build your library and provide the necessary repository meta data 构建库并提供必要的存储库元数据
  2. Put your library with meta data in a repository 将包含元数据的库放在存储库中
  3. Tell your project to look in that repository for dependencies 告诉您的项目在该存储库中查找依赖项

After that is in place you can have remote dependencies like this in your app's build.gradle file: 在此之后,您可以在应用程序的build.gradle文件中拥有这样的远程依赖项:

compile 'com.example.developer:lib-util:1.0.0'

This will look in all the repositories that you have registered for a group or organization called 'com.example.developer', and then an artifact named 'lib-util' with version '1.0.0'. 这将查看您为名为“com.example.developer”的组或组织注册的所有存储库,然后查找名为“lib-util”且版本为“1.0.0”的工件。

Build your library and provide the necessary repository meta data 构建库并提供必要的存储库元数据

That may sound complex but it's really not that hard. 这可能听起来很复杂,但实际上并不那么难。 It's just a directory structure that looks a little like this: 它只是一个看起来像这样的目录结构:

com
 |-- example
      |-- developer
           |-- lib-util
                |-- 1.0.0
                |    |-- lib-util-1.0.0.aar
                |    |-- lib-util-1.0.0.aar.md5
                |    |-- lib-util-1.0.0.pom
                |    |-- lib-util-1.0.0.pom.md5
                |-- maven-metadata.xml
                |-- maven-metadata.xml.md5

The file lib-util-1.0.0.aar is the compiled library (in Android Archive format). lib-util-1.0.0.aar文件是编译库(采用Android Archive格式)。 The lib-util-1.0.0.pom file contains information about the library itself, such as the authors and its dependencies. lib-util-1.0.0.pom文件包含有关库本身的信息,例如作者及其依赖项。 The maven-metadata.xml file contains the necessary information to know which versions are available (in this case just one). maven-metadata.xml文件包含了解哪些版本可用的必要信息(在这种情况下只有一个版本)。 And lastly the *.md5 files contain a checksum to verify file integrities. 最后, *.md5文件包含校验和以验证文件的完整性。 (There are also *.sha1 checksum files that I've left out for brevity.) (为简洁起见,我还遗漏了*.sha1校验和文件。)

To build this structure you can make use of the Maven Gradle plugin. 要构建此结构,您可以使用Maven Gradle插件。 Put it in your library's build.gradle and configure the mavenDeployer properties: 将它放在库的build.gradle中并配置mavenDeployer属性:

library build.gradle: library build.gradle:

apply plugin: 'com.android.library'
apply plugin: 'maven'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.1"

    defaultConfig {
        minSdkVersion 16
        targetSdkVersion 23
        versionCode 1
        versionName "1.0.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

uploadArchives {
    repositories.mavenDeployer {
        pom.groupId = 'com.example.developer'
        pom.artifactId = 'lib-util'
        pom.version = android.defaultConfig.versionName
        // Add other pom properties here if you want (developer details / licenses)
        repository(url: "file:./releases/")
    }
}

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

The above example is a stock library build.gradle as Android Studio 1.4 produces it for a new library but with the Maven Gradle plugin added and configured. 上面的示例是一个库存build.gradle,因为Android Studio 1.4为新库生成它,但添加并配置了Maven Gradle插件。

The second line: apply plugin: 'maven' , adds the plugin. 第二行: apply plugin: 'maven' ,添加插件。 The uploadArchives closure configures it. uploadArchives闭包配置它。

  • pom.groupId is the name of the organization/group. pom.groupId是组织/组的名称。 This will be written in the .pom file but is primarily used to actually find your library as a remote dependency. 这将写在.pom文件中,但主要用于实际查找您的库作为远程依赖项。
  • pom.artifactId is the name of your library. pom.artifactId是库的名称。 Again, this will be put in the .pom file but is used to actually find your lib. 同样,这将被放在.pom文件中,但用于实际找到你的lib。
  • pom.version is the version of your library. pom.version是您的库的版本。 You will have to increase this when you make a new release. 在制作新版本时,您必须增加此值。 Also put in the .pom and used to locate your lib. 也放入.pom并用于定位你的lib。

The line repository(url: "file:./releases/") configures the plugin to write the structure to a filesystem directory relative to the project root. repository(url: "file:./releases/")将插件配置为将结构写入相对于项目根目录的文件系统目录。

If you run the following Gradle command in the root directory of your project you should see the Maven directory structure being built. 如果在项目的根目录中运行以下Gradle命令,则应该看到正在构建的Maven目录结构。

./gradlew clean uploadArchives

This will first clean your build and then build and perform the uploadArchives task which will create the releases directory. 这将首先清理您的构建,然后构建并执行uploadArchives任务,该任务将创建版本目录。

Put your library with meta data in a repository 将包含元数据的库放在存储库中

You already have a local Maven repository right now, namely on your local filesystem in your lib-util's project directory. 您现在已经有了一个本地Maven存储库,即在lib-util的项目目录中的本地文件系统上。

To make it an online remote Maven repository it only has to be reachable by http (or https) get-requests. 要使其成为在线远程Maven存储库,只需通过http(或https)get-requests即可访问。 If you have a server with a webserver like Apache or Nginx or IIS you can configure that to host the files. 如果您的服务器具有Apache或Nginx或IIS等Web服务器,则可以配置该服务器来托管文件。 Or you can check them into your Github account and let that host it. 或者您可以将它们检入您的Github帐户并让它托管它。 You could even copy those files to Dropbox and use it to host them. 您甚至可以将这些文件复制到Dropbox并使用它来托管它们。

If you want to have it publicly available over Maven Central or JCenter you will have to go to their websites and register for an account where you claim your 'groupId' and you can then use their systems to upload the files so they will be hosted by them. 如果您希望通过Maven Central或JCenter公开发布,您必须访问他们的网站并注册一个您声称拥有'groupId'的帐户,然后您可以使用他们的系统上传文件,以便他们将托管他们。

Tell your project to look in that repository for dependencies 告诉您的项目在该存储库中查找依赖项

By default Android Studio registers your project to look for dependencies in JCenter. 默认情况下,Android Studio会注册您的项目以查找JCenter中的依赖项。 Previously it used to default to Maven Central. 以前它曾经默认为Maven Central。 So if you have actually uploaded your library to those repositories you wont have to do anything as it will already work. 因此,如果您实际上已将您的库上传到这些存储库,则您不必执行任何操作,因为它已经可以正常工作。

Suppose you want to configure your project to look for remote dependencies on your local filesystem in /Users/rob/projects/lib-util/releases, and in a self-hosted Maven repository at http://developer.example.com/repo/ . 假设您要将项目配置为在/ Users / rob / projects / lib-util / releases中以及在http://developer.example.com/repo上的自托管Maven存储库中查找本地文件系统的远程依赖项。 / Add those repository urls to your project's build.gradle (so not in your library's): 将这些存储库URL添加到项目的build.gradle中(因此不在库中):

project build.gradle: project build.gradle:

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.3.0'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        maven { url '/Users/rob/projects/lib-util/releases/' }
        maven { url 'http://developer.example.com/repo/' }
        jcenter()
    }
}

The example above is the stock project build.gradle as Android Studio provides it with just two extra repositories. 上面的示例是stock项目build.gradle,因为Android Studio只为它提供了两个额外的存储库。 Looking dependencies up goes in the order listed. 查找依赖关系按列出的顺序排列。 So in this case it first checks the local filesystem, then the self-hosted repo and finally it looks in JCenter. 所以在这种情况下,它首先检查本地文件系统,然后检查自托管的repo,最后查看JCenter。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在.aar文件中添加gradle依赖项(build.gradle) Android - Add gradle dependencies(build.gradle) in .aar file Android 如何发送具有远程依赖项(gradle)的Android库(aar)? - How do I ship an Android Library (aar) with remote dependencies (gradle)? 懒惰的 Android 依赖项。 如何在 gradle 依赖解析之前构建 AARs 库 - Lazy Android dependencies. How to build AARs libraries before gradle dependency resolution 如何在 Android Studio 中构建 CHAQUOPY Android 库(.AAR 或 .JAR 文件) - How to build CHAQUOPY Android library (.AAR or .JAR file) in Android Studio Android:如何将AAR的依赖关系构建到APK中? - Android: How to build AAR's dependencies into APK? android studio build.gradle文件中的flavor依赖关系,这可能吗? - dependencies by flavor in android studio build.gradle file, is it possible? Android AAR文件依赖项 - Android aar file dependencies 远端AAR在Android Studio中如何运作 - How does remote aar work in Android Studio 无法使用gradle build dystem将生成的.aar文件上传到android studio中的本地Maven存储库 - Not able to upload the generated .aar file to local maven repository in android studio with gradle build dystem gradle可以解决Android .aar文件中的依赖关系吗? - Can gradle resolve dependencies from just a Android .aar file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM