最近实现了人生第一个APK,中间遇到点问题,比较基础,但还是在此做个总结
我将和播放器相关的类放在一个Android Lib,生成aar,新建一个项目module包含这个aar,同时在module中intent aar包中PlayerView.class 显示界面
参考
Android开发之Android Studio依赖aar包的四种方法(附加第三方库依赖方式)_xiayiye5的博客-CSDN博客
lib_A启动lib_B的activity即两个依赖工程互相启动组件_网鱼的栈-CSDN博客
Android Lib 可以生成jar包或aar包作为第三方给其他项目工程使用
jar包 不包含资源文件
aar包 包含资源文件,比如layout value等
经过验证,Android Studio添加aar依赖 只有以下两种方法好用
首先将第三方aar包 放在所在 module的libs下
然后再module的buid.gradle 中添加如下两种方法的依赖
dependencies{
implementation fileTree(include: ['*.jar',"*.aar"],dir:'libs')
}
dependencies{
implementation files('libs/xxxxxxxxx.aar')
}
有两点值得注意的:
1. 使用aar不包含依赖关系
项目module需要再次依赖aar包中所依赖的包
如果将aar包上传仓库,使用仓库引用就包含了依赖关系
2.module包名com.a.aa,aar的包名为com.b.bb。在module中启动aar中的PlayerView.class, 会使用到ComponentName,定义如下
* @param pkg The name of the package that the component exists in. Can
* not be null.
* @param cls The name of the class inside of <var>pkg</var> that
* implements the component. Can not be null.
*/
public ComponentName(@NonNull String pkg, @NonNull String cls)
如果如下实现,在运行apk时就会报找不到com.b.bb.PlayerView.class 错误
ComponentName componetName =new ComponentName("com.b.bb", "com.b.bb.PlayerView");
Intent intent=new Intent();
intent.setComponent(componetName);
startActivity(intent);
但是将pkg换成com.a.aa 或者 this, 如下,运行就没问题,可以启动PlayerView.class
Intent intent=new Intent();
intent.setComponent(new ComponentName(this, PlayerView.class));
startActivity(intent);
所以,如果已经依赖,直接显示调用这个activity就行
如果没有依赖,就需要填入activity所在的包名
PlayerView.class再aar包中的manifest.xml中已经有如下申明
<activity android:name=".PlayerView"></activity>
项目module 依赖 aar包,并不需要在项目module的manifest.xml中再次申明(查阅有的博客说需要在module的.xml中再次申明,经验证不需要)
<activity
android:name="com.b.bb.PlayerView"
android:exported="true" />