Compiling for Android SDK 1 (Part 3)

Part 1 Installing JDK1.5 and Eclipse Europa
Part 2 Generating R.java and compiling Java source
Part 3 Cross compile to JDK 1.5, convert to dex, package .apk
Part 4 Code sign .apk
Part 5 Troubleshooting and success

After fixing the Build.VERSION issues javac is happy, the next step is to convert the compiled Java class files into .dex (Dalvik Executeable?), the bash script now looks like this:

#!/bin/bash

echo "Oppen APK build script for Android G1/HTC Dream"

# Get arguments
# eg. ~/phaedra
projectDirectory=$1
androidJar=$2
outputDirectory="pbld_out"

# Generate R.java
echo "1. Generate Android R.java..."
rm -r "$outputDirectory"
mkdir "$outputDirectory"
./aapt package -f -M "$projectDirectory/app/src/main/AndroidManifest.xml" -I "$androidJar" -S "$projectDirectory/app/src/main/res" -m -J "./$outputDirectory"

# Compile Java source
echo "2. Finding Java source..."

rFile=$(find $outputDirectory -type f -name "*.java")
javaFiles=""
javaFiles+="$rFile "
projectFiles=$(find $projectDirectory -type f -name "*.java")
for projectFile in $projectFiles
do 
    javaFiles+="$projectFile "
done

echo "javaFiles: $javaFiles"

echo "3. Compile Java source..."
classesDirectory="$outputDirectory/classes"
mkdir "$classesDirectory"
javac $javaFiles -cp $androidJar -d $classesDirectory

echo "4. Convert to Dalvik executeable (DEX) files..."
./dx --dex --output=output.dex $classesDirectory

At this point the dx tools output errors:

trouble processing:
bad class file magic (cafebabe) or version (0037.0000)
...while parsing oppen/phaedra/MertextAdapter.class
...while processing oppen/phaedra/MertextAdapter.class

This is because javac used the modern system Java, I need to tell it to target an older Java version:

javac -source 1.5 -target 1.5 -bootclasspath /usr/java/jdk1.5.0_22/jre/lib/rt.jar $javaFiles -cp $androidJar -d $classesDirectory

I found another old blog post with some alternative final steps for generating the .apk:

Building Android programs on the command line

Using that I edit the bash script and seem to be generating a valid unsigned .apk:

#!/bin/bash

echo "Oppen APK build script for Android G1/HTC Dream"

# Get arguments
# eg. ~/phaedra
name=$1
projectDirectory=$2
androidJar=$3
outputDirectory="${name}_output"

# Generate R.java
echo "1. Generate Android R.java..."
rm -r "$outputDirectory"
mkdir "$outputDirectory"
./aapt package -f -M "$projectDirectory/app/src/main/AndroidManifest.xml" -I "$androidJar" -S "$projectDirectory/app/src/main/res" -m -J "./$outputDirectory"

# Compile Java source
echo "2. Finding Java source..."

rFile=$(find $outputDirectory -type f -name "*.java")
javaFiles=""
javaFiles+="$rFile "
projectFiles=$(find $projectDirectory -type f -name "*.java")
for projectFile in $projectFiles
do 
    javaFiles+="$projectFile "
done

echo "javaFiles: $javaFiles"

echo "3. Compile Java source..."
classesDirectory="$outputDirectory/classes"
mkdir "$classesDirectory"
javac -source 1.6 -target 1.6 -bootclasspath /usr/java/jdk1.5.0_22/jre/lib/rt.jar $javaFiles -cp $androidJar -d $classesDirectory

# DEX/Dalvik conversion
echo "4. Convert to Dalvik executeable (DEX) files..."
dexFile="$outputDirectory/$name.dex"
./dx --dex --output=$dexFile $classesDirectory

# Build APK
apkName="${name}_unsigned.apk"
echo "5. Building $apkName"
./aapt package -v -f -M "$projectDirectory/app/src/main/AndroidManifest.xml" -S "$projectDirectory/app/src/main/res" -I "$androidJar" -F $apkName $dexDirectory


© 2019 - 2021 ÖLAB
Part of a webring
view in Gemini