Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 380
mr49 Avatar asked Feb 03 '15 23:02

mr49


People also ask

Can you use any Java library on Android?

The android developers recommendation is that you assign unique prefixes for your resource names in each android library. Java libraries cannot reference any android SDK classes such as android.

Can use Java library in Android Studio?

To use a Java library (JAR file) inside your Android project, you can simple copy the JAR file into the folder called libs in your application. *. jar files in this folder are included into the compile classpath via the default build.

Can we import module from source in Android Studio?

Select the source directory of the Module you want to import and click Finish. Open Project Structure Dialog (You can open the PSD by selecting File > Project Structure) and from the left panel click on Dependencies. Select the module from the Module(Middle) section In which you want to add module dependency.

Is Java SDK required for Android Studio?

A copy of the latest OpenJDK comes bundled with Android Studio 2.2 and higher, and this is the JDK version we recommend you use for your Android projects. So if you are using the newest version, you won't need any additional JDK.


1 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')
like image 90
Bradford2000 Avatar answered Nov 07 '22 05:11

Bradford2000