0

I am trying to integrate an Android archive (aar) from local Maven to Android studio in my sample project.

I am getting the following build error in Android studio:

A problem occurred evaluating project :app Could not find property HOME on org.gradle.api.internal.artifacts.repositories.DefaultMavenArtifactRepository_Decorated@181db40.

tomtomssi
  • 1,017
  • 5
  • 20
  • 33

1 Answers1

0

If you are using local maven to work with android studio you need to do this means that to add a reference to an .aar package it would have to ideally be stored in the central maven repository.

A simple and extremely straightforward option is to create a local maven repository on your dev machine, and install your library in there. Then reference it from your gradle build. And doing it is surprisingly simple!.

  1. Since you're developing for android, I assume you already installed the latest JDK and set the JAVA_HOME environment variable, but if you didn't - now is the time.
  2. Then you'd want to install Maven. You can download it here: http://maven.apache.org/download.cgi Set the MAVEN_HOME environment variable to the path where you extracted maven, and add the maven's bin folder to the PATH environment variable.
  3. To test that maven is working fine, open a new console window and run the following:

    mvn -version

  4. If everything is fine, it's time to add your library to the maven repository. In the command prompt run the following:

    mvn install:install-file -Dfile=d:\mylibrary-{version}.aar -DgroupId=com.example -DartifactId=mylibrary -Dversion={version} -Dpackaging=aar

Don't forget to replace the proper path to your library, setting your groupId, artifactId and version number.

  1. Finally, edit your build.gradle to start looking at the local maven repository. For example, if you want to use both maven central and your local repo you can add both of them to the repositories configuration. Here's an example of a very basic build.gradle for an android app using the library we registered above:

     buildscript {
           repositories {
           mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.4.2'
    }
    }
    
    apply plugin: 'android'
    
    repositories {
        mavenCentral()
        mavenLocal()
    }
    
    dependencies {
        compile('com.example:mylibrary:0.2')
    }
    
    android {
        compileSdkVersion 17
        buildToolsVersion '17.0.0'
    }
    
  2. Finally, run the build command to build your app: gradle clean build.

The main problem is to get the resulting .aar file in the maven publication profile. To do that we'll run a call to android.libraryVariants, which will initialise this object, and create all the subtasks required for the build, including "bundleRelease" which is creating the .aar file.

Here is what my build.gradle file looks like:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.4.2'
    }
}

apply plugin: 'android-library'
apply plugin: 'maven-publish'

version '0.2'
group 'com.example'

android {
    compileSdkVersion 17
    buildToolsVersion '17.0.0'

    defaultConfig {
        versionCode 2
        versionName '0.2'
    }
}

android.libraryVariants
publishing {
    publications {
        maven(MavenPublication) {
            artifact bundleRelease
        }
    }
}

If you want to change the artifactId to something custom, you can change the project name in settings.gradle by adding this line:

rootProject.name = 'mylibrary' That's it. Now open a command prompt in your project's folder and run the following to build and publish your library to the local maven repository:

gradle clean build publishToMavenLocal

A couple basic articles that I used to get this to work:

How to install maven on windows.

Adding local .aar to gradle build

Community
  • 1
  • 1
Sandeep Devhare
  • 440
  • 1
  • 6
  • 16