2

我想通过 aar 在不同的项目中分享我的源代码。当前版本的 Android Studio 能够为库模块自动生成 aar 文件。但我有几个问题:

  1. 如何将aar文件输出到特定文件夹?

当前输出位于 build/outputs/aar 文件夹中。编译完成后可以自动移动文件吗?

  1. 库模块中的依赖项不会被应用模块继承。例如:

我的库模块(build.gradle):

apply plugin: 'com.android.library'
......
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:21.0.3'
    compile 'com.google.code.gson:gson:2.2.4'
}

我的应用模块:

apply plugin: 'com.android.application'

repositories {
    mavenCentral()
    flatDir {
        dirs 'myaarfolder'
    }
}

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"

    defaultConfig {
        applicationId "com.company.appid"
        minSdkVersion 8
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:21.0.3'
    compile(name:'mylib-release', ext:'aar')
}

我收到类似的错误:

错误:(3, 35) 错误: com.google.gson.annotations 包不存在

我必须在我的应用程序模块(build.gradle)的依赖项中添加compile 'com.google.code.gson:gson:2.2.4'

任何想法?谢谢

4

1 回答 1

1

AAR 文件没有将其依赖项烘焙到其中,因此如果您包含这样的裸 AAR,则需要在父模块中手动添加依赖项。要执行您想要的操作,您需要将 AAR 打包到 Maven 工件中并在 POM 中指定其依赖项。快速搜索 Google 会发现此链接,它看起来像是 Android Gradle 插件的旧版本,但它可能会让您继续前进,或者您可以自己搜索更好的资源:

http://www.vandalsoftware.com/post/52468430435/publishing-an-android-library-aar-to-a-maven

如果你想操作你的输出文件,你可以从这个问题中获得灵感,它重命名了输出:

在 com.android.build.gradle.internal.api.ApplicationVariantImpl 上找不到属性“outputFile”

本质上,它涉及一个如下所示的构建脚本块:

applicationVariants.all { variant ->
    variant.outputs.each  { output ->
        output.outputFile = new File(output.outputFile.parent, /* calculate new path here */)
    }
}
于 2015-02-05T18:04:42.373 回答