這是我在windows環境下,試驗過的最簡單Android項目,只用記事本和命令行便可完成。java
開發環境須要Java SDK(官網下載),Android SDK(官網下載)。
android
首先安裝Java SDK,而後將Android SDK解壓到任意目錄(建議解壓到某個盤根目錄,方便後續命令輸入)。express
接着須要下載Android SDK。打開剛纔解壓目錄下的SDK Manager,勾選須要安裝版本的SDK Platform點擊Install package下載安裝。apache
在任意地方新建一個目錄,保存這個項目,而後新建一個src目錄,用於存放源文件。由於Java有包的概念,因此進入src目錄後,根據包名的層次,依次創建相應目錄,而後新建Java源程序文件,好比:windows
1 package test.android; 2 3 import android.app.Activity; 4 import android.os.Bundle; 5 import android.app.AlertDialog; 6 7 public class Mini extends Activity { 8 public void onCreate(Bundle savedInstanceState) { 9 super.onCreate(savedInstanceState); 10 new AlertDialog.Builder(this).setMessage("It works.").show(); 11 } 12 }
將文件保存爲Mini.javaoracle
回到項目根目錄,新建另外一個文件,保存爲AndroidManifest.xml,內容以下:app
1 <?xml version="1.0" encoding="utf-8"?> 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="test.android"> 3 <application> 4 <activity android:name=".Mini"> 5 <intent-filter> 6 <action android:name="android.intent.action.MAIN" /> 7 <category android:name="android.intent.category.LAUNCHER" /> 8 </intent-filter> 9 </activity> 10 </application> 11 </manifest>
還能夠從這裏下載演示項目。less
好了,項目至此已經完成了,下面進入編譯打包環節。ui
先打開命令行,輸入javac -version,若是沒有顯示出java版本號,請將Java SDK的bin目錄添加到path環境變量。this
把當前目錄切換到項目的根目錄,而後新建兩個目錄
1 mkdir bin 2 mkdir bin\classes
由於沒有用到資源文件,因此第一步,直接編譯Java源文件。
1 javac -encoding utf-8 -source 1.7 -target 1.7 -bootclasspath \Android\android-sdk-windows\platforms\android-23\android.jar -d bin\classes src\test\android\Mini.java
將編譯好的文件打包成dex格式
1 D:\Android\android-sdk-windows\build-tools\23.0.3\dx.bat --dex --output=bin\classes.dex bin\classes
將資源文件打包
1 D:\Android\android-sdk-windows\build-tools\23.0.3\aapt.exe package -f -M AndroidManifest.xml -I \Android\sdk\platforms\android-19\android.jar -F bin\mini
用apkbuilder將全部文件打包成apk
1 D:\Android\android-sdk-windows\tools\apkbuilder.bat \workspace\test\android\minimum\mini.apk -v -u -z D:\workspace\test\android\minimum\bin\mini -f D:\workspace\test\android\minimum\bin\classes.dex
高版本的Java SDK裏已經不提供apkbuilder.bat了,這裏將文件內容貼一下,能夠本身建立一個:
1 @echo off 2 rem Copyright (C) 2007 The Android Open Source Project 3 rem 4 rem Licensed under the Apache License, Version 2.0 (the "License"); 5 rem you may not use this file except in compliance with the License. 6 rem You may obtain a copy of the License at 7 rem 8 rem http://www.apache.org/licenses/LICENSE-2.0 9 rem 10 rem Unless required by applicable law or agreed to in writing, software 11 rem distributed under the License is distributed on an "AS IS" BASIS, 12 rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 rem See the License for the specific language governing permissions and 14 rem limitations under the License. 15 16 rem don't modify the caller's environment 17 setlocal 18 19 rem Set up prog to be the path of this script, including following symlinks, 20 rem and set up progdir to be the fully-qualified pathname of its directory. 21 set prog=%~f0 22 23 rem Change current directory and drive to where the script is, to avoid 24 rem issues with directories containing whitespaces. 25 cd /d %~dp0 26 27 rem Check we have a valid Java.exe in the path. 28 set java_exe= 29 call lib\find_java.bat 30 if not defined java_exe goto :EOF 31 32 set jarfile=sdklib.jar 33 set frameworkdir= 34 35 if exist %frameworkdir%%jarfile% goto JarFileOk 36 set frameworkdir=lib\ 37 38 if exist %frameworkdir%%jarfile% goto JarFileOk 39 set frameworkdir=..\framework\ 40 41 :JarFileOk 42 43 set jarpath=%frameworkdir%%jarfile% 44 45 call %java_exe% -classpath %jarpath% com.android.sdklib.build.ApkBuilderMain %*
生成簽名文件
1 keytool -genkey -alias my.keystore -keyalg RSA -validity 20000 -keypass 123456 -storepass 123456 -keystore my.keystore
生成簽名文件時,提示輸入姓名單位之類均可以直接回車忽略,最後輸入y確認便可
對apk文件簽名
1 jarsigner -verbose -keystore my.keystore -keypass 123456 -storepass 123456 -signedjar mini_signed.apk mini.apk my.keystore
這時就生成了最終的apk文件,能夠安裝到手機上了。