1. 程式人生 > >android studio生成aar包並在其他工程引用aar包

android studio生成aar包並在其他工程引用aar包

file jar 源碼 tool details bsp pbo key com

1.aar包是android studio下打包android工程中src、res、lib後生成的aar文件,aar包導入其他android studio 工程後,其他工程可以方便引用源碼和資源文件

2.生成aar包步驟:

①.用android studio打開一個工程,然後新建一個Module,新建Module時候選擇Android Library,後面按新建普通工程操作

技術分享圖片

技術分享圖片

②.新建Module 類型為android Library 後如下圖所示

技術分享圖片

③.在新建的Module中編寫完代碼後,接下來編譯整個工程後就會自動生成aar包,包的路徑在新建的Module ==》 build ===》outputs ==>aar目錄下。 如圖

技術分享圖片

技術分享圖片

3.其他androidstudio工程引用aar包

①.將aar包復制到lib目錄下

②.配置build.gradle文件:

加入

repositories {
flatDir {
dirs ‘libs‘
}

compile(name:‘camerascan-1.0‘, ext:‘aar‘)

完整的配置文件:

[java] view plain copy
  1. apply plugin: ‘com.android.application‘
  2. android {
  3. compileSdkVersion 22
  4. buildToolsVersion "22.0.1"
  5. defaultConfig {
  6. applicationId "com.geenk.testcamerascanarr"
  7. minSdkVersion 11
  8. targetSdkVersion 22
  9. versionCode 1
  10. versionName "1.0"
  11. }
  12. buildTypes {
  13. release {
  14. minifyEnabled false
  15. proguardFiles getDefaultProguardFile(‘proguard-android.txt‘), ‘proguard-rules.pro‘
  16. }
  17. }
  18. repositories {
  19. flatDir {
  20. dirs ‘libs‘
  21. }
  22. }
  23. }
  24. dependencies {
  25. compile fileTree(dir: ‘libs‘, include: [‘*.jar‘])
  26. compile(name:‘camerascan-1.0‘, ext:‘aar‘)
  27. compile ‘com.android.support:appcompat-v7:22.2.0‘
  28. }

③.編譯一次工程


如圖復制aar包到lib下

技術分享圖片

圖復制到lib下後,點擊下圖按鈕讓aar包可以在在代碼中引用,或者編譯一次工程

技術分享圖片

以上操作成功後可以在擴展包下看到被引用的aar包文件

技術分享圖片

android studio生成aar包並在其他工程引用aar包