我最近开始在Android Studio 2.3中使用buildFlavors

今天,我想压缩我们的aar库以进行分发,但是distZip任务根本不使用aar

我应该如何配置任务?

我想要一个包含以下内容的zip文件:

  • 自述文件
  • JavaDoc
  • AAR

  • build.gradle
    apply plugin: 'com.android.library'
    apply plugin: 'distribution'
    
    
    ...
    
    distributions {
        main {
            baseName = archivesBaseName
            contents {
                from 'README.md'
                from 'build/libs'
                from 'build/docs'
                from 'outputs/aar'
            }
        }
    }
    
    android {
        compileSdkVersion 19
        buildToolsVersion '25.0.3'
        publishNonDefault true
        defaultConfig {
            minSdkVersion 10
            targetSdkVersion 19
            versionCode 1
            versionName "1.0"
            testInstrumentationRunner "android.test.InstrumentationTestRunner"
    
         //
        }
    
        productFlavors {
            dev {
                version version + "-dev"
              //
            }
            prod {}
        }
    
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
            }
        }
    
        lintOptions {
            checkReleaseBuilds false
            abortOnError false
            ignore 'AllowBackup'
        }
    
        packagingOptions {
            exclude "/META-INF/LICENCE"
            exclude "/META-INF/LICENSE"
            exclude "/META-INF/LICENCE.txt"
            exclude "/META-INF/LICENSE.txt"
            exclude "/META-INF/NOTICE"
            exclude "/META-INF/NOTICE.txt"
            exclude "/LICENCE"
            exclude "/LICENCE.txt"
            exclude "/NOTICE"
            exclude "NOTICE.txt"
        }
    }
    
    dependencies {
        compile group: 'com.fasterxml.jackson.core', name: 'jackson-core', version: '2.6.7'
        compile group: 'com.fasterxml.jackson.core', name: 'jackson-annotations', version: '2.6.7'
        compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.6.7'
    
        androidTestCompile group: 'com.fasterxml.jackson.core', name: 'jackson-core', version: '2.6.7'
        androidTestCompile group: 'com.fasterxml.jackson.core', name: 'jackson-annotations', version: '2.6.7'
        androidTestCompile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.6.7'
    
        androidTestCompile 'com.jayway.awaitility:awaitility:1.6.0'
        androidTestCompile 'com.jayway.jsonpath:json-path-assert:0.9.1'
        androidTestCompile 'com.squareup.okhttp:mockwebserver:1.5.4'
    }
    
    distZip.dependsOn {
        tasks.findByName('generateProdReleaseJavadoc')
    }
    
    // create javadoc
    android.libraryVariants.all { variant ->
        task("generate${variant.name.capitalize()}Javadoc", type: Javadoc) {
            description "Generates Javadoc for $variant.name."
            source = variant.javaCompile.source
            classpath += project.files(variant.javaCompile.classpath.files)
            classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
        }
    
    }
    

    最佳答案

    您可以这样配置发行版吗?
    这样,您可以扩展您的分布并根据需要配置它。
    然后,应用程序插件中包含的distZip将对其进行拾取。

    创建任务以执行所需的任务

    task createDocs {
        def docs = file("$buildDir/docs")
        outputs.dir docs
        doLast {
            docs.mkdirs()
            new File(docs, "readme.txt").write("Read me!")
        }
    }
    

    扩大您的分布:
    distributions {
        main {
            //in here you can use from() to bring what you wish, files,
            // artifacts from configurations, built projects etc
            contents {
                from(createDocs) {
                    into "docs"
                }
                from ({project(':subproject.aar').aar}) {
                    into "whatever"
                }
                from ("$buildDir/libs") //its always better to use a variable for the build directory imho
            }
        }
    }
    

    还要检查distZip任务的依赖关系。
    如果这取决于构建 Artifact 的任务。

    资料来源:https://docs.gradle.org/3.3/userguide/application_plugin.html

    关于android - 带有buildFlavors的Android distZip,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44776853/

    10-10 20:03