美文网首页
gradle脚本生成jar包

gradle脚本生成jar包

作者: whhccc | 来源:发表于2018-05-23 19:53 被阅读0次

    生成Jar包

    指定模块下执行 gradle makeJar

    task deleteJar(type: Delete) {
        delete project.buildDir.absolutePath +'/libs/test.jar'
    }
    
    task makeJar(dependsOn: ['deleteJar'], type: org.gradle.api.tasks.bundling.Jar) {
        //指定生成的jar名
        baseName 'test'
    
        def srcClassDir = [project.buildDir.absolutePath + "/intermediates/classes/release",
                           project.buildDir.absolutePath + "/generated/source/proto/release/grpc",
                           project.buildDir.absolutePath + "/generated/source/proto/release/javalite"];
    
        from srcClassDir
    
        //去掉不需要打包的目录和文件
        exclude "**/BuildConfig.class"
        exclude "**/BuildConfig\$*.class"
        exclude "**/R.class"
        exclude "**/R\$*.class"
    
        //导入路径集下的指定资源
        include "com/**/*.class"
    }
    

    生成混淆jar

     buildTypes {
            release {
                minifyEnabled true
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }
    
    def mJarName = "test"
    
    task clearJar(type: Delete) {
        delete 'build/libs/' + mJarName + ".jar"
    }
    
    // 拷贝混淆后的jar
    task makeJar(dependsOn: ['clearJar','assembleRelease'],type: Copy) {
        from('build/intermediates/bundles/release/')
        into('build/libs/')
        include('classes.jar')
        rename('classes.jar', mJarName + ".jar")
        doLast {
            String isJarRelease = project.hasProperty('isJarRelease') ? isJarRelease : "false"
            print("isJarRelease:" + isJarRelease)
        }
    }
    

    idea中打包所有jar为可发布tar包

    CreateStartScripts API说明链接

    执行gradle clean build

    // Generate IntelliJ IDEA's .idea & .iml project files
    apply plugin: 'idea'
    
    // Provide convenience executables for trying out the examples.
    apply plugin: 'application'
    
    startScripts.enabled = false
    
    tasks.withType(JavaCompile) {
        options.encoding = "UTF-8"
    }
    
    task helloWorldServer(type: CreateStartScripts) {
        mainClassName = 'com.test.ttt.Main'
        applicationName = 'DumpJar'
        outputDir = new File(project.buildDir, 'tmp')
        classpath = jar.outputs.files + project.configurations.runtime
    }
    
    
    applicationDistribution.into('bin') {
        from(helloWorldServer)
        fileMode = 0755
    }
    

    导出android studio的依赖jar, 使用那个任务导出,可能会变化,目前自己使用 transformClassesAndResourcesWithProguardForRelease

    project.afterEvaluate {
        project.tasks.findByName("transformClassesAndResourcesWithProguardForRelease").doFirst { task ->
            task.inputs.files.each { file ->
                if (file.absolutePath.endsWith(".jar")) {
                    copy {
                        from(file)
                        into 'dist'
                    }
                }
            }
        }
    }
    

    相关文章

      网友评论

          本文标题:gradle脚本生成jar包

          本文链接:https://www.haomeiwen.com/subject/fpddjftx.html