Android Studio中手動導入Eclipse Project
來源:程序員人生 發布時間:2014-10-13 04:02:27 閱讀次數:2049次
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目錄結構,還是使用上面的方法吧,嘿嘿。
生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈