標籤:android studio gradle 手動匯入project
RT,這應該是很多朋友剛從Eclipse轉到Android Studio後遇到最大的一個問題,首先我們需要重新認識AS裡面的目錄結構,在我前一篇文章裡面也有提到(Android Studio中的Project相當於Eclipse中的Workspace,Module則相當於Eclipse中的Project)。
所以我們手動匯入Project,其實就是匯入AS裡面的Module。主要有以下幾個步驟:
1.複製build.gradle到需要匯入的項目中
2.複製你需要匯入的項目至AS Project根目錄檔案夾下(即存在gradlew, gradlew.bat, .gradle的那個檔案夾)
3.修改AS Project中的settings.gradle,添加include,告訴AS我們的Project裡面需要包含這個Module(例如include ‘:SlidingMenuLibrary‘)
4.Rebuild Project,會為項目自動產生.iml檔案(iml檔案是AS識別項目的設定檔,跟Eclipse裡面的.project檔案作用類似)
下面貼出build.gradle主要內容:
apply plugin: 'com.android.application'dependencies { compile fileTree(dir: 'libs', include: '*.jar')}android { compileSdkVersion 17 buildToolsVersion "20.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'] } // 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') }}其實上面這個檔案就是用Eclipse匯出的,大家可以放心的直接複製使用。還有需要注意以下幾點:
1.上面提供的是App的gradle檔案,如果是Library項目,則需要修改apply plugin: ‘com.android.application‘ 為 apply plugin: ‘com.android.library‘即可
2. compileSdkVersion 和 buildToolsVersion,需要根據本地的SDK版本具體修改,開啟SDK Manager看下就行了
3. sourceSets main裡面指定了原始碼的目錄位置,因為AS預設的代碼結構與Eclipse的是不一樣的
4. dependencies裡面指定了依賴庫,compile fileTree(dir: ‘libs‘, include: [‘*.jar‘])編譯libs目錄下所有的.jar庫。如果依賴某些庫項目,則可以添加:compile project(‘:Cordova‘)
結束語:其實,最新版本的AS已經支援直接匯入Module了,但有一個問題,它在匯入的時候,會Copy一份你的項目(相當於重建一份),然後匯入後目錄結構就變成了AS的目錄結構,如果想保持Eclipse目錄結構,還是使用上面的方法吧,嘿嘿。
Android Studio中手動匯入Eclipse Project