1. 程式人生 > >Eclipse專案遷移AndroidStudio的兩種方式

Eclipse專案遷移AndroidStudio的兩種方式

Eclipse專案遷移到 AndroidStudio

方式:
1.使用AndroidStudio匯入嚮導,自動進行遷移處理
2.在Eclipse專案中新增Gradle指令碼,並手動設定一切

1.匯入嚮導方式:

AndroidStudio可以自動的匯入Eclipse專案:
File——->close project 回到Studio桌面——> import project即可 匯入工具會幫你自動重構專案

這裡寫圖片描述

在根目錄會自動生成一個import-summary.txt檔案 裡面展示了專案重構所發生的一些事 比如:資料夾的位置改變 比如:將之前的
依賴jar包更換成了gradle依賴方式

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:

* ic_launcher-web.png
* proguard-project.txt

Replaced Jars with Dependencies: -------------------------------- The importer recognized the following .jar files as third party libraries and replaced them with Gradle dependencies instead. This has the advantage that more explicit version information is known, and the libraries can be updated automatically. However, it is possible that the .jar file in your project was of an older version than the dependency we picked, which could render the project not compileable. You can disable the jar replacement in the import wizard and try again: android-support-v4.jar => com.android.support:support-v4:20.0.0 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.)

注意:在專案構建過程中可能需要網路去下載一些必要的依賴

2.手動匯入方式:

手動匯入的方式有許多種 ,不需要更改為新的目錄結構,甚至可以在gradle指令碼上執行ant指令碼

1.保留舊的專案結構
可以保留舊的專案結構不變,但是需要更改一下源集的配置(比如Studio工程的程式碼源集在app/main/java下 但是Eclipse在 src中
所以需要更改源集的配置)

第一步:在專案的根目錄下新建builder.gradle檔案 在其中申請Android外掛,並設定Gradle和Android外掛的屬性:

     buildscript{

                repositories{
                  jcenter()
                }

                dependencies{
                  classpath 'com.android.tools.build:gradle:2.2.2'
                }
      }

     apply plugin: 'com.android.application'

                android {
                 /**
                  * 編譯版本的sdk 一般使用最新的Android api版本作為編譯版本
                  */
                  compileSdkVersion 24
                  buildToolsVersion "25.0.0"
                }

第二步:修改源集按照Eclipse的專案結構來複寫main源集的示例如下:

需要告訴gradle 所有的組建都應該放在src資料夾下 也可以將他們都放進去

       android{
               sourceSets{
                  main{
                   manifest.srcFile 'AndroidManifest.xml'
                   java.srcDirs=['src']
                   resources.srcDirs=['src']
                   aidl.srcDirs=['src']
                   renderscript.srcDirs=['src']
                   res.srcDirs=['res']
                   assets.srcDirs=['assets']
                 }
                 androidTest.setRoot('tests')
               }

        }

如果你的專案中存在jar檔案依賴,則需要告訴gradle的檔案依賴路徑,假設jar檔案在libs中,那麼需要新增一下配置
將libs資料夾下的 所有 以,jar結尾的檔案視為一個依賴

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

這樣 專案舊可以進行執行 並且可以將libs中的jar包進行替換 替換成maven依賴

2.轉換到新的專案結構

如果需要匯入AndroidSudio 並且轉換下古墓結構,那你就必須新建一些資料夾並且移動一些檔案 下面有一個對照表
可以看到檔案”何去何從”

        舊的位置                                 新的位置
         src/                                 app/src/main/java/
         res/                                 app/src/main/res/
         assets/                              app/src/main/assets
         AndroidManifest.xml                  app/src/mai/AndroidManifest.xml

如果你存在測試單元模組 還需要將測試的原始碼移動到 app/src/test/java/下,以遍讓gradle對其進行識別,功能測試則需要放到
app/src/androidTest/java/下

接下來需要在工程的根目錄下面建立 settings.gradle,給檔案用來告訴Gradle將app模組包含到構建中去
include ‘:app’

需要建立兩個build.gradle檔案 第一個用於專案根目錄 與 settings.gradle平齊 用來定義專案級的引數設定:

            buildscript{

                   repositories{
                       jcenter()
                   }
                    dependencies{
                       classpath 'com.android.tools.build:gradle:2.2.2'
                    }

             }

第二個build.gradle檔案存在於 app目錄下,包含了特定模組的引數設定

     apply plugin: 'com.android.application'

                 android {
                      /**
                      * 編譯版本的sdk 一般使用最新的Android api版本作為編譯版本
                      */
                      compileSdkVersion 24
                      buildToolsVersion "25.0.0"
                 }

如果 app模組存在依賴 則需要進行依賴新增

                  dependencies{
                      compile fileTree(dir:'libs',include:['*.jar'])
                      //宣告所需要新增的依賴
                      compile 'XXXXXXX'
                  }

原Eclipse自動生成的 gen bin目錄可以進行刪除