如何在安卓系统的主要项目中包含用于图书馆项目的aar文件?

时间:2020-12-02 22:53:50

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.

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

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->lib中保留了重复的aar文件,并在app->级文件中定义它们的依赖性,那么就没有问题了。但这种做法不应该是正确的。

Please find below the error:

请在错误下面找到:

A problem occurred configuring project ':app'.

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

在app gradle文件中已经提到过包括项目

dependencies { compile project(':misnapworkflow') }

依赖项{编译项目(':misnapworkflow')}

Please find below the misnapworkflow gradle file:

请在以下的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'
    }
}

5 个解决方案

#1


19  

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

aar文件不包含传递性依赖项,也没有描述库使用的依赖项的pom文件。

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

这意味着,如果您使用flatDir repo导入aar文件,则必须指定项目中的依赖项。

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.

您应该使用maven存储库(必须在私有或公共的maven repo中发布库),您不会遇到相同的问题。在本例中,gradle将使用包含依赖列表的pom文件下载依赖项。

#2


5  

For Android studio

为Android工作室

Follow this steps:

参照下面的步骤:

Step 1:

步骤1:

Import .aar

进口. aar

File ---> New ---> New Module ---> (select) import .JAR/.AAR package ---> Next --->(select .aar file then)Finish

文件-->新--->新模块-->(选择)导入. jar /。AAR package——-> Next -->(选择。AAR file然后)完成

Now your existing project is imported.

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

Step 2:

步骤2:

Add dependencies

添加依赖关系

File ---> Project Structure ---> (Now you will get module list in left side at bottom.) ---> (Select app module) ---> select dependencies tab ---> click on (+) button ---> select module dependencies ---> (select module which you added) ---> ok ---> ok

--->项目结构-->(现在你会在左下角看到模块列表)--->(选择app模块)--->选择依赖项标签--->点击(+)按钮--->选择模块依赖项--- -->(选择添加的模块)--- --- -> --> ok

如何在安卓系统的主要项目中包含用于图书馆项目的aar文件? 如何在安卓系统的主要项目中包含用于图书馆项目的aar文件?

Note: To check dependency is added

注意:要检查依赖项被添加

your build.gradle looks like

您的构建。它看起来像

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

#3


0  

You need to include the jcenter repository. In the app level build.gradle type:

您需要包含jcenter存储库。在应用程序级构建中。gradle类型:

   repositories {
           jcenter()
    }

dependencies {
      compile 'com.package.youraarpackagename@aar'
}

These dependencies are different from your other ones.

这些依赖项与其他依赖项不同。

#4


0  

In my case, following thing worked:

在我的例子中,以下方法奏效了:

Put your .aar file in the libs directory(create, if needed), then, add the following code in your build.gradle(app level):

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

    repositories { 
        flatDir { 
            dirs 'libs' 
        } 
    } 

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

#5


0  

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->lib中保留了重复的aar文件,并在app->级文件中定义它们的依赖性,那么就没有问题了。但这种做法不应该是正确的。

You're right, your app shouldn't define your AAR library dependencies in build.gradle. That's a common practice for 3rd party libraries like OkHttp, Picasso or RxJava. Those libraries are, in fact, has their own dependencies, just like your AAR library.

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

So, how come OkHttp, Picasso or RxJava doesn't ask your App to include their dependencies? Because they have included their dependencies on a POM file. A POM file contains configuration file for your AAR, including your artifact, group name, version, and its dependencies.

那么,为什么OkHttp、毕加索或RxJava不要求您的应用程序包含它们的依赖项呢?因为它们包含了对POM文件的依赖项。POM文件包含AAR的配置文件,包括您的工件、组名、版本及其依赖项。

Let's take OkHttp as an example. OkHttp and its dependencies are stored in other people computer. Go to mvnrepository.com and search for OkHttp.

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

如何在安卓系统的主要项目中包含用于图书馆项目的aar文件?

You will find OkHttp and its POM file.

您将找到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>

When you include a library in your build.gradle(), Gradle will search that library on repositories define in top-level build.gradle. For OkHttp it was stored in mavenCentral().

当在build.gradle()中包含库时,Gradle将会在*build.gradle中定义的库中搜索库。对于OkHttp,它存储在mavenCentral()中。

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

Gradle will download the dependencies automatically, you don't need to specify library dependency on your App project.

Gradle将自动下载依赖项,您不需要指定应用程序项目的库依赖项。

But it shouldn't be the right approach.

但这种做法不应该是正确的。

The right approach is:

正确的方法是:

  1. Store your library and its dependencies in a Maven repository.
  2. 将库及其依赖项存储在Maven存储库中。

You can use local Maven repository, host your own Maven repo, or publish your library on Maven Central or Bintray. inthecheesefactory has a good tutorial for that.

您可以使用本地Maven存储库,托管自己的Maven repo,或者在Maven中心或Bintray上发布库。在奶酪工厂有一个很好的教程。

  1. Create a POM file for your library.
  2. 为您的库创建一个POM文件。

When you deploy your AAR you have to include POM file. It can be done manually.

部署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/"

Or using android-maven-publish Gradle plugin.

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

gradle yourlibrary:assembleRelease yourlibrary:publishMavenReleaseAarPublicationToMavenRepository
  1. Share your library to your peers:
  2. 与你的同伴分享你的图书馆:

In app-level build.gradle add the GAV of your library.

在app-level构建。添加您的库的GAV。

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

You and your peers should be able to use yourlibrary now.

你和你的同龄人现在应该可以使用你的图书馆了。

#1


19  

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

aar文件不包含传递性依赖项,也没有描述库使用的依赖项的pom文件。

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

这意味着,如果您使用flatDir repo导入aar文件,则必须指定项目中的依赖项。

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.

您应该使用maven存储库(必须在私有或公共的maven repo中发布库),您不会遇到相同的问题。在本例中,gradle将使用包含依赖列表的pom文件下载依赖项。

#2


5  

For Android studio

为Android工作室

Follow this steps:

参照下面的步骤:

Step 1:

步骤1:

Import .aar

进口. aar

File ---> New ---> New Module ---> (select) import .JAR/.AAR package ---> Next --->(select .aar file then)Finish

文件-->新--->新模块-->(选择)导入. jar /。AAR package——-> Next -->(选择。AAR file然后)完成

Now your existing project is imported.

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

Step 2:

步骤2:

Add dependencies

添加依赖关系

File ---> Project Structure ---> (Now you will get module list in left side at bottom.) ---> (Select app module) ---> select dependencies tab ---> click on (+) button ---> select module dependencies ---> (select module which you added) ---> ok ---> ok

--->项目结构-->(现在你会在左下角看到模块列表)--->(选择app模块)--->选择依赖项标签--->点击(+)按钮--->选择模块依赖项--- -->(选择添加的模块)--- --- -> --> ok

如何在安卓系统的主要项目中包含用于图书馆项目的aar文件? 如何在安卓系统的主要项目中包含用于图书馆项目的aar文件?

Note: To check dependency is added

注意:要检查依赖项被添加

your build.gradle looks like

您的构建。它看起来像

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

#3


0  

You need to include the jcenter repository. In the app level build.gradle type:

您需要包含jcenter存储库。在应用程序级构建中。gradle类型:

   repositories {
           jcenter()
    }

dependencies {
      compile 'com.package.youraarpackagename@aar'
}

These dependencies are different from your other ones.

这些依赖项与其他依赖项不同。

#4


0  

In my case, following thing worked:

在我的例子中,以下方法奏效了:

Put your .aar file in the libs directory(create, if needed), then, add the following code in your build.gradle(app level):

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

    repositories { 
        flatDir { 
            dirs 'libs' 
        } 
    } 

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

#5


0  

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->lib中保留了重复的aar文件,并在app->级文件中定义它们的依赖性,那么就没有问题了。但这种做法不应该是正确的。

You're right, your app shouldn't define your AAR library dependencies in build.gradle. That's a common practice for 3rd party libraries like OkHttp, Picasso or RxJava. Those libraries are, in fact, has their own dependencies, just like your AAR library.

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

So, how come OkHttp, Picasso or RxJava doesn't ask your App to include their dependencies? Because they have included their dependencies on a POM file. A POM file contains configuration file for your AAR, including your artifact, group name, version, and its dependencies.

那么,为什么OkHttp、毕加索或RxJava不要求您的应用程序包含它们的依赖项呢?因为它们包含了对POM文件的依赖项。POM文件包含AAR的配置文件,包括您的工件、组名、版本及其依赖项。

Let's take OkHttp as an example. OkHttp and its dependencies are stored in other people computer. Go to mvnrepository.com and search for OkHttp.

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

如何在安卓系统的主要项目中包含用于图书馆项目的aar文件?

You will find OkHttp and its POM file.

您将找到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>

When you include a library in your build.gradle(), Gradle will search that library on repositories define in top-level build.gradle. For OkHttp it was stored in mavenCentral().

当在build.gradle()中包含库时,Gradle将会在*build.gradle中定义的库中搜索库。对于OkHttp,它存储在mavenCentral()中。

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

Gradle will download the dependencies automatically, you don't need to specify library dependency on your App project.

Gradle将自动下载依赖项,您不需要指定应用程序项目的库依赖项。

But it shouldn't be the right approach.

但这种做法不应该是正确的。

The right approach is:

正确的方法是:

  1. Store your library and its dependencies in a Maven repository.
  2. 将库及其依赖项存储在Maven存储库中。

You can use local Maven repository, host your own Maven repo, or publish your library on Maven Central or Bintray. inthecheesefactory has a good tutorial for that.

您可以使用本地Maven存储库,托管自己的Maven repo,或者在Maven中心或Bintray上发布库。在奶酪工厂有一个很好的教程。

  1. Create a POM file for your library.
  2. 为您的库创建一个POM文件。

When you deploy your AAR you have to include POM file. It can be done manually.

部署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/"

Or using android-maven-publish Gradle plugin.

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

gradle yourlibrary:assembleRelease yourlibrary:publishMavenReleaseAarPublicationToMavenRepository
  1. Share your library to your peers:
  2. 与你的同伴分享你的图书馆:

In app-level build.gradle add the GAV of your library.

在app-level构建。添加您的库的GAV。

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

You and your peers should be able to use yourlibrary now.

你和你的同龄人现在应该可以使用你的图书馆了。