Android 源码编译更换平台签名

简介: Android 源码编译更换平台签名

平台


Android 7.1 + RK3288


环境


当前的签名文件如下:


build/target/product/security/
-rw-rw-r-- 1 anson anson  260 6月  19  2018 Android.mk
-rw-rw-r-- 1 anson anson 1675 6月  19  2018 media.pem
-rw-rw-r-- 1 anson anson 1217 6月  19  2018 media.pk8
-rw-rw-r-- 1 anson anson 1440 6月  19  2018 media.x509.pem
-rw-rw-r-- 1 anson anson 1675 6月  19  2018 platform.pem
-rw-rw-r-- 1 anson anson 1216 6月  19  2018 platform.pk8
-rw-rw-r-- 1 anson anson 1440 6月  19  2018 platform.x509.pem
-rw-rw-r-- 1 anson anson 3123 6月  19  2018 README
-rw-rw-r-- 1 anson anson 1679 6月  19  2018 shared.pem
-rw-rw-r-- 1 anson anson 1218 6月  19  2018 shared.pk8
-rw-rw-r-- 1 anson anson 1440 6月  19  2018 shared.x509.pem
-rw-rw-r-- 1 anson anson 1675 6月  19  2018 testkey.pem
-rw-rw-r-- 1 anson anson 1216 6月  19  2018 testkey.pk8
-rw-rw-r-- 1 anson anson 1440 6月  19  2018 testkey.x509.pem
-rw-rw-r-- 1 anson anson  524 6月  19  2018 verity_key
-rw-rw-r-- 1 anson anson 1219 6月  19  2018 verity.pk8
-rw-rw-r-- 1 anson anson 1444 6月  19  2018 verity.x509.pem


对应的几个密钥的用途:

testkey   # 普通APK,默认情况下使用
platform  # 该APK完成一些系统的核心功能,这种方式编译出来的APK所在进程的UID为system
shared    # 该APK是media/download系统中的一环
media     # 该APK是media/download系统中的一环


build/core/main.mk
include $(BUILD_SYSTEM)/config.mk


build/core/config.mk
include $(BUILD_SYSTEM)/envsetup.mk
DEFAULT_SYSTEM_DEV_CERTIFICATE := build/target/product/security/testkey


testkey 不仅会影响应用的签名, 同时还有打包的固件OTA升级包等.


实现


最简单的方案, 替换掉 build/target/product/security 相关的密钥文件


自定义密钥名, 同时修改Makefile 和 相应的.MK文件


build/core/config.mk: 中DEFAULT_SYSTEM_DEV_CERTIFICATE := build/target/product/security/mykey

build/core/Makefile: ifeq ($(DEFAULT_SYSTEM_DEV_CERTIFICATE),build/target/product/security/mykey)

关于apk签名,可通过修改Android.mk中的声明修改签名:


LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := optional
LOCAL_SRC_FILES := $(call all-subdir-java-files)
LOCAL_PACKAGE_NAME := Provision
LOCAL_CERTIFICATE := mykey
## 也可定义绝对路径:
#LOCAL_CERTIFICATE := build/target/product/security/mykey/mykey
LOCAL_PRIVILEGED_MODULE := true
LOCAL_PROGUARD_FLAG_FILES := proguard.flags
include $(BUILD_PACKAGE)


确认build/target/product/security/mykey相关文件存在, 否则会出现:


ninja: error: 'build/target/product/security/mykey.pk8', needed by 'out/target/product/rk3288/obj/APPS/Provision_intermediates/package.apk', missing and no known rule to make it


build/core/package_internal.mk

# Pick a key to sign the package with.  If this package hasn't specified
# an explicit certificate, use the default.
# Secure release builds will have their packages signed after the fact,
# so it's ok for these private keys to be in the clear.
ifeq ($(LOCAL_CERTIFICATE),)
    LOCAL_CERTIFICATE := $(DEFAULT_SYSTEM_DEV_CERTIFICATE)
endif
ifeq ($(LOCAL_CERTIFICATE),EXTERNAL)
  # The special value "EXTERNAL" means that we will sign it with the
  # default devkey, apply predexopt, but then expect the final .apk
  # (after dexopting) to be signed by an outside tool.
  LOCAL_CERTIFICATE := $(DEFAULT_SYSTEM_DEV_CERTIFICATE)
  PACKAGES.$(LOCAL_PACKAGE_NAME).EXTERNAL_KEY := 1
endif
# If this is not an absolute certificate, assign it to a generic one.
ifeq ($(dir $(strip $(LOCAL_CERTIFICATE))),./)
    LOCAL_CERTIFICATE := $(dir $(DEFAULT_SYSTEM_DEV_CERTIFICATE))$(LOCAL_CERTIFICATE)
endif
private_key := $(LOCAL_CERTIFICATE).pk8
certificate := $(LOCAL_CERTIFICATE).x509.pem

从生成的中间ninja->out/build-rk3288-mmm-packages_apps_Provision_Android.mk.ninja

description = target Package: Provision (out/target/product/rk3288/obj/APPS/Provision_intermediates/package.apk)
 command = /bin/bash -c 
  "(touch out/target/product/rk3288/obj/APPS/Provision_intermediates/zipdummy ) 
  && ((cd out/target/product/rk3288/obj/APPS/Provision_intermediates/ 
  && jar cf package.apk zipdummy) ) 
  && (zip -qd out/target/product/rk3288/obj/APPS/Provision_intermediates/package.apk zipdummy ) 
  && (rm out/target/product/rk3288/obj/APPS/Provision_intermediates/zipdummy ) 
  && (out/host/linux-x86/bin/aapt 
  package 
  -u 
  -z  
  --pseudo-localize 
  -c zh_CN,en_US,cs_CZ,da_DK,de_AT,de_CH,de_DE,de_LI,el_GR,en_AU,en_CA,en_GB,en_NZ,en_SG,eo_EU,es_ES,fr_CA,fr_CH,fr_BE,fr_FR,it_CH,it_IT,ja_JP,ko_KR,nb_NO,nl_BE,nl_NL,pl_PL,pt_PT,ru_RU,sv_SE,tr_TR,zh_CN,zh_HK,zh_TW,am_ET,hi_IN,en_US,en_AU,en_IN,fr_FR,it_IT,es_ES,et_EE,de_DE,nl_NL,cs_CZ,pl_PL,ja_JP,zh_TW,zh_CN,zh_HK,ru_RU,ko_KR,nb_NO,es_US,da_DK,el_GR,tr_TR,pt_PT,pt_BR,sv_SE,bg_BG,ca_ES,en_GB,fi_FI,hi_IN,hr_HR,hu_HU,in_ID,iw_IL,lt_LT,lv_LV,ro_RO,sk_SK,sl_SI,sr_RS,uk_UA,vi_VN,tl_PH,ar_EG,fa_IR,th_TH,sw_TZ,ms_MY,af_ZA,zu_ZA,am_ET,en_XA,ar_XB,fr_CA,km_KH,lo_LA,ne_NP,si_LK,mn_MN,hy_AM,az_AZ,ka_GE,my_MM,mr_IN,ml_IN,is_IS,mk_MK,ky_KG,eu_ES,gl_ES,bn_BD,ta_IN,kn_IN,te_IN,uz_UZ,ur_PK,kk_KZ,sq_AL,gu_IN,pa_IN,be_BY,bs_BA  
  -M packages/apps/Provision/AndroidManifest.xml   
  -I out/target/common/obj/APPS/framework-res_intermediates/package-export.apk 
  --min-sdk-version 25 
  --target-sdk-version 25 
  --product tablet 
  --version-code 25 
  --version-name 7.1.2   
  --skip-symbols-without-default-localization 
  -F out/target/product/rk3288/obj/APPS/Provision_intermediates/package.apk ) 
  && (find out/target/common/obj/APPS/Provision_intermediates/ 
    -maxdepth 1 
    -name \"classes*.dex\" | sort | xargs zip -qjX out/target/product/rk3288/obj/APPS/Provision_intermediates/package.apk ) 
    && (if [ -d out/target/common/obj/APPS/Provision_intermediates/jack-rsc ] ; 
    then find out/target/common/obj/APPS/Provision_intermediates/jack-rsc -type f |
    sort | sed -e \"s?^out/target/common/obj/APPS/Provision_intermediates/jack-rsc/? 
    -C \\\"out/target/common/obj/APPS/Provision_intermediates/jack-rsc\\\" \\\"?\" -e \"s/\$$/\\\"/\" > out/target/product/rk3288/obj/APPS/Provision_intermediates/jack_res_jar_flags; 
    if [ -s out/target/product/rk3288/obj/APPS/Provision_intermediates/jack_res_jar_flags ] ; 
    then jar uf out/target/product/rk3288/obj/APPS/Provision_intermediates/package.apk 
    @out/target/product/rk3288/obj/APPS/Provision_intermediates/jack_res_jar_flags; fi; fi ) 
    && (mv out/target/product/rk3288/obj/APPS/Provision_intermediates/package.apk out/target/product/rk3288/obj/APPS/Provision_intermediates/package.apk.unsigned ) 
    && (java -Djava.library.path=out/host/linux-x86/lib64 -jar out/host/linux-x86/framework/signapk.jar --min-sdk-version \
    $$((out/host/linux-x86/bin/aapt dump badging out/target/product/rk3288/obj/APPS/Provision_intermediates/package.apk.unsigned 2>&1 
    | grep '^sdkVersion' || echo \"sdkVersion:'0'\") | cut -d\"'\" -f2 | sed -e s/^.*[^0-9].*\$$/25/) 
    build/target/product/security/mykey.x509.pem 
    build/target/product/security/mykey.pk8  
    out/target/product/rk3288/obj/APPS/Provision_intermediates/package.apk.unsigned 
    out/target/product/rk3288/obj/APPS/Provision_intermediates/package.apk.signed ) 
    && (mv out/target/product/rk3288/obj/APPS/Provision_intermediates/package.apk.signed out/target/product/rk3288/obj/APPS/Provision_intermediates/package.apk )"


可以看出, 编译系统会去找build/target/product/security/mykey.x509.pem, build/target/product/security/mykey.pk8


扩展


android系统release签名

Android OTA releasekey 替换

Android系统build阶段签名机制

Manually generating keys 创建密钥


方法1:

development/tools/make_key testkey  '/C=US/ST=California/L=Mountain View/O=Android/OU=Android/CN=Android/emailAddress=android@android.com'


方法2:

# generate RSA key
openssl genrsa -3 -out temp.pem 2048
Generating RSA private key, 2048 bit long modulus
....+++
.....................+++
e is 3 (0x3)
# create a certificate with the public part of the key
openssl req -new -x509 -key temp.pem -out releasekey.x509.pem -days 10000 -subj '/C=US/ST=California/L=San Narciso/O=Yoyodyne, Inc./OU=Yoyodyne Mobility/CN=Yoyodyne/emailAddress=yoyodyne@example.com'
# create a PKCS#8-formatted version of the private key
openssl pkcs8 -in temp.pem -topk8 -outform DER -out releasekey.pk8 -nocrypt
# securely delete the temp.pem file
shred --remove temp.pem


Android签名生成和互转


打印编译时的环境变量:

build/core/dumpvar.mk
print_build_config_vars := \
  PLATFORM_VERSION_CODENAME \
  PLATFORM_VERSION \
  TARGET_PRODUCT \
  TARGET_BUILD_VARIANT \
  TARGET_BUILD_TYPE \
  TARGET_BUILD_APPS \
  TARGET_ARCH \
  TARGET_ARCH_VARIANT \
  TARGET_CPU_VARIANT \
  TARGET_2ND_ARCH \
  TARGET_2ND_ARCH_VARIANT \
  TARGET_2ND_CPU_VARIANT \
  HOST_ARCH \
  HOST_2ND_ARCH \
  HOST_OS \
  HOST_OS_EXTRA \
  HOST_CROSS_OS \
  HOST_CROSS_ARCH \
  HOST_CROSS_2ND_ARCH \
  HOST_BUILD_TYPE \
  BUILD_ID \
  OUT_DIR
ifneq ($(filter report_config,$(DUMP_MANY_VARS)),)
# Construct the shell commands that print the config banner.
report_config_sh := echo '============================================';
report_config_sh += $(foreach v,$(print_build_config_vars),echo '$v=$($(v))';)
report_config_sh += echo '============================================';
endif


============================================
PLATFORM_VERSION_CODENAME=REL
PLATFORM_VERSION=7.1.2
TARGET_PRODUCT=rk3288
TARGET_BUILD_VARIANT=userdebug
TARGET_BUILD_TYPE=release
TARGET_BUILD_APPS=
TARGET_ARCH=arm
TARGET_ARCH_VARIANT=armv7-a-neon
TARGET_CPU_VARIANT=cortex-a15
TARGET_2ND_ARCH=
TARGET_2ND_ARCH_VARIANT=
TARGET_2ND_CPU_VARIANT=
HOST_ARCH=x86_64
HOST_2ND_ARCH=x86
HOST_OS=linux
HOST_OS_EXTRA=Linux-4.15.0-64-generic-x86_64-with-Ubuntu-16.04-xenial
HOST_CROSS_OS=windows
HOST_CROSS_ARCH=x86
HOST_CROSS_2ND_ARCH=x86_64
HOST_BUILD_TYPE=release
BUILD_ID=NHG47K
OUT_DIR=out
BUILD_SYSTEM=build/core
============================================


build/core/Makefile

# The "test-keys" tag marks builds signed with the old test keys,
# which are available in the SDK.  "dev-keys" marks builds signed with
# non-default dev keys (usually private keys from a vendor directory).
# Both of these tags will be removed and replaced with "release-keys"
# when the target-files is signed in a post-build step.
ifeq ($(DEFAULT_SYSTEM_DEV_CERTIFICATE),build/target/product/security/testkey)
BUILD_KEYS := test-keys
else
BUILD_KEYS := dev-keys
endif
ifeq ($(TARGET_BUILD_VARIANT),user)
BUILD_KEYS := release-keys
endif


判断编译所使用的密钥, 并会显示设置 > 关于设备 > 版本号末尾, 如:XXX-userdebug 7.1.2 NHG47K eng.XXX.20191125.144140 test-keys


相关文章
|
2月前
|
Java Android开发 Swift
安卓与iOS开发对比:平台选择对项目成功的影响
【10月更文挑战第4天】在移动应用开发的世界中,选择合适的平台是至关重要的。本文将深入探讨安卓和iOS两大主流平台的开发环境、用户基础、市场份额和开发成本等方面的差异,并分析这些差异如何影响项目的最终成果。通过比较这两个平台的优势与挑战,开发者可以更好地决定哪个平台更适合他们的项目需求。
114 1
|
3月前
|
IDE Android开发 iOS开发
探索Android与iOS开发的差异:平台选择对项目成功的影响
【9月更文挑战第27天】在移动应用开发的世界中,Android和iOS是两个主要的操作系统平台。每个系统都有其独特的开发环境、工具和用户群体。本文将深入探讨这两个平台的关键差异点,并分析这些差异如何影响应用的性能、用户体验和最终的市场表现。通过对比分析,我们将揭示选择正确的开发平台对于确保项目成功的重要作用。
|
2月前
|
Linux API 开发工具
FFmpeg开发笔记(五十九)Linux编译ijkplayer的Android平台so库
ijkplayer是由B站研发的移动端播放器,基于FFmpeg 3.4,支持Android和iOS。其源码托管于GitHub,截至2024年9月15日,获得了3.24万星标和0.81万分支,尽管已停止更新6年。本文档介绍了如何在Linux环境下编译ijkplayer的so库,以便在较新的开发环境中使用。首先需安装编译工具并调整/tmp分区大小,接着下载并安装Android SDK和NDK,最后下载ijkplayer源码并编译。详细步骤包括环境准备、工具安装及库编译等。更多FFmpeg开发知识可参考相关书籍。
93 0
FFmpeg开发笔记(五十九)Linux编译ijkplayer的Android平台so库
|
2月前
|
编译器 Android开发
配置环境变量,使CMakeLists.txt可直接使用Android NDK工具链编译项目
配置环境变量,使CMakeLists.txt可直接使用Android NDK工具链编译项目
|
2月前
|
Ubuntu Shell API
Ubuntu 64系统编译android arm64-v8a 的openssl静态库libssl.a和libcrypto.a
Ubuntu 64系统编译android arm64-v8a 的openssl静态库libssl.a和libcrypto.a
|
3月前
|
监控 Android开发 iOS开发
深入探索安卓与iOS的系统架构差异:理解两大移动平台的技术根基在移动技术日新月异的今天,安卓和iOS作为市场上最为流行的两个操作系统,各自拥有独特的技术特性和庞大的用户基础。本文将深入探讨这两个平台的系统架构差异,揭示它们如何支撑起各自的生态系统,并影响着全球数亿用户的使用体验。
本文通过对比分析安卓和iOS的系统架构,揭示了这两个平台在设计理念、安全性、用户体验和技术生态上的根本区别。不同于常规的技术综述,本文以深入浅出的方式,带领读者理解这些差异是如何影响应用开发、用户选择和市场趋势的。通过梳理历史脉络和未来展望,本文旨在为开发者、用户以及行业分析师提供有价值的见解,帮助大家更好地把握移动技术发展的脉络。
96 6
|
3月前
|
开发工具 Android开发 iOS开发
安卓与iOS开发环境对比:选择适合你的平台
【9月更文挑战第26天】在移动应用开发的广阔天地中,安卓和iOS是两大巨头。它们各自拥有独特的优势和挑战,影响着开发者的选择和决策。本文将深入探讨这两个平台的开发环境,帮助你理解它们的核心差异,并指导你根据个人或项目需求做出明智的选择。无论你是初学者还是资深开发者,了解这些平台的异同都至关重要。让我们一起探索,找到最适合你的那片开发天地。
|
3月前
|
Android开发 开发者
Android平台无纸化同屏如何实现实时录像功能
Android平台无纸化同屏,如果需要本地录像的话,实现难度不大,只要复用之前开发的录像模块的就可以,对我们来说,同屏采集这块,只是数据源不同而已,如果是自采集的其他数据,我们一样可以编码录像。
|
3月前
|
安全 API 开发工具
Android平台RTMP推送|轻量级RTSP服务如何实现麦克风|扬声器声音采集切换
Android平台扬声器播放声音的采集,在无纸化同屏等场景下,意义很大,早期低版本的Android设备,是没法直接采集扬声器audio的(从Android 10开始支持),所以,如果需要采集扬声器audio,需要先做系统版本判断,添加相应的权限。
|
3月前
|
编解码 开发工具 Android开发
Android平台实现屏幕录制(屏幕投影)|音频播放采集|麦克风采集并推送RTMP或轻量级RTSP服务
Android平台屏幕采集、音频播放声音采集、麦克风采集编码打包推送到RTMP和轻量级RTSP服务的相关技术实现,做成高稳定低延迟的同屏系统,还需要有配套好的RTMP、RTSP直播播放器