【问题标题】:Sharing files between Android mobile and wear modules在 Android 移动和穿戴模块之间共享文件
【发布时间】:2016-03-20 17:55:07
【问题描述】:

几个月前,我最初只用一个移动模块开始我的项目,现在我也有兴趣为可穿戴设备配置我的应用程序。也就是说,我的所有文件(Java、XML、drawable 等)都在移动模块中,所以我需要将我想要在移动模块和穿戴模块之间共享的所有文件转移到新创建的“通用”中模块?

编辑

有人可以向我解释一下来自移动设备和 Wear 的 Gradle 文件的以下 Gradle 项目同步错误是什么意思:

...这发生在我在两个模块中包含 compile project(':common') 之后:

首先,这是我的通用模块:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.1"

    defaultConfig {
        applicationId "dpark.gameoflife"
        minSdkVersion 9
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.2.1'
}

移动模块:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.1"

    defaultConfig {
        applicationId "dpark.gameoflife"
        minSdkVersion 9
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    wearApp project(':wear')
    compile project(':common')
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.2.1'
    compile 'com.google.android.gms:play-services:8.4.0'
}

最后,我的穿戴模块:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.1"

    defaultConfig {
        applicationId "dpark.gameoflife"
        minSdkVersion 20
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.google.android.support:wearable:1.3.0'
    compile 'com.google.android.gms:play-services-wearable:8.4.0'
    compile project(':common')
}

...如果有的话,我在发布模式下签署了我的公共模块的 APK,但我得到了与上面所示相同的错误。

【问题讨论】:

标签: android android-studio module wear-os


【解决方案1】:

正确,如果您的文件被两个模块(移动和穿戴)使用,那么显然,您应该让这两个模块使用一个共享模块,您可以在其中放置所有必要的文件。

在每个模块中添加compile project(': common')

【讨论】:

  • 好的,那么除了 Gradle 文件之外,我应该将哪些类型的文件留在手机和穿戴模块中?因为,我从字面上将除了 Gradle 文件之外的所有文件从移动模块转移到了公共模块。
  • 你应该传输常见的文件,比如drawables、styles或者一些共享的java类,比如utilsbase activity或者......在每个模块上只保留这个设备的具体实现
  • 假设您在两个设备中都使用了 recyclerview,移动设备包含一个包含两列的列表,而穿戴包含一列的列表。在这种情况下,您将从共享模块中检索数据,然后在移动设备上显示两列,在穿戴设备上显示一列
  • 因为你的通用模块不是应用程序(不可执行),它只能像一个库一样工作,你应该使用apply plugin: 'com.android.library'而不是apply plugin: 'com.android.application'
  • 没错,就是这样package name of the common module.R
【解决方案2】:

如上所述here

1- 确保build.gradle (Module:my-library-module) 中的第一行正确定义为:

apply plugin: 'com.android.library'

2- 将库添加到settings.gradle 为:

include ':app', ':my-library-module'

3- 将build.gradle (Module:xxx)(Module:mobile) 中的库编译为:

dependencies {
    compile project(":my-library-module")
}

4- 在您的项目中,import 库为:

import xxx.xx.xx

【讨论】:

  • compile 指令已弃用。目前,编译行应为implementation project(':my-library-module')
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-12
  • 2016-04-20
  • 2011-06-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多