Android-AAR创建
记录一下编写AAR时,需要注意的问题,及引用方式。
首先在项目名字处,点击右键,创建一个module,选择Android Library。
创建完成之后,在项目目录中会显示出,aar名称。
然后将引用的jar及so放到相应的位置。编写一个公用类,来调用封装的功能。
TMCUtil:
public class TMCUtil {
private TMCAPI tmcapi;// 我这里是引用的第三方的jar
public TMCUtil() {
tmcapi = new TMCAPI();
}
// 设置打印
public void setLog(boolean b) {
tmcapi.SetLogFlag(b);
}
// 开启卡
public void open(Context context, final OpenListener openListener) {
tmcapi.OpenSEService(context, new TMCAPI.TMCAPICallBack() {
@Override
public void isSupported(boolean b) {
openListener.onOpenSuccess(b);
}
});
}
public String getOtiType() {
return tmcapi.GetOtiType();
}
public int getRandom(int len, byte[] backData) {
int ret = tmcapi.GenRandom(len, backData);
return ret;
}
public void close() {
tmcapi.CloseSEService();
}
// 开启卡的回调,返回给调用层开启结果
public interface OpenListener {
void onOpenSuccess(boolean isOpen);
}
}
aar编写完成之后,rebuild一下项目,会在Module的build-outputs-aar中生成 module名称.aar 的包。
将.aar的包拷贝到项目中的lib中,在项目的build.gradle进行一下配置
android {
repositories {
flatDir {
dirs 'libs'
}
}
}
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation(name: 'aar名称', ext: 'aar')// 引用aar
}
注意:如果aar中使用了第三方的jar,在项目中不能再进行重复添加,否则会报jar重复。