最簡單的Android項目

來源:互聯網
上載者:User

標籤:

這是我在windows環境下,實驗過的最簡單Android項目,只用記事本和命令列即可完成。

環境準備

開發環境需要Java SDK(官網下載),Android SDK(官網下載)。

首先安裝Java SDK,然後將Android SDK解壓到任意目錄(建議解壓到某個盤根目錄,方便後續命令輸入)。

接著需要下載Android SDK。開啟剛才解壓目錄下的SDK Manager,勾選需要安裝版本的SDK Platform點擊Install package下載安裝。

 

項目建立及代碼編寫

在任意地方建立一個目錄,儲存這個項目,然後建立一個src目錄,用於存放源檔案。因為Java有包的概念,所以進入src目錄後,根據包名的層次,依次建立相應目錄,然後建立Java來源程式檔案,比如:

 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.java

 

回到項目根目錄,建立另一個檔案,儲存為AndroidManifest.xml,內容如下:

 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>

 

還可以從這裡下載示範項目。

 

好了,項目至此已經完成了,下面進入編譯打包環節。

 

編譯打包

 先開啟命令列,輸入javac -version,如果沒有顯示出java版本號碼,請將Java SDK的bin目錄添加到path環境變數。

 

把目前的目錄切換到項目的根目錄,然後建立兩個目錄

1 mkdir bin2 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 rem10 rem Unless required by applicable law or agreed to in writing, software11 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 and14 rem limitations under the License.15 16 rem don‘t modify the caller‘s environment17 setlocal18 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=%~f022 23 rem Change current directory and drive to where the script is, to avoid24 rem issues with directories containing whitespaces.25 cd /d %~dp026 27 rem Check we have a valid Java.exe in the path.28 set java_exe=29 call lib\find_java.bat30 if not defined java_exe goto :EOF31 32 set jarfile=sdklib.jar33 set frameworkdir=34 35 if exist %frameworkdir%%jarfile% goto JarFileOk36     set frameworkdir=lib37 38 if exist %frameworkdir%%jarfile% goto JarFileOk39     set frameworkdir=..\framework40 41 :JarFileOk42 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檔案,可以安裝到手機上了。

 

最簡單的Android項目

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.