Android Studio生成自定义jar包

方式一:


Android Studio生成自定义jar包_第1张图片
image.png
/**
 * 这里还不够完美,里面包含了BuildConfig.class文件,暂时手动删除
 */
task clearJar(type: Delete) {
    delete 'libs'
}
task makeJar(type: Copy) {
    from('build/intermediates/bundles/default/')
    into('libs/')
    include('classes.jar')
    rename('classes.jar', 'FPE.jar')
}
makeJar.dependsOn(clearJar, build)

此种方式生成的jar包中包含BuildConfig.class文件,这里是我不想要的,暂时手动删除

方式二:
1.将文件的plugin改为apply plugin: ‘com.android.library’,或者开始就创建library项目。
2.build.gradle中添加

task makeJar(dependsOn: ['compileReleaseJavaWithJavac'], type: Jar) {
    delete 'build/libs/yourname1.jar'
    delete 'build/libs/yourname2.jar'
    appendix = "demo"
    baseName = "androidJar"
    version = "1.0.0"
    classifier = "release"
    //后缀名
    extension = "jar"
    //最终的 Jar 包名,如果没设置,默认为 [baseName]-[appendix]-[version]-[classifier].[extension]
    archiveName = "yourname1.jar"
    //需打包的资源所在的路径集
    def srcClassDir = [project.buildDir.absolutePath + "/intermediates/classes/release"];
    //初始化资源路径集
    from srcClassDir
    //去除路径集下部分的资源
    exclude "okhttphelper/clz/com/okhttphelper/BuildConfig.class"
    exclude "**/R.class"
    exclude "**/R\$*.class"
    //只导入资源路径集下的部分资源
    include "okhttphelper/clz/com/okhttphelper/**/*.class"

}

task proguard(type: ProGuardTask, dependsOn: makeJar) {
//  输入路径
    injars "build/libs/yourname1.jar"
//  输出路径
    outjars 'build/libs/yourname2.jar'
//  添加配置信息
    configuration 'proguard-rules.pro'
}

3.Termial中输入gradlew makejar,等待
4.\build\libs的路径下生成yourname*.jar

这个方式还没有尝试,有时间再看看.

你可能感兴趣的:(Android Studio生成自定义jar包)