代码如下
//apply plugin: 'com.android.application'
apply plugin: 'com.android.library'
android {
compileSdkVersion 28
defaultConfig {
//applicationId "com.sdke.fixdavid"
minSdkVersion 14
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
externalNativeBuild {
cmake {
cppFlags ""
}
}
}
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
externalNativeBuild {
cmake {
path "CMakeLists.txt"
}
}
lintOptions {
abortOnError false
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.alipay.euler:andfix:0.5.0@aar'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
//生成JNI的jar文件 这样不直接暴露给 第三方 提供so和jar包
task makeJar(type: Copy) {
//删除存在的
delete 'build/libs/jni.jar'
//设置拷贝的文件
from('build/intermediates/packaged-classes/release/')
//打进jar包后的文件目录
into('build/libs/')
//将classes.jar放入build/libs/目录下
//include ,exclude参数来设置过滤
//(我们只关心classes.jar这个文件)
include('classes.jar')
//重命名
rename('classes.jar', 'jni.jar')
}
makeJar.dependsOn(build)
关键点:
- 修改头文件apply plugin: ‘com.android.library’
- 注释掉//applicationId “com.sdke.fixdavid”
- 写上底部 生成处理命令
- 注意 from(‘build/intermediates/packaged-classes/release/’)路径,不同的 Android studio版本目录存在差异
- 点击 Android Studio 右侧(一般在这个位置)的 Gradle 面板,在项目或者该类库的目录中找到 Tasks -> other -> makeJar 命令,双击这个makeJar之后等一会就会编译好jar包
- :通过 cmd 命令行,在项目根目录下,执行 gradlew makeJar 命令,看到编译完成的信息以后就OK了,也能生成一样的jar包,和直接使用5中的方式是等价的。
注意:使用此方式前要配置好 gradle 的“ 环境变量 ”,否则直接在项目根目录下找不到这个命令。