Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

安卓自动化之旅(一)——gradle命令行及环境配置 #59

Open
soapgu opened this issue Jun 29, 2021 · 0 comments
Open

安卓自动化之旅(一)——gradle命令行及环境配置 #59

soapgu opened this issue Jun 29, 2021 · 0 comments
Labels
JAVA This doesn't seem right 安卓 安卓

Comments

@soapgu
Copy link
Owner

soapgu commented Jun 29, 2021

  • 前言

安卓的入门的学习已经完成大部分的目标。需要迈向更高的追求了。可能自动化测试还有一段路要走,但是自动化打包应该还是相对简单,而且有一定实用性,以后王毅再问我要程序的时候,就不用再这么low,发一个微信或者qq很手工的感觉。最好直接点一下页面,告诉他到什么地址下载,version及release notes一应俱全,能装逼还能偷懒,多好。
图片

  • coding中的持续集成

先看看我们身边有啥趁手的工具,把目标瞄到了coding上
图片
还是蛮好找的
看了下示例代码

pipeline {
  agent any
  stages {
    stage("检出") {
      steps {
        checkout(
          [$class: 'GitSCM',
          branches: [[name: GIT_BUILD_REF]],
          userRemoteConfigs: [[
            url: GIT_REPO_URL,
              credentialsId: CREDENTIALS_ID
            ]]]
        )
      }
    }
        
    // 您可以根据自己的实际情况调整构建过程
    stage('单元测试') {
      
      steps {
        sh "./gradlew test"
      }
      post {
        always {
          // 收集测试报告
          junit 'app/build/test-results/testReleaseUnitTest/TEST-net.coding.android_demo.ExampleUnitTest.xml'
        }
      }
    }
    
    stage('编译构建') {
      steps {
        sh "./gradlew clean && rm -rf ./app/build/"
        sh "./gradlew assembleRelease"
      }
    } 
    stage("APK 签名") {
      steps {
        signAndroidApks(
          keyStoreId: ANDROID_CREDENTIAL_ID,
          keyAlias: ANDROID_CREDENTIAL_ALIAS,
          apksToSign: "app/build/outputs/apk/**/*.apk",
          archiveSignedApks: true,
          archiveUnsignedApks: true,
          skipZipalign: true
        )
      }
    }
    stage('上传到 Generic') {
      steps {
        codingArtifactsGeneric(
          credentialsId: "${CODING_ARTIFACTS_CREDENTIALS_ID}",
          withBuildProps: true,
          files: "app/build/outputs/apk/**/*.apk",
          repoName: "${GENERIC_REPO_NAME}",
          version: "${CI_BUILD_NUMBER}"
        )
      }
    }
  }
}

精髓都在Jenkinsfile。
核心部分也并没有多高级,gradlew命令行是关键。可惜相关基础知识为零,果断先撤退

  • gradlew命令行

从官网上找到相关资料

准备来个gradlew命令行版本的helloworld
结果
JAVA_HOME is not set and no 'java' command could be found in your PATH
这货还要JAVA环境啊

  • 倒腾JAVA环境

  • 安装JDK
    从oracle官网下载JDK
    环境变量配置 新增 “JAVA_HOME”='C:\Program Files\Java\jdk1.8.0_181'
    PATH='C:\Program Files\Java\jdk1.8.0_181\bin'
    运行gradlew,正常显示了

  • Android Studio的警告
    但是Android Studio出现了告警

Android Studio is using the following JDK location when running Gradle: // D:\Program Files\Android\Android Studio\jre // Using different JDK locations on different processes might cause Gradle to spawn multiple daemons, for example, by executing Gradle tasks from a terminal while using Android Studio. // More info... // Select a JDK from the File System // Do not show this warning again (a minute ago)

就是JAVA环境并不是没装,否则我以前的安卓程序是怎么编译出来的那。
图片
就是说Android Studio自带了Java Runtime Environment,一般使用JRE足够了,JDK有点杀鸡牛刀了。

  • 卸载JDK,配置JRE
    所以JDK不需要安装,只是把现有的JER配置好就行
  1. Uninstall JDK
  2. JAVA_HOME='D:\Program Files\Android\Android Studio\jre'
  3. PATH=‘D:\Program Files\Android\Android Studio\jre\bin;D:\Program Files\Android\Android Studio\jre\lib’
    JAVA环境总算折腾完了
  • gradle & gradlew 傻傻分不清楚

为啥网关命令行的命令是gradlew,我们明明不是用gradle编译的嘛,两者有啥关系。
gradlew = gradle wrapper
就是gradle脚手架,即用即装,居家旅行方便快捷。不仅可以执行gradle命令还包安装,适配不同机型及版本兼容。
https://stackoverflow.com/questions/39627231/difference-between-using-gradlew-and-gradle

  1. gradlew -version
    查看gradle当前版本以及本机环境

  2. gradlew tasks
    查看项目的所有可用构建任务的列表

  3. gradlew assembleDebug
    构建调试版 APK

  4. gradlew assembleRelease
    构建发布版 APK

  5. gradlew clean
    清理编译文件

  6. gradlew test
    Run unit tests for all variants

  • 修复AndroidManifest.xml资源监测 Release Build Error

这次在实验过程中发现了一个奇怪的现象

在AndroidManifest.xml中定义android:enabled="${boot}"会被监测编译报错,而在Debug的编译中并不报错。
看来manifestPlaceholders并不能这样用。
并难不倒我,换一种方式,改成bool资源,通过app模块的资源文件配置来确定receiver是否使用。

basic模块的bools.xml资源文件定义

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <bool name="BootReceiver">false</bool>
</resources>

默认配置为false。

app模块的bools.xml资源文件定义

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <bool name="BootReceiver">true</bool>
</resources>

由于资源优先级app模块高,所以最后被解析为true

AndroidManifest.xml修改如下

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.space365.basic">
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
    <application android:allowBackup="false">
        <receiver
            android:name=".BootReceiver"
            android:enabled="@bool/BootReceiver"
            android:exported="true">
            <intent-filter android:priority="1000">
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
        </receiver>
    </application>
</manifest>

相关参考链接

有点小插曲,总体还算顺利。gradle命令行是脱离拐杖(IDE)的第一步。一步一个脚印迈向持续集成、TDD。

@soapgu soapgu added IDE Good for newcomers JAVA This doesn't seem right 安卓 安卓 and removed IDE Good for newcomers labels Jun 29, 2021
@soapgu soapgu changed the title 安卓自动化之旅(一) 安卓自动化之旅——gradle命令行及环境配置(一) Jun 29, 2021
@soapgu soapgu changed the title 安卓自动化之旅——gradle命令行及环境配置(一) 安卓自动化之旅(一)——gradle命令行及环境配置 Aug 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
JAVA This doesn't seem right 安卓 安卓
Projects
None yet
Development

No branches or pull requests

1 participant