使用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包,查看是否已包含指定文件..