- 先说导入aar 方式(举例abc.aar,原module名字abc),在app下新建包libs,并放入abc.aar文件,之后在app的build.gradle中加入
android {
repositories {
flatDir {
dirs 'libs'
}
}
}
之后导入aar
dependencies {
implementation (name:'abc', ext:'aar')
}
2.遇到的问题
2.1 在app中使用aar里面的自定义view,但是一直报自定义view找不到。
只有解析xml出错的log,找不到原因。
后来想办法通过代码方式把aar中的自定义view加入到fragment的rootView中,然后才打印出来了aar里自定义View引用另一个类找不到,而这个类是第三个module中的,及在导出aar之前名字叫abc的module引用了别的module,而我只加入了abc的aar,所以出错
解决方法:将abc的module中引入的别的module生成的aar文件都导入到此app中
2.2导入了那么多aar文件,有可能你的方法数超过64K,所以需要导入multidex,方法如下
// 1. Gralde 配置
android {
defaultConfig {
...
multiDexEnabled true
}
...
}
dependencies {
api 'com.android.support:multidex:1.0.3'
api 'com.android.support:multidex-instrumentation:1.0.3'
}
// 2. 配置
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapp">
<application
android:name="com.xxx.MyApplication" >
...
</application>
</manifest>
// 3. Application初始化
public class MyApplication extends SomeOtherApplication {
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
MultiDex.install(this);
}
}