[Android]-Android Studio 使用gradle 导出Jar

使用Eclipse 导出jar包习惯了,.突然转用Android Studio导出jar,很是不习惯..技术时时刻刻都在更新,我们要跟上这个波节奏,要不然就落伍了..哈哈.

从Android Studio 中导出jar,首先我们创建一个module项目..
这里写图片描述
再配置项目下的build.gradle文件..


//这里是library,而不是application
apply plugin: 'com.android.library'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.3"

    defaultConfig {
        minSdkVersion 11
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

//这里是当前项目包含的所有依赖
dependencies {
    testCompile 'junit:junit:4.12'
    compile files('libs/android-support-v4.jar')
    compile files('libs/okhttp-3.4.1.jar')
    compile files('libs/okhttputils-2_6_2.jar')
    compile files('libs/okio-1.9.0.jar')
    compile files('libs/open_sdk_r5788_lite.jar')
    compile files('libs/unity-classes.jar')
    compile files('libs/wechat-sdk-android-without-mta-1.1.7.jar')
    compile files('libs/gson-2.8.1.jar')
}

task buildMyJar(type: Jar, dependsOn: ['build']) {
    //导出的jar文件名称
    archiveName = 'mylib.jar'
    //从哪个目录打包jar
    from('build/intermediates/classes/debug')
    //导出的jar文件的存放目录(未指定则默认存放在build/libs下)
    destinationDir = file('build/libs')
    //去掉不要的类
    exclude('com.example.mylibrary/BuildConfig.class')
    exclude('com.example.mylibrary/BuildConfig\$*.class')
    exclude('**/R.class')
    exclude('**/R\$*.class')
    //需要打包的类
    include('com.example.mylibrary/*.class')
}

写好gradle脚本后点击“sync now”

打开android studio右侧的Gradle面板,选择[项目名]——>other——>buildMyJar(task名称)
这里写图片描述
buildMyJar时如有报错提示

Error:Execution failed for task ':mylibrary:lint'.
> Lint found errors in the project; aborting build.
Fix the issues identified by lint, or add the following to your build script to proceed with errors:
...
android {
    lintOptions {
        abortOnError false
    }
}
...

则在 android{ } 中添加

  lintOptions {
        abortOnError false
    }

再执行buildMyJar

如果执行正常,最终会在 build/libs目录下看到mylib.jar文件
我们可以用解压工具打开jar包,查看是否已包含指定文件..

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值