ANT Android项目到Android Studio [英] ANT Android project to Android Studio
问题描述
我有一个基于 ANT 构建工具的功能齐全的Android应用程序.它与 Eclipse 一起使用,但我也必须使其与 Android Studio 一起使用.我设法使用 ADT Gradle Export Tool 导出了项目,该应用程序本身运行良好.但是,我无法使测试项目正常工作.
I have a fully working Android application based on ANT build tools. It is used with Eclipse but I have to make it work with Android Studio as well. I managed to export the project with ADT Gradle Export Tool and the application itself works great. However, I can't get the test project to work.
这是 Eclipse ADT 插件生成的build.gradle
.
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.8.+'
}
}
apply plugin: 'android'
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
}
android {
compileSdkVersion 19
buildToolsVersion "19.0.1"
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')
}
}
我的项目结构
.
├── AndroidManifest.xml
├── ant.properties
├── build.gradle
├── build.xml
├── gradle
│ └── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── LICENSE
├── local.properties
├── MyApp.iml
├── proguard-project.txt
├── project.properties
├── README.md
├── res
│ ├── drawable-hdpi
│ │ └── ic_launcher.png
│ ├── drawable-ldpi
│ │ └── ic_launcher.png
│ ├── drawable-mdpi
│ │ └── ic_launcher.png
│ ├── drawable-xhdpi
│ │ └── ic_launcher.png
│ ├── layout
│ │ └── main.xml
│ └── values
│ └── strings.xml
├── src
│ └── com
│ └── example
│ └── myapp
│ └── MainActivity.java
└── tests
├── AndroidManifest.xml
├── ant.properties
├── build.xml
├── proguard-project.txt
├── project.properties
└── src
└── com
└── example
└── myapp
└── MainActivityTest.java
对我来说, Android Studio 似乎无法将tests/src/PACKAGE
检测为程序包.而是归为临时目录.
For me it seems that Android Studio is not able to detect tests/src/PACKAGE
as a package. It is classified as casual directories instead.
我真的很感谢任何提示.
I would really appreciate any hints.
如果将tests/src
重命名为tests/java
,则一切正常.但这不是解决方案,因为测试将停止使用 ANT 和/或 Eclipse .
If tests/src
is renamed into tests/java
everything works. But this is not a solution since tests will stop working with ANT and/or Eclipse.
推荐答案
在同事的帮助下,我设法解决了该问题.问题中提到的导出项目的正确build.gradle
文件应该看起来像
With some help from a colleague I managed to resolve the issue. The proper build.gradle
file for a project exported as mention in the question should look like
// (...)
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']
instrumentTest {
java.srcDirs = ['tests/src']
res.srcDirs = ['tests/res']
assets.srcDirs = ['tests/assets']
}
}
// 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')
}
}
另外,可能需要更改默认的测试程序包名称,因为Gradle默认情况下希望该程序包类似于application-package.test
,而Ant使用的是application-package.tests
约定.但是,这可能是一些仅针对我的项目的问题,但是我认为共享它对将来的某人可能会有所帮助.
Additionally, changing default test package name may be required since Gradle by default expects that package to be like application-package.test
whereas Ant used application-package.tests
convention. This may be however, some issue specific only to my project nevertheless I thought sharing it can be beneficial to someone in the future.
这篇关于ANT Android项目到Android Studio的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!