“Hello world” Android应用程序,文件尽可能少,没有IDE,只有文本编辑器 [英] "Hello world" Android app with as few files as possible, no IDE, and text editor only
问题描述
在我使用的几乎所有语言(Python,C,C ++等)中,可以使用文本编辑器编写hello world应用程序并从命令运行它-line(解释语言)或从命令行(编译语言)编译/构建它,例如 cl.exe helloworld1.cpp
。
另一方面,每次我在做Android应用程序时,我需要使用Android Studio(在我的机器上很慢),用IDE创建一个新项目等。
问题:最小数字是多少最小的Java源代码文件/项目文件生成.apk Android应用程序?如何从命令行构建它? (从不必须打开IDE)
注意:我读了很多 hello world for Android 但所有这些都涉及到使用IDE。
NB2:我正在寻找用Java编写的标准应用程序,而不是Kivy等解决方案。
NB3:即使IDE可能更方便编程Android应用程序,我也看不到任何技术原因,编译/构建许多文件绝对需要带有GUI的IDE /编程。有些人(像我一样)只喜欢命令行和文本编辑器,这样一个无IDE的解决方案会有所帮助。
NB4:我在Windows平台上工作,我已经基于没有IDE的Hello Worldgithub repo //stackoverflow.com/questions/29178552/hello-world-using-the-android-sdk-alone-no-ide/29313378#29313378>这个答案,但我有一些问题,比如这一个。另一方面,那里使用的方法似乎已被弃用...
是的,你可以很容易地做到这一点所有来自命令行(NO IDE
涉及,我保证)。
这使用旧忠实 Apache Ant 。它不使用 Gradle
,需要更多工作。
总结
你键入的是(只有2行来制作apk):
android create project --targetandroid-16--path basj --activity TestActivity --package com.android.basj
(这会产生一个 Apache Ant
构建文件,名为 build.xml
喜欢 build.gradle
文件的文件。现在编写一些代码,但TestActivity.java已经存在并将编译)
ant debug
< h1>设置
(注意: android.bat
命令为<自建立工具 v26 以来强烈>弃用,因此请使用旧版本(请参阅下面的链接),已弃用,在这种情况下意味着 TOTALLY 已删除!{顽皮的Google })。
-
安装 Java JDK 如果尚未安装(例如,您可以使用jdk-8u151-windows-x64.exe),并确保
JAVA_HOME
环境变量已定义,例如:
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运行时环境。
-
安装
Android SDK工具
(例如 installer_r24.4.1-windows.exe ,请参阅这个答案)如果还没有完成,然后在SDK Manager GUI中取消选择所有内容并选择Android SDK Build-Tools(例如Android SDK Build-Tools 19.1
)+一个(或多个)平台(例如Android 4.1.2(API 16)JELLY_BEAN
) 。为了证明你不需要Android Studio
,不打算下载它! (仅限SDK)。 -
下载 Apache Ant (例如 apache -ant-1.9.9-bin.zip )
明细
使用 Android SDK
从命令行创建项目:
决定放置项目的地方:
cd c:\ android
mkdir antTest
cd antTest
运行命令:
C:\ Android \\\ddk1 \tools \ android创建项目--targetandroid-16--path basj --activity TestActivity --package com.android.basj
^
|
-------------- +(这里是我保留旧版工具的地方(在我的情况下是25版)
这是创建的目录结构(以及您需要构建的所有文件):
C:。
+ --- basj
+ --- bin
+ --- libs
+ --- res
| + --- drawable-hdpi
| + --- drawable-ldpi
| + --- drawable-mdpi
| + --- drawable-xhdpi
| + - --layout
| + ---值
+ --- src
+ --- com
+ --- android
+ --- basj
创建项目的详细输出:
创建的项目目录:C:\ Anroid \antTest \ basj
工作正常,因为
创建目录C:\ Android2 \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ \\ basj
添加文件C:\ Android2 \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ \antTest\basj\res
创建目录C:\ Android2 \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ b $ b创建目录C:\ Android2 \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
创建目录C:\ Android2 \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ xml
创建目录C:\ Android2 \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ hdpi
创建目录C:\ Android2 \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ ldpi
添加文件C:\ Android2 \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ p>
从 Apache Ant ://ant.apache.org/rel =nofollow noreferrer> http://ant.apache.org/ 。
查看本教程安装: 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)。
运行此命令即可获得Android Apk在另一边(称为TestActivity-debug.apk):
ant debug
嘿presto,你有一个安卓apk!
添加新结构:C:。 $ b $b├───bin$ b $b│├───类$ b $b││└───com$ b $b││└───android$ b $b││└─── ─basj$ b $b│├───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── │├───drawable-mdpi $ b $b│└───drawable-xhdpi $ b $b├──────$ b $b│└───com$ b $b│└───android$ b $b│└───basj
最终版本:
蚂蚁释放
如果你的话感兴趣的是一个更广泛的
Ant build.xml
或DEX
文件的例子,以及Android的更深层次的工作方式< a href =https://stackoverflow.com/questions/39175169/how-to-build-an-apk-and-separate-libraries-that-the-app-loads-dynamically/39278805#39278805>这里
如何签署已编译的apk
请参阅如何签署已编译的apk 和此
来自@ for3st的回答这里是该帖子的相关部分:
手动流程:
< h2>步骤1:生成密钥库(仅一次)
您需要生成一次密钥库并使用它来签署
unsigned
apk。
使用keytool
由JDK提供%JAVA_HOME%/ bin /
keytool -genkey -v -keystore my.keystore -keyalg RSA -keysize 2048 -validity 10000 -alias app
步骤2或4:Zipalign
zipalign
这是一个例如,在Android SDK中提供的工具如果您想将apk上传到Play商店,%ANDROID_HOME%/ sdk / build-tools / 24.0.2 /
是必须的优化步骤。zipalign -p 4 my.apk my-aligned.apk
注意:使用旧的
jarsigner
时,您需要zipalign
AFTER 签名。当使用新的apksigner
方法时,你可以 BEFORE 签名(令人困惑,我知道)。 在<$之前调用zipalign
c $ c> apksignerapksigner
保留APK
对齐和压缩(与jarsigner
不同)。
您可以验证对齐方式:
zipalign -c 4 my-aligned.apk
第3步:签署&验证
使用构建工具24.0.2及更早版本
使用
jarsigner
keytool,附带JDK发行版%JAVA_HOME%/ bin /
并像这样使用它:jarsigner -verbose -sigalg SHA1withRSA -digestalg SHA1 -keystore my。 keystore my-app.apk my_alias_name
并且可以通过
$进行验证b $ bjarsigner -verify -verbose my_application.apk
< h3>使用构建工具24.0.3和更新版
Android 7.0
介绍APK Signature Scheme v2
,一种新的应用程序签名方案,可提供更快的应用程序安装时间,并提供更多保护,防止对APK文件进行未经授权的更改(请参阅此处和这里了解更多详情)。因此,apk
签名者:apksigner
(呃!)
可以找到脚本文件在%ANDROID_HOME%/ sdk / build-tools / 24.0.3 /
(.jar位于/ lib
中子文件夹)。像这样使用它:apksigner sign --ks my.keystore my-app.apk --ks-key-alias alias_name
并且可以通过以下方式进行验证:
apksigner验证my-app.apk
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
.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.
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)
NB: I've read many hello world for Android but all of them involve using the IDE.
NB2: I'm looking for standard apps written in Java, not solutions like Kivy, etc.
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: 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...
解决方案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 useGradle
, that takes more work.To Summarize
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
(This produces an
Apache Ant
build file calledbuild.xml
file which is like thebuild.gradle
file. Now write some code but TestActivity.java is there already and will compile)ant debug
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\binJDK is the Java Development Kit.
JRE is the Java Run-time Environment.
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).Download Apache Ant (for example apache-ant-1.9.9-bin.zip)
Detail
To create a project from the command line using
Android SDK
:Decide on a place to put your project:
cd c:\android mkdir antTest cd antTest
Run the command:
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
detailed output of create project:
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
Download Apache Ant from http://ant.apache.org/.
See this tutorial for setup:http://www.vogella.com/tutorials/ApacheAnt/article.html
Also see this tutorial:http://blog.vogella.com/2011/03/16/creating-android-applications-via-the-command-line-ant/
Write your code (Hello world).
Run this command and you get an Android Apk out the other side (called TestActivity-debug.apk):
ant debug
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
For a final build :
ant release
If your interested in a more extensive example of
Ant build.xml
, orDEX
files, and the deeper workings of Android look hereHow to sign an already compiled apk
See how to sign an already compiled apk and also this
From an answer by @for3st here's a relevant piece of that post:Manual Process:
Step 1: Generate Keystore (only once)
You need to generate a keystore once and use it to sign your
unsigned
apk. Use thekeytool
provided by the JDK found in%JAVA_HOME%/bin/
keytool -genkey -v -keystore my.keystore -keyalg RSA -keysize 2048 -validity 10000 -alias app
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
Note: when using the old
jarsigner
you need tozipalign
AFTER signing. When using the newapksigner
method you do it BEFORE signing (confusing, I know). Invokingzipalign
beforeapksigner
works fine becauseapksigner
preservesAPK
alignment and compression (unlikejarsigner
).You can verify the alignment with:
zipalign -c 4 my-aligned.apk
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
and can be verified with
jarsigner -verify -verbose my_application.apk
Using build-tools 24.0.3 and newer
Android 7.0
introducesAPK 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,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
这篇关于“Hello world” Android应用程序,文件尽可能少,没有IDE,只有文本编辑器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!