如何建立与Android Studio和摇篮的机器人库? [英] How to build an android library with Android Studio and gradle?
问题描述
我试图将项目从Eclipse的迁移,但没有我曾尝试在工作。在Eclipse我有3个项目(2 Android应用程序项目和1个机器人库项目)。 2.应用项目依赖于库项目。当我做出口的摇篮,我得到3个项目,不工作。我打开重组项目,但还没有找到这个应该怎么做任何文件。
I am trying to migrate a project from Eclipse but nothing I have tried is working. In Eclipse I have 3 projects (2 android app projects and 1 android library project). The 2 app projects depend on the library project. When I do the gradle export I get 3 projects that don't work. I am open to restructuring the project but haven't found any documentation on how this should be done.
有没有一种方法,使3个项目是Eclipse的出口协同工作?我是最好的重组的事情,如果是这样的文档,这个应该怎么做?
Is there a way to make the 3 projects that Eclipse exports work together? Am I better off restructuring things and if so is documentation for how this should be done?
更新
我已经上传了整个项目的GitHub <一个href="https://github.com/respectThe$c$c/android-studio-library-example">https://github.com/respectThe$c$c/android-studio-library-example
I have uploaded the entire project to GitHub https://github.com/respectTheCode/android-studio-library-example
更新1
根据从帕德玛·库马尔的建议,这是我都试过了。
Based the suggestions from Padma Kumar this is what I have tried.
- 创建一个新的项目名为
的MyApp
- 点击
文件&gt;新模块
,选择的Android库
并将其命名为MyLib中
- 点击
构建&GT;使项目
- Create a new Project called
MyApp
- Click
File > New Module
, chooseAndroid Library
and name itMyLib
- Click
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.
然后我加了包
属性清单使它
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.mylib" >
建立之后,我得到这个错误
After building I get this error
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
添加依赖似乎并没有对错误的任何影响。从上面继续
Adding dependency doesn't seem to have any impact on the error. Continuing from above
- 点击
文件&gt;项目结构&gt;模块&gt; MyApp的-的MyApp
- 切换到
依赖
标签 - 点击
+&GT;模块依赖
并选择MyLib中
- 点击
应用
和确定
- 点击
构建&GT;使项目
- Click
File > Project Structure > Modules > MyApp-MyApp
- Switch to
Dependencies
Tab - Click
+ > Module Dependency
and pickMyLib
- Click
Apply
andOK
- Click
Build > Make Project
更新2
根据从伊森的建议,这是我们得到
Based the suggestions from Ethan this is where we get
在2分项目 build.gradle
似乎有所有正确的部分,唯一的区别是插件线波纹管是 MyApp的/ build.gradle
。
The 2 sub project build.gradle
seem to have all of the correct parts and the only difference is the plugin line bellow is the 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
是空
The root project build.gradle
was empty so I added the two projects like this
dependencies {
compile project(":MyLib")
compile project(":MyApp")
}
我现在正在建设的时候得到这个错误
I now get this error when building
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
非常感谢阮经天为解决这一点。
Big thanks Ethan for solving this.
- 添加
编制项目(:子项目:MyLib中')
到MyLib中/ build.gradle
李> - 删除
编译文件(库/ Android的支持 - v4.jar')
从MyLib中/ build.gradle
从摇篮 - 关闭项目,并导入根项目
- Add
compile project(':SubProjects:MyLib')
to theMyLib/build.gradle
- Remove
compile files('libs/android-support-v4.jar')
from theMyLib/build.gradle
- Close project and Import the root project from gradle
更新4
截至0.1.2,你现在可以包括编译com.android.support:support-v4:13.0.0
而不是编译文件(库/ Android的支持 - v4.jar')
。由于它是从mavin来了,现在你可以将此在多个项目中没有问题。
As of 0.1.2 you can now include compile "com.android.support:support-v4:13.0.0"
instead of compile files('libs/android-support-v4.jar')
. Since it is coming from mavin now you can include this in multiple projects without problems.
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
文件后重新导入您的项目。
As of 0.1.3 there is now a "Sync Project" button in the toolbar. You can click that instead of reimporting your project after making changes to .gradle
files.
推荐答案
注意:这个答案是一个纯粹的摇篮答案,我用这个在的IntelliJ定期,但我不知道该怎么整合与Android的工作室。我在知道是怎么回事,我的信徒,所以这是我如何使用摇篮和Android。
Note: This answer is a pure Gradle answer, I use this in IntelliJ on a regular basis but I don't know how the integration is with Android Studio. I am a believer in knowing what is going on for me, so this is how I use Gradle and Android.
TL; DR 完整的例子 - <一个href="https://github.com/ethankhall/driving-time-tracker/">https://github.com/ethankhall/driving-time-tracker/
免责声明:这是一个项目,我/正致力于
Disclaimer: This is a project I am/was working on.
摇篮有href="http://tools.android.com/tech-docs/new-build-system/user-guide">定义结构的
Gradle has a defined structure ( that you can change, link at the bottom tells you how ) that is very similar to Maven if you have ever used it.
Project Root
+-- src
| +-- main (your project)
| | +-- java (where your java code goes)
| | +-- res (where your res go)
| | +-- assets (where your assets go)
| | \-- AndroidManifest.xml
| \-- instrumentTest (test project)
| \-- java (where your java code goes)
+-- build.gradle
\-- settings.gradle
如果你只有一个项目,则不需要settings.gradle文件。然而,你想添加更多的项目,所以我们需要它。
If you only have the one project, the settings.gradle file isn't needed. However you want to add more projects, so we need it.
现在,让我们快来看看那个build.gradle文件。你将需要这个它(加入了Android工具)
Now let's take a peek at that build.gradle file. You are going to need this in it (to add the android tools)
build.gradle
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.3'
}
}
现在,我们需要告诉摇篮一些Android的部分。这是pretty的简单。一个基本的(在我的大部分情况下工作)看起来像下面这样。我在此块注释,它可以让我产生了APK时指定版本名称和code。
Now we need to tell Gradle about some of the Android parts. It's pretty simple. A basic one (that works in most of my cases) looks like the following. I have a comment in this block, it will allow me to specify the version name and code when generating the APK.
build.gradle
apply plugin: "android"
android {
compileSdkVersion 17
/*
defaultConfig {
versionCode = 1
versionName = "0.0.0"
}
*/
}
这是我们打算要添加,帮忙的人还没有见过摇篮的光呢,一种让他们使用项目,而不安装它。
Something we are going to want to add, to help out anyone that hasn't seen the light of Gradle yet, a way for them to use the project without installing it.
build.gradle
task wrapper(type: org.gradle.api.tasks.wrapper.Wrapper) {
gradleVersion = '1.4'
}
所以,现在我们有一个项目,以建立。现在,我们要添加其他人。我把它们放在一个目录下,也许叫它DEPS,或子项目。这其实并不重要,但你需要知道你所说的那样。告诉摇篮,其中的项目,你将需要将它们添加到settings.gradle。
So now we have one project to build. Now we are going to add the others. I put them in a directory, maybe call it deps, or subProjects. It doesn't really matter, but you will need to know where you put it. To tell Gradle where the projects are you are going to need to add them to the settings.gradle.
目录结构:
Project Root
+-- src (see above)
+-- subProjects (where projects are held)
| +-- reallyCoolProject1 (your first included project)
| \-- See project structure for a normal app
| \-- reallyCoolProject2 (your second included project)
| \-- See project structure for a normal app
+-- build.gradle
\-- settings.gradle
settings.gradle:
include ':subProjects:reallyCoolProject1'
include ':subProjects:reallyCoolProject2'
过去的事情,你应该确保的是在子项目/ reallyCoolProject1 / build.gradle 的应用插件:机器人库
而不是对应用插件:机器人
。
像每一个摇篮工程(和Maven),我们现在需要告诉根项目有关它的依赖。这也可以包括你想要的任何普通的Java的相关性。
Like every Gradle project (and Maven) we now need to tell the root project about it's dependency. This can also include any normal Java dependencies that you want.
build.gradle
dependencies{
compile 'com.fasterxml.jackson.core:jackson-core:2.1.4'
compile 'com.fasterxml.jackson.core:jackson-databind:2.1.4'
compile project(":subProjects:reallyCoolProject1")
compile project(':subProjects:reallyCoolProject2')
}
我知道这似乎是一个很大的台阶,但他们是pretty的容易,一旦你一次两次做到这一点。这种方式也可以让你建立一个CI服务器上假设你已经安装有Android SDK中。
I know this seems like a lot of steps, but they are pretty easy once you do it once or twice. This way will also allow you to build on a CI server assuming you have the Android SDK installed there.
NDK旁注:如果你要使用NDK你会需要像下面。示例build.gradle文件可以在这里找到: https://gist.github.com/khernyo/4226923
NDK Side Note: If you are going to use the NDK you are going to need something like below. Example build.gradle file can be found here: https://gist.github.com/khernyo/4226923
build.gradle
task copyNativeLibs(type: Copy) {
from fileTree(dir: 'libs', include: '**/*.so' ) into 'build/native-libs'
}
tasks.withType(Compile) { compileTask -> compileTask.dependsOn copyNativeLibs }
clean.dependsOn 'cleanCopyNativeLibs'
tasks.withType(com.android.build.gradle.tasks.PackageApplication) { pkgTask ->
pkgTask.jniDir new File('build/native-libs')
}
来源:
- <一个href="http://tools.android.com/tech-docs/new-build-system/user-guide">http://tools.android.com/tech-docs/new-build-system/user-guide
- https://gist.github.com/khernyo/4226923
- <一个href="https://github.com/ethankhall/driving-time-tracker/">https://github.com/ethankhall/driving-time-tracker/
- http://tools.android.com/tech-docs/new-build-system/user-guide
- https://gist.github.com/khernyo/4226923
- https://github.com/ethankhall/driving-time-tracker/
这篇关于如何建立与Android Studio和摇篮的机器人库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!