Android在AndroidStudio中引用Module依赖注意事项
来源:互联网 发布:淘宝账号名字怎么修改 编辑:程序博客网 时间:2024/04/30 12:25
Android在AndroidStudio中添加Module依赖的注意事项
我们在开发过程中常常会有一些模块需要作为依赖,或者将部分功能单独抽出作为一个library使用,在Android studio中,我们可以通过建立多个module进行实现。
首先我们看看作为普通module和作为library的module的区别,
主要在build.gradle中
app的
apply plugin: 'com.android.application'defaultConfig { applicationId "com.julit.elevator_maint" minSdkVersion 17 targetSdkVersion 21 ... }
作为library的module
apply plugin: 'com.android.library'defaultConfig { minSdkVersion 17 targetSdkVersion 21 ... }
添加依赖 打开Project的Project Structure,可以看到目前项目下面有3个module(都是可以运行的module),目前选中了app(一般项目在创建时的默认module)然后我们点击右上角的Dependencies可以看到右侧有加号,点击在此我们可以添加3种类型的依赖,选择 Module Dependency
然后我们就成功的添加了依赖
踩坑记录
依照上面的过程,我们可以很容易地给主程序添加依赖,但是存在一些坑
我们都会遇到的一些问题,在此我将自己的一些遇到的一些问题的解决办法进行记录和说明,希望减少这种问题的遇到。
(1)Manifest merger failed with multiple errors
Error:Execution failed for task ':app:processDebugManifest'.> Manifest merger failed with multiple errors, see logs
原因:
AS的Gradle插件默认会启用Manifest Merger Tool,如果Library项目中也定义了与主项目相同的属性(例如默认生成的Android:icon和android:theme),则此时会合并失败,并报上面的错误。
或者是因为
app’s minSdk is higher than any library’s minSdk.
要求app’s minSdk >= libraries minSdk
解决方法有以下2种:
方法1:在Manifest.xml的application标签下添加tools:replace=”android:icon, android:theme”(多个属性用,隔开,并且在manifest根标签上加入xmlns:tools=”http://schemas.android.com/tools”)
<manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" // add tools line here package="yourpackage"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme" tools:replace="android:icon"> .....</application></manifest>
方法2:在build.gradle根标签上加上useOldManifestMerger true,但是我们在使用这种方法时有可能会爆出下面的错误
Error:(4, 0) Gradle DSL method not found: 'useOldManifestMerger()'Possible causes:<ul><li>The project 'JuliAPP_AS' may be using a version of the Android Gradle plug-in that does not contain the method (e.g. 'testCompile' was added in 1.1.0).<a href="fixGradleElements">Upgrade plugin to version 2.3.2 and sync project</a></li><li>The project 'JuliAPP_AS' may be using a version of Gradle that does not contain the method.<a href="open.wrapper.file">Open Gradle wrapper file</a></li><li>The build file may be missing a Gradle plugin.<a href="apply.gradle.plugin">Apply Gradle plugin</a></li>Error:(10, 0) Could not find method useOldManifestMerger() for arguments [true] on object of type com.android.build.gradle.AppExtension.
(2)APP安装时出现2个图标
原因是我们在AndroidMamifest.xml中依然有
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.juli.commonlibrary"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name="com.juli.commonlibrary.PhotoUtil.ImageGetActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application></manifest>
修改成
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.juli.commonlibrary"> <application android:allowBackup="true" android:supportsRtl="true" > <activity android:name="com.juli.commonlibrary.PhotoUtil.ImageGetActivity"/> </application></manifest>
但是在使用第二种方法时也有可能爆出上面的问题,
(3)报名相同导致冲突 A library uses the same package as this project
解决方法在Build.gradle中添加enforceUniquePackageName = false,如下
android { compileSdkVersion 23 buildToolsVersion "23.0.1" enforceUniquePackageName = false ...}
Android studio其他使用注意事项(借鉴)
1、每次保存的时候,每行多余的空格和TAB会被自动删除(例如结尾、空行的多余空格或TAB)
特别是每次准备提交SVN,Review代码时候你就蛋疼了,显示一堆不相关的更改,看的眼花。
解决方法:
Settings->IDE Settings->Editor->Other->Strip trailing spaces on Save->None
2、Library Project里面的BuildConfig.DEBUG永远都是false。这是Android Studio的一个已知问题,某Google的攻城狮说,Library projects目前只会生成release的包。
Issue 52962: https://code.google.com/p/android/issues/detail?id=52962
解决方法:(某Google的攻城狮推荐的方法)
Workaround: instaed of BuildConfig.DEBUG create another boolean variable at lib-project’s e.g. BuildConfig.RELEASE and link it with application’s buildType.
https://gist.github.com/almozavr/d59e770d2a6386061fcb
- Android在AndroidStudio中引用Module依赖注意事项
- Android Studio中引用Module依赖注意事项
- AndroidStudio 删除依赖的 Android Library Module
- AndroidStudio 删除依赖的 Android Library Module
- android studio将项目中module变成library引用依赖
- 在Androidstudio中删除Module
- 如何在AndroidStudio中导入Module
- AndroidStudio如何依赖Module dependency?
- 在Android Studio中讲一个module作为另一个module的依赖
- AndroidStudio Module 对library中 aar 引用问题
- android studio 导入外部库文件,以及将项目中module引用依赖
- android studio 导入外部库文件,以及将项目中module引用依赖
- android studio 导入外部库文件,以及将项目中module变成library引用依赖
- android studio 导入外部库文件,以及将项目中module变成library引用依赖
- android studio 导入外部库文件,以及将项目中module变成library引用依赖
- AndroidStudio如何引用一个Module
- androidstudio中module是什么
- AndroidStudio中删除module
- 按Enter或Esc,MFC程序退出的解决方法
- 2017年第0届浙江工业大学之江学院程序设计竞赛决赛
- java源码分析之ArrayList
- 视觉跟踪综述
- Novate 封装Rxjava2.0 的使用
- Android在AndroidStudio中引用Module依赖注意事项
- MD5 校验和(checksum)
- MySql 里的IFNULL用法
- (一)基于SSH实现员工管理系统之框架整合篇
- Activity的启动模式和Intent标记位
- 语义分割--Dilated Residual Networks
- iOS开发系列--Objective-C之类和对象
- 学习CI框架数据库相关知识点总结
- 1061. 判断题(15) PAT