ví dụ tiêu chuẩn được cung cấp bởi Google đang ở đây
http://tools.android.com/tech-docs/new-build-system/gradle-samples-0.4.2.zip?attredirects=0&d=1
Đối với tự động thay đổi các giá trị trong sử dụng mã BuildConfig lớp. Ví dụ nằm trong liên kết ở trên.
biến thể được giải thích ở đây http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Build-Variants
CẬP NHẬT
như ví dụ này được chút cũ ở đây là pasetbin lên phiên bản mới hơn http://pastebin.com/FmcCZwA5
khác biệt chính là hỗ trợ Robolectric cung cấp bởi plugin, và hỗ trợ thư viện lấy từ SDK nội bộ repo
Phiên bản cũ
Ít ví dụ cơ bản với Robolectric và AndroidAnnotations
Sử dụng mối quan hệ
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.4'
}
}
apply plugin: 'android'
repositories {
mavenCentral()
maven {
url 'https://oss.sonatype.org/content/repositories/snapshots/'
}
}
vi xử lý sử dụng AndroidAnnotation, kiểm tra địa phương Robolectric và Jackson
configurations {
compile
testLocalCompile.extendsFrom(compile)
androidannotations.extendsFrom(compile)
}
dependencies {
compile files('libs/android-support-v4.jar')
compile 'org.androidannotations:androidannotations-api:3.0-SNAPSHOT'
compile 'com.github.japgolly.android:svg-android:2.0.3'
compile 'org.codehaus.jackson:jackson-mapper-asl:1.9.12'
testLocalCompile 'junit:junit:4.8.2'
testLocalCompile 'org.robolectric:robolectric:2.2-SNAPSHOT'
testLocalCompile 'com.google.android:android:4.0.1.2'
testLocalCompile 'com.google.android:support-v4:r6'
testLocalCompile 'org.roboguice:roboguice:2.0'
androidannotations 'org.androidannotations:androidannotations:3.0-SNAPSHOT'
}
android {
compileSdkVersion 17
buildToolsVersion "17.0.0"
Cấu hình thử nghiệm tiêu chuẩn thiết bị đo đạc
defaultConfig {
minSdkVersion 7
targetSdkVersion 16
testPackageName "com.mypackage.myapp.test"
testInstrumentationRunner "com.maypackage.myapp.test.Runner"
}
}
xử lý
Gọi AndroidAnnotations trên tất cả các biến thể
afterEvaluate { project ->
android.applicationVariants.each { variant ->
variant.javaCompile.options.compilerArgs += [
'-classpath', configurations.compile.asPath,
'-processorpath', configurations.androidannotations.asPath,
'-processor', 'org.androidannotations.AndroidAnnotationProcessor',
'-AandroidManifestFile=' + variant.processResources.manifestFile
]
}
}
Xác định sourcesets cho kiểm tra địa phương Robolectric
sourceSets {
testLocal {
java.srcDir file('src/test/java')
resources.srcDir file('src/test/resources')
}
}
kiểm tra Robolectric địa phương nhiệm vụ
task localTest(type: Test, dependsOn: assemble) {
testClassesDir = sourceSets.testLocal.output.classesDir
android.sourceSets.main.java.srcDirs.each { dir ->
def buildDir = dir.getAbsolutePath().split('/')
buildDir = (buildDir[0..(buildDir.length - 4)] + ['build', 'classes', 'debug']).join('/')
sourceSets.testLocal.compileClasspath += files(buildDir)
sourceSets.testLocal.runtimeClasspath += files(buildDir)
}
classpath = sourceSets.testLocal.runtimeClasspath
}
Run Robolectric trong chế độ gỡ lỗi
localTest.doFirst {
jvmArgs '-Xdebug',
'-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005'
}