Android 如何在主项目中包含库项目中使用的 aar 文件

How to include aar files used in library projects in main project in Android

我的项目包括一些图书馆项目。库正在使用一些 aar 文件,其依赖项已在模块中定义:gradle 文件。我在将这个库包含在我的项目中时遇到问题。

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

请在下方查找错误:

配置项目“: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

请在下面找到项目结构:

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

在应用程序中 gradle 已提及以下文件以包含项目

dependencies { compile project(':misnapworkflow') }

请在 misnapworkflow gradle 文件下方找到:

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'
    }
}

For Android studio

按照以下步骤操作:

第 1 步:

导入.aar

文件 ---> 新建 ---> 新建模块 ---> (select) 导入 .JAR/.AAR 包 ---> 下一步 --->(select .aar 然后文件)完成

现在您的现有项目已导入。

第 2 步:

添加dependencies

文件 ---> 项目结构 ---> (现在您将在底部的左侧获得模块列表。) ---> (Select app 模块) --- > select 依赖项选项卡 ---> 单击 (+) 按钮 ---> select 模块依赖项 ---> (select 您添加的模块) ---> 确定 - --> 好的

注意:检查依赖是否添加

你的build.gradle长得像

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.2.1'
    compile 'com.android.support:design:23.2.1'
    compile 'com.squareup.picasso:picasso:2.5.0'
    compile project(':ScreenSharingSDK')
}

aar 文件 不包含 transitive 依赖关系 并且没有描述库使用的依赖项。

这意味着,如果您使用 flatDir 存储库导入 aar 文件,您还必须在项目中指定依赖项

您应该使用 maven 存储库(您必须在私有或 publicmaven 存储库中发布该库),您不会遇到同样的问题。
在这种情况下,gradle 使用包含依赖项列表的 pom 文件下载依赖项。

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.

你是对的,你的 app 不应该在 build.gradle 中定义你的 AAR 库依赖项。这是 OkHttp、Picasso 或 RxJava 等第三方库的常见做法。事实上,这些库有它们自己的依赖项,就像您的 AAR 库一样。

那么,为什么 OkHttp、Picasso 或 RxJava 不要求您的 App 包含它们的依赖项?因为他们已经包含了对 POM file 的依赖。 POM 文件包含 AAR 的配置文件,包括工件、组名、版本及其依赖项。

我们以OkHttp为例。 OkHttp 及其依赖项存储在其他人的计算机中。转到 mvnrepository.com 并搜索 OkHttp。

您会找到 OkHttp 及其 POM 文件。

<project>
   <modelVersion>4.0.0</modelVersion>
   <parent>...</parent>
   <artifactId>okhttp</artifactId>
   <name>OkHttp</name>
   <dependencies>
      <dependency>
         <groupId>com.squareup.okio</groupId>
         <artifactId>okio</artifactId>
      </dependency>
      <dependency>
         <groupId>com.google.android</groupId>
         <artifactId>android</artifactId>
         <scope>provided</scope>
      </dependency>
      <dependency>
         <groupId>com.google.code.findbugs</groupId>
         <artifactId>jsr305</artifactId>
         <scope>provided</scope>
      </dependency>
   </dependencies>
   <build>...</build>
</project>

当您在 build.gradle() 中包含一个库时,Gradle 将在顶级 build.gradle 中定义的存储库中搜索该库。对于 OkHttp,它存储在 mavenCentral().

repositories {
    google()
    mavenCentral()
    jcenter()
}

Gradle会自动下载依赖,你的App项目不需要指定库依赖

But it shouldn't be the right approach.

正确的做法是:

  1. 将您的库及其依赖项存储在 Maven 存储库中。

您可以使用本地 Maven 存储库、托管您自己的 Maven 存储库,或者在 Maven Central 或 Bintray 上发布您的库。 inthecheesefactory 有一个很好的教程。

  1. 为您的图书馆创建 POM 文件。

部署 AAR 时必须包含 POM 文件。可以手动完成。

mvn deploy:deploy-file \
    -DgroupId=com.example \
    -DartifactId=your-library \
    -Dversion=1.0.1 \
    -Dpackaging=aar \
    -Dfile=your-library.aar \
    -DpomFile=path-to-your-pom.xml \
    -DgeneratePom=true \
    -DupdateReleaseInfo=true \
    -Durl="https://mavenUserName:mavenPassword@nexus.example.com/repository/maven-releases/"

或使用 android-maven-publish Gradle 插件。

gradle yourlibrary:assembleRelease yourlibrary:publishMavenReleaseAarPublicationToMavenRepository
  1. 与同行分享您的图书馆:

在应用级别 build.gradle 添加您的库的 GAV

dependencies{
    implementation "com.example:yourlibrary:1.0.1"
}

您和您的同伴现在应该可以使用 yourlibrary

就我而言,以下方法有效:

将你的.aar文件放在libs目录中(如果需要创建),然后,在你的build.gradle(应用程序级别)中添加以下代码:

    repositories { 
        flatDir { 
            dirs 'libs' 
        } 
    } 

    dependencies { 
        compile fileTree(dir: 'libs', include: ['*.jar']) 
        compile(name:'your_arr_filename', ext:'aar') 
    } 

Monika Moon 让我走上了正确的道路,但我没有足够的代表点数来在上面发表评论。叹息

因此,2020 年使用 Android Studio 3.5.3 构建的默认 Android 应用程序将通过项目视图具有以下项目结构:

--[yourAppName]
  --[app]
  --[gradle]
  build.gradle <this is the TOP LEVEL one and NOT the one you normally mess with>

在顶级 build.gradle 文件中添加 'flatDir' 项:

allprojects {
        repositories {
            google()
            jcenter()

            // needed so it picks up my aar files
            flatDir {
                dirs 'libs'
            }
        }
}

然后在上面显示的 'app' 文件夹中。您将拥有这两个关键资源:

 -- [libs] folder where you should drop your aar files
 build.gradle file that is the one you add your aar dependencies to.

默认项目构建已经包含一个 'libs' 包含给您,但以防万一您的版本没有它,这就是您需要添加的内容:

dependencies {
    implementation fileTree(dir: './libs', include: ['*.jar'])
    implementation(name: 'fileNameBeforeExtension', ext:'aar')

这很干净,可以按预期工作。

我使用的 AAR 文件是为内部硬件构建的内部自定义文件,永远不会出现在 public 报告中。