“你好世界"Android 应用程序尽可能少的文件,没有 IDE,只有文本编辑器

在我使用过的几乎所有语言(Python、C、C++ 等)中,仅可以使用文本编辑器编写hello world"应用程序并从命令运行它-line(解释语言)或从命令行(编译语言)编译/构建它,例如cl.exe helloworld1.cpp.

In nearly all languages that I've used (Python, C, C++, etc.), it's possible to write a "hello world" application with a text editor only and run it from command-line (interpreted languages) or compile/build it from command-line (compiled languages), e.g. cl.exe helloworld1.cpp.

另一方面,每次我做一个Android App时,我都需要使用Android Studio(在我的机器上很慢),用IDE创建一个新项目,等等.

On the other hand, every time I'm doing an Android App, I need to use Android Studio (which is slow on my machine), create a new project with the IDE, etc.

问题:要生成 .apk Android 应用,最少需要多少极简 Java 源代码文件/项目文件? 如何从命令行构建它?(并且永远不必打开 IDE)

Question: What is the smallest number of minimalist Java source code files/project files to produce an .apk Android app? How to build it from command-line? (and never have to open the IDE)

注意:我读过很多 hello world for Android 但所有这些都涉及使用 IDE.

NB: I've read many hello world for Android but all of them involve using the IDE.

NB2:我正在寻找用 Java 编写的标准应用程序,而不是 Kivy 等解决方案.

NB2: I'm looking for standard apps written in Java, not solutions like Kivy, etc.

NB3:即使 IDE 可能更方便地编写 Android 应用程序,我也看不出有任何技术原因导致编译/构建大量文件绝对需要 IDE/使用 GUI 进行编程.有些人(比如我)只喜欢命令行和文本编辑器,这样的无 IDE 解决方案会很有帮助.

NB3: even if an IDE is probably more convenient to program an Android app, I don't see any technical reason for which compiling/building a number of files would absolutely require an IDE / programming with a GUI. Some people (like me) prefer command-line and text editor only, and such an IDE-free solution would be helpful.

NB4:我在 Windows 平台上工作,我在这里启动了 "Hello World without IDE" github repo 基于 这个答案,但我有一些问题,例如 这个.另一方面,那里使用的方法似乎已被弃用...

NB4: I'm working on Windows platform, I have started a "Hello World without IDE" github repo here based on this answer, but I have a few problems such as this one. On the other hand, the method used there seems to be deprecated...

推荐答案

是的,您可以从命令行轻松完成 ALL(我保证不涉及 IDE).
这使用了老忠实 Apache Ant.它确实不使用 Gradle,这需要更多的工作.

Yes you can easily do it ALL from the command line (NO IDE involved, I promise).
This uses the old faithful Apache Ant. It does not use Gradle, that takes more work.

您输入的是(只需 2 行即可生成 apk):

What you type is (just 2 lines to produce an apk):

    android create project --target "android-16" --path basj --activity TestActivity --package com.android.basj 

(这会生成一个名为 build.xmlApache Ant 构建文件,该文件类似于 build.gradle 文件.现在写一些代码,但 TestActivity.java 已经存在并且会编译)

(This produces an Apache Ant build file called build.xml file which is like the build.gradle file. Now write some code but TestActivity.java is there already and will compile)

    ant debug

设置

(注意:android.bat"命令自构建工具v26以来已弃用,因此请使用一个旧的(见下面的链接),deprecated 在这种情况下意味着 TOTALLY 已删除!{淘气的 Google}).

Setup

(Note: The "android.bat" command is deprecated since Build Tools v26, so use an old one (see link below), deprecated in this case means TOTALLY removed !{naughty Google}).

  1. 安装 Java JDK如果尚未安装(例如,您可以使用 jdk-8u151-windows-x64.exe),并确保 JAVA_HOME 环境变量 已定义 例如:

  1. Install Java JDK if not installed already (you can use jdk-8u151-windows-x64.exe for example), and make sure JAVA_HOME environment variable is defined e.g.:

JAVA_HOME=C:Program FilesJavajdk1.8.0_112
JAVA_PATH=C:Program FilesJavajre1.8.0_112in

JAVA_HOME=C:Program FilesJavajdk1.8.0_112
JAVA_PATH=C:Program FilesJavajre1.8.0_112in

JDK 是 Java 开发工具包.
JRE 是 Java 运行时环境.

JDK is the Java Development Kit.
JRE is the Java Run-time Environment.

  1. 安装 Android SDK 工具(例如 installer_r24.4.1-windows.exe,如果尚未完成,请参阅 此答案),然后在 SDK 中管理器 GUI,取消选择所有内容并选择Android SDK Build-Tools"(例如 Android SDK Build-Tools 19.1)+ 一个(或多个)平台(例如 Android 4.1.2 (API 16)JELLY_BEAN).为了证明你不需要 Android Studio,不打算下载它!(仅限 SDK).

  1. Install Android SDK Tools (e.g. installer_r24.4.1-windows.exe, see this answer) if not already done, and then in the SDK Manager GUI, deselect everything and choose "Android SDK Build-Tools" (e.g. Android SDK Build-Tools 19.1) + one (or many) platforms (e.g. Android 4.1.2 (API 16) JELLY_BEAN). To prove you don't need Android Studio, were not going to download it ! (only the SDK).

下载 Apache Ant(例如 apache-ant-1.9.9-bin.zip)

详情

使用 Android SDK 从命令行创建项目:

决定放置你的项目的地方:

Decide on a place to put your project:

cd c:android
mkdir antTest
cd antTest

运行命令:

C:Androidsdk1	oolsandroid create project --target "android-16" --path basj --activity TestActivity --package com.android.basj 
              ^
              |
--------------+ (here's where I keep an old version of tools (version 25 in my case)

这是创建的目录结构(以及您需要构建的所有文件):

Here is the directory structure created (and all the files you need to build):

C:.
+---basj
    +---bin
    +---libs
    +---res
    ¦   +---drawable-hdpi
    ¦   +---drawable-ldpi
    ¦   +---drawable-mdpi
    ¦   +---drawable-xhdpi
    ¦   +---layout
    ¦   +---values
    +---src
        +---com
            +---android
                +---basj

创建项目的详细输出:

Created project directory: C:AndroidantTestasj
Created directory C:AndroidantTestasjsrccomandroidasj
Added file C:AndroidantTestasjsrccomandroidasjTestActivity.java
Created directory C:AndroidantTestasjes
Created directory C:AndroidantTestasjin
Created directory C:AndroidantTestasjlibs
Created directory C:AndroidantTestasjesvalues
Added file C:AndroidantTestasjesvaluesstrings.xml
Created directory C:AndroidantTestasjeslayout
Added file C:AndroidantTestasjeslayoutmain.xml
Created directory C:AndroidantTestasjesdrawable-xhdpi
Created directory C:AndroidantTestasjesdrawable-hdpi
Created directory C:AndroidantTestasjesdrawable-mdpi
Created directory C:AndroidantTestasjesdrawable-ldpi
Added file C:AndroidantTestasjAndroidManifest.xml
Added file C:AndroidantTestasjuild.xml
Added file C:AndroidantTestasjproguard-project.txt

从 Apache Ant="noreferrer">http://ant.apache.org/.

Download Apache Ant from http://ant.apache.org/.

请参阅本教程进行设置:http://www.vogella.com/tutorials/ApacheAnt/article.html

See this tutorial for setup:http://www.vogella.com/tutorials/ApacheAnt/article.html

另请参阅本教程:http://blog.vogella.com/2011/03/16/creating-android-applications-via-the-command-line-ant/

编写您的代码(Hello world).

Write your code (Hello world).

运行此命令,您会从另一端获得一个 Android Apk(称为 TestActivity-debug.apk):

Run this command and you get an Android Apk out the other side (called TestActivity-debug.apk):

ant debug

你好,你有一个 android apk !
添加了新结构:

Hey presto, you got an android apk !
With new structure added:

C:.
├───bin
│   ├───classes
│   │   └───com
│   │       └───android
│   │           └───basj
│   ├───dexedLibs
│   └───res
│       ├───drawable-hdpi
│       ├───drawable-ldpi
│       ├───drawable-mdpi
│       └───drawable-xhdpi
├───gen
│   └───com
│       └───android
│           └───basj

对于最终构建:

ant release

如果您对 Ant build.xmlDEX 文件的更广泛示例以及 Android 更深入的工作原理感兴趣,请查看 这里

If your interested in a more extensive example of Ant build.xml, or DEX files, and the deeper workings of Android look here

请参阅 如何签署已编译的 apk 以及 这个
来自@for3st 的回答是该帖子的相关部分:

See how to sign an already compiled apk and also this
From an answer by @for3st here's a relevant piece of that post:

您需要生成一次密钥库并使用它来签署您的 unsigned apk.使用 keytool 由JDK提供在%JAVA_HOME%/bin/中找到代码>

You need to generate a keystore once and use it to sign your unsigned apk. Use the keytool provided by the JDK found in %JAVA_HOME%/bin/

keytool -genkey -v -keystore my.keystore -keyalg RSA -keysize 2048 -validity 10000 -alias app

第 2 步或第 4 步:压缩对齐

zipalign 这是Android SDK提供的工具,例如%ANDROID_HOME%/sdk/build-tools/24.0.2/ 如果您想将 apk 上传到 Play 商店,这是一个强制性的优化步骤.

Step 2 or 4: Zipalign

zipalign which is a tool provided by the Android SDK found in e.g. %ANDROID_HOME%/sdk/build-tools/24.0.2/ is a mandatory optimization step if you want to upload the apk to the Play Store.

zipalign -p 4 my.apk my-aligned.apk

注意: 使用旧的jarsigner 时需要zipalign AFTER 签名.当使用新的 apksigner 方法时,您会在 BEFORE 签名之前进行(我知道这很令人困惑).在 apksigner 工作之前调用 zipalign很好 因为 apksigner 保留 APK 对齐和压缩(与 jarsigner 不同).

Note: when using the old jarsigner you need to zipalign AFTER signing. When using the new apksigner method you do it BEFORE signing (confusing, I know). Invoking zipalign before apksigner works fine because apksigner preserves APK alignment and compression (unlike jarsigner).

您可以验证对齐方式:

zipalign -c 4 my-aligned.apk

第 3 步:签名 &验证

使用 24.0.2 及更早版本的构建工具

使用 jarsigner 与 keytool 一样,随 JDK 发行版提供,可在 %JAVA_HOME% 中找到/bin/ 并像这样使用它:

Step 3: Sign & Verify

Using build-tools 24.0.2 and older

Use jarsigner which, like the keytool, comes with the JDK distribution found in %JAVA_HOME%/bin/ and use it like so:

jarsigner -verbose -sigalg SHA1withRSA -digestalg SHA1 -keystore my.keystore my-app.apk my_alias_name

并且可以通过验证

jarsigner -verify -verbose my_application.apk

使用构建工具 24.0.3 和更新版本

Android 7.0 引入了 APK 签名方案 v2,这是一种新的应用程序签名方案,可提供更快的应用程序安装时间并更好地防止未经授权的 APK 文件更改(请参阅 这里和这里了解更多详情).因此,Google 实现了他们的 自己的 apk签名者称为:apksigner(duh!)脚本文件可以在 %ANDROID_HOME%/sdk/build-tools/24.0.3/ 中找到(.jar 在 /lib 子文件夹中).像这样使用它:

Using build-tools 24.0.3 and newer

Android 7.0 introduces APK Signature Scheme v2, a new app-signing scheme that offers faster app install times and more protection against unauthorized alterations to APK files (See here and here for more details). Therefore, Google implemented their own apk signer called: apksigner (duh!) The script file can be found in %ANDROID_HOME%/sdk/build-tools/24.0.3/ (the .jar is in the /lib subfolder). Use it like this:

apksigner sign --ks my.keystore my-app.apk --ks-key-alias alias_name

并且可以通过以下方式进行验证:

and can be verified with:

apksigner verify my-app.apk

相关文章