什么是arr包?
arr包是AndroidStudio下打包Android工程中的src、res、lib后生成的arr文件,arr包导入其他AndroidStudio工程后,其他工程可以方便引用源码和资源文件
生成arr包步骤
1.用AndroidStudio打开一个工程,然后新建一个Module,如下图:
2.新建Module时候选择Android Library,后面按新建普通工程操作,如下图所示:
3.新建Module类型为Android Library后,如下图所示:
4.在新建的Module中编写完代码后,接下来编译整个工程后就会自动生成aar包
5.包的路径在新建Module->build->outputs->aar目录下,如下图所示
在其他工程引用aar
1.将aar复制到lib目录下:
2.配置build.gradle文件,加入下面的代码:
repositories {
flatDir {
dirs 'libs'
}
compile(name:'mylibrary-debug', ext:'aar')
完整的配置文件:
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.example.myapplication"
minSdkVersion 21
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
repositories {
flatDir {
dirs 'libs'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
compile(name: 'mylibrary-debug', ext: 'aar')
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
3.编译一次工程
4.以上操作成功后可以在扩展包下看到被引用的aar包文件,如下图所示: