Andriod生成aar包、引用aar包

一、生成aar包

  1. 将 apply plugin: ‘com.android.application’ 改为 apply plugin:
    ‘com.android.library’

  2. 去掉applicationId

  3. 点击右侧的assembleRelease,看到module层的outputs文件夹下生成了.aar文件

二、引用aar包

  1. 将aar包拷贝进libs目录下
  2. 配置module下的build.gradle文件在这里插入图片描述
repositories {
    flatDir {
        dirs 'libs'
    }
}
 implementation project(':libaarname')

上面的方法就可以正常使用了

三、注意事项

有些情况我们要生成的aar包需要引用其他第三方库

  1. 在项目根目录的build.gradle里添加
 dependencies {
        classpath 'com.kezong:fat-aar:1.2.8'
    }

  1. 在需要打包成aar包的module工程里添加
apply plugin: 'com.kezong.fat-aar'
  1. arr包以及第三方库包的引用方式如下:
dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'androidx.appcompat:appcompat:1.0.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
    embed(name: 'aar包名称', ext: 'aar'){ transitive = true}
   
    embed ('com.squareup.okio:okio:1.14.1'){ transitive = true}
    embed ('com.squareup.okhttp3:okhttp:3.8.0'){ transitive = true}
    embed ('com.google.code.gson:gson:2.8.0'){ transitive = true}
}

版权声明:本文为qq_34906385原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。