Is it possible to have a simple Java library module depending on the Android SDK in Android Studio

In my multi-module Android Studio project, I would like to create a simple Java module. But in this module I also want to be able to use a specific Android API. Is it possible? If so, what should build.gradle look like?

Thanks Jia

+5
source share
1 answer

As long as the Android functionality that you need is in the bank instead of aar, then you should be able to do this quite easily, since my team has a couple of artifacts like this. For jar artifacts for Android 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 through the Android SDK Manager, you can simply add the dependency as described above, but you will need to add a local repo for Android to pull the artifacts:

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

Edit

I also forgot to mention that you will want to tag Android artifacts as indicated so that you do not encounter dependency conflicts. You can do this using the following:

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

Let me know if you need a build.gradle example and I will add it.

Edit 2

The following 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') 
+6
source

Source: https://habr.com/ru/post/1212581/


All Articles