【发布时间】:2013-05-19 01:19:00
【问题描述】:
我正在尝试从 Eclipse 迁移一个项目,但我没有尝试过任何工作。在 Eclipse 中,我有 3 个项目(2 个 android 应用程序项目和 1 个 android 库项目)。 2 个应用程序项目依赖于库项目。当我进行 gradle 导出时,我得到了 3 个不起作用的项目。我对重组项目持开放态度,但还没有找到任何关于如何进行重组的文档。
有没有办法让 Eclipse 导出的 3 个项目一起工作?我是不是最好重组一些东西,如果是这样,应该如何做的文档?
更新
我已经把整个项目上传到了 GitHub https://github.com/respectTheCode/android-studio-library-example
更新 1
根据 Padma Kumar 的建议,这是我尝试过的。
- 创建一个名为
MyApp的新项目 - 点击
File > New Module,选择Android Library并命名为MyLib - 点击
Build > Make Project
构建失败并出现此错误
Module "MyLib" was fully rebuilt due to project configuration/dependencies changes
Compilation completed with 1 error and 0 warnings in 19 sec
1 error
0 warnings
/.../MyApp/MyLib/build/bundles/debug/AndroidManifest.xml
Gradle: <manifest> does not have package attribute.
然后我在清单中添加了一个package 属性
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.mylib" >
编译后出现此错误
Module "MyApp" was fully rebuilt due to project configuration/dependencies changes
Compilation completed with 2 errors and 0 warnings in 13 sec
2 errors
0 warnings
/.../MyApp/MyLib/src/main/java/com/example/mylib/MainActivity.java
Gradle: package R does not exist
Gradle: package R does not exist
添加依赖似乎对错误没有任何影响。从上面继续
- 点击
File > Project Structure > Modules > MyApp-MyApp - 切换到
Dependencies标签 - 单击
+ > Module Dependency并选择MyLib - 点击
Apply和OK - 点击
Build > Make Project
更新 2
根据 Ethan 的建议,这就是我们得到的地方
2 个子项目build.gradle 似乎包含所有正确的部分,唯一的区别是下面的插件行是MyApp/build.gradle。
buildscript {
repositories {
maven { url 'http://repo1.maven.org/maven2' }
}
dependencies {
classpath 'com.android.tools.build:gradle:0.4'
}
}
apply plugin: 'android'
dependencies {
compile files('libs/android-support-v4.jar')
}
android {
compileSdkVersion 17
buildToolsVersion "17.0.0"
defaultConfig {
minSdkVersion 7
targetSdkVersion 16
}
}
根项目build.gradle 是空的,所以我像这样添加了两个项目
dependencies {
compile project(":MyLib")
compile project(":MyApp")
}
我现在在构建时遇到此错误
Gradle:
FAILURE: Build failed with an exception.
* Where:
Build file '/Users/kevin/GitHub/AppPress/MyApp/build.gradle' line: 2
* What went wrong:
A problem occurred evaluating root project 'MyApp'.
> Could not find method compile() for arguments [project ':MyLib'] on root project 'MyApp'.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
更新 3
非常感谢 Ethan 解决了这个问题。
- 将
compile project(':SubProjects:MyLib')添加到MyLib/build.gradle - 从
MyLib/build.gradle中删除compile files('libs/android-support-v4.jar') - 关闭项目并从 gradle 导入根项目
更新 4
从 0.1.2 开始,您现在可以包含 compile "com.android.support:support-v4:13.0.0" 而不是 compile files('libs/android-support-v4.jar')。由于它现在来自 mavin,因此您可以将其包含在多个项目中而不会出现问题。
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.4.2'
}
}
apply plugin: 'android'
dependencies {
compile "com.android.support:support-v4:13.0.0"
compile project(':SubProjects:MyLib')
}
更新 5
从 0.1.3 开始,工具栏中现在有一个“同步项目”按钮。您可以在更改 .gradle 文件后单击它而不是重新导入您的项目。
【问题讨论】:
-
我为您的 github 项目提出了拉取请求。有 2 个简单的变化。从 /build.gradle 中删除内容。 {我这样做是因为你没有定义它是什么类型的项目,所以它抱怨不知道如何处理它},然后我将依赖项从 :SubProject:MyLib 添加到 :SubProject:MyApp。然后我运行 ./gradlew :SubProject:MyApp:iD (iD 映射到 installDebug)。我还必须删除重复的 libs/android-support-v4.jar。因为它不是一个“真正的”依赖,所以你不能在两个地方拥有它。
-
值得一提的是,如果您将
compile 'com.google.android:support-v4:r6'添加到您的每个项目和maven 插件(如下面的build.gradle 示例中)而不是compile files('libs/android-support-v4.jar')gradle 将意识到这两个项目都包括相同的东西,只会包含一次。 -
@respectTheCode 我发送了一个拉取请求来清理您的示例 github 项目。感谢分享!
标签: android gradle android-studio