简体   繁体   中英

How to include google-play-services library in android library module in aar package as optional

I am creating an android library module, which has some code dependency on google-play-service ads and gcm package only. I want them to mark as optional package during package creation for jcenter.

Below is what i want my pom.xml to include <optional>true</optional> during package creation.

<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>

Below is what i am using to create package of my android library from android studio.

Also i wanted source and javadocs jar to be created as empty files so that i can sync my repo to maven-central as well. For that what is to be updated in below script?

And this ./gradlew clean build generateRelease is what i am currently using to run below packaging script to run.

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
}

After searching for answer came up with below which is working perfectly.

Just add below the above mentioned gradle file to create package for jcenter.

At the end you will have zip file in your build folder of project library module with google-play-service ads and gcm as optional.

It will help you in using any google play service version with your app ir-respective of which version was used to build library.

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM