2013-07-06 9 views
5

Ich habe dieses Tutorial benutze mich zu erziehen, wie durch nur mit der Befehlszeile (und Ant) APK außerhalb von Eclipse zu bauen - wird http://www.androidengineer.com/2010/06/using-ant-to-automate-building-android.htmlGebäude APK mit Gradle außerhalb IDE (von Ant Migration)

nun, dass Build-System auf Gradle zu verschieben Ich hätte gerne ein ähnliches fortgeschrittenes Tutorial als Referenz. Die meisten Tutorials (like this one) beschäftigen sich nur mit grundlegenden Dingen, aber ich würde gerne wissen, wie man einige "fortgeschrittene" Dinge wie das automatische Ersetzen von Werten im Code während des Builds durchführt (damit ich mehrere Varianten von APK haben kann).

Antwort

4

Standard-Beispiele zur Verfügung gestellt von Google sind hier

http://tools.android.com/tech-docs/new-build-system/gradle-samples-0.4.2.zip?attredirects=0&d=1

Für Werte automatisch in Code verwenden BuildConfig Klasse zu ändern. Beispiele finden Sie im obigen Link.

Varianten sind http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Build-Variants

UPDATE

wie dieses Beispiel hier wenig abgestanden wird auf neuere Version ist pasetbin http://pastebin.com/FmcCZwA5

Hauptunterschied ist Robolectric Unterstützung von Plugins zur Verfügung gestellt, hier erklärt und Unterstützung geholt Bibliothek von SDK intern Repo

Ältere Version

Weniger einfaches Beispiel mit Robolectric und AndroidAnnotations

Verwendung nexus

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/' 
    } 
} 

Verwendung AndroidAnnotation Prozessor, Robolectric lokale Tests und 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" 

konfigurieren Standardinstrumentierung Tests

defaultConfig { 
    minSdkVersion 7 
    targetSdkVersion 16 
    testPackageName "com.mypackage.myapp.test" 
    testInstrumentationRunner "com.maypackage.myapp.test.Runner" 
    } 
} 

Invoke AndroidAnnotations Prozessor auf alle Varianten

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 
    ] 
    } 
} 

definieren sourcesets für Robolectric lokale Tests

sourceSets { 
    testLocal { 
    java.srcDir file('src/test/java') 
    resources.srcDir file('src/test/resources') 
    } 
} 

Lokale Robolectric Tests Aufgabe

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 im Debug-Modus

localTest.doFirst { 
    jvmArgs '-Xdebug', 
     '-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005' 
} 
Verwandte Themen