在我的多模块Android Studio项目中,我想创建一个普通的java模块。但在该模块中,我还希望能够使用某些Android API。这可能吗?如果是的话,build.gradle应该怎么样?
由于 佳
答案 0 :(得分:8)
只要您需要的Android功能是在jar而不是aar中,那么您应该能够相当轻松地完成此操作,因为我的团队有几个这样的工件。对于Maven Central中的Android jar工件,您只需添加依赖项:
compile 'com.google.android:android:4.1.1.4'
如果功能在通过Android SDK Manager安装的其中一个工件中,那么您可以像上面一样添加依赖项,但是您需要添加本地Android repo以提取工件:
maven { url "file:///${System.env.ANDROID_HOME}/extras/android/m2repository" }
修改强>
也忘了提一下,你想要标记所提供的Android工件,这样你就不会发生依赖性冲突。您可以使用以下方法执行此操作:
configurations {
provided
compile.extendsFrom provided
}
dependencies {
provided('com.google.android:android:4.1.1.2')
}
如果您需要一个示例build.gradle,请告诉我,我将添加一个。
修改2
下面是我们用于其中一个项目的示例build.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')