项目中制作SDK
1.aar包是Android studio下打包Android工程中src、res、lib后生成的aar文件,aar包导入其他Android studio 工程后,其他工程可以方便引用源码和资源文件
2.生成aar包步骤:
① 新版本上Android studio4.2打开一个工程,然后新建一个Module,新建Module时候侧边选择Android Library,后面按新建普通工程操作:
② 新建Module 类型为android Library 后如下图所示
③ .在新建的Module中编写完代码后,接下来编译整个工程后就会自动生成aar包,包的路径在新建的Module ==》 build ===》outputs ==>aar目录下。 如图
3.其他androidstudio工程引用aar包
①.将aar包复制到lib目录下
②.配置build.gradle文件:
加入
repositories {
flatDir {
dirs ‘libs’
}
}
implementation(name: ‘mylibrary-debug’, ext: ‘aar’)
完整的配置文件:
plugins {
id ‘com.android.application’
}
android {
compileSdkVersion 30
buildToolsVersion “30.0.3”
defaultConfig {
applicationId "com.hidi.demo4"
minSdkVersion 16
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
repositories {
flatDir {
dirs 'libs'
}
}
repositories {
flatDir {
dirs 'libs'
}
}
}
dependencies {
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.2.1'
implementation 'androidx.constraintlayout:constraintlayout:2.0.1'
testImplementation 'junit:junit:4.+'
implementation(name: 'mylibrary-debug', ext: 'aar')
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}
④ .编译一次工程
如图复制aar包到lib下
图复制到lib下后,点击下图按钮让aar包可以在在代码中引用,或者编译一次工程
如上全部操作完成后,可以在自己的工程中引用 SDK 包中的内容。这样就完成了 aar 的引入
注意:
1.同一个工程中配置中只需要
implementation(name: ‘libraryName-debug’, ext: ‘aar’)
不需要:
implementation project(path: ‘:libraryName’)
否则会报错
2.library 中的包名不要和主Module 中的报名重复,自己重新去一个新的不一样的名字。