如何使用 Android Studio 和 gradle 构建一个 android 库? [英] How to build an android library with Android Studio and gradle?

查看:21
本文介绍了如何使用 Android Studio 和 gradle 构建一个 android 库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从 Eclipse 迁移一个项目,但我尝试过的任何内容都不起作用.在 Eclipse 中,我有 3 个项目(2 个 android 应用程序项目和 1 个 android 库项目).2 个应用程序项目依赖于库项目.当我进行 gradle 导出时,我得到 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.

有没有办法让 Eclipse 导出的 3 个项目一起工作?我最好重组一些东西吗?如果是这样,应该如何做的文档?

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-例子

I have uploaded the entire project to GitHub https://github.com/respectTheCode/android-studio-library-example

更新 1

根据 Padma Kumar 的建议,这是我尝试过的.

Based the suggestions from Padma Kumar this is what I have tried.

  1. 创建一个名为 MyApp
  2. 的新项目
  3. 点击<代码>文件>新建模块,选择Android Library并命名为MyLib
  4. 点击<代码>构建>制作项目
  1. Create a new Project called MyApp
  2. Click File > New Module, choose Android Library and name it MyLib
  3. 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.

然后我在清单中添加了一个 package 属性

I then added a package attribute to the manifest making it

<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

添加依赖似乎对错误没有任何影响.从上面继续

Adding dependency doesn't seem to have any impact on the error. Continuing from above

  1. 点击<代码>文件>项目结构模块 >MyApp-MyApp
  2. 切换到 Dependencies 标签
  3. 点击<代码>+ >模块依赖并选择MyLib
  4. 点击ApplyOK
  5. 点击<代码>构建>制作项目
  1. Click File > Project Structure > Modules > MyApp-MyApp
  2. Switch to Dependencies Tab
  3. Click + > Module Dependency and pick MyLib
  4. Click Apply and OK
  5. Click Build > Make Project

更新 2

根据 Ethan 的建议,我们得到了

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

非常感谢 Ethan 解决了这个问题.

Big thanks Ethan for solving this.

  1. compile project(':SubProjects:MyLib')添加到MyLib/build.gradle
  2. MyLib/build.gradle
  3. 中删除 compile files('libs/android-support-v4.jar')
  4. 关闭项目并从 gradle 导入根项目

更新 4

从 0.1.2 开始,您现在可以包含 compile "com.android.support:support-v4:13.0.0" 而不是 compile files('libs/android-support-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.

推荐答案

注意:这个答案是一个纯粹的 Gradle 答案,我经常在 IntelliJ 中使用它,但我不知道如何与 Android Studio 集成.我相信知道我发生了什么,所以这就是我使用 Gradle 和 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 完整示例 - https://github.com/ethankhall/driving-time-tracker/

免责声明:这是我正在/正在从事的项目.

Disclaimer: This is a project I am/was working on.

Gradle 有一个 定义 结构(你可以更改,底部的链接会告诉您如何 ) 如果您曾经使用过它,它与 Maven 非常相似.

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'
    }
}

现在我们需要告诉 Gradle 一些 Android 部分.这很简单.一个基本的(适用于我的大多数情况)如下所示.我在这个块有一个注释,它允许我在生成 APK 时指定版本名称和代码.

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"
        }
        */
    }

我们将要添加的东西,以帮助那些还没有看到 Gradle 光芒的人,一种让他们无需安装即可使用该项目的方式.

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 或 subProjects.这并不重要,但你需要知道你把它放在哪里.要告诉 Gradle 项目在哪里,您需要将它们添加到 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'

您应该确保的最后一件事是 subProjects/reallyCoolProject1/build.gradle 具有 apply plugin: "android-library" 而不是 apply 插件:安卓".

The last thing you should make sure of is the subProjects/reallyCoolProject1/build.gradle has apply plugin: "android-library" instead of apply plugin: "android".

像每个 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')
}

我知道这看起来像很多步骤,但是一旦你做了一两次,它们就很容易了.这种方式还允许您在 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')
}

来源:

  1. http://tools.android.com/tech-文档/新构建系统/用户指南
  2. https://gist.github.com/khernyo/4226923
  3. https://github.com/ethankhall/driving-time-tracker/

这篇关于如何使用 Android Studio 和 gradle 构建一个 android 库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆