Whether it is possible to have a common Java library module depends on the Android SDK in Android studio
In my multi - module Android studio project, I want to create a normal Java module But in this module, I also want to be able to use some Android APIs Is that possible? If so, build What about gradle?
Xie Xie Jia
Solution
As long as the Android function you need is in jar instead of AAR, you should be able to do this quite easily, because my team has several such artifacts For Android jar artifacts in Maven central, you only need to add dependencies:
compile 'com.google.android:android:4.1.1.4'
If this function is in one of the artifacts installed through the Android SDK manager, you can add dependencies as above, but you need to add a local Android repo to extract the artifacts:
maven { url "file:///${System.env.ANDROID_HOME}/extras/android/m2repository" }
edit
Also forget to mention that you need to tag the provided Android artifacts so that dependency conflicts do not occur You can do this using the following methods:
configurations { provided compile.extendsFrom provided } dependencies { provided('com.google.android:android:4.1.1.2') }
If you need a sample build Gradle, please tell me I'll add one
Edit 2
Here's an example build. Exe we used for one of the projects gradle.
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')