pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

在 jCenter 中分發 Android 庫以在 gradle 中使用

distribute Android library in jCenter to use in gradle(在 jCenter 中分發 Android 庫以在 gradle 中使用)
本文介紹了在 jCenter 中分發 Android 庫以在 gradle 中使用的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我有一個庫項目,其中包含一個僅用于庫類和視圖的模塊.我一直在互聯網上搜索如何在 jCenter 中分發它以用作 gradle 依賴項,但沒有任何效果.

雖然這還沒有完成,我如何在其他項目中使用這個模塊?

PS:我在 Windows 10 上使用 Android Studio.

解決方案

網上的很多教程和指導已經過時或者很難理解.我自己剛剛學會了如何做到這一點,所以我添加了希望對您來說是一個快速解決方案的內容.它包括以下幾點

  • 從您的 Android 庫開始
  • 設置 Bintray 帳戶
  • 編輯項目的 gradle 文件
  • 將您的項目上傳到 Bintray
  • 將其鏈接到 jCenter

您要共享的庫

現在您可能已經建立了一個庫.為了這個示例,我在 Android Studio 中創建了一個包含一個 demo-app 應用程序模塊和一個 my-library 庫模塊的新項目.

下面是同時使用 Project 和 Android 視圖的樣子:

設置 Bintray 帳戶

Bintray 托管 jCenter 存儲庫.

將存儲庫命名為 maven.(不過,如果您想將多個庫項目組合在一起,您可以將其命名為其他名稱.如果這樣做,您還需要在下面的 gradle 文件中更改 bintrayRepo 名稱.)

選擇 Maven 作為存儲庫類型.

您可以根據需要添加說明.然后點擊創建.這就是我們現在在 Bintray 中需要做的所有事情.

編輯 gradle 文件

我將盡可能地進行剪切和粘貼,但不要忘記編輯必要的部分.您無需對演示應用模塊的 build.gradle 文件進行任何操作,只需對項目和庫的 gradle 文件進行操作即可.

項目構建.gradle

將 Bintray 和 Mavin 插件添加到您的項目 build.gradle 文件中.這是我的整個文件:

buildscript {存儲庫{中心()}依賴{類路徑'com.android.tools.build:gradle:2.3.2'//添加這些行(將它們更新為最新版本)類路徑'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7.3'類路徑'com.github.dcendents:android-maven-gradle-plugin:1.5'}}所有項目{存儲庫{中心()}}任務清理(類型:刪除){刪除 rootProject.buildDir}

上傳項目到 Bintray

打開終端并轉到項目的根文件夾.或者只使用 Android Studio 中的終端.

輸入以下命令

./gradlew install./gradlew bintray上傳

如果一切設置正確,它應該將您的庫上傳到 Bintray.如果失敗,請谷歌解決方案.(我第一次嘗試時必須更新我的 JDK.)

轉到您在 Bintray 中的帳戶,您應該會看到在您的存儲庫下輸入的庫.

鏈接到 jCenter

在您的 Bintray 庫中,有一個 添加到 jCenter 按鈕.

點擊它并發送您的請求.如果您獲得批準(需要一兩天時間),那么您的庫將成為 jCenter 的一部分,世界各地的開發人員只需在應用程序 build.gradlebuild.gradle 中添加一行即可將您的庫添加到他們的項目中code> 依賴塊.

依賴項{編譯 'com.example:my-library:1.0.0'}

恭喜!

注意事項

  • 您可能想要添加 PGP 加密,尤其是當您將其鏈接到 Maven Central 時.(不過,jCenter 已將 Maven Central 替換為 Android Studio 中的默認設置.)請參閱 本教程 尋求幫助.但也從 Bintray 閱讀此.

如何添加新版本

您最終會希望將新版本添加到您的 Bintray/jCenter 庫中.請參閱此答案了解如何操作.

進一步閱讀

  • 如何分發您的通過 Android Studio 中的 jCenter 和 Maven Central 擁有自己的 Android 庫
  • 創建和發布 Android 庫

I have a library project with a module that is just for library classes and views. I've been searching over the internet how to distribute it in jCenter to use as a gradle dependency but nothing works.

While this isn't done yet, how can I use this module in others projects?

PS: I use Android Studio on Windows 10.

解決方案

Many of the tutorials and directions online are out of date or are very hard to follow. I just learned how to do this myself, so I am adding what will hopefully be a quick solution for you. It includes the following points

  • Start with your Android library
  • Set up a Bintray account
  • Edit your project's gradle files
  • Upload your project to Bintray
  • Link it to jCenter

The library you want to share

By now you probably already have a library set up. For the sake of this example I made a new project with one demo-app application module and one my-library library module in Android Studio.

Here is what it looks like using both the Project and Android views:

Set up a Bintray account

Bintray hosts the jCenter repositories. Go to Bintray and set up a free account.

After you sign in click Add New Repository

Name the repository maven. (You can call it something else, though, if you want to group several library projects together. If you do you will also need to change the bintrayRepo name in the gradle file below.)

Chose Maven as the repository type.

You can add a description if you want. Then click Create. That's all we need to do in Bintray for now.

Edit the gradle files

I'm going to make this as cut-and-paste as possible, but don't forget to edit the necessary parts. You don't need to do anything with the demo app module's build.gradle file, only the gradle files for the project and the library.

Project build.gradle

Add the Bintray and Mavin plugins to your project build.gradle file. Here is my whole file:

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

        // Add these lines (update them to whatever the newest version is)
        classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7.3'
        classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

The newest version for Bintray is here and Maven is here.

Library build.gradle

Edit everything you need to in the ext block below.

apply plugin: 'com.android.library'

// change all of these as necessary
ext {
    bintrayRepo = 'maven'  // this is the same as whatever you called your repository in Bintray
    bintrayName = 'my-library' // your bintray package name. I am calling it the same as my library name.

    publishedGroupId = 'com.example'
    libraryName = 'my-library'
    artifact = 'my-library' // I'm calling it the same as my library name

    libraryDescription = 'An example library to make your programming life easy'

    siteUrl = 'https://github.com/example/my-library'
    gitUrl = 'https://github.com/example/my-library.git'

    libraryVersion = '1.0.0'

    developerId = 'myID' // Maven plugin uses this. I don't know if it needs to be anything special.
    developerName = 'My Name'
    developerEmail = 'myemail@example.com'

    licenseName = 'The MIT License (MIT)'
    licenseUrl = 'https://opensource.org/licenses/MIT'
    allLicenses = ["MIT"]
}

// This next section is your normal gradle settings
// There is nothing special that you need to change here
// related to Bintray. Keep scrolling down.

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"

    defaultConfig {
        minSdkVersion 9
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.3.1'
    testCompile 'junit:junit:4.12'
}

// Maven section
// You shouldn't need to change anything. It just uses the
// values you set above.

apply plugin: 'com.github.dcendents.android-maven'

group = publishedGroupId // Maven Group ID for the artifact

install {
    repositories.mavenInstaller {
        // This generates POM.xml with proper parameters
        pom {
            project {
                packaging 'aar'
                groupId publishedGroupId
                artifactId artifact

                // Add your description here
                name libraryName
                description libraryDescription
                url siteUrl

                // Set your license
                licenses {
                    license {
                        name licenseName
                        url licenseUrl
                    }
                }
                developers {
                    developer {
                        id developerId
                        name developerName
                        email developerEmail
                    }
                }
                scm {
                    connection gitUrl
                    developerConnection gitUrl
                    url siteUrl

                }
            }
        }
    }
}

// Bintray section
// As long as you add bintray.user and bintray.apikey to the local.properties
// file, you shouldn't have to change anything here. The reason you 
// don't just write them here is so that they won't be publicly visible
// in GitHub or wherever your source control is.

apply plugin: 'com.jfrog.bintray'

version = libraryVersion

if (project.hasProperty("android")) { // Android libraries
    task sourcesJar(type: Jar) {
        classifier = 'sources'
        from android.sourceSets.main.java.srcDirs
    }

    task javadoc(type: Javadoc) {
        source = android.sourceSets.main.java.srcDirs
        classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
    }
} else { // Java libraries
    task sourcesJar(type: Jar, dependsOn: classes) {
        classifier = 'sources'
        from sourceSets.main.allSource
    }
}

task javadocJar(type: Jar, dependsOn: javadoc) {
    classifier = 'javadoc'
    from javadoc.destinationDir
}

artifacts {
    archives javadocJar
    archives sourcesJar
}

Properties properties = new Properties()
properties.load(project.rootProject.file('local.properties').newDataInputStream())

bintray {
    user = properties.getProperty("bintray.user")
    key = properties.getProperty("bintray.apikey")

    configurations = ['archives']
    pkg {
        repo = bintrayRepo
        name = bintrayName
        desc = libraryDescription
        websiteUrl = siteUrl
        vcsUrl = gitUrl
        licenses = allLicenses
        publish = true
        publicDownloadNumbers = true
        version {
            desc = libraryDescription
            gpg {
                // optional GPG encryption. Default is false.
                sign = false
                //passphrase = properties.getProperty("bintray.gpg.password")
            }
        }
    }
}

local.properties

The library build.gradle file above referenced some values in the local.properties file. We need to add those now. This file is located in the root of your project. It should be included in .gitignore. (If it isn't then add it.) The point of putting your username, api key, and encryption password here is so that it won't be publicly visible in version control.

## This file is automatically generated by Android Studio.
# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
#
# This file should *NOT* be checked into Version Control Systems,
# as it contains information specific to your local configuration.
#
# Location of the SDK. This is only used by Gradle.
# For customization when using a Version Control System, please read the
# header note.
sdk.dir=/home/yonghu/Android/Sdk

# Add these lines (but change the values according to your situation)
bintray.user=myusername
bintray.apikey=1f2598794a54553ba68859bb0bf4c31ff6e71746

There is a warning about not modifying this file but it seems to work well anyway. Here is how you get the values:

  • bintray.user: This is your Bintray username.
  • bintray.apikey: Go to Edit Profile in the Bintray menu and choose API Key. Copy it from here.

Upload project to Bintray

Open a terminal and go to your project's root folder. Or just use the terminal in Android Studio.

Enter the following commands

./gradlew install
./gradlew bintrayUpload

If everything is set up right it should upload your library to Bintray. If it fails then Google the solution. (I had to update my JDK the first time I tried.)

Go to your account in Bintray and you should see the library entered under your repository.

Link to jCenter

In your library in Bintray there is an Add to jCenter button.

Click it and send your request. If you are approved (which takes a day or two), then your library will be a part of jCenter and developers around the world can add your library to their projects simply by adding one line to the app build.gradle dependencies block.

dependencies {
    compile 'com.example:my-library:1.0.0'
}

Congratulations!

Notes

  • You may want to add PGP encryption, especially if you are linking it to Maven Central. (jCenter has replaced Maven Central as the default in Android Studio, though.) See this tutorial for help with that. But also read this from Bintray.

How to add a new version

You will eventually want to add a new version to your Bintray/jCenter library. See this answer to directions on how to do it.

Further Reading

  • How to distribute your own Android library through jCenter and Maven Central from Android Studio
  • Creating and Publishing an Android Library

這篇關于在 jCenter 中分發 Android 庫以在 gradle 中使用的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

IncompatibleClassChangeError after updating to Android Build Tools 25.1.6 GCM / FCM(更新到 Android Build Tools 25.1.6 GCM/FCM 后出現 IncompatibleClassChangeError)
How to get current flavor in gradle(如何在 gradle 中獲取當前風味)
How to fix quot;unexpected element lt;queriesgt; found in lt;manifestgt;quot; error?(如何修復“意外元素lt;查詢gt;在“清單中找到錯誤?)
Multi flavor app based on multi flavor library in Android Gradle(基于 Android Gradle 中多風味庫的多風味應用)
Android dependency has different version for the compile and runtime(Android 依賴在編譯和運行時有不同的版本)
Transitive dependencies for local aar library(本地 aar 庫的傳遞依賴)
主站蜘蛛池模板: 注塑机-压铸机-塑料注塑机-卧式注塑机-高速注塑机-单缸注塑机厂家-广东联升精密智能装备科技有限公司 | 变色龙云 - 打包app_原生app_在线制作平台_短链接_ip查询 | 查分易-成绩发送平台官网 | 通辽信息港 - 免费发布房产、招聘、求职、二手、商铺等信息 www.tlxxg.net | 郑州水质检测中心_井水检测_河南废气检测_河南中环嘉创检测 | 打包箱房_集成房屋-山东佳一集成房屋有限公司 | AR开发公司_AR增强现实_AR工业_AR巡检|上海集英科技 | 软膜天花_软膜灯箱_首选乐创品牌_一站式天花软膜材料供应商! | 泉州陶瓷pc砖_园林景观砖厂家_石英砖地铺石价格 _福建暴风石英砖 | 自动焊锡机_点胶机_螺丝机-锐驰机器人 | 网站建设-高端品牌网站设计制作一站式定制_杭州APP/微信小程序开发运营-鼎易科技 | 北京网络营销推广_百度SEO搜索引擎优化公司_网站排名优化_谷歌SEO - 北京卓立海创信息技术有限公司 | 网站seo优化_seo云优化_搜索引擎seo_启新网络服务中心 | 水厂自动化|污水处理中控系统|水利信息化|智慧水务|智慧农业-山东德艾自动化科技有限公司 | 干粉砂浆设备-干粉砂浆生产线-干混-石膏-保温砂浆设备生产线-腻子粉设备厂家-国恒机械 | 电磁流量计厂家_涡街流量计厂家_热式气体流量计-青天伟业仪器仪表有限公司 | 机器视觉检测系统-视觉检测系统-机器视觉系统-ccd检测系统-视觉控制器-视控一体机 -海克易邦 | 众品地板网-地板品牌招商_地板装修设计_地板门户的首选网络媒体。 | 517瓜水果特产网|一个专注特产好物的网站 | 磨煤机配件-高铬辊套-高铬衬板-立磨辊套-盐山县宏润电力设备有限公司 | 施工电梯_齿条货梯_烟囱电梯_物料提升机-河南大诚机械制造有限公司 | 不锈钢拉手厂家|浴室门拉手厂家|江门市蓬江区金志翔五金制品有限公司 | 垃圾压缩设备_垃圾处理设备_智能移动式垃圾压缩设备--山东明莱环保设备有限公司 | 飞行者联盟-飞机模拟机_无人机_低空经济_航空技术交流平台 | 众品地板网-地板品牌招商_地板装修设计_地板门户的首选网络媒体。 | 企典软件一站式企业管理平台,可私有、本地化部署!在线CRM客户关系管理系统|移动办公OA管理系统|HR人事管理系统|人力 | SDI车窗夹力测试仪-KEMKRAFT方向盘测试仪-上海爱泽工业设备有限公司 | 淋巴细胞分离液_口腔医疗器材-精欣华医疗器械(无锡)有限公司 | 全自动包装秤_全自动上袋机_全自动套袋机_高位码垛机_全自动包装码垛系统生产线-三维汉界机器(山东)股份有限公司 | 手机游戏_热门软件app下载_好玩的安卓游戏下载基地-吾爱下载站 | 闭端端子|弹簧螺式接线头|防水接线头|插线式接线头|端子台|电源线扣+护线套|印刷电路板型端子台|金笔电子代理商-上海拓胜电气有限公司 | 中式装修设计_室内中式装修_【云臻轩】中式设计机构 | 上海律师咨询_上海法律在线咨询免费_找对口律师上策法网-策法网 广东高华家具-公寓床|学生宿舍双层铁床厂家【质保十年】 | 电销卡_北京电销卡_包月电话卡-豪付网络 | 东风体检车厂家_公共卫生体检车_医院体检车_移动体检车-锦沅科贸 | 电动葫芦-河北悍象起重机械有限公司 | 蓝米云-专注于高性价比香港/美国VPS云服务器及海外公益型免费虚拟主机 | 袋式过滤器,自清洗过滤器,保安过滤器,篮式过滤器,气体过滤器,全自动过滤器,反冲洗过滤器,管道过滤器,无锡驰业环保科技有限公司 | 合肥抖音SEO网站优化-网站建设-网络推广营销公司-百度爱采购-安徽企匠科技 | 无锡市珂妮日用化妆品有限公司|珂妮日化官网|洗手液厂家 | 物联网卡_物联网卡购买平台_移动物联网卡办理_移动联通电信流量卡通信模组采购平台? |