Android如何构建自己的库在build.gradle中引用-教程+跳坑

开发Android也有一定的年头的,承蒙大神们通过不懈的努力带给我们众多的便利!或许也到自己贡献的时候了,那么我们先学习一下如何构建自己的库,并且在gradle中的引用!

步骤

前期准备:

新建一个project,提交到github上面去

 

Android如何构建自己的库在build.gradle中引用-教程+跳坑_第1张图片

Android如何构建自己的库在build.gradle中引用-教程+跳坑_第2张图片

 

创建lib的module

这个module里的代码就是别人可以引用的代码

Android如何构建自己的库在build.gradle中引用-教程+跳坑_第3张图片

创建你分享的代码,例如下图

Android如何构建自己的库在build.gradle中引用-教程+跳坑_第4张图片

在github上创建release版本

把所有的代码提交到github上面后,如下图所示在github上创建release版本

Android如何构建自己的库在build.gradle中引用-教程+跳坑_第5张图片

Android如何构建自己的库在build.gradle中引用-教程+跳坑_第6张图片

Android如何构建自己的库在build.gradle中引用-教程+跳坑_第7张图片

jitpack上获取路径

生成release包之后开始,打开https://jitpack.io,登录自己的github账号,把仓库的地址复制进去即可,如果log图标和status的按钮都是原谅色,那么恭喜你,成功了。点击get it,下面有引入教程

Android如何构建自己的库在build.gradle中引用-教程+跳坑_第8张图片

 

引用自己的库

Android如何构建自己的库在build.gradle中引用-教程+跳坑_第9张图片

 

跳坑

project目录下的build.gradle设置

//低版本的gradle使用的plugin也相应会低一点
classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1'

Android如何构建自己的库在build.gradle中引用-教程+跳坑_第10张图片

 

module目录下的build.gradle设置

apply plugin: 'com.github.dcendents.android-maven'
group='com.github.KubyWong'//com.github.{你的的账号名}

Android如何构建自己的库在build.gradle中引用-教程+跳坑_第11张图片

 

https://jitpack.io红色log如何处理

Android如何构建自己的库在build.gradle中引用-教程+跳坑_第12张图片

Android如何构建自己的库在build.gradle中引用-教程+跳坑_第13张图片

如上图,正确的情况下log是绿色的,搞这个的时候感觉有点背,无论怎么提交都是出错的,在项目中当然也无法正常引入。当出现下面这个问题的时候,别想了,重新来一次创建流程。(起一个project,放到github上,创建release版本,jitpack中获取状态信息)

Build starting...
Start: Thu Jun 27 14:34:38 UTC 2019 861df077f5ba
Git:
1.2.3-0-gd0f9aee
commit d0f9aeec13db0bbe6abd4aafeffc6bda18f423a7
Author: KubyHuang 
Date:   Thu Jun 27 22:29:49 2019 +0800

    使用1.5的插件地址
    com.github.dcendents:android-maven-gradle-plugin:1.5

Found Android manifest
Android SDK version: 28. Build tools: 29.0.0
Picked up JAVA_TOOL_OPTIONS: -Dfile.encoding=UTF-8 -Dhttps.protocols=TLSv1.2
Warning: File /opt/android-sdk-linux/.android/repositories.cfg could not be loaded.

Installing Android platform 28
Picked up JAVA_TOOL_OPTIONS: -Dfile.encoding=UTF-8 -Dhttps.protocols=TLSv1.2
Warning: File /opt/android-sdk-linux/.android/repositories.cfg could not be loaded.
Installing Build-tools 29.0.0 
Picked up JAVA_TOOL_OPTIONS: -Dfile.encoding=UTF-8 -Dhttps.protocols=TLSv1.2
Warning: File /opt/android-sdk-linux/.android/repositories.cfg could not be loaded.
Found gradle
Gradle build script
Using gradle wrapper
Picked up JAVA_TOOL_OPTIONS: -Dfile.encoding=UTF-8 -Dhttps.protocols=TLSv1.2
Downloading https://services.gradle.org/distributions/gradle-5.1.1-all.zip
.
Unzipping /home/jitpack/.gradle/wrapper/dists/gradle-5.1.1-all/97z1ksx6lirer3kbvdnh7jtjg/gradle-5.1.1-all.zip to /home/jitpack/.gradle/wrapper/dists/gradle-5.1.1-all/97z1ksx6lirer3kbvdnh7jtjg
Set executable permissions for: /home/jitpack/.gradle/wrapper/dists/gradle-5.1.1-all/97z1ksx6lirer3kbvdnh7jtjg/gradle-5.1.1/bin/gradle

Welcome to Gradle 5.1.1!

Here are the highlights of this release:
- Control which dependencies can be retrieved from which repositories
- Production-ready configuration avoidance APIs

For more details see https://docs.gradle.org/5.1.1/release-notes.html

------------------------------------------------------------
Gradle 5.1.1
------------------------------------------------------------

Build time:   2019-01-10 23:05:02 UTC
Revision:     3c9abb645fb83932c44e8610642393ad62116807

Kotlin DSL:   1.1.1
Kotlin:       1.3.11
Groovy:       2.5.4
Ant:          Apache Ant(TM) version 1.9.13 compiled on July 10 2018
JVM:          1.8.0_191 (Oracle Corporation 25.191-b12)
OS:           Linux 4.14.63-xxxx-std-ipv6-64 amd64

Getting tasks: ./gradlew tasks --all
0m4.655s
Picked up JAVA_TOOL_OPTIONS: -Dfile.encoding=UTF-8 -Dhttps.protocols=TLSv1.2
Tasks: 

WARNING:
Gradle 'install' task not found. Please add the 'maven' or 'android-maven' plugin.
See the documentation and examples: https://jitpack.io/docs/

Adding maven plugin
Found android library build file in app
Running: ./gradlew clean -Pgroup=com.github.KubyWong -Pversion=1.2.3 install
Picked up JAVA_TOOL_OPTIONS: -Dfile.encoding=UTF-8 -Dhttps.protocols=TLSv1.2

> Configure project :
Gradle version Gradle 5.1.1

FAILURE: Build failed with an exception.

* What went wrong:
Task 'install' not found in root project 'build'.

* Try:
Run gradlew tasks to get a list of available tasks. Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 1s
Build tool exit code: 0
Looking for artifacts...
Picked up JAVA_TOOL_OPTIONS: -Dfile.encoding=UTF-8 -Dhttps.protocols=TLSv1.2
Picked up JAVA_TOOL_OPTIONS: -Dfile.encoding=UTF-8 -Dhttps.protocols=TLSv1.2
Looking for pom.xml in build directory and ~/.m2
2019-06-27T14:35:20.897507593Z
Exit code: 0

ERROR: No build artifacts found

 

你可能感兴趣的:(Android开发)