1. 新建一个独立模块 pos_service
pos_service的gradle里定义Jar包的信息
apply plugin: 'com.android.library'
android {
compileSdkVersion 27
defaultConfig {
minSdkVersion 19
targetSdkVersion 27
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
def _BASENAME = "UnionPay";
def _VERSION = "_V0.6";
def _DestinationPath = "build/libs"; //生成jar包的位置
def zipFile = file('build/intermediates/packaged-classes/release/classes.jar'); //待打包文件位置
task deleteBuild(type:Delete){
delete _DestinationPath + _BASENAME + _VERSION + ".jar"
}
task makeJar(type:Jar){
from zipTree(zipFile)
from fileTree(dir:'src/main',includes:['assets/**']) //将assets目录打入jar包
baseName = _BASENAME + _VERSION
destinationDir = file(_DestinationPath)
}
makeJar.dependsOn(deleteBuild, build)
externalNativeBuild {
cmake {
path '../app/CMakeLists.txt'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:27.1.1'
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'
}
2. 代码混淆
gradle的配置
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
proguard-rules.pro
#不要压缩(这个必须,因为开启混淆的时候 默认 会把没有被调用的代码 全都排除掉)
-dontshrink
#============保持相应的类不被混淆===========================
#保持BuildConfig不被混淆(因为混淆之后就无法在导出jar时排除该类)
-keep class com.unionpay.pos.BuildConfig{
public *;
}
#保持调用接口不被混淆
-keep class com.unionpay.pos.UPPosAddon{
public *;
}
-keep public interface com.unionpay.pos.UPPosAddon$UPPosConnectionListener{*;}
-keep public interface com.unionpay.pos.UPPosAddon$IUPCallback{*;}
3. 编译Jar包
打开右上角Gradle侧栏,选中:pos_service模块,Tasks->other->makeJar,双击makeJar
打完的Jar包位于 /pos_service/build/libs/下
4. 处理.so文件
对于在Jar包里用到ndk开发的项目,需要对.so文件做处理。
有两种处理方法:
4.1 复制.so文件到目标工程的指定目录下
gradle里指定目录
sourceSets {
main {
jniLibs.srcDirs = ['jniLibs']
}
}
makeJar打完包后生成.so文件位于 /模块/build/intermediates/cake/debug和/release下
将需要的.so文件拷贝到目标工程的指定目录下
4.2 打包.so文件
打完的Jar包生成的.so文件位于 /模块/build/intermediates/cake/debug和/release下
复制obj文件夹,改名为lib,压缩lib为zip,改zip后缀为.jar,即最终得到一个lib.so.jar文件
最后导入这两个Jar包即可
将这两个Jar包拖入libs,jar包右键Add As Library.