简体   繁体   English

如何在命令行上构建没有 Gradle 的 Android apk?

[英]How can I build an Android apk without Gradle on the command line?

I wrote an Android app that uses no dependencies or modules, has a single activity, and has a single layout file.我编写了一个 Android 应用程序,它不使用任何依赖项或模块,只有一个活动,并且只有一个布局文件。

How can I build an apk file of my app on the command line without using Gradle (or other "build systems" or "dependency management" software)?如何在不使用 Gradle(或其他“构建系统”或“依赖管理”软件)的情况下在命令行上构建我的应用程序的 apk 文件?

Use the following steps to build your apk manually, if you don't want use ant/gralde to build.如果您不想使用 ant/gralde 构建,请使用以下步骤手动构建您的 apk。 But you must have Android SDK installed at least.但你必须至少安装 Android SDK。

  • create R.java from aaptaapt创建R.java

  • use javac to compile all java source to *.class使用javac将所有 java 源码编译为*.class

  • use dx to convert all *.class to dex file, eg output is classes.dex使用dx将所有*.class转换为dex文件,例如输出为classes.dex

  • create initial version of APK from assets, resources and AndroidManfiest.mk , eg output is MyApplication.apk.unaligned从资产、资源和AndroidManfiest.mk创建初始版本的 APK,例如输出是MyApplication.apk.unaligned

  • use aapt to add classes.dex generated in step 3 to MyApplication.apk.unaligned使用aapt将步骤 3 中生成的classes.dex添加到MyApplication.apk.unaligned

  • use jarsigner to sign MyApplication.apk.unaligned with debug or release key使用 jarsigner 签署MyApplication.apk.unaligned与调试或发布密钥

  • use zipalign to align the final APK, eg output is MyApplication-debug.apk or MyApplication-release.apk if signing with release key使用zipalign对齐最终的 APK,例如,如果使用发布密钥签名,则输出为MyApplication-debug.apkMyApplication-release.apk

  • Done完毕

I have created a sample script to do all the stuffs above, see here我创建了一个示例脚本来完成上述所有操作,请参见此处

Actually, Some articles have discussed this topic, see the following links.实际上,有些文章已经讨论过这个话题,请参阅以下链接。

https://www.apriorit.com/dev-blog/233-how-to-build-apk-file-from-command-line https://www.apriorit.com/dev-blog/233-how-to-build-apk-file-from-command-line

https://spin.atomicobject.com/2011/08/22/building-android-application-bundles-apks-by-hand/ https://spin.atomicobject.com/2011/08/22/building-android-application-bundles-apks-by-hand/

alijandro gave a perfect answer. alijandro给出了完美的答案。 I managed to write simple ANT script that builds production APK with AdMob and without gradle usage.我设法编写了简单的 ANT 脚本,该脚本使用 AdMob 构建生产 APK,无需使用 gradle。 A couple useful comments:一些有用的评论:

  1. If you want to obfuscate classes you have to jar the compiled classes (between javac and dx steps) and run proguard on it如果你想混淆类,你必须 jar 编译的类(在javacdx步骤之间)并在它上面运行proguard

  2. For AdMob you have to extract the following jars from zip archives (like C:\\Users\\<User>\\AppData\\Local\\Android\\sdk\\extras\\google\\m2repository\\com\\google\\android\\gms\\play-services-ads\\10.2.6\\play-services-ads-10.2.6.aar ):对于AdMob您必须从 zip 存档中提取以下 jar(例如C:\\Users\\<User>\\AppData\\Local\\Android\\sdk\\extras\\google\\m2repository\\com\\google\\android\\gms\\play-services-ads\\10.2.6\\play-services-ads-10.2.6.aar ):

    • play-services-ads-10.2.6.jar play-services-ads-10.2.6.jar
    • play-services-ads-lite-10.2.6.jar play-services-ads-lite-10.2.6.jar
    • play-services-base-10.2.6.jar play-services-base-10.2.6.jar
    • play-services-basement-10.2.6.jar play-services-basement-10.2.6.jar
    • play-services-clearcut-10.2.6.jar play-services-clearcut-10.2.6.jar
    • play-services-gass-10.2.6.jar play-services-gass-10.2.6.jar
    • play-services-tasks-10.2.6.jar play-services-tasks-10.2.6.jar

These archives should be passed in javac and dx这些档案应该在javacdx传递

  1. For AdMob there are several additional simple config steps as well对于AdMob还有几个额外的简单配置步骤

Gradle does a lot of mess with android projects, so own script looks like a singular solution for projects that are going to go into production Gradle对 android 项目做了很多处理,所以自己的脚本看起来像是将要投入生产的项目的单一解决方案

Try this for building apps with support libraries from command line.尝试使用此方法从命令行构建具有支持库的应用程序。 https://github.com/HemanthJabalpuri/AndroidExplorer https://github.com/HemanthJabalpuri/AndroidExplorer

The short answer is: U can't 简短的答案是:你不能

And the long is: 长是:

The process of creating apk is not as easy as it seams. 创建apk的过程并不容易。 https://developer.android.com/studio/build/index.html https://developer.android.com/studio/build/index.html

So not only you would need to: 因此,不仅您需要:

  • Marge resources, create R file 整理资源,创建R文件
  • Compile source to java, and then classes to dalvik 将源代码编译为Java,然后将类编译为dalvik
  • Package resources, then create zip with all necessary files 打包资源,然后使用所有必需的文件创建zip文件
  • Sign it with your key, 用钥匙签名

But well do a lot, a and i really mean it 但是很好,很多,我真的是说真的

There is a bit. 有一点。

Where if u use Android studio 如果您使用Android Studio

  • Just click green triangle to build and install 只需单击绿色三角形即可构建和安装
  • Or if you want to realease apk, Build -> Assemble Release Apk 或者,如果您想重新实现apk,请构建->组装发行apk

Even better you can use simple commands to run those task 更好的是,您可以使用简单的命令来运行这些任务

./gradlew assembleRelease

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM