[GUIDE] How to build .apk file from command line

If you don’t want to  install  a number of programs for building your  Android  project, this article is for you. You will need only JDK, the  Android  SDK platform  tools  and minimum  Android  platform for building the project using the batch  file .

1. Introduction

In this article, I will describe how to use the  Android   tools  for building the .apk  file  using the command line. As a result, you can use the batch  file  for building your  Android   application . I use  Windows  commands in this article. But you can use the same  Android  commands and  tools  and the same program if you are working on  Linux   OS . The example of the script for  Linux   OS  is included in the sample project, attached to this article.

2. Preparation

You must  install   Java  JDK and  Android  SDK on the  system  where you will build your  Android   application  using the  Windows  command line. The private keystore also must be present. 

2.1 Java JDK

You can use  Java  JDK from Eclipse or download it from this link: oracle(dot)com/technetwork/java/javase/downloads/index.html and then  install  it.

2.2 Android SDK

Android  SDK and its HOWTO document are available here: developer.android.com/sdk/index.html[/url].
The  Android  SDK Platform-tools, SDK Platform  Android  2.2, and API 8 are the minimum that is needed for the test project work.

2.3 Environment Variables

If you want to have a readable batch script and a program without very long  file  path definitions, you should use the environment variables. I recommend you to define the following ones:
• JAVABIN path to the  Java  JDK bin folder. For example: C:\Program Files\Java\jdk\bin. This folder must contain the javac.exe and jarsigner.exe  files .
• ANDROID-SDK path to the  Android  SDK folder. For example: C:\Program Files\Andoroid\android-sdk-windows.

2.4 Private Key for Signing

You can read everything about signing, creating private  key , and other operations here: developer.android(dot)com/guide/publishing/app-signing.html
In this article, I describe only one important command that will generate my-release-key.keystore:

Code:
%JAVABIN%\keytool  -genkey -v -keystore  my-release-key.keystore -alias alias_name  -keyalg RSA -keysize 2048  -validity 10000
Specify your info and you will receive the my-release-key.keystore  file . You must remember the password. You will need it later. Also, I recommend you to put the keystore  file  in the same folder as the project. In the example,It is in the keystore folder in the project directory.

2.5 Example Files Structure

You must remember that the dx  Android   tool  requires the full path, which MUST not contain any spaces. So, check the fact, that your project path satisfies the requirement.
The  file  structure of the example project, which is built using the build.bat  file , is the following:

Code:
SecureMessages/
assets/
keystore/
	my-release-key.keystore
res/
	drawable-hdpi/
		icon.png
drawable-ldpi/
icon.png
drawable-mdpi/
icon.png
layout/
		main.xml
values/
		strings.xml
src/
org/
secure/
sms/
		SecureMessagesActivity.java 
StringCryptor.java 
SmsReceiver.java
AndroidManifest.xml
build.bat						 // Windows build script
build.sh 
						 // Linux build script
3. Command Sequence

The following commands are for the  Windows   OS , but you can find the script for the  Linux   OS  project building in the attached sample.
First of all, we must save current path. Then we must change the CD variable to the path to the build.bat  file :

Code:
SET PREV_PATH=%CD%
cd /d %0\..
Then, bin and gen old folders should be recreated:
rmdir "bin" /S /Q
rmdir "gen" /S /Q
mkdir "bin"
mkdir "gen"
I add some definitions. They make the batch  file  readable and easy to update. So, I recommend you the following definitions:
• minimum  Android  revision; 
• the path to aapt  Android   tool  and its arguments for adding  files  into the existing archive;
• the path to aapt  Android   tool  and its arguments for packing and generating resources;
• the path to dx  Android   tool ;
• the path to javac  utility  of JDK.
This list of independent definitions can be used for building the majority of  Android  projects (of course, with the changed  Android  revision value). For example:

Code:
SET ANDROID_REV=android-8
SET ANDROID_AAPT_ADD="%ANDROID-SDK%\platforms\%ANDROID_REV%\tools\aapt.exe" add
SET ANDROID_AAPT_PACK="%ANDROID-SDK%\platforms\%ANDROID_REV%\tools\aapt.exe" package -v -f -I "%ANDROID-SDK%\platforms\%ANDROID_REV%\android.jar"
SET ANDROID_DX="%ANDROID-SDK%\platform-tools\dx.bat" --dex
SET JAVAC="%JAVABIN%\javac.exe" -classpath "%ANDROID-SDK%\platforms\%ANDROID_REV%\android.jar"
Also I need the defined variables for my project:
• APP_NAME is the name of  application  that will be used for the output APK  file .
• JAVAC_BUILD and JAVAC are the same commands, but there are path to the sources, generated R class, and output folder in JAVAC_BUILD.
These variables let us change the project name and paths to the sources easier.

Code:
SET APP_NAME=SecureSms
SET JAVAC_BUILD=%JAVAC% -sourcepath "src;gen" -d "bin"
And now, all preparations are finished and the  application  can be built. The R  file  will be generated using aapt  tool . All resources will be packed into the resources.ap_  file :
Code:
call %ANDROID_AAPT_PACK% -M "AndroidManifest.xml" -A "assets" -S "res" -m -J "gen" -F "bin\resources.ap_"
Android  manifest  file , res and assets folder are the input  data . Aapt will generate the R class and put it into the gen folder. Then, aapt will pack the resource  files  into the resourses.ap_  file .
Every folder that contains *.java  file  must be called with javac. In my example, there is only one folder with javac. So, I have only one command line for building sources:

Code:
call %JAVAC_BUILD% src\org\secure\sms\*.java
As you remember, the JAVAC_BUILD command has already contained the arguments that specify the bin folder. The bin folder is an output folder for the compiled sources.
At this moment, the sources have been compiled successfully and they can be packed in the special dex  file :
Code:
call %ANDROID_DX% --output="%CD%\bin\classes.dex" %CD%\bin
For the  application  signing, the resources  file  must be copied to another  file , which will be used for the signing:
Code:
copy "%CD%\bin\resources.ap_" "%CD%\bin\%APP_NAME%.ap_"
The classes.dex  file  must be added to the  new   file  that has an ap_  extension
Code:
call %ANDROID_AAPT_ADD% "%CD%\bin\%APP_NAME%.ap_" "%CD%\bin\classes.dex"
Now, the ap_  file  is a correct apk  file . But it is not signed yet and it cannot be installed to the  Android   device .
The creation of the signed  Android   application  from the *.ap_  file  is the following (Output  file  name must differ from the input  file  name – in this  case  the  file   extension  is changed to the *.apk):

Code:
call "%JAVABIN%\jarsigner" -keystore "%CD%\keystore\my-release-key.keystore" -storepass "password" -keypass "password" -signedjar "%CD%\bin\%APP_NAME%.apk" "%CD%\bin\%APP_NAME%.ap_" "alias_name"
And delete temp ap_  file :

Code:
del "bin\%APP_NAME%.ap_"
Finally, let’s return to the start folder and clear local variables:

Code:
:EXIT
cd "%PREV_PATH%"
ENDLOCAL
exit /b %ERRORLEVEL%
To test the example, you must:
• unpack it
• do the steps 2.1-2.5 from the Preparations paragraph
• run the build.bat  file


You can download the sample project source at the article official page apriorit(dot)com/our-company/dev-blog/233-how-to-build-apk-file-from-command-line

https://forum.xda-developers.com/general/general/guide-how-to-build-apk-file-command-line-t3176502

<think>好的,用户遇到了Gradle构建错误,具体是在构建文件'D:\project\cloudsdk\build.gradle'的第20行,提示找不到'id 'maven''插件。我需要先分析可能的原因,然后给出解决方案。首先,用户可能错误地应用了'maven'插件,而正确的插件应该是'maven-publish'或者'maven'插件的使用方式在较新的Gradle版本中已经改变。 接着,用户提到的Gradle版本是8.13,而错误信息中提到不兼容Gradle 9.0,这说明用户可能在使用旧版Gradle的语法或插件。需要确认用户是否在build.gradle中正确引用了插件。比如,是否使用了plugins块或者旧的apply方式。 另外,错误信息建议运行带有--stacktrace等选项的命令,可能需要指导用户如何获取更详细的日志来定位问题。用户可能对Gradle的日志选项不太熟悉,需要解释这些选项的作用,比如--info或--debug会输出更多信息,帮助找到具体错误位置。 同时,Deprecated Gradle features的警告说明用户脚本中使用了过时的特性,可能需要更新构建脚本以适配新版本Gradle。例如,'maven'插件可能已被弃用,推荐使用'maven-publish'。需要检查用户build.gradle中第20行的内容,可能那里有类似apply plugin: 'maven'的语句,应该替换为id 'maven-publish'。 另外,仓库配置是否正确也可能影响插件的下载。用户之前的配置中可能正确配置了镜像仓库,但如果插件在特定仓库中不存在,也可能导致找不到。需要确认插件是否在声明的仓库中存在,或者是否需要添加其他仓库。 用户可能不太清楚Gradle插件ID的变化,或者混淆了插件ID和旧的应用方式。比如,'maven'插件现在可能需要通过不同的ID或方式应用。需要给出具体的修改示例,如使用plugins块来声明插件,并确认是否在buildscript中正确配置了仓库。 最后,建议用户升级Gradle版本或者调整构建脚本,同时处理过时的警告,确保项目兼容性。可能还需要指导用户如何检查Gradle版本,以及如何更新wrapper等步骤。</think>这个错误表明在Gradle构建过程中遇到了两个关键问题,以下是详细解析和解决方案: --- ### **错误核心原因** 1. **直接报错**:`Plugin with id 'maven' not found` - 在Gradle 7.0+版本中,传统的 `maven` 插件已被废弃,需要用新的 `maven-publish` 插件替代 - 错误通常发生在 `build.gradle` 中错误地使用了旧版语法 `apply plugin: 'maven'` 2. **次要警告**:`Deprecated Gradle features... incompatible with Gradle 9.0` - 当前构建脚本使用了过时的Gradle特性(与Gradle 8.13兼容,但未来升级到9.0时会出问题) --- ### **定位问题步骤** 1. **检查构建脚本第20行**(你的 `build.gradle` 文件第20行) - 典型错误示例: ```groovy // 旧版写法(已废弃) apply plugin: 'maven' // ❌ 导致报错 ``` 2. **验证Gradle版本** ```bash ./gradlew --version # 查看当前Gradle版本 ``` --- ### **解决方案** #### 1. 修复插件引用(关键) **旧版写法(已废弃)**: ```groovy apply plugin: 'maven' // ❌ 错误写法 ``` **新版正确写法**: ```groovy plugins { id 'maven-publish' // ✅ 使用新插件 } ``` #### 2. 同步更新发布配置 在 `build.gradle` 中添加发布配置: ```groovy publishing { publications { maven(MavenPublication) { from components.java } } } ``` #### 3. 检查仓库配置 确保 `buildscript` 块中包含插件仓库(参考你之前的配置): ```groovy buildscript { repositories { maven { url 'https://maven.aliyun.com/repository/public' } // 阿里云镜像 maven { url 'https://plugins.gradle.org/m2/' } // Gradle插件仓库 } } ``` --- ### **附加建议** 1. **处理废弃警告**: ```bash ./gradlew build --warning-mode all # 查看所有废弃警告 ``` - 按警告提示逐步替换过时API 2. **升级Gradle Wrapper**(可选): ```bash ./gradlew wrapper --gradle-version=8.6 # 升级到稳定版本 ``` 3. **国内镜像加速**(基于你之前的配置): ```groovy // settings.gradle pluginManagement { repositories { maven { url 'https://maven.aliyun.com/repository/public' } gradlePluginPortal() } } ``` --- ### **调试技巧** 1. 获取完整堆栈: ```bash ./gradlew tasks --stacktrace ``` 2. 查看依赖树: ```bash ./gradlew dependencies ``` 通过上述调整,应该可以解决插件未找到的问题,同时为后续Gradle版本升级做好准备。若仍有问题,建议提供 `build.gradle` 第20行具体内容以便精准诊断。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值