Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Studio publish local library

I've recently switched to Android Studio from Eclipse, and its better for the most part.

But now I'm at the point of wanting to create libraries to be reused later. I know about modules, but don't want to use them, as it seems to copy a duplicate in each project (I'd rather have a reference to a lib as in Eclipse).

So I've turned my attention to a Maven/Gradle solution. Ideally I'd like to be able to export my lib to a local repo and/or maven repo, and reference it through that via gradle. I've been looking for quite a while now, and each answer is different, and none of them worked for me. This is the closest I've found to what I'm looking for, but there is an error in javadoc creation.

These sites (link and link) require a Bintray or Sonatype account to publish the libraries globally. Publishing the library is an end goal for me so I'd like to keep that option open if possible, but for now I just want my libs to be private.

It would be pretty great if there was a plugin that I could just specify a maven repo to export to, but I haven't found anything that looks promising yet.

So my question is: Is there a recommended "simple" way to export a library in Android Studio, which I can reference then through Gradle?

Bonus Marks: Would I be able to obfuscate libraries with proguard once published? Currently setting minifyEnabled=true on my lib results in the .aar file not being generated when building.

like image 598
Kevin Avatar asked Sep 29 '15 12:09

Kevin


1 Answers

You can use maven-publish plugin to publish your artifacts to any repository you have access to. I am using it combined with Amazon S3. The repository can be your local maven repo (the .m2 directory), local Artifactory, Archiva or any other maven repository server or a hosted solution. S3 works well and I am sure there are hosted Artifactories etc out there.

If you are using an external repository, add it to the repositories section, but just using the plugin should give you the choice of publishing to local maven. The gradle task is called publishToMavenLocal. You need to define your publication. Publication is simply a set of artifacts created by your build, in your case it will probably be a jar, but it can be anything. Short example how do I use it with S3.

apply plugin: 'maven'
apply plugin: 'java'
apply plugin: 'maven-publish'

repositories {
    maven {
        name "s3Repo"
        url "your S3 URL"
        credentials(AwsCredentials) {
            accessKey awsAccessKey
            secretKey awsSecretKey
        }
    }
    maven {
        ... any other repo here
    }
}

publishing {
    publications {
        maven(MavenPublication) {
            artifactId 'artifact that you want'
            from components.java
            artifact distZip {
                ... example artifact created from zip distribution
            }
        }
    }
    repositories {
        add "s3Repo"
    }
}

The publishing -> repositories -> add is the place where you specify all repos where you want to upload your archives.

If you want to use library from your local maven, simply add:

repositories {
...
    mavenLocal()
...
}

And then refer to your lib as to any other dependency with group id, artifact id and version.


As far as I know, obfuscated libraries are the same as the non-obfuscated, just with changed names of identifiers. If the requirement is to publish an obfuscated library, that should be fine. Just don't obfuscate the API interfaces and classes (the entry points). If you publish non-obfuscated library, I am not sure if it gets obfuscated in the final archive.

like image 71
MartinTeeVarga Avatar answered Oct 22 '22 13:10

MartinTeeVarga