Jenkins+Gradle+android+SonarQube

一、环境

1. ubuntu14.04(64位)
2. jdk 1.7
3. ant 1.9
4. jenkins 1.606
5. tomcat 7.0.5
6. android sdk
7. gradle
8. nexus
9. sonarqube

二、安装

1. JDK环境变量
    vim ~/.bashrc
    export JAVA_HOME=/usr/local/lib/jvm/jdk1.7.0_60
    export JRE_HOME=$JAVA_HOME/jre
    export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib
    export PATH=$JAVA_HOME/bin:$JRE_HOME/bin:$PATH

2. gradle环境变量
    http://gradle.org/下载最新的包
    vim ~/.bashrc
    export GRADLE_HOME=/usr/local/lib/gradle2.2
    export PATH=$GRADLE_HOME/bin:$PATH

3. Jenkins环境变量
    http://jenkins-ci.org/下载稳定版本
    vim ~/.bashrc
    export JENKINS_HOME=/home/roy/jenkins_home
    export PATH=$JENKINS_HOME:$PATH

4. android sdk 环境变量
    vim ~/.bashrc
    export ANDROID_HOME=/home/roy/soft/android-sdk
    export PATH=$ANDROID_HOME/tools:$ANDROID_HOME/platform-tools:$PATH

5. Jenkins插件安装
    gradle plugin
    sonar plugin

6. 因为android sdk的文件都是在32位的库里编译在,所以在64为的操作系统上无法找打对应的库,需要安装
    sudo apt-get install lib32stdc++6
    sudo apt-get install lib32z1 lib32z1-dev

7. nexus安装配置
    http://www.sonatype.org 下载最新的包
    1. 解压
    2. 进入bin目录执行./nexus start命令
    3. http://localhost:8081/nexus 接口访问用户名和密码是admin/admin123

8. sonarqube安装
    http://www.sonarqube.org 下载最新的包
    1. 解压
    2. 安装mysql数据库 sudo apt-get install mysql-server
        创建sonar数据库和用户
        CREATE DATABASE sonar CHARACTER SET utf8 COLLATE utf8_general_ci;

        CREATE USER ‘sonar‘ IDENTIFIED BY ‘sonar‘;
        GRANT ALL ON sonar.* TO ‘sonar‘@‘%‘ IDENTIFIED BY ‘sonar‘;
        GRANT ALL ON sonar.* TO ‘sonar‘@‘localhost‘ IDENTIFIED BY ‘sonar‘;

    2. 编辑配置文件conf/sonar.properties
        修改mysql数据库的配置和web端口及url的配置
        sonar.jdbc.username=sonar
        sonar.jdbc.password=sonar

        sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance

        sonar.web.host=192.168.33.7
        sonar.web.context=
        sonar.web.port=9000

    3. sonar-runner配置conf/sonar-runner.properties
        sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8
        sonar.jdbc.username=sonar
        sonar.jdbc.password=sonar
        sonar.login=admin
        sonar.password=jstinno

三、 Jenkins配置

四、工程配置

    buildscript{

repositories{

mavenCentral();

}

dependencies{

classpath ‘com.android.tools.build:gradle:0.14+‘

}

tasks.withType(JavaCompile){

options.encoding = ‘UTF-8‘

}
}

apply plugin:‘android‘

dependencies{
compile fileTree(dir: ‘libs‘, include:“*.jar”)
}

android{

applicationVariants.all { variant ->

variant.outputs.each { output ->
  if(output.outputFile.name.indexOf(‘-debug.apk‘) > -1){
    output.outputFile = new File(
      output.outputFile.parent,
      output.outputFile.name.replace(".apk", "mirror.apk"))
    }
  }

}

signingConfigs{

myConfig{
  storeFile file("/home/roy/jenkins_home/jobs/ApkKeyStore")
  storePassword "android"
  keyAlias "brilliance"
  keyPassword "android"
}

}

buildTypes{

release{
  signingConfig signingConfigs.myConfig
}

}

compileSdkVersion 22
buildToolsVersion “22.0.1”
enforceUniquePackageName=false

lintOptions{

abortOnError false

}

sourceSets{

main{
  manifest.srcFile ‘AndroidManifest.xml‘
  java.srcDirs = [‘src‘]
  resources.srcDirs = [‘src‘]
  aidl.srcDirs = [‘src‘]
  renderscript.srcDirs = [‘src‘]
  res.srcDirs = [‘res‘]
  assets.srcDirs = [‘assets‘]
}

}

lintOptions{

abortOnError false

}
}

五、build.xml示例

六、apk自动安装到手机中

1. 开启手机开发者模式(通过单击设置->关于手机->版本号, 连续单击四次)
2. 在开发者选项中,开启USB调试和USB调试授权
问题处理:
    1. adb devices命令, 返回的结果是: 

List of devices attached
???????????? no permissions
因为adb server需要以root的权限启动
在android-sdk/platform-tools目录下,执行

       $ ./adb kill-server
       $ sudo ./adb start-server
       在手机上选择Always allow from this computer?
       adb devices可以看到了

安装命令 gradle installRelease (gradle iR)

git clone [email protected]:jstinno/apps/CloudFile
git clone [email protected]:jstinno/apps/cloudclient
git clone [email protected]:jstinno/apps/CommonUpgrade

gradle AndroidDependencies 检查项目依赖关系
gradle clean uploadArchives 打包并上传到maven仓库

    buildscript{

repositories{

mavenCentral();

}

dependencies{

classpath ‘com.android.tools.build:gradle:0.14+‘

}

tasks.withType(JavaCompile){

options.encoding = ‘UTF-8‘

}
}

apply plugin: ‘android-library‘
apply plugin: ‘maven‘

android{

signingConfigs{

myConfig{
  storeFile file("/home/roy/jenkins_home/jobs/ApkKeyStore")
  storePassword "android"
  keyAlias "brilliance"
  keyPassword "android"
}

}

buildTypes{

release{
  signingConfig signingConfigs.myConfig
}

}

compileSdkVersion 22
buildToolsVersion “22.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‘]
}

}

lintOptions{

abortOnError false

}
}

dependencies{
compile fileTree(dir: ‘libs‘, include:“*.jar”)
}

uploadArchives{
repositories.mavenDeployer{

name = ‘mavenCentralReleaseDeployer‘
repository(url: "http://192.168.33.25:8081/nexus/content/repositories/releases"){
  authentication(userName: ‘admin‘, password: ‘admin123‘)
}

snapshotRepository(url: "http://192.168.33.25:8081/nexus/content/repositories/snapshots/"){
  authentication(userName: ‘admin‘, password: ‘admin123‘)
}

pom.version = "1.0.0"
pom.artifactId = "cloudclient"
pom.groupId = "com.cloud"
pom.name = "cloudclient"
pom.packaging = "aar"

}

}

编译jar包上传本地maven仓库
buildscript{
repositories{

mavenCentral();

}

dependencies{

classpath ‘com.android.tools.build:gradle:0.14+‘

}

tasks.withType(JavaCompile){

options.encoding = ‘UTF-8‘

}
}

apply plugin: ‘android-library‘
apply plugin: ‘maven‘

dependencies{
compile fileTree(dir: ‘libs‘, include:“*.jar”)
}

android{

signingConfigs{

myConfig{
  storeFile file("/home/roy/jenkins_home/jobs/ApkKeyStore")
  storePassword "android"
  keyAlias "brilliance"
  keyPassword "android"
}

}

buildTypes{

release{
  signingConfig signingConfigs.myConfig
}

}

compileSdkVersion 22
buildToolsVersion “22.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‘]
}

}

lintOptions{

abortOnError false

}

android.libraryVariants.all { variant ->

def name = variant.buildType.name
if (name.equals(com.android.builder.core.BuilderConstants.DEBUG)) {
  return; // Skip debug builds.
}
def task = project.tasks.create "jar${name.capitalize()}", Jar
task.dependsOn variant.javaCompile
task.from variant.javaCompile.destinationDir
artifacts.add(‘archives‘, task);

}

uploadArchives{

repositories.mavenDeployer{
  name = ‘mavenCentralReleaseDeployer‘
  repository(url: "http://192.168.33.25:8081/nexus/content/repositories/releases"){
    authentication(userName: ‘admin‘, password: ‘admin123‘)
  }

  snapshotRepository(url: "http://192.168.33.25:8081/nexus/content/repositories/snapshots/"){
    authentication(userName: ‘admin‘, password: ‘admin123‘)
  }

  pom.version = "1.0.0"
  pom.artifactId = "cloudclient"
  pom.groupId = "com.cloud"
  pom.name = "cloudclient"
  pom.packaging = "jar"
}

}
}

依赖其他apk的编译并安装
buildscript{
repositories{

mavenCentral()

}

dependencies{

classpath ‘com.android.tools.build:gradle:0.14+‘

}

tasks.withType(JavaCompile){

options.encoding = ‘UTF-8‘

}

}

apply plugin:‘android‘

repositories{

maven{

url "http://192.168.33.25:8081/nexus/content/repositories/releases"

}
}
dependencies{
compile fileTree(dir: ‘libs‘, include:“*.jar”)
compile “com.common:upgrade:1.0.0”
compile “com.cloud:cloudclient:1.0.0”
}

android{

applicationVariants.all { variant ->

variant.outputs.each { output ->
  String apk_name = output.outputFile.name;
  if(output.outputFile.name.indexOf(‘-debug.apk‘) > -1){
    output.outputFile = new File(
      output.outputFile.parent,
      output.outputFile.name.replace(".apk", "mirror.apk"))
  }else if(output.outputFile.name.indexOf(‘-release.apk‘) > -1){
      output.outputFile = new File(
      output.outputFile.parent,
      output.outputFile.name.replace(apk_name, "CloudFile-release.apk"))
  }
}

}

signingConfigs{

myConfig{
  storeFile file("/home/roy/jenkins_home/jobs/ApkKeyStore")
  storePassword "android"
  keyAlias "brilliance"
  keyPassword "android"
}

}

buildTypes{

release{
  signingConfig signingConfigs.myConfig
}

}

compileSdkVersion 22
buildToolsVersion “22.0.1”
enforceUniquePackageName=false

lintOptions{

abortOnError false

}

sourceSets{

main{
  manifest.srcFile ‘AndroidManifest.xml‘
  java.srcDirs = [‘src‘]
  resources.srcDirs = [‘src‘]
  aidl.srcDirs = [‘src‘]
  renderscript.srcDirs = [‘src‘]
  res.srcDirs = [‘res‘]
  assets.srcDirs = [‘assets‘]
}

}

lintOptions{

abortOnError false

}

}

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。