Android的Gradle技巧 2.2将应用程序从Eclipse ADT移植到Android Studio

32 篇文章 0 订阅
2.2将应用程序从Eclipse ADT移植到Android Studio
问题
您要将现有的Eclipse ADT项目导入到Android Studio。

Android Studio提供了一个“导入”向导来重写现有项目。
讨论
图2-1显示了从Android ADT或Gradle导入项目的Android Studio欢迎页面上的链接。


图2-1。 Android Studio欢迎页面显示导入项目选项
链接将显示一个视图,您可以在其中导航到现有的Eclipse ADT项目。
图2-2显示了这样一个项目。它使用旧的项目结构,其中res,src和AndroidManifest.xml都是根的直接子节点。
在选择目标目录(向导不覆盖原始项目)之后,向导提供将lib文件夹中的jar文件转换为Gradle构建文件中的依赖关系以及其他选项,如图2-3所示。


图2-2。选择Eclipse ADT项目


图2-3。导入项目选项
然后向导重新构建项目并构建它。默认情况下,import-summary.txt窗口显示主要更改。实施例2-11显示了一个样品。
实施例2-11。项目导入摘要文本文件

ECLIPSE ANDROID PROJECT IMPORT SUMMARY
======================================
Ignored Files:
--------------
The following files were *not* copied into the new Gradle project; you
should evaluate whether these are still needed in your project and if
so manually move them:
* proguard-project.txt
Moved Files:
------------
Android Gradle projects use a different directory structure than ADT
Eclipse projects. Here's how the projects were restructured:
* AndroidManifest.xml => app/src/main/AndroidManifest.xml
* assets/ => app/src/main/assets
* res/ => app/src/main/res/
* src/ => app/src/main/java/
Next Steps:
-----------
You can now build the project. The Gradle project needs network
connectivity to download dependencies.
Bugs:
-----
If for some reason your project does not build, and you determine that
it is due to a bug or limitation of the Eclipse to Gradle importer,
please file a bug at http://b.android.com with category
Component-Tools.
(This import summary is for your information only, and can be deleted
after import once you are satisfied with the results.)
除了ProGuard文件推荐,其余的更改大多是移动文件。
生成的顶级gradle.build文件与创建新项目时相同,如示例2-12所示。
实施例2-12。顶级生成的构建文件
sub-projects/modules.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.0.0'
}
}
allprojects {
repositories {
jcenter()
}
}

应用程序文件夹包含原始项目,结果类似于示例2-13。
实施例2-13。应用程序级构建文件
apply plugin: 'com.android.application'
android {
compileSdkVersion 17
buildToolsVersion "23.0.3"
defaultConfig {
applicationId "com.example.tips"
minSdkVersion 8
targetSdkVersion 17
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'),
'proguard-rules.txt'
}
}
}
(请注意,此特定项目没有任何其他jar文件,因此不需要添加依赖性块。)
最后,生成了一个settings.gradle文件(示例2-14),其中显示了应用程序项目是唯一包含的模块。

实施例2-14。生成settings.gradle文件
include':app'
虽然AndroidManifest.xml文件尚未更改,但在Android Studio中打开它会给您一些警告(示例2-15)。
实例2-15。 AndroidManifest.xml中的警告
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.tips"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" /> //1
<application
<!-- no problems --!>
</application>
</manifest>

1多个警告
Android Studio警告您targetSdkVersion已设置为旧版本的Android SDK。它还指出,minSdkVersion和targetSdkVersion的值被它们在Gradle构建文件中的副本覆盖(示例1-3)。
因为Gradle构建胜利,最好的方法是从清单中简单地删除uses-sdk标记,然后根据需要更改build.gradle文件中的值。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值