如何在aar包中的android库模块中包含google-play-services库作为可选

时间:2016-10-13 19:52:08

标签: java android gradle android-gradle

我正在创建一个Android库模块,它只对google-play-service广告和gcm包有一些代码依赖。 我希望它们在jcenter的包创建过程中标记为可选包。

以下是我希望我的pom.xml在包创建过程中包含<optional>true</optional>

<dependencies>
    <dependency>
      <groupId>com.google.android.gms</groupId>
      <artifactId>play-services-ads</artifactId>
      <version>[7.8,)</version>
      <scope>compile</scope>
      <optional>true</optional>
    </dependency>
    <dependency>
      <groupId>com.google.android.gms</groupId>
      <artifactId>play-services-gcm</artifactId>
      <version>[7.8,)</version>
      <scope>compile</scope>
      <optional>true</optional>
    </dependency>
  </dependencies>

以下是我用来从android studio创建我的android库的包。

此外,我希望将源和javadocs jar创建为空文件,以便我可以将我的repo同步到maven-central。为什么要在下面的脚本中更新?

./gradlew clean build generateRelease是我目前用于在包装脚本下运行的内容。

apply plugin: 'maven'
def groupId = project.PUBLISH_GROUP_ID
def artifactId = project.PUBLISH_ARTIFACT_ID
def version = project.PUBLISH_VERSION
def localReleaseDest = "${buildDir}/release/${version}"
task androidJavadocs(type: Javadoc) {
    source = android.sourceSets.main.java.srcDirs
    ext.androidJar = "${android.sdkDirectory}/platforms/${android.compileSdkVersion}/android.jar"
    classpath += files(ext.androidJar)
}
task androidJavadocsJar(type: Jar, dependsOn: androidJavadocs) {
    classifier = 'javadoc'
    from androidJavadocs.destinationDir
}
task androidSourcesJar(type: Jar) {
    classifier = 'sources'
    from android.sourceSets.main.java.srcDirs
}
uploadArchives {
    repositories.mavenDeployer {
        pom.groupId = groupId
        pom.artifactId = artifactId
        pom.version = version
        // Add other pom properties here if you want (developer details / licenses)
        repository(url: "file://${localReleaseDest}")
    }
}
task zipRelease(type: Zip) {
    from localReleaseDest
    destinationDir buildDir
    archiveName "release-${version}.zip"
}
task generateRelease << {
    println "Release ${version} can be found at ${localReleaseDest}/"
    println "Release ${version} zipped can be found ${buildDir}/release-${version}.zip"
}
generateRelease.dependsOn(uploadArchives)
generateRelease.dependsOn(zipRelease)
artifacts {
    archives androidSourcesJar
    archives androidJavadocsJar
}

1 个答案:

答案 0 :(得分:0)

在找到答案之后,下面提出了完美的工作。

只需添加上面提到的gradle文件,即可为jcenter创建包。

最后,您将在项目库模块的构建文件夹中使用google-play-service ads和gcm作为可选项。

它可以帮助您在您的应用中使用任何Google Play服务版本,而不是使用哪个版本来构建库。

def deployer = uploadArchives.repositories.mavenDeployer

[deployer]*.pom*.whenConfigured {
    pom -> pom.dependencies.find {dep -> dep.groupId == 'com.google.android.gms' && dep.artifactId == 'play-services-ads' }.optional = true
}
[deployer]*.pom*.whenConfigured {
    pom -> pom.dependencies.find {dep -> dep.groupId == 'com.google.android.gms' && dep.artifactId == 'play-services-gcm' }.optional = true
}