《你好世界》使用尽可能少的文件、没有 IDE、只有文本编辑器的 Android 应用程序
在我使用过的几乎所有语言(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)
注意:我读过很多Android hello world 但所有这些都涉及使用 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.xml
的 Apache Ant
构建文件,该文件like 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
"命令已弃用,因为 Build Tools v26,所以使用一个旧的(见下面的链接),弃用在这种情况下意味着完全删除!{naughty 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}).
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 Files\Java\jdk1.8.0_112
JAVA_PATH=C:\Program Files\Java\jre1.8.0_112\bin
JAVA_HOME=C:\Program Files\Java\jdk1.8.0_112
JAVA_PATH=C:\Program Files\Java\jre1.8.0_112\bin
JDK 是 Java 开发工具包.
JRE 是 Java 运行时环境.
JDK is the Java Development Kit.
JRE is the Java Run-time Environment.
安装
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).
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 needAndroid Studio
, were not going to download it ! (only the SDK).
下载 Apache Ant(例如 apache-ant-1.9.9-bin.zip)
详情
使用 Android SDK
从命令行创建项目:
决定放置项目的位置:
cd c:\android
mkdir antTest
cd antTest
运行命令:
C:\Android\sdk1\tools\android 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:\Android\antTest\basj
Created directory C:\Android\antTest\basj\src\com\android\basj
Added file C:\Android\antTest\basj\src\com\android\basj\TestActivity.java
Created directory C:\Android\antTest\basj\res
Created directory C:\Android\antTest\basj\bin
Created directory C:\Android\antTest\basj\libs
Created directory C:\Android\antTest\basj\res\values
Added file C:\Android\antTest\basj\res\values\strings.xml
Created directory C:\Android\antTest\basj\res\layout
Added file C:\Android\antTest\basj\res\layout\main.xml
Created directory C:\Android\antTest\basj\res\drawable-xhdpi
Created directory C:\Android\antTest\basj\res\drawable-hdpi
Created directory C:\Android\antTest\basj\res\drawable-mdpi
Created directory C:\Android\antTest\basj\res\drawable-ldpi
Added file C:\Android\antTest\basj\AndroidManifest.xml
Added file C:\Android\antTest\basj\build.xml
Added file C:\Android\antTest\basj\proguard-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
嘿,presto,你有一个安卓 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.xml
或 DEX
文件的更广泛示例以及 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
由%JAVA_HOME%/bin/中的JDK提供
代码>
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
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 Signature Scheme v2
,这是一种新的应用签名方案,可提供更快的应用安装时间和更多保护,防止对 APK 文件进行未经授权的更改(请参阅Google
实施了他们的自己的apk代码>签名者调用:
(废话!)脚本文件位于 apksigner
%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