Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了android – 在IDE外部使用Gradle构建APK(从Ant迁移)大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

我一直在使用本教程来教育自己如何使用命令行(和Ant) – http://www.androidengineer.com/2010/06/using-ant-to-automate-building-android.html在Eclipse外部构建APK

现在构建系统将转向Gradle我希望有类似的高级教程供参.那里的大多数教程(like this one)只涉及基本的东西,但我想知道如何执行一些“高级”的东西,比如在构建期间自动替换代码中的值(这样我就可以有多种APK版本).

最佳答案
Google提供的标准示例如下

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

要在代码自动更改值,请使用BuildConfig类.示例在上面的链接中.

变量在这里解释http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Build-Variants

updatE

因为这个例子在这里有点陈旧是pasetbin到更新的版本http://pastebin.com/FmcCZwA5

主要区别是插件提供的Robolectric支持,以及从SDK内部仓库获取支持

旧版本

Robolectric和AndroidAnnotations的基本示例

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

使用AndroidAnnotation处理器,Robolectric本地测试和杰克逊

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"

配置标准检测测试

  defaultConfig {
    minSdkVersion 7
    targetSdkVersion 16
    testPackagename "com.mypackage.myapp.test"
    testinstrumentationRunner "com.maypackage.myapp.test.Runner"
  }
}

在所有变体上调用AndroidAnnotations处理器

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

定义Robolectric本地测试的源集

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

本地Robolectric测试任务

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

}

在调试模式下运行Robolectric

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

大佬总结

以上是大佬教程为你收集整理的android – 在IDE外部使用Gradle构建APK(从Ant迁移)全部内容,希望文章能够帮你解决android – 在IDE外部使用Gradle构建APK(从Ant迁移)所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。