Home » Android » Is it possible to have a plain java library module depending on android SDK in Android Studio

Is it possible to have a plain java library module depending on android SDK in Android Studio

Posted by: admin June 16, 2020 Leave a comment

Questions:

In my multi-module Android Studio project, I would like to create a plain java module. But in that module, I also want to be able to use certain Android API. Is this possible? If yes, how should build.gradle look like?

Thanks
jia

How to&Answers:

As long as the Android functionality that you need is in a jar instead of an aar, then you should be able to do this fairly easily as my team has a couple of artifacts like this. For Android jar artifacts in Maven Central, you just need to add the dependency:

compile 'com.google.android:android:4.1.1.4'

If the functionality is in one of the artifacts installed via the Android SDK Manager, then you can just add the dependency as above, but you’ll need to add the local Android repo to pull the artifacts:

maven { url "file:///${System.env.ANDROID_HOME}/extras/android/m2repository" }

Edit

Also forgot to mention, you’ll want to mark the Android artifacts as provided so that you don’t get dependency clashes. You can do that by using the following:

configurations {
    provided
    compile.extendsFrom provided
}
dependencies {
    provided('com.google.android:android:4.1.1.2')
}

Let me know if you need an example build.gradle and I will add one.

Edit 2

Below is an example build.gradle that we use for one of our projects.

apply plugin: 'java'
apply plugin: 'maven'
apply plugin: 'pmd'
apply plugin: 'jacoco'
apply plugin: 'findbugs'
apply plugin: 'project-report'
apply plugin: 'jxr'

group = 'com.example'
archivesBaseName = 'project-name'
version = '1.0.0-SNAPSHOT'
sourceCompatibility = 1.7

configurations {
    provided
    compile.extendsFrom provided
}

buildscript {
    repositories {
        maven { url 'http://repo1.maven.org/maven2/' }
        maven { url "http://jcenter.bintray.com" }
    }

    dependencies {
        classpath('net.davidecavestro:gradle-jxr-plugin:0.1')
    }
}

repositories {
    mavenCentral()
    if (project.hasProperty("mavenLocal")) {
        maven { url "${System.env.HOME}/.m2/repository" }
    }
    maven { url "file:///${System.env.ANDROID_HOME}/extras/android/m2repository" }
}

dependencies {
    compile('com.google.code.findbugs:annotations:2.0.2')
    compile('com.google.code.gson:gson:2.2.4')
    compile('com.google.guava:guava:15.0')
    provided('com.google.android:android:4.0.1.2')
    testCompile('commons-io:commons-io:2.4')
    testCompile('junit:junit:4.11')
    testCompile('org.robolectric:robolectric:2.3')
    testCompile('org.mockito:mockito-all:1.10.8')
}

test {
    dependsOn ':assemble'
    testLogging {
        showExceptions = true
        showStackTraces = true
        exceptionFormat = "full"
        events "passed", "skipped", "failed"
    }
}

javadoc {
    source = sourceSets.main.allJava
    classpath = test.classpath
}

jacocoTestReport {
    dependsOn test
    description = "Generate Jacoco coverate reports after running tests."
    reports {
        xml.enabled false
        csv.enabled false
        html.destination "${buildDir}/reports/jacoco"
    }
}

pmd.ignoreFailures = true
pmdTest.enabled = false
pmdMain.enabled = true
pmdMain {
    reports {
        xml.enabled = false
        html.enabled = true
    }
}

findbugs.ignoreFailures = true
findbugs.excludeFilter = file('./findbugs-exclude-filter.xml')
findbugsTest.enabled = false
findbugsMain.enabled = true
findbugsMain {
    reports {
        xml.enabled = false
        html.enabled = true
    }
}

task wrapper(type: Wrapper) {
    gradleVersion = '2.1'
    distributionUrl = "http://services.gradle.org/distributions/gradle-${gradleVersion}-all.zip"
}

task sourcesJar(type: Jar) {
    classifier = 'sources'
    from sourceSets.main.allSource
}

task javadocJar(type: Jar) {
    classifier = 'javadoc'
    from "${projectDir}/build/docs"
}

artifacts {
    archives sourcesJar
    archives javadocJar
}

uploadSite.dependsOn(':check')
check.dependsOn('sourcesJar')
check.dependsOn('javadoc')
check.dependsOn('jacocoTestReport')
check.dependsOn('projectReport')
check.dependsOn('jxr')