9

please tell me how to build apk files of a project without the use of ECLIPSE ide. i found some infos about using a batch file but i don't know how to remake it.

echo on

SET PREV_PATH=%CD%
cd /d %0\..

REM Clear bin folder
rmdir "bin" /S /Q
rmdir "gen" /S /Q
mkdir "bin" || goto EXIT
mkdir "gen" || goto EXIT

REM Set your application name
SET APP_NAME=SecureSms

REM Define minimal Android revision
SET ANDROID_REV=android-8

REM Define aapt add command
SET ANDROID_AAPT_ADD="%ANDROID-SDK%\platforms\%ANDROID_REV%\tools\aapt.exe" add

REM Define aapt pack and generate resources command
SET ANDROID_AAPT_PACK="%ANDROID-SDK%\platforms\%ANDROID_REV%\tools\aapt.exe" package -v -f -I "%ANDROID-SDK%\platforms\%ANDROID_REV%\android.jar"

REM Define class file generator command
SET ANDROID_DX="%ANDROID-SDK%\platform-tools\dx.bat" --dex

REM Define Java compiler command
SET JAVAC="%JAVABIN%\javac.exe" -classpath "%ANDROID-SDK%\platforms\%ANDROID_REV%\android.jar"
SET JAVAC_BUILD=%JAVAC% -sourcepath "src;gen" -d "bin"

REM Generate R class and pack resources and assets into resources.ap_ file
call %ANDROID_AAPT_PACK% -M "AndroidManifest.xml" -A "assets" -S "res" -m -J "gen" -F "bin\resources.ap_" || goto EXIT

REM Compile sources. All *.class files will be put into the bin folder
call %JAVAC_BUILD% src\org\secure\sms\*.java || goto EXIT

REM Generate dex files with compiled Java classes
call %ANDROID_DX% --output="%CD%\bin\classes.dex" %CD%\bin || goto EXIT

REM Recources file need to be copied. This is needed for signing.
copy "%CD%\bin\resources.ap_" "%CD%\bin\%APP_NAME%.ap_" || goto EXIT

REM Add generated classes.dex file into application package
call %ANDROID_AAPT_ADD% "%CD%\bin\%APP_NAME%.ap_" "%CD%\bin\classes.dex" || goto EXIT

REM Create signed Android application from *.ap_ file. Output and Input files must be different.
call "%JAVABIN%\jarsigner" -keystore "%CD%\keystore\my-release-key.keystore" -storepass "password" -keypass "password" -signedjar "%CD%\bin\%APP_NAME%.apk" "%CD%\bin\%APP_NAME%.ap_" "alias_name" || goto EXIT

REM Delete temp file
del "bin\%APP_NAME%.ap_"

:EXIT
cd "%PREV_PATH%"
ENDLOCAL
exit /b %ERRORLEVEL%

i got this codes from a site. (http://www.apriorit.com/our-company/dev-blog/233-how-to-build-apk-file-from-command-line)

i downloaded the source code sample and opened the batch file in there but it didn't generate it's apk file. usually the apk file is located at its bin\ right? but when i opened the folder, the file is not in there. please help me how to use this one. i'd appreciate you help.

sam
  • 133
  • 1
  • 2
  • 6

4 Answers4

5

You’ll have to have Apache ant for this one:

ant debug

This will build and sign the necessary .apk files.

For more info, please see this: http://codeseekah.com/2012/02/09/command-line-android-development-basics/

EDIT:

ant is not a part of standard Android SDK setup. You'll have to install it.

Download the latest ant zip file from The Apache Ant Project.

Extract the zip file to a folder, say c:\ant\ 

Add c:\ant to your path environment variable

Once these are done, you'll be able to run ant from the command line

Shrikant Ballal
  • 7,067
  • 7
  • 41
  • 61
  • am i gonna put that line in the batch file? – sam Oct 01 '12 at 05:55
  • No, you have to write it in the prompt. I will not say its not possible with batch, but try it on prompt itself. Writing directly on prompt and writing a batch file will do the same job. Please see my edit. – Shrikant Ballal Oct 01 '12 at 06:01
  • I need to change directory to "c:\ant" then type `ant debug` then `android update project -t android-10 -p` <-- axis told me to do this from his reply here. Is this correct? – sam Oct 01 '12 at 06:52
  • You don't have to change your pwd, If you haven't added your ant path to environment variable, then you need to write this whole "ant path" again and again. You can try what Axis is saying, or only "ant debug" should also work. – Shrikant Ballal Oct 01 '12 at 07:00
4

The apk by default is located under bin and this is correct, but when you distribute source code it's better to not add any apk because a "fresh" compiled apk is always a better solution.

if you have a file called build.xml in the root of your project just do

ant debug

otherwise you need to update your project with the minimum informations required for the building phase with

android update project -t android-10 -p .

in this case android-10 is a target for your apk/app/api and you can customize this option for your targeted device.

After this you get your build.xml and everything is in place for generating an apk.

axis
  • 874
  • 2
  • 7
  • 13
  • where will i put the `ant debug` ? sorry i'm very noob in android programming. i just wanted to build an apk file without using the emulator so that the admin of the system can just open the executable file or that batch file to create an apk file and distribute it to the user. – sam Oct 01 '12 at 05:25
  • @sam both commands are for the root of your project and you need a terminal open to input them. – axis Oct 01 '12 at 05:40
  • so it's not possible for just a single batch file to create an apk file? after the execution of the batch file, i must manually type that android update from the ant? – sam Oct 01 '12 at 05:43
  • @sam `android update ...` and `ant debug` are 2 different commands. I suppose that you are using Windows and under Windows you can create a batch file http://www.google.com/search?#q=windows+bat+file that will just execute `android update ...` and after this `ant debug` or whatever command you like to execute in batch. – axis Oct 01 '12 at 06:17
0
ant debug install

is actually viable if you create project with (note that you should specify the virtual device with "adb -s" command while installing if you're running multiple devices)

android create project -n <project_name>
        -t <target_version> -p <path> -k <package> -a <activity>

and use this command to run on the avd (if you've started one)

adb -e shell "am start -a android.intent.action.MAIN -n com.your.package/.YourActivity"

(change -e option to -d if you're running on devices)

reference

better use "AVD Manager.exe" to start a virtual device if you don't know those command or parameters, as there're quite a lot.

Valen
  • 1,693
  • 1
  • 20
  • 17
0

One of the best example I found on Internet for creating Android APK is https://geosoft.no/development/android.html.Also you can use assembleDebug command in root directory (make sure setting gradle as environment variable), if you are using Gradle.