基于android build system 的APP编译

本文介绍如何在Android系统中编译简单的HelloWorld应用及系统级应用。通过创建特定文件夹和编写必要的代码,配合Android.mk文件定义编译规则,可以在Android环境中成功编译并安装应用程序。

-------------------------------------------------------------------------------------
编译自己的测试APP(helloworld)
在system文件夹下建立一个helloworld文件夹,编辑一个helloworld.cpp文件
代码如下:
#include
int main(int argc, char argv[])
{
printf("hello world! --- by carey");
}
另外建立一个Android.mk 文件,内容如下:
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES:=                                      /
helloworld.cpp
LOCAL_MODULE:= helloworld
LOCAL_STATIC_LIBRARIES := libcutils libc
include $(BUILD_EXECUTABLE)
在源码文件夹输入make helloworld
root@ubuntu810:/mnt/newdisk/android22# make helloworld
============================================
PLATFORM_VERSION_CODENAME=REL
PLATFORM_VERSION=2.2.1
TARGET_PRODUCT=generic
TARGET_BUILD_VARIANT=eng
TARGET_SIMULATOR=
TARGET_BUILD_TYPE=release
TARGET_BUILD_APPS=
TARGET_ARCH=arm
HOST_ARCH=x86
HOST_OS=linux
HOST_BUILD_TYPE=release
BUILD_ID=MASTER
============================================
target thumb C++: helloworld <= system/helloworld/helloworld.cpp
system/helloworld/helloworld.cpp:3: warning: second argument of 'int main(int, char*)' should be 'char **'
Notice file: external/elfcopy/NOTICE -- out/host/linux-x86/obj/NOTICE_FILES/src//lib/libelfcopy.a.txt
target Prelink: libdl (out/target/product/generic/symbols/system/lib/libdl.so)
target Strip: libdl (out/target/product/generic/obj/lib/libdl.so)
target SharedLib: libc
(out/target/product/generic/obj/SHARED_LIBRARIES/libc_intermediates/LINKED/libc.so)
target Prelink: libc (out/target/product/generic/symbols/system/lib/libc.so)
libelfcopy: Warning: DW_FORM_data8 is unsupported when sizeof (unsigned long) != 8
libelfcopy: Warning: Range lists in .debug_info section aren't in ascending order!
target Strip: libc (out/target/product/generic/obj/lib/libc.so)
target SharedLib: libstdc++
(out/target/product/generic/obj/SHARED_LIBRARIES/libstdc++_intermediates/LINKED/libstdc++.so)
target Prelink: libstdc++ (out/target/product/generic/symbols/system/lib/libstdc++.so)
target Strip: libstdc++ (out/target/product/generic/obj/lib/libstdc++.so)
target SharedLib: libm
(out/target/product/generic/obj/SHARED_LIBRARIES/libm_intermediates/LINKED/libm.so)
target Prelink: libm (out/target/product/generic/symbols/system/lib/libm.so)
libelfcopy: Warning: Range lists in .debug_info section aren't in ascending order!
target Strip: libm (out/target/product/generic/obj/lib/libm.so)
prebuilt/linux-x86/toolchain/arm-eabi-4.4.0/bin/arm-eabi-gcc -mthumb-interwork -o
out/target/product/generic/obj/lib/crtend_android.o -c bionic/libc/arch-arm/bionic/crtend.S
target Executable: helloworld
(out/target/product/generic/obj/EXECUTABLES/helloworld_intermediates/LINKED/helloworld)
target Non-prelinked: helloworld (out/target/product/generic/symbols/system/bin/helloworld)
target Strip: helloworld
(out/target/product/generic/obj/EXECUTABLES/helloworld_intermediates/helloworld)
Notice file: bionic/libc/NOTICE -- out/target/product/generic/obj/NOTICE_FILES/src//system/lib/libc.so.txt
Notice file: bionic/libdl/NOTICE -- out/target/product/generic/obj/NOTICE_FILES/src//system/lib/libdl.so.txt
Install: out/target/product/generic/system/lib/libdl.so
Notice file: bionic/libc/NOTICE --
out/target/product/generic/obj/NOTICE_FILES/src//system/lib/libc_common.a.txt
Install: out/target/product/generic/system/lib/libc.so
Notice file: bionic/libstdc++/NOTICE --
out/target/product/generic/obj/NOTICE_FILES/src//system/lib/libstdc++.so.txt
Install: out/target/product/generic/system/lib/libstdc++.so
Notice file: bionic/libm/NOTICE -- out/target/product/generic/obj/NOTICE_FILES/src//system/lib/libm.so.txt
Install: out/target/product/generic/system/lib/libm.so
Notice file: system/core/liblog/NOTICE --
out/target/product/generic/obj/NOTICE_FILES/src//system/lib/liblog.a.txt
Notice file: system/core/libcutils/NOTICE --
out/target/product/generic/obj/NOTICE_FILES/src//system/lib/libcutils.a.txt
Notice file: bionic/libc/NOTICE -- out/target/product/generic/obj/NOTICE_FILES/src//system/lib/libc.a.txt
Install: out/target/product/generic/system/bin/helloworld
编译系统就会自动输出一个编译好的helloworld程序,并且放置在out/target/product/generic/system/bin/
文件夹中
文件属性:
root@ubuntu810:/mnt/newdisk/android22/out/target/product/generic/system/bin# file helloworld
helloworld: ELF 32-bit LSB executable, ARM, version 1 (SYSV), dynamically linked (uses shared libs),
stripped
root@ubuntu810:/mnt/newdisk/android22/out/target/product/generic/system/bin# ./helloworld
bash: ./helloworld: cannot execute binary file
-------------------------------------------------------------------------------------
编译System级别的应用APP
1. 在eclipse开发环境中建立android工程项目
2. 修改AndroidManifest.xml文件,在manifest中加入android:sharedUserId="android.uid.system"
3. 建立Android.mk文件,内容如下:
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := optional
LOCAL_SRC_FILES := $(call all-java-files-under, src)
LOCAL_PACKAGE_NAME := (此处为android工程名)
LOCAL_CERTIFICATE := platform
include $(BUILD_PACKAGE)
4. 将该android工程放入到android源码的packages/apps/目录下
5. 在android源码目录下执行make (android工程名)
6. 编译完成后就会将apk生成到
out/target/product/generic/system/app/(android工程名).apk
-------------------------------------------------------------------------------------
Add a new component into open source Android
The android build system is complicated. It is not easy to create a simple build script to build single
component.
But it is much easier to add a new component into the build system. Here is a sample to build the "Hello
world!"
for Android, and it is dynamic linked executable.
1. create a directory for the new component in $(YOUR_ANDROID)/external folder:
$(YOUR_ANDROID)/external/hello
2. put the hello.c into the directory: $(YOUR_ANDROID)/external/hello/
3. create Android.mk in $(YOUR_ANDROID)/external/hello/ directory, the Android.mk likes the following:
LOCAL_PATH:=$(call my-dir)
include $(CLEAR_VARS)
LOCAL_ARM_MODE := arm
LOCAL_SRC_FILES:= /
hello.c
LOCAL_MODULE := hello
include $(BUILD_EXECUTABLE)
4. add the following line into $(BUILD_SYSTEM)/main.mk,$(BUILD_SYSTEM) is in
$(YOUR_ANDROID)/build/core:
external/hello
5. launch the build command in $(YOUR_ANDROID): make hello
you will have the build target hello in $(YOUR_ANDROID)/out/target/product/generic/system/bin.
You can use include $(BUILD_SHARED_LIBRARY) to create shared library.
Find more in $(YOUR_ANDROID)/build/core/config.mk.

<think>好的,用户想了解如何在Android Studio中设置编译APP的相关配置。我需要根据提供的引用资料来整理回答。首先,用户提到的引用内容主要涉及Android Studio的安装、Gradle配置、多渠道打包等。应该分步骤讲解编译配置的设置,包括项目结构、Gradle文件、构建变体、签名配置和依赖管理。 首先,项目结构部分,用户需要知道如何配置模块和SDK路径。引用[1]提到Android Studio基于IntelliJ IDEA,所以项目结构类似,可以指导用户进入Project Structure设置SDK和JDK路径。 接下来是Gradle配置,引用[4]指出Gradle的重要性,特别是build.gradle文件中的compileSdkVersion、buildToolsVersion等参数。需要详细说明每个参数的作用,比如minSdkVersion和targetSdkVersion的区别,以及如何设置版本代码和名称。 构建变体部分,引用[5]提到多渠道打包,所以需要解释productFlavors的用法,如何配置不同渠道的参数,并引用示例代码。同时,构建类型如debug和release也需要说明,比如启用混淆和签名配置。 签名配置是关键步骤,用户需要生成签名密钥并配置到Gradle中。引用[4]提到Gradle的任务驱动特性,这里可以指导用户使用keytool生成密钥,并在signingConfigs中设置。同时提醒将签名信息放在local.properties中以提高安全性。 依赖管理部分,引用[3]提到com.android.application和library插件,需要说明如何添加本地和远程依赖,以及项目模块依赖。比如implementation语句的使用和不同依赖配置的区别。 最后,多渠道打包的实现,结合引用[5],说明如何在productFlavors中定义不同渠道,并动态替换参数。示例代码中的manifestPlaceholders和versionNameSuffix是重点。 检查是否需要其他配置,比如ProGuard规则或JNI支持,但根据引用资料,可能不需要深入。确保步骤清晰,每个部分都有示例代码块,并正确引用来源。需要验证Gradle语法是否正确,比如signingConfigs的配置和buildTypes的关联。 可能用户的实际问题包括如何找到build.gradle文件,如何生成签名密钥,以及如何处理依赖冲突。回答中应涵盖这些常见问题,但根据现有资料,可能主要围绕配置步骤展开。确保语言口语化,避免技术术语过多,但必要的术语需要保留并解释。例如,解释productFlavors和buildTypes的区别,以及为什么需要签名配置。 最后,生成相关问题,考虑用户可能想了解的下一步骤,比如性能优化、构建加速或特定错误处理,但根据引用资料,问题可能围绕配置的更多细节、多渠道打包的扩展或其他Gradle插件功能。</think>### Android Studio 编译 APP 配置详解 #### 一、项目结构配置 1. **模块配置** Android Studio 中每个模块对应一个组件(如 APP 模块、库模块)。通过 `File > Project Structure` 可配置 SDK 路径、NDK 版本等关键参数[^1]。 2. **SDK 管理** 在 `Appearance & Behavior > System Settings > Android SDK` 中安装所需版本的 SDK 和构建工具,确保 `build.gradle` 中的 `compileSdkVersion` 与本地版本一致。 --- #### 二、Gradle 核心配置 在模块的 `build.gradle` 文件中设置核心参数: ```groovy android { compileSdkVersion 33 buildToolsVersion "30.0.3" defaultConfig { applicationId "com.example.myapp" minSdkVersion 21 targetSdkVersion 33 versionCode 1 versionName "1.0" } } ``` - `minSdkVersion`:APP 支持的最低 Android 版本 - `targetSdkVersion`:APP 适配的目标版本(影响系统兼容行为)[^4] --- #### 三、构建变体(Build Variants) 1. **构建类型(Build Types)** 默认包含 `debug` 和 `release` 两种类型,可在 `build.gradle` 中自定义: ```groovy buildTypes { release { minifyEnabled true // 启用代码混淆 proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } ``` 2. **多渠道打包(Product Flavors)** 通过 `productFlavors` 实现不同渠道配置(如应用图标、API 地址): ```groovy flavorDimensions "channel" productFlavors { free { dimension "channel" applicationIdSuffix ".free" } pro { dimension "channel" applicationIdSuffix ".pro" } } ``` 运行 `./gradlew assembleFreeRelease` 可生成免费渠道的正式包[^5]。 --- #### 四、签名配置(Signing Config) 1. **生成签名密钥** 使用 `keytool` 生成密钥: ```bash keytool -genkey -v -keystore my-release-key.jks -keyalg RSA -keysize 2048 -validity 10000 -alias my-alias ``` 2. **配置 Gradle** 在 `build.gradle` 中添加签名信息: ```groovy android { signingConfigs { release { storeFile file("my-release-key.jks") storePassword "your_password" keyAlias "my-alias" keyPassword "your_password" } } buildTypes { release { signingConfig signingConfigs.release } } } ``` > **注意**:建议将密码存储在 `local.properties` 中避免泄露。 --- #### 五、依赖管理 1. **本地依赖** 添加 JAR/AAR 文件: ```groovy implementation files('libs/mylibrary.jar') ``` 2. **远程仓库依赖** 通过 Maven Central 或 JitPack 引入: ```groovy implementation 'com.google.android.material:material:1.8.0' ``` 3. **模块依赖** 依赖其他模块: ```groovy implementation project(':mylibrary') ``` --- #### 六、高级配置示例 ```groovy android { // 动态配置渠道参数 productFlavors.all { flavor -> flavor.manifestPlaceholders = [CHANNEL_VALUE: name] flavor.versionNameSuffix "-${name}" } // 启用多 Dex 文件(解决 64K 方法数限制) defaultConfig { multiDexEnabled true } } ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值