android 定制新产品】 定制新设备 (Configuring a New Product)

本文深入探讨了Android系统如何通过解析环境变量TARGET_PRODUCT来定制特定的设备,从解析Makefile到配置架构,再到具体配置分析,一步步揭示了Android设备定制的内部机制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


定制新设备

 一、Android如何编译特定product
  
  首先来看看Android Makefile是如何解析环境变量TARGET_PRODUCT的。
  Android Makefile 的引用关系是这样的:   Makefile  -> build/core/main.mk -> build/core/config.mk ->build/core/envsetup.mk   -> build/core/product_config.mk  在build/core/product_config.mk 中编译系统首先调用 build/core/product.mk中定义的函数  get-all-product-makefiles,来遍历整个device 的子目录, 找到device下所有的   AndroidProducts.mk, 不同子目录下的AndroidProducts.mk 中定义了不同的 PRODUCT_NAME,   PRODUCT_DEVICE 等信息,(我们也可以通过 打开build/core/product_config.mk中的#$(dump-  products)语句使控制台编译的时候输出所有product 的信息),接着  build/core/product_config.mk 会调用resolve-short-product-name将TARGET_PRODUCT匹配的  AndroidProducts.mk 中定义的 PRODUCT_DEVICE 赋值给TARGET_DEVICE。有了这个  TARGET_DEVICE, 再回到 build/core/config.mk,  会include $(TARGET_DEVCIE)/BoardConfig.mk       board_config_mk := \
       $(strip $(wildcard \
       $(SRC_TARGET_DIR)/board/$(TARGET_DEVICE)/BoardConfig.mk \
       vendor/*/$(TARGET_DEVICE)/BoardConfig.mk \
      ))
     include $(board_config_mk)   而这个配置文件BoardConfig.mk 决定了目标系统编译属性,比如使用ALSA还是不是 GENERIC_AUDIO   等等, 另外在这里TARGET_DEVICE 宏也决定了TARGET_DEVICE_DIR, 因为TARGET_DEVICE_DIR 取的  是上面提到的BoardConfig.mk 的路径。
     TARGET_DEVICE_DIR := $(patsubst %/,%,$(dir $(board_config_mk)))  当然Android 的Ob目标输出也是由TARGET_DEVICE决定,见build/core/envsetup.mk       TARGET_OUT_ROOT_release := $(OUT_DIR)/target
     TARGET_OUT_ROOT_debug := $(DEBUG_OUT_DIR)/target
     TARGET_OUT_ROOT := $(TARGET_OUT_ROOT_$(TARGET_BUILD_TYPE))     TARGET_PRODUCT_OUT_ROOT := $(TARGET_OUT_ROOT)/product
     PRODUCT_OUT := $(TARGET_PRODUCT_OUT_ROOT)/$(TARGET_DEVICE)   再回到 build/core/main.mk, 编译系统接着做的一个件事情是,遍历所有字目录,找到所有  Android.mk文件,并将这些Android.mk文件include 进来      #
     # Typical build; include any Android.mk files we can find.
     #     subdir_makefiles := \
      $(shell build/tools/findleaves.py --prune=out --prune=.repo --prune=.git $(subdirs) Android.mk)     include $(subdir_makefiles)   再来看其中的./build/target/board/Android.mk,对了它引用了     include $(TARGET_DEVICE_DIR)/AndroidBoard.mk   由上面TARGET_DEVICE_DIR的定义,这下又进入了device下TARGET_DEVICE指向的目录了,这个mk文件  中定义了特定Product需要编译和安装的app 和 script.
二、创建新设备的配置架构
    
  device-|
         |-<company_name>-|
                          |-<board_name>-|
                                         |-AndroidBoard.mk
                                         |-BoardConfig.mk                      
                          |-products-|
                                     |-AndroidProducts.mk
                                     |-<first_product_name>.mk
                                     |-<second_product_name>.mk
  三、具体配置分析
  
  1、AndroidProducts.mk
    在AndroidProducts.mk中只会包括PRODUCT_MAKEFILES变量,一般格式如下:
    PRODUCT_MAKEFILES := \
      $(LOCAL_DIR)/<first_product_name>.mk
      
  2、<first_product_name>.mk
    在<first_product_name>.mk中一般包括如下配置:
    $(call inherit-product, &(SRC_TARGET_DIR)/product/languages_full.mk)
      
    PRODUCT_BRAND := freescale
    PRODUCT_MANUFACTUER := freescale
    PRODUCT_NAME := <first_product_name>
    PRODUCT_DEVICE := <first_product_name>
    PRODUCT_COPY_FILE += \
          device/mine/mine_mx53/init.rc:root/init.freescale.rc
    如果选择通用package的话,添加generic.mk:
    $(call inherit-product, &(SRC_TARGET_DIR)/product/generic.mk)
    如果要重新选择安装的package,需要重定义
      PRODUCT_PACKAGES := \
          Music \
          Email \
          ...
    
  3、AndroidBoard.mk
    一般会在此添加Keypad的定义:
    LOCAL_PATH := $(call my-dir)
    file := $(TARGET_OUT_KEYLAYOUT)/mxckpd.kl
    ALL_PREBUILT += $(file)
    $(file): $(LOCAL_PATH)/mxckpd.kl | $(ACP)
         $(transform-prebuilt-to-target)
    include $(CLEAR_VARS)
    LOCAL_SRC_FILES := mxckpd.kcm
    include $(BUILD_KEY_CHAR_MAP)
    
  4、BoardConfig.mk
    一般在此定义board的特性,例如:
    TARGET_CPU_ABI := armeabi
    TARGET_NO_BOOTLOADER := true
    TARGET_NO_KERNEL := true
    TARGET_PROVIDES_INIT_RC := true
    BOARD_HAVE_WIFI := true
    BOARD_HAVE_MODEM := true
    BOARD_USES_ALSA_AUDIO := true
### Latest Android Studio Project Structure Version In the latest versions of Android Studio, the project structure has undergone significant changes to improve modularity and dependency management. When creating or configuring an Android project, developers must ensure that all settings align with modern standards. The **Project Structure** can be accessed via `File -> Project Structure` within Android Studio[^3]. This dialog allows users to configure various aspects such as modules, SDKs, libraries, and build configurations. Below is a detailed breakdown: #### Key Components of Modern Android Studio Project Structure 1. **Module Configuration**: Each module represents a distinct component (e.g., app, library). Modules are defined under the `settings.gradle` file. ```gradle include ':app', ':library' ``` 2. **Build Gradle Files**: The primary configuration files for building projects have been updated significantly over time. For instance: - In older versions, dependencies were declared using `compile`. However, this keyword was deprecated starting from Android Studio 3.0[^4]. - Newer versions use `implementation`, which optimizes how dependencies are included during compilation. ```gradle implementation 'com.android.support:appcompat-v7:28.0.0' // Example for support library ``` 3. **Java Version Compatibility**: To specify the Java version used by your project, you need to adjust the `sourceCompatibility` and `targetCompatibility` properties inside the `build.gradle` file[^2]: ```gradle android { ... compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } } ``` 4. **SDK Path Validation**: If there’s any issue related to missing SDK directories, verify whether the correct path is specified in the global configuration (`local.properties`) or through environment variables. Ensure the following line points correctly: ``` sdk.dir=/path/to/your/android/sdk ``` 5. **Restarting Android Studio**: After making critical updates like changing JDK paths or resolving plugin issues, restarting Android Studio ensures proper initialization without conflicts[^1]. By adhering to these guidelines, one can successfully manage their Android development environments while leveraging advanced features introduced in recent releases. ```gradle // Sample minimalistic setup demonstrating key elements discussed above apply plugin: 'com.android.application' android { compileSdkVersion 33 defaultConfig { applicationId "com.example.myapp" minSdkVersion 21 targetSdkVersion 33 versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } } dependencies { implementation 'androidx.appcompat:appcompat:1.6.1' } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值