如何在Android中的主项目中包含库项目中使用的aar文件 [英] How to include aar files used in library projects in main project in Android

查看:197
本文介绍了如何在Android中的主项目中包含库项目中使用的aar文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目包括一些图书馆项目。库正在使用一些aar文件,并且它的dependecny已经在模块中定义:gradle文件。我在我的项目中包含此库时遇到问题。

如果我在app-> lib中保留重复的aar文件并在app-> gradle文件中定义它们的依赖关系,那么就没有问题了。但它不应该是正确的做法。



请在下面找到错误:

配置出现问题project':app'。

 无法解析配置':app:_qaDebugCompile'的所有依赖关系。找不到:api-release :.在以下位置搜索:
https://jcenter.bintray.com//api-release//api-release-.pom
https://jcenter.bintray.com//api-release //api-release-.aar
file:/ D:/sample/sample-android-app/app/libs/api-release-.aar
file:/ D:/ sample / sample- android-app / app / libs / api-release.aar
必须为:
sample-android-app:app:unspecified> sample-android-app:misnapworkflow:unspecified

请在下面找到项目结构:

  sample 
| - app
| - misnapworkflow
|
| - lib
| - api-release.aar

应用程序gradle文件已被提及包含该项目



依赖关系{compile project(':misnapworkflow') }


请在下面找到misnapworkflow gradle文件:

  apply plugin:'com.android.library'

android {
compileSdkVersion 23
buildToolsVersion23.0.1

defaultConfig {
minSdkVersion 10
targetSdkVersion 23
consumerProguardFiles'proguard-rules.pro'
}

lintOptions {
abortOnError false
}

//发布调试和发布库
publishNonDefault true

uildTypes {
debug {
debuggable true
jniDebuggable true
minifyEnabled false
shrinkResources false
testCoverageEnabled true
}

release {
signingConfig signingConfigs.debug
debuggable false
jniDebuggable false
minifyEnabled true
shrinkResources false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'),'proguard-rules.pro'
}
}
}

任务grantPermissions(类型:Exec,dependsOn:'installDebugTest'){
logger.warn('Granting permissions ...')
commandLineadb shell pm grant com .miteksystems.misnap.misnapworkflow.test android.permission.WRITE_EXTERNAL_STORAGE.split('')
commandLineadb shell pm grant com.miteksystems.misnap.misnapworkflow.test android.permission.CAMERA.split('' )
logger.warn('Permissions granted。')
}

tasks.whenTaskAdded {task - >
if(task.name.startsWith('connected')
|| task.name.startsWith('create')){
task.dependsOn grantPermissions
}
}

依赖关系{
编译fileTree(包括:['* .jar'],dir:'libs')
编译'com.android.support:appcompat-v7 :23.0.1'

//为MiSnap外部API添加依赖项
compile(名称:'api-release',ext:'aar')

/ /添加MiSnap的依赖关系
编译(名称:'misnap-release',ext:'aar'){
排除模块:'appcompat-v7'
}

// Eventbus依赖关系
编译'de.greenrobot:eventbus:2.4.0'

//为Manatee添加可选依赖项
compile(name:'manatee-release', ext:'aar')

编译(名称:'cardio-release',ext:'aar')
}

存储库{
flatDir {
dirs'libs'
}
}


解决方案

aar 文件不包含传递依赖项,并且没有描述所使用的依赖项的pom文件这意味着如果你使用 flatDir repo 导入aar文件,必须在您的项目中指定依赖关系。



您应该使用 maven存储库私有或公共Maven回购),你不会有同样的问题。

在这种情况下,gradle使用包含依赖关系列表的pom文件下载依赖关系。


My project is including some library project. Library is using some aar files and its dependecny is already defined in the module: gradle file. I am facing problem in including this library in my project.

If I keep duplicate aar files in app->lib and define their dependency in app->gradle file then there is no problem. But it shouldn't be the right approach.

Please find below the error:

A problem occurred configuring project ':app'.

Could not resolve all dependencies for configuration ':app:_qaDebugCompile'. Could not find :api-release:. Searched in the following locations:
         https://jcenter.bintray.com//api-release//api-release-.pom
         https://jcenter.bintray.com//api-release//api-release-.aar
         file:/D:/sample/sample-android-app/app/libs/api-release-.aar
         file:/D:/sample/sample-android-app/app/libs/api-release.aar
     Required by:
         sample-android-app:app:unspecified > sample-android-app:misnapworkflow:unspecified

please find below the project structure:

sample
|-- app
|-- misnapworkflow
    |
    |-- lib
        |-- api-release.aar

In app gradle file following has been mentioned to include the project

dependencies { compile project(':misnapworkflow') }

Please find below the misnapworkflow gradle file:

apply plugin: 'com.android.library'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.1"

    defaultConfig {
        minSdkVersion 10
        targetSdkVersion 23
        consumerProguardFiles 'proguard-rules.pro'
    }

    lintOptions {
        abortOnError false
    }

    // Publish both debug and release libraries
    publishNonDefault true

    buildTypes {
        debug {
            debuggable true
            jniDebuggable true
            minifyEnabled false
            shrinkResources false
            testCoverageEnabled true
        }

        release {
            signingConfig signingConfigs.debug
            debuggable false
            jniDebuggable false
            minifyEnabled true
            shrinkResources false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

task grantPermissions(type: Exec, dependsOn: 'installDebugTest') {
    logger.warn('Granting permissions...')
    commandLine "adb shell pm grant com.miteksystems.misnap.misnapworkflow.test android.permission.WRITE_EXTERNAL_STORAGE".split(' ')
    commandLine "adb shell pm grant com.miteksystems.misnap.misnapworkflow.test android.permission.CAMERA".split(' ')
    logger.warn('Permissions granted.')
}

tasks.whenTaskAdded { task ->
    if (task.name.startsWith('connected')
            || task.name.startsWith('create')) {
        task.dependsOn grantPermissions
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:appcompat-v7:23.0.1'

    // Add dependency for MiSnap external API
    compile(name: 'api-release', ext: 'aar')

    // Add dependency for MiSnap
    compile(name: 'misnap-release', ext: 'aar') {
        exclude module: 'appcompat-v7'
    }

    // Eventbus dependency
    compile 'de.greenrobot:eventbus:2.4.0'

    // Add OPTIONAL dependency for Manatee
    compile(name: 'manatee-release', ext: 'aar')

    compile(name: 'cardio-release', ext: 'aar')
}

repositories {
    flatDir {
        dirs 'libs'
    }
}

解决方案

The aar file doesn't contain the transitive dependencies and doesn't have a pom file which describes the dependencies used by the library.

It means that, if you are importing a aar file using a flatDir repo you have to specify the dependencies also in your project.

You should use a maven repository (you have to publish the library in a private or public maven repo), you will not have the same issue.
In this case, gradle downloads the dependencies using the pom file which will contains the dependencies list.

这篇关于如何在Android中的主项目中包含库项目中使用的aar文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆