将项目作为module导入另一个项目

记录下

将项目作为module导入另一个项目

1.进入要作为module的项目 app下的build.gradle

a. 将apply plugin: 'com.android.application' 改成apply plugin: 'com.android.library'

b. 去掉 build.gradle(app) 里面的android-> defaultConfig 里面的applicationId "com.mogujie.tt"

c. ActivityManifest去掉 android.intent.action.MAIN 防止生成2个图标

    <intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
复制代码

2.在主项目导入module

File->New->ImportModule 进入New Module模块

Source direcory选择module,勾选import, Modulename 为module name

3.添加依赖引用

File->Project Structure 进入Project Structure 模块

添加依赖: app->Dependencies 点击绿色“+” 选择Module dependency。选择要导入的module。

注意:如果导入的module里面导入有module,也需要添加依赖。

导入module, module 可能出现错误

1.需要常量表达式

module 里面switch 不能是R.id.xx 全部替换成 if else

快捷键:选中switch 按alt+enter ,选择Replace ‘switch’ with ‘if’

2.我们的主工程中已经使用了android:theme=”@style/AppTheme”,而在module中也使用到了android:theme=”@style/AppTheme”,所以编辑器在运行程序的时候会去合并,但是合并失败就会报此错误。icon,label等都可能出现上面问题。

  解决:在主工程AndroidManifest文件里面application中加上 tools:replace="android:icon,android:name,android:theme"

注: 某些输入文件使用或覆盖了已过时的 API。

注: 有关详细信息, 请使用 -Xlint:deprecation 重新编译。

注: 某些输入文件使用了未经检查或不安全的操作。

注: 有关详细信息, 请使用 -Xlint:unchecked 重新编译。

在app build.gradle里面的根目录加入以下配置可以查看详细信息。

allprojects {
    gradle.projectsEvaluated {
        tasks.withType(JavaCompile) {
            options.compilerArgs << "-Xlint:unchecked" << "-Xlint:deprecation"
        }
    }
}
复制代码

4.包重复

Error:Execution failed for task ':app:transformClassesWithJarMergingForDebug'.

com.android.build.api.transform.TransformException: java.util.zip.ZipException

解决方法 使用Search Everywhere(默认快捷键双击 shift )查询是否有重复的包

去重: 选择以project显示项目,找到最下面的External Libraries,然后继续找,找到相对应的类库:

gson 如图依次点开之后发现了一个pom.xml,这里面就是关于这个jar的一些配置文件,点开之后长这个样子

pom.xml 往下找,发现了一个gson的引用

gson引用 没错,这个就是导致报错的直接原因。复制里面groupId,到你的app的build.gradle里,找到那个依赖,添加 {exclude group: 'com.google.code.gson'} 把这个groudId的引用去除掉。如下图所示

去除重复依赖

5.版本不统一,有重复不同的版本

Caused by: java.lang.NoSuchMethodError: No virtual method isSuccess()Z in class Lretrofit2/Response; or its super classes (declaration of 'retrofit2.Response' appears in /data/data/com.hengda.smart.retrofitdemo/files/instant-run/dex/slice-retrofit-classes.dex)
复制代码

6.引用两个及以上的module library或者其他的jar包 出现错误:当你的程序需要引用两个及以上的module library或者其他的jar包是出现错误类型如下

Error:Execution failed for task ':app:transformResourcesWithMergeJavaResForDebug'.
> com.android.build.api.transform.TransformException: com.android.builder.packaging.DuplicateFileException: Duplicate files copied in APK org/apache/log4j/xml/log4j.dtd
File1: /Users/minyuchun/androidwork/projectstudio/StarFaceFrame/app/build/intermediates/exploded-aar/StarFaceFrame/facelibrary/unspecified/jars/classes.jar
File2: /Users/minyuchun/androidwork/projectstudio/StarFaceFrame/app/build/intermediates/exploded-aar/StarFaceFrame/rylibrary/unspecified/jars/classes.jar
复制代码

出现上述错误的原因是因为 你在引用的labrary中多个存在相同的包导致在打包是冲突 解决方式如下,在android下的 写

packagingOptions{
&emsp;&emsp;exclude 'org/apache/log4j/xml/log4j.dtd'
}
复制代码

7.module 里面找不到so文件

EXCEPTION, please implement com.mogujie.tt.imservice.network.MsgServerHandler.exceptionCaught()  for proper
handling.java.lang.UnsatisfiedLinkError: dalvik.system.PathClassLoader[DexPathList[[zip file
"/data/app/com.zsdudu.dudu1/base.apk"],nativeLibraryDirectories=[/data/app/com.zsdudu.dudu-1/lib/arm64,/data/app/c
om.zsdudu.dudu-1/base.apk!/lib/arm64-v8a,/system/lib64, /vendor/lib64, /system/vendor/lib64, /product/lib64]]]
couldn't find "libsecurity.so"
复制代码

原因:主项目里面有 'armeabi', 'armeabi-v7a', 'arm64-v8a'这些文件夹, 而module 里面只有 'armeabi', 'armeabi-v7a。CPU架构arm64-v8a的手机找so文件,发现有 'arm64-v8a' 文件夹就会在里面找so文件(没有就会直接去找armeali文件夹里面的so文件),而module 里面没有 'arm64-v8a'的so文件就会报错。 解决方法: 在主项目里面(不是module)build.gradle文件 android -> defaultConfig 里面设置只支持主项目和module都支持的架构。

ndk {
    // 设置项目和module都支持的SO库架构
    abiFilters 'armeabi', 'armeabi-v7a'
}
复制代码
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值