本文主要是介绍Android在AndroidStudio中引用Module依赖注意事项,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
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 17targetSdkVersion 21...}
作为library的module
apply plugin: 'com.android.library'
defaultConfig {minSdkVersion 17targetSdkVersion 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"><applicationandroid: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 23buildToolsVersion "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依赖注意事项的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!