如何從Eclipse遷移到AndroidStudio

來源:互聯網
上載者:User

參考http://developer.android.com/sdk/installing/studio.html

Android Studio與Eclipse的不同點主要有

Gradle-based build support.

Built-in support for Google Cloud Platform, making it easy to integrate Google Cloud Messaging and App Engine as server-side components.

其中原生支援Google Cloud Platform貌似很有吸引力,可以很容易地把架在App Engine上的web端與Android端統一起來。

由於我做的項目是基於Eclipse做的開發,非常希望能丟棄Ant,使用Gradle來徹底基於AndroidStudio做開發,這當然也是Google推薦的方式。

為了使用AndroidStudio,需要利用ADT從Eclipse中匯出Gradle build files。按照官方文檔中記載的步驟,

Update your Eclipse ADT Plugin (you must have version 22.0 or higher).In Eclipse, select File > Export.In the window that appears, open Android and select Generate Gradle build files.Select the project you want to export for Android Studio and click Finish.

此時工程根目錄下會產生一個build.gradle檔案和一個gradle檔案夾,繼續執行以下步驟:

In Android Studio, close any projects currently open. You should see the Welcome to Android Studio window.Click Import Project.Locate the project you exported from Eclipse, expand it, select the build.gradle file and click OK.In the following dialog, leave Use gradle wrapper selected and click OK. (You do not need to specify the Gradle home.)

但是第2步之後,出現錯誤,彈出一個Resolve error的對話方塊。錯位內容是:

Unknown host 'repo1.maven.org’

因為公司內部訪問外網需要代理,所以這個錯誤應該是由於沒有給AndroidStudio設定代理導致的。

關掉錯誤對話方塊,回到起始頁面,選擇Configure -> settings, 搜尋proxy,設定好代理。

然後順利到達第4步,進入Import Project from Gradle對話方塊。接下來可能會有各種情況出現,但是解決方案其實也很簡單,大可不必覺得麻煩。

本質上只要保證build.gradle以及\gradle\wrapper\gradle-wrapper.properties的兩個設定檔的內容與AndroidStudio的版本相相容,並且AndroidStudio中提供了要匯入的工程所需要的Android SDK。

其中build.gradle檔案內容如下

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

dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
}

android {
compileSdkVersion 19
buildToolsVersion "19.0.0"

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

instrumentTest.setRoot('tests')
}
}


gradle-wrapper.properties檔案內容如下

#Wed Apr 10 15:27:10 PDT 2013
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=http\://services.gradle.org/distributions/gradle-1.8-bin.zip

第一,保證這兩個設定檔中紅色部分一致。AndroidStudio會自動下載Gradle包,比方說gradle-1.8-bin.zip

第二,由於最新版AndroidStudio已經不支援gradle1.6,所以如果由Eclipse產生的兩個設定檔版本太舊,則手動修改為新版即可。

第三,確保AndroidStudio確實提供了如下版本的SDK,當然也可以把設定檔中的版本號碼修改為AndroidStudio支援的版本。

android {
compileSdkVersion 19
buildToolsVersion "19.0.0"

這樣應該就可成功遷移了。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.