从 Eclipse 迁移到 Android-Studio (gradle) [英] Migrating from Eclipse to Android-Studio (gradle)

查看:32
本文介绍了从 Eclipse 迁移到 Android-Studio (gradle)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试迁移我的应用程序以使用 gradle,但我面临一些问题,包括库项目.

我的项目树是这样的:

我的项目根- MyLib1-- 资源-- 源代码-- 库- MyLib2-- 资源-- 源代码-- 库- MyLib3-- 资源-- 源代码-- 库- 我的应用程序库-- 资源-- 源代码-- 库- MyApp - 完整版-- 资源-- 源代码-- 库- MyAppFree - 免费版-- 资源-- 源代码-- 库

使用 Eclipse 我有以下依赖

MyAppBase 依赖于:-MyLib1-MyLib2-MyLib3MyApp 取决于:-MyAppBase-MyLib1-MyLib2-MyLib3MyAppFree 取决于:-MyAppBase-MyLib1-MyLib2-MyLib3

这个组织在 Eclipse 中运作良好,但现在使用 Android-Studio 和 gradle 我遇到了问题.

我有以下 build.gradle 文件:

buildscript {存储库{MavenCentral()}依赖{类路径 'com.android.tools.build:gradle:0.7.+'}}应用插件:'android'依赖{//编译项目('../MyLib1') <- 错误编译文件树(目录:'libs',包括:'*.jar')}安卓 {compileSdkVersion 18构建工具版本19.0.1"默认配置{minSdk 版本 4目标SDK版本14}源集{主要的 {manifest.srcFile 'AndroidManifest.xml'java.srcDirs = ['src']resources.srcDirs = ['src']aidl.srcDirs = ['src']renderscript.srcDirs = ['src']res.srcDirs = ['res']assets.srcDirs = ['assets']}//将测试移动到tests/java、tests/res 等...instrumentTest.setRoot('测试')//将构建类型移动到 build-types///比如 build-types/debug/java, build-types/debug/AndroidManifest.xml, ...//这会将它们从 src//... 下的默认位置移出//与主源集使用的 src/冲突.//添加新的构建类型或产品风格应该伴随//通过类似的定制.debug.setRoot('build-types/debug')release.setRoot('build-types/release')}}

如何将项目 MyLib1、MyLib2、MyLib3 作为依赖项包含在内,以便与我的项目一起编译???

解决方案

目前所有的依赖都需要在项目的根目录下,所以你需要设置你的项目根目录在上面MyLib1MyLib2MyApp等,以后会解除这个限制;您可以在 https://code.google.com/p 跟踪其进度/android/issues/detail?id=56367.您的库将是该根目录下的库模块,而您的应用程序将是 Android 应用程序模块.每个模块都有自己独立的 build.gradle 文件,可以编译为 JAR(纯 Java 库)、AAR(Android 库,包括代码 + 资源)或 APK(Android 应用程序).

我不确定 MyAppFreeMyApp 是否是独立的 Eclipse 项目;如果是,在 Android Studio 和 Gradle 下,我鼓励您将它们组合成一个具有免费和付费风格的模块.构建风格被明确设计来帮助这种用例.请参阅 http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Product-flavors 了解更多信息.

更新

在下面的评论中,您似乎拥有大量的库.因为您可能不想一直从源代码构建它们,或者管理具有数十个模块的项目.在这种情况下,将不经常更改的模块作为单独的项目保留更有意义.这些项目可以编译为 JAR 或 AAR,这让我们回到了您最初的问题,即如何在 Android Studio 中实现这些项目.

您可以将 JAR 文件复制到项目根目录下的 libs 目录中并将它们链接进去.我相信尝试对 AAR 库执行相同操作会出现问题;请参阅 https://code.google.com/p/android/issues/detail?id=63908 以跟踪进度.如果您不想维护库的多个副本,您可以尝试符号链接目录(我认为会起作用),或者您可以设置本地 Maven 存储库并拥有辅助项目发布他们的工件.我没有触手可及的详细说明链接;你可以从 http://www.gradle.org/docs/current/userguide/publishing_maven.html.

设置本地 Maven 存储库将有一个公平的学习曲线,但是一旦完成,您可能会发现它非常干净地解决了您的问题,并且如果您在一个拥有多个开发人员的商店并且会就像一个组织范围的 Maven 存储库,也许包含从构建服务器发布到它的工件,您可以设置它.

I'm trying to migrate my applications to use gradle, but I'm facing some problems including library projects.

My project tree is this:

My projects root
- MyLib1
-- res
-- src
-- libs
- MyLib2
-- res
-- src
-- libs
- MyLib3
-- res
-- src
-- libs
- MyAppBase
-- res
-- src
-- libs
- MyApp - full version
-- res
-- src
-- libs
- MyAppFree - free version
-- res
-- src
-- libs

With Eclipse I had the following dependencies

MyAppBase depends on:
-MyLib1
-MyLib2
-MyLib3

MyApp depends on:
-MyAppBase
-MyLib1
-MyLib2
-MyLib3

MyAppFree depends on:
-MyAppBase
-MyLib1
-MyLib2
-MyLib3

This organization worked well within Eclipse, but now with Android-Studio and gradle I'm having problems.

I've got the following build.gradle file:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.7.+'
    }
}
apply plugin: 'android'

dependencies {
    //compile project('../MyLib1')  <- error
    compile fileTree(dir: 'libs', include: '*.jar')
}

android {
    compileSdkVersion 18
    buildToolsVersion "19.0.1"

    defaultConfig {
        minSdkVersion 4
        targetSdkVersion 14
    }
    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src']
            resources.srcDirs = ['src']
            aidl.srcDirs = ['src']
            renderscript.srcDirs = ['src']
            res.srcDirs = ['res']
            assets.srcDirs = ['assets']
        }

        // Move the tests to tests/java, tests/res, etc...
        instrumentTest.setRoot('tests')

        // Move the build types to build-types/<type>
        // For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ...
        // This moves them out of them default location under src/<type>/... which would
        // conflict with src/ being used by the main source set.
        // Adding new build types or product flavors should be accompanied
        // by a similar customization.
        debug.setRoot('build-types/debug')
        release.setRoot('build-types/release')
    }
}

How can I include the projects MyLib1, MyLib2, MyLib3 as a dependency so It will be compiled along with my project???

解决方案

At present, all dependencies need to live under the project's root directory, so you'd need to set up your project to be rooted at the directory above MyLib1, MyLib2, MyApp, etc. This limitation will be lifted in the future; you can track its progress at https://code.google.com/p/android/issues/detail?id=56367. Your libraries would be library modules under that root, and your app(s) would be Android application modules. Each module has its own separate build.gradle file and can compile to a JAR (plain Java library), AAR (Android library, which includes code + resources), or APK (Android app).

I'm not sure if MyAppFree and MyApp are separate Eclipse projects; if they are, under Android Studio and Gradle I'd encourage you to combine them into one module that has free and paid flavors. Build flavors are designed explicitly to aid this sort of use case. See http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Product-flavors for more info.

UPDATE

In the comments below it looks like you have a very large number of libraries. In that you probably don't want to build them all from source all the time, or manage a project that has dozens of modules. In that scenario, keeping modules that don't change very often as separate projects makes more sense. Those projects can compile to JAR or AAR, which brings us back to your original question of how to make those work in Android Studio.

You could copy JAR files into a libs directory under your project root and link them in. I believe there are problems trying to do the same with AAR libraries; see https://code.google.com/p/android/issues/detail?id=63908 to track the progress of that. If you don't want to maintain multiple copies of the libraries, you could either try symlinking the directories (I think that will work), or you could set up a local Maven repository and have the side projects publish their artifacts to that. I don't have links with detailed instructions on that at my fingertips; you could start with http://www.gradle.org/docs/current/userguide/publishing_maven.html.

There will be a fair learning curve to getting a local Maven repo set up, but once it's done, you'll probably find that it solves your problem pretty cleanly, and if you're in a shop with multiple developers and would like an organization-wide Maven repo, perhaps with artifacts that are published to it from a build server, you can set that up.

这篇关于从 Eclipse 迁移到 Android-Studio (gradle)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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