目前有个项目,验收时有个要求是在linux中进行编译打包生成apk文件。我平时都是在windows环境android studio中进行打包的,花了半天时间研究了一下,记录如下:
安装安卓sdk
cd /opt
wget https://dl.google.com/android/repository/sdk-tools-linux-4333796.zip
unzip sdk-tools-linux-4333796.zip
mkdir sdk
mv tools/ sdk/
配置环境变量
gedit /etc/profile
profile文件中加入以下两行 export ANDROID_HOME=/opt/sdk export PATH=${ANDROID_HOME}/tools/bin:${ANDROID_HOME}/tools/bin:${ANDROID_HOME}:${PATH}
source /etc/profile
sdkmanager --list
如果有以下报错,说明还没装jdk
ERROR:JAVA HOME is not set and no 'java' command could be found in your PATH.
安装jdk
apt update
apt install openjdk-8-jdk
安装安卓build-tools和platforms
sdkmanager "build-tools;29.0.3"
sdkmanager "platforms;android-29"
再次执行sdkmanager --list
如果看到以下3行输出,说明安装正常
Installed packages:=====================] 100% Computing updates... Path | Version | Description | Location ------- | ------- | ------- | ------- build-tools;29.0.3 | 29.0.3 | Android SDK Build-Tools 29.0.3 | build-tools/29.0.3/ platforms;android-29 | 5 | Android SDK Platform 29 | platforms/android-29/tools | 26.1.1 | Android SDK Tools 26.1.1 | tools/
安装gradle
gradle的版本号看项目配置文件/gradle/wrapper/gradle-wrapper.properties
cd /opt
wget https://services.gradle.org/distributions/gradle-6.5-bin.zip
unzip gradle-6.5-bin.zip
mkdir gradle
mv gradle-6.5 /opt/gradle/
配置环境变量
gedit /etc/profile
profile文件中加入以下两行
export GRADLE_HOME=/opt/gradle/gradle-6.5
export PATH=${GRADLE_HOME}/bin:${PATH}
source /etc/profile
导入android studio项目并打包
opt下面建一个code文件夹,将demo项目放到里面,执行gradle assembleRelease
接着有报错,说是证书路径找不到。我的证书配置在项目路径/app/build.gradle中
FAILURE: Build failed with an exception.* Where:
Build file '/opt/code/demo/app/build.gradle' line: 19* What went wrong:
A problem occurred evaluating project ':app'.
> Cannot convert URL 'C:/Users/Administrator/.android/demo.keystore' to a file.
我把证书复制到/app/目录下,修改一下项目路径/app/build.gradle配置
再次执行gradle assembleRelease
编译成功后apk文件会输出到项目路径/app/build/outputs/apk/release/目录中