简体   繁体   English

Android - AAR 文件导致错误

[英]Android - AAR file causes errors

I build an aar file.我建立了一个 aar 文件。 It use some dependency.它使用了一些依赖。 build.gradle file of my libray:我的库的 build.gradle 文件:

apply plugin: 'com.android.library'

android {
    compileSdkVersion 27
    defaultConfig {
        minSdkVersion 19
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

        vectorDrawables.useSupportLibrary = true

    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }


}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation 'com.android.support:design:27.1.1'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    implementation 'com.android.support:support-vector-drawable:27.1.1'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

My aar file path -> app/libs/mylibrary.aar我的 aar 文件路径 -> app/libs/mylibrary.aar

I edit top level build.gradle file我编辑顶级 build.gradle 文件

allprojects {
    repositories {
        ...
        flatDir {
            dirs 'libs'
        }
    }
} 

Add the library as a dependency将库添加为依赖项

implementation(name:"mylibrary", ext:"aar") {
    transitive = true
}

My application id: my.sample.app我的应用程序 ID:my.sample.app

./gradlew app:assembleRelease

Task:app:processReleaseManifest /SampleApp/app/src/main/AndroidManifest.xml:6:5-21:19 Warning: application@android:label was tagged at AndroidManifest.xml:6 to replace other declarations but no other declaration present /SampleApp/app/src/main/AndroidManifest.xml:6:5-21:19 Warning: application@android:icon was tagged at AndroidManifest.xml:6 to replace other declarations but no other declaration present /user/.gradle/caches/transforms-2/files-2.1/eb66b88fff0dbb2e75f036f5373b5bbf/res/layout/activity_btb.xml:10: AAPT: error: attribute 'my.sample.app:layout_constraintLeft_toLeftOf' not found. Task:app:processReleaseManifest /SampleApp/app/src/main/AndroidManifest.xml:6:5-21:19 Warning: application@android:label was tagged at AndroidManifest.xml:6 to replace other declarations but no other declaration present / SampleApp/app/src/main/AndroidManifest.xml:6:5-21:19 警告:application@android:icon 被标记在 AndroidManifest.xml:6 以替换其他声明,但没有其他声明存在 /user/.gradle/cache /transforms-2/files-2.1/eb66b88fff0dbb2e75f036f5373b5bbf/res/layout/activity_btb.xml:10:AAPT:错误:找不到属性“my.sample.app:layout_constraintLeft_toLeftOf”。

I add dependencies that used by library, the error is gone.我添加了库使用的依赖项,错误消失了。

implementation 'com.android.support:design:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'

implementation(name:"mylibrary", ext:"aar") {
    transitive = true
}

But this is not best way.但这不是最好的方法。 How can I fix this error?我该如何解决这个错误? Why my library couldn't find its dependecies.为什么我的图书馆找不到它的依赖项。

I just found the reason of this error.我刚刚找到了这个错误的原因。

The aar file doesn't contain the nested (or 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 存储库导入 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.您应该使用 maven 存储库(您必须在私有或公共 maven 存储库中发布库),您不会遇到同样的问题。 In this case, gradle downloads the dependencies using the pom file which will contains the dependencies list.在这种情况下,gradle 使用包含依赖项列表的 pom 文件下载依赖项。

Original answer is here: https://stackoverflow.com/a/36475009/4319615原始答案在这里: https://stackoverflow.com/a/36475009/4319615

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM