在我的项目里,使用到了UVC camera子Module,但是看着总感觉不舒服。所以就想抽取出这个子模块,打包成AAR文件,引用进来。
第一步:打包
把libuvccamera独立出去成一个项目打开。并修改Gradle配置文件。
//apply plugin: 'com.android.application' 如果是application要改成library
apply plugin: 'com.android.library'
然后同步一下,报了一个错误:
Gradle sync failed: Plugin with id 'com.android.library' not found.
发现是没有配置Gradle信息,加上吧。repositories也得加上,不然会无法下载依赖包。
buildscript {
repositories {
maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }
maven { url 'http://maven.aliyun.com/nexus/content/repositories/jcenter' }
maven { url 'http://maven.aliyun.com/nexus/content/repositories/google' }
maven { url 'http://maven.aliyun.com/nexus/content/repositories/gradle-plugin' }
maven { url "https://jitpack.io" }
google()
jcenter()
mavenCentral()
}
dependencies { classpath 'com.android.tools.build:gradle:3.6.3'}
}
allprojects {
repositories {
maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }
maven { url 'http://maven.aliyun.com/nexus/content/repositories/jcenter' }
maven { url 'http://maven.aliyun.com/nexus/content/repositories/google' }
maven { url 'http://maven.aliyun.com/nexus/content/repositories/gradle-plugin' }
maven { url "https://jitpack.io" }
google()
jcenter()
maven { url "https://maven.google.com" }
}
}
一切正常后,开始打包
点击右侧的assembleRelease,执行完毕后,可以看到outputs文件夹下生成了.aar文件,不同的task生成不同的aar文件。
第二步:使用
aar已经打包完毕,接下来就是使用了。回到原项目中,把aar包扔进libs目录下,然后在Gradle文件添加以下信息
repositories {
flatDir { dirs 'libs' }
}
然后
dependencies {
implementation(name: 'libuvccamera-release', ext: 'aar')
}
同步一下,正常的话,就可以使用了。
aar文件和jar文件的区别
jar文件:只包含了class文件与清单文件,不包含资源文件,如图片等所有res中的文件,
aar文件则包含所有资源,class以及res资源文件。所以如果你有资源文件需要,那么你就以aar文件形式引入到工程中去,反之直接引入jar即可。