如何在 Github 上创建库并通过 Android Studio 中的 gradle 依赖项使用它

How to create a library on Github and use it through gradle dependencies in Android Studio

我想创建库并通过 Internet 访问它。 在 Android Studio 中(通过 Gradle)可以这样添加依赖:

build.gradle(模块应用)中:

dependencies {
    ...
    compile 'com.android.support:design:23.1.0'
    compile 'com.squareup:otto:1.3.8'
    compile 'com.squareup.picasso:picasso:2.5.2'
    compile 'com.j256.ormlite:ormlite-core:4.48'
    compile 'com.j256.ormlite:ormlite-android:4.48'
    ...
}

如何从 github 以这种方式添加我自己的库?

参考 Jitpack 最好将您的项目或库从 Github 导入到 gradle

有关详细信息,请参阅 Gabriele Mariotti 答案

要实现它,您有一些方法:

  1. 在 central maven 或 jcenter 中发布你的库(工件)。
  2. 使用 github 存储库和 jitpack plugin
  3. 使用私有专家

第2点很简单。

只需将您的代码推送到 github 并在您要使用它的项目中修改 gradle 脚本。

只需将此 repo 添加到您的 build.gradle

repositories {
        // ...
        maven { url "https://jitpack.io" }
    }

和依赖项:

dependencies {
        compile 'com.github.User:Repo:Tag'
    }

要在Central Maven或JCenter中发布一个库,在一个答案中解释很长。但是您可以阅读这些帖子:

对于快速解决方案,正如其他人所说 JitPack is probably the way to go. However, if you want to make your library available to a wider audience you should probably add it to jcenter,因为现在 Android Studio 中已默认设置。 (以前是 Maven Central。)

This post 详细介绍了如何操作。以下为摘要:

  1. 创建 Android 库
  2. 测试以确保库在本地可用
  3. 在 Bintray 上发布库
  4. 将库添加到 Jcenter

然后所有使用你的库的人都必须在他们的 build.gradle 依赖项中添加一行。