[Android][Componentization] Aar in sub-module cannot be found

 

Problem Description

After the project is created, the first module: app will be generated.
module: app depends on module: baseapp
module: baseapp depends on module: c

If there is no special dependency, the compilation will not cause any problems at this time.

module:baseapp depends on baseapp/libs/android_gif_drawable.aar. According to the usual processing, it should be added in the build.gradle file of baseapp

//【步骤1】
dependencies { 
    compile(name: ‘android_gif_drawable’, ext: ‘aar’) 
} 

//【步骤2】
repositories { 
    flatDir { 
        dirs ‘libs’ 
    } 
} 

Compile at this time, an error is found, prompt: Android_gif_drawable.aar is found in the app’s build.gradle

Error:A problem occurred configuring project ':app'.
> Could not resolve all dependencies for configuration ':app:_prodDebugApkCopy'.
   > Could not find :android_gif_drawable:.
     Required by:
         project :app > project :baseapp

problem causes

Reason 1: What is configured in flatDir.dirs?

//【步骤1】
dependencies { 
    compile(name: ‘android_gif_drawable’, ext: ‘aar’) 
} 

In the baseapp/build.gradlemiddle, we declare as 'aar' dependent 'android_gif_drawable' format, this dependency is at the same level and library Module

However, when we will be similar reference library Module compile project(':jni')this way borrow project()function to inform gradle path corresponding to the position-dependent

So how to tell gradle the specific resource path for the dependency of the aar method? This is step 2

//【步骤2】
repositories { 
    flatDir { 
        dirs ‘libs’ 
    } 
} 

This is a relative path. Note that we are configured in baseapp/build.gradle, then baseapp knows when gradle compiles "oh~'android_gif_drawable' is an aar, and its path is in libs in my current directory, which is baseapp/libs "

This also explains the reason why baseapp can be compiled independently when packaged as aar

Reason 2: Application needs to find all dependent dependencies

aar itself is a kind of library Module, directly used in apply plugin:'com.android.application'

In other words, app needs to know the location of all dependencies of baseapp, if it is a remote dependency, pull down the remote dependency code, if it is a local dependency, find the location of the local dependency

Therefore, there is such a logic:

App found when integrating baseapp,
"Oh~ android_gif_drawable is an aar, where is it?
The path pointed to by flatDir.dirs
is also in the libs in my current directory, which is app/libs"

Then I found out, "Huh, why didn't you?" and reported an error

Solutions

Path 1: Exhaustive path under the whole project

Add the corresponding references to repositories in build.gradle under Project as follows:

allprojects {
    repositories {
        jcenter()

        flatDir {
            // 由于Library module中引用了 gif 库的 aar,在多 module 的情况下,
            // 其他的module编译会报错,所以需要在所有工程的repositories
            // 下把Library module中的libs目录添加到依赖关系中
            dirs 'libs'
            dirs project(':baseapp').file('libs') //借助 project()函数的位置查找
            //dirs '../baseapp/libs'  // 直接指明路径,注意前边的../, 要先回到根目录去查找别的module
        }
    }
}

Path 2: Add all modules/build.gradle that depend on Module baseapp

repositories {
    flatDir {
        dirs 'xxx/libs' // Module A的libs的目录地址
    }
}

Add all Modules to the relative address of Module A's libs directory.

Guess you like

Origin blog.csdn.net/xfb1989/article/details/110956963