Use Android Studio's Gradle Tasks to generate jar packages from source code

I. Introduction

        As tools can be used to compile third-party source code (such as okhttp, retrofit source code), and to create jar packages or aar packages. The Tasks tool that comes with the tool can help us complete these things.

2. Encountering problems

Android Studio Gradle packaging Tasks are not displayed, as shown in the figure:

3. Solution

step one:

Step 2:

Step three:

 

Step four:

After restarting, you can display tasks

 

4. How to use

In Tasks ---》build there are built-in task scripts build and clean 

Their role is to compile the module and generate the jar package. 

Of course, you can compile and write a custom gradle script file in the build.gradle file. Example:

android {

.....

    task clearJar(type:Delete){

    //这行表示如果你已经打过一次包了,再进行打包则把原来的包删掉  3.9.0版本

    delete'build/libs/okhttp3.9.0.jar'

    }



    task makeJar(type:Copy){

    //删除存在的

    delete'build/libs/okhttp3.9.0.jar'

    //设置拷贝的文件

    from('build/intermediates/aar_main_jar/release/')

    //打进jar包后的文件目录

    into('build/libs/')

    //将classes.jar放入build/libs/目录下

    //include ,exclude参数来设置过滤

    //(我们只关心classes.jar这个文件)

    include('classes.jar')

    //重命名 3.9.0版本

    rename ('classes.jar','okhttp3.9.0.jar')

    }



    makeJar.dependsOn(clearJar,build)



.......

}

In this way, you can find the two task scripts makeJar and clearJar you wrote in tasks---》other-----》

 

Guess you like

Origin blog.csdn.net/u012514113/article/details/134939889