美文网首页
Android Studio在多Module中依赖aar的配置问

Android Studio在多Module中依赖aar的配置问

作者: 因为我的心 | 来源:发表于2022-03-03 19:26 被阅读0次

    一、前言:

    本想添加一个aar到基础module中,结果引用该module的库全部报


    图片.png

    二、依赖:

    1、不存在module依赖的情况

    1)添加arr到app module -> libs目录
    2) 在build.gradle中配置 (注:repositories和android、dependencies处在同一级别的目录下。如不添加该配置,同步依赖会失败,提示 Failed to resolve)

    repositories {
        flatDir {
            dirs 'libs'
        }
    }
    

    3)引入

    implementation(name: 'xxx', ext: 'aar')
    

    2、当存在其它module依赖aar文件时

    注:当项目存在多个module时,如果想在某个library module中依赖aar文件时,那么其它所有直接或间接依赖该library module的module中都应声明build.gradle文件中进行统一配置声明。否则会同步依赖失败,提示 Failed to resolve

    1、方法一

    1. 在project的build.gradle文件中统一配置如下:下:
    repositories {
        flatDir {
            dirs '../xxx/libs','libs'  //将xxx替换为引入aar文件的module名
        }
    }
    
    1. 在module的build.gradle文件中统一配置如下:
     repositories {
            flatDir {
                dirs 'libs'
            }
        }
    

    3)在依赖aar文件的library module下的build.gradle文件中添加依赖如下:

    //只允许在依赖aar文件的module下调用该aar文件
    implementation(name: 'xxx', ext: 'aar') 
    或
    //在其他依赖该library module的module中,也可调用该library module所依赖的aar文件
    api (name:'xxx',ext:'aar')  
    

    2、方法二(简单粗爆)
    1)在project的build.gradle文件中统一配置如下:

    allprojects {
        repositories 
            flatDir {
                dirs project(':xxx').file('libs')  //将xxx替换为引入aar文件的module名
            }
        }
    }
    
    1. 在module的build.gradle文件中统一配置如下:
     repositories {
            flatDir {
                dirs 'libs'
            }
        }
    

    3)在依赖aar文件的library module下的build.gradle文件中添加依赖如下:

    //只允许在依赖aar文件的module下调用该aar文件
    implementation(name: 'xxx', ext: 'aar') 
    或
    //在其他依赖该library module的module中,也可调用该library module所依赖的aar文件
    api (name:'xxx',ext:'aar')  
    

    3、示例:

    图片.png 图片.png 图片.png

    1、主mouble

    import com.basestonedata.plugin.versioncontroller.*
    plugins {
        id 'com.android.application'
        id 'kotlin-android'
        id 'com.basestonedata.plugin.versioncontroller'
    }
    
    android {
        compileSdkVersion BuildVersionTool.COMPILE_SDK_VERSION as int
        buildToolsVersion BuildVersionTool.BUILD_TOOLS_VERSION as String
    
        defaultConfig {
            applicationId "com.weifeng.hobby"
            minSdkVersion BuildVersionTool.MIN_SDK_VERSION as int
            targetSdkVersion BuildVersionTool.TARGET_SDK_VERSION as int
            versionName BuildVersionTool.VERSION_NAME as String
            versionCode BuildVersionTool.VERSION_CODE as int
            multiDexEnabled true
            aaptOptions.cruncherEnabled = false
            aaptOptions.useNewCruncher = false
    
            javaCompileOptions {
                annotationProcessorOptions {
                    arguments = [AROUTER_MODULE_NAME: project.getName()]
                }
            }
            ndk {
                abiFilters 'arm64-v8a'
            }
        }
    
        dataBinding {
            enabled = true
        }
    
     
    
    
        //主moudel引入的两种方式
        repositories{
            flatDir{
                // dirs project(':realname').file('libs')
                dirs '../realname/libs','libs'
            }
        }
    }
    
    dependencies {
        api project(':userCenter')
        api project(':realname')
    }
    

    2、子mouble

    import com.basestonedata.plugin.versioncontroller.*
    
    plugins {
        id 'com.basestonedata.plugin.versioncontroller'
        id 'kotlin-android'
    }
    apply plugin: 'com.android.library'
    apply plugin: 'maven-publish'
    apply plugin: 'kotlin-kapt'
    
    kapt {
        arguments {
            arg("AROUTER_MODULE_NAME", project.getName())
        }
    }
    
    android {
        compileSdkVersion BuildVersionTool.COMPILE_SDK_VERSION as int
        resourcePrefix 'realname_'
    
        defaultConfig {
            minSdkVersion BuildVersionTool.MIN_SDK_VERSION as int
            targetSdkVersion BuildVersionTool.TARGET_SDK_VERSION as int
            versionCode 1
            versionName "1.0"
    
            testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
            consumerProguardFiles "proguard-rules.pro"
        }
    
        buildFeatures {
            dataBinding = true
            viewBinding true
        }
        buildTypes {
            debug {
                consumerProguardFiles "proguard-rules.pro"
            }
    
            beta {
                consumerProguardFiles "proguard-rules.pro"
            }
    
            release {
                consumerProguardFiles "proguard-rules.pro"
            }
        }
        compileOptions {
            sourceCompatibility JavaVersion.VERSION_1_8
            targetCompatibility JavaVersion.VERSION_1_8
        }
        kotlinOptions {
            jvmTarget = '1.8'
        }
        afterEvaluate {
            publishing {
                repositories {
                    maven {
                        url = MavenConfig.MAVEN_URL as String
                        credentials {
                            username MavenConfig.MAVEN_USERNAME as String
                            password MavenConfig.MAVEN_PASSWORD as String
                        }
                    }
                }
    
                publications {
                    releaseAAR(MavenPublication) {
                        groupId = 'com.weifeng.hobby-func'
                        artifactId = 'realname'
                        version = ModuleVersions.realnameVersion
    
                        afterEvaluate { artifact(tasks.getByName("bundleReleaseAar")) }
                    }
                }
            }
        }
    
    
        repositories {
            flatDir {
                dirs 'libs'
            }
        }
    }
    dependencies {
        api project(':apiModule')
        api project(':commonUI')
       //引入aar
    //    implementation(name:'meglive_still', ext:'aar')
        compile(name:'meglive_still', ext:'aar')
    
    }
    

    作者:小王子_c285
    链接:https://www.jianshu.com/p/db33f4850005
    参考链接:https://www.cnblogs.com/thlin/p/11803922.html

    相关文章

      网友评论

          本文标题:Android Studio在多Module中依赖aar的配置问

          本文链接:https://www.haomeiwen.com/subject/afdurrtx.html