2

I'd like to share my source code among different projects through aar. Current version of Android studio is able to generate aar files automatically for library module. But I have a few issues:

  1. How to output aar files to specific folder?

Currently the output is in build/outputs/aar folder. Can I move the files automatically once the compilation has been finished.

  1. The dependencies in library module is not inherited by app module. For example:

My library module (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'
}

My app module:

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

I got an error something like:

Error:(3, 35) Error: Package com.google.gson.annotations not exist

I have to add compile 'com.google.code.gson:gson:2.2.4' in the dependency of my app module (build.gradle)

Any idea? Thanks

Bagusflyer
  • 12,675
  • 21
  • 96
  • 179

1 Answers1

1

AAR files don't have their dependencies baked into them, so if you're including a bare AAR like this, you'll need to manually add dependencies in the parent module. To do what you want you'd need to package the AAR into a Maven artifact and specify its dependencies in the POM. A quick search of Google reveals this link, which looks like it's for an older version of the Android Gradle plugin, but it might get you going, or you can do your own searching for a better resource:

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

If you want to manipulate of your output files, you can take inspiration from this question, which renames outputs:

Could not find property 'outputFile' on com.android.build.gradle.internal.api.ApplicationVariantImpl

Essentially it involves a block of build script that looks like this:

applicationVariants.all { variant ->
    variant.outputs.each  { output ->
        output.outputFile = new File(output.outputFile.parent, /* calculate new path here */)
    }
}
Community
  • 1
  • 1
Scott Barta
  • 79,344
  • 24
  • 180
  • 163
  • Thanks for your reply. Actually I know the method you mentioned. But because new Android Studio (1.0.0) is able to generate aar during compiling. So I think we probably don't need the Maven method. The problem is I don't know how to output the aar file to a specific directory. – Bagusflyer Feb 06 '15 at 01:10
  • Edited. My answer stands, though -- if you're including an AAR directly, you'll need to add dependencies to the parent project. – Scott Barta Feb 06 '15 at 17:05
  • I'm quite new on Gradle. I got the following error: "Error:(23, 0) Could not find property 'applicationVariants' on com.android.build.gradle.LibraryExtension_Decorated@5f0a7d2c." Where should I add this script? Thanks – Bagusflyer Feb 09 '15 at 01:06