我們知道,Eclipse工程的目錄結(jié)構(gòu)跟Android Studio的有很大的不同,采用Android Studio的導(dǎo)入Eclipse工程方法,卻也會(huì)導(dǎo)致原本的目錄結(jié)構(gòu)改變。如果想要用Android Studio編譯Eclipse工程,但卻不想改變?cè)镜哪夸浗Y(jié)構(gòu),那么,只需要Eclipse工程的根目錄下,新建一個(gè)build.gradle,更改下studio默認(rèn)的工程目錄即可。具體做法如下:
- 1.在根目錄創(chuàng)建一個(gè)build.gradle,對(duì)當(dāng)前工程(模塊)進(jìn)行配置.
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.0.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
//this is module build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
applicationId "com.example.eclipse2studio"
versionCode 1
versionName "1.0"
}
packagingOptions {
exclude 'META-INF/NOTICE.txt'
exclude 'META-INF/LICENSE.txt'
}
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
aidl.srcDirs = ['src']
java.srcDirs = ['src']
resources.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
jniLibs.srcDirs = ['libs']
renderscript.srcDirs = ['src']
}
// 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')
}
}
//signHelper.setNeedSign()
dependencies {
//compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.2.0' exclude module: 'support-v4'
compile files('../libs/bugly_crash_upgrade_v1.1.2.jar')
compile project(':lib01')
compile (project(':lib02')) {
exclude module: 'support-v4'
}
}
注:關(guān)鍵的代碼部分是sourceSets部分,該部分就將studio的目錄結(jié)構(gòu)更改為跟eclipse的相匹配,路徑都是相對(duì)應(yīng)該build.gradle的。
- 2.如果該工程還加載了其他工程作為庫文件,那么則要在工程根目錄下創(chuàng)建一個(gè)settings.gradle,來對(duì)庫工程進(jìn)行配置。
include ':lib01'
project(':lib01').projectDir = new File('../lib01')
include ':lib02'
project(':lib02').projectDir = new File('E:/android/lib02')
經(jīng)過以上步驟,eclipse工程就已經(jīng)順利轉(zhuǎn)換程Android Studio工程了,同時(shí),它也仍然還是eclipse工程。
更多詳細(xì)配置,請(qǐng)參考官方文檔