问题
Several months ago, I originally started my project with just a mobile module, and now I am interested in configuring my app for wearable devices as well. That said, all of my files (Java, XML, drawables, and etc.) are in the mobile module, so do I need to transfer all of the files I want to share between mobile and wear modules into a newly created "common" module?
EDIT:
Can somebody explain to me what the following Gradle project sync errors coming from the mobile and wear's Gradle files mean:
... This happened after I included compile project(':common') in both modules as follows:
First, here's my common module:
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'
}
Mobile module:
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'
}
Lastly, my wear module:
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')
}
... If anything, I signed an APK of my common module in release mode, and yet I get the same errors as shown above.
回答1:
Correct, If your files are used by both modules (mobile and wear) then obviously, you should make these two modules use a shared module where you put all the necessary files.
in each module add compile project(': common')
回答2:
As mentioned here:
1- Make sure the first line in the build.gradle (Module:my-library-module)
, is defined correctly as:
apply plugin: 'com.android.library'
2- Add the library to the settings.gradle
as:
include ':app', ':my-library-module'
3- Compile the library in the build.gradle (Module:xxx)
(Module:mobile) as:
dependencies {
compile project(":my-library-module")
}
4- In your project, import
the library as:
import xxx.xx.xx
来源:https://stackoverflow.com/questions/36117365/sharing-files-between-android-mobile-and-wear-modules