You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

无Gradle命令行构建含AppCompat的Android应用遇问题求助

Building Android Apps with AppCompat Without Gradle (Pure SDK Tools/Ant)

Absolutely, it's totally possible to build an Android app with AppCompat (and other libraries) using only the Android SDK tools—no Gradle required. I've done this myself with both pure command-line scripts and Ant. Let's break down what's going wrong with your current setup and fix it step by step.

Why Your Current Commands Are Failing

Let's unpack the errors you're seeing:

  1. Android 23 SDK + AppCompat v1.2.0 mismatch: AppCompat v1.2.0's values-v24 resources reference system styles that only exist in Android 24+ (like android:TextAppearance.Material.Widget.Button.Borderless.Colored). Using the Android 23 platform JAR means those resources don't exist, hence the "no resource found" errors.
  2. Missing dependency resources/classes when switching to Android 30: AppCompat doesn't work in isolation—it depends on other AndroidX libraries (like AndroidX Core). You only added AppCompat's res directory to your aapt command, but missed the resources and JAR files of its dependencies, leading to layout constraint and resource attribute errors.

Step-by-Step Solution

1. Prepare Required Dependencies

First, gather all necessary AndroidX libraries for AppCompat v1.2.0 (you can download these from Maven Central as AAR/JAR files):

  • androidx.appcompat:appcompat:1.2.0: Extract the res directory and rename the embedded classes.jar to appcompat.jar
  • androidx.core:core:1.3.0 (AppCompat 1.2.0's required core dependency): Extract the res directory and rename classes.jar to core.jar
  • androidx.annotation:annotation:1.1.0: Grab the standalone JAR file

2. Modified Build Script

Replace your existing commands with this updated script, adjusting paths to match your local setup:

Generate R.java (Resource Compilation)

mkdir -p ${PROJECT_BASE_DIR}/build/gen/java
aapt package --auto-add-overlay -f -m -J ${PROJECT_BASE_DIR}/build/gen/java \
  -M ${PROJECT_BASE_DIR}/src/AndroidManifest.xml \
  -S ${PROJECT_BASE_DIR}/src/res \
  -S /path/to/appcompat-1.2.0/res \
  -S /path/to/core-1.3.0/res \
  -I ${ANDROID_SDK_HOME}/platforms/android-30/android.jar
  • --auto-add-overlay: Merges resources from all included res directories to avoid conflicts
  • Include all dependency res directories: Ensures AppCompat can find its required resources

Compile Java Source Code

mkdir -p ${PROJECT_BASE_DIR}/build/classes
javac -d ${PROJECT_BASE_DIR}/build/classes \
  -source 1.8 -target 1.8 \
  -classpath ${PROJECT_BASE_DIR}/src/java:${PROJECT_BASE_DIR}/build/gen/java:/path/to/appcompat.jar:/path/to/core.jar:/path/to/annotation.jar \
  -bootclasspath ${ANDROID_SDK_HOME}/platforms/android-30/android.jar \
  ${PROJECT_BASE_DIR}/src/java/jj/android/clb/MainClass.java
  • Java 8 target: AppCompat v1.2.0 requires Java 8 compatibility
  • Include all dependency JARs: Lets the compiler find AppCompat classes like AppCompatActivity

Generate DEX File

dx --dex --output=${PROJECT_BASE_DIR}/build/classes.dex \
  ${PROJECT_BASE_DIR}/build/classes \
  /path/to/appcompat.jar \
  /path/to/core.jar \
  /path/to/annotation.jar
  • Include all dependency JARs: Packages all required classes into the DEX file

Package Unaligned APK

aapt package -f -m -F ${PROJECT_BASE_DIR}/build/app.unaligned.apk \
  -M ${PROJECT_BASE_DIR}/src/AndroidManifest.xml \
  -S ${PROJECT_BASE_DIR}/src/res \
  -S /path/to/appcompat-1.2.0/res \
  -S /path/to/core-1.3.0/res \
  -I ${ANDROID_SDK_HOME}/platforms/android-30/android.jar

Finalize APK (Add DEX, Align, Sign)

aapt add ${PROJECT_BASE_DIR}/build/app.unaligned.apk ${PROJECT_BASE_DIR}/build/classes.dex
zipalign -f 4 ${PROJECT_BASE_DIR}/build/app.unaligned.apk ${PROJECT_BASE_DIR}/build/app.aligned.apk
apksigner sign --ks Android-KeyStore.jks --in ${PROJECT_BASE_DIR}/build/app.aligned.apk --out ${PROJECT_BASE_DIR}/build/app.sign.apk

3. Key Notes

  • Dependency version matching: Always verify the correct dependency versions for your AppCompat release (check Maven Central for dependency trees) to avoid version conflicts.
  • Ant support: You can wrap these commands into an Ant build.xml using <exec> tasks if you prefer using Ant over raw command lines.
  • Legacy Support Library: If you're using the old non-AndroidX Support Library (e.g., com.android.support:appcompat-v7:28.0.0), the process is identical—just replace AndroidX dependencies with their Support Library equivalents and use the Android 28 platform JAR.

内容的提问来源于stack exchange,提问作者J J

火山引擎 最新活动