移植App源码从Android Studio到系统源码编译

在项目管理中,我们有时候会有需要将Android Studio中的App源码移植到系统源码中编译,与直接将编译打包好的Apk内置到系统中不同,我们需要加上整个src代码和res资源和一些依赖。

Android Studio目录树

server@dev:~/code/app/ToolBox$ tree
.
├── Android.mk
├── app
│   └── src
│       ├── main
│       │   ├── AndroidManifest.xml
│       │   ├── java
│       │   ├── libs
│       │   └── res
├── build.gradle
├── gradle
├── gradle.properties
├── local.properties
└── settings.gradle

2 directories, 5 files

将App源码从Android Studio移植到系统源码编译,主要是添加Android.mk,指定src、res、AndroidManifest.xml文件,比较重要的点在于引入第三方依赖库,如jar包、aar包、so库等。以下列出重要的点:

  1. 引入第三方依赖库的方式
1.Android系统自带的库
LOCAL_STATIC_ANDROID_LIBRARIES
2.第三方jar包
LOCAL_STATIC_JAVA_LIBRARIES
3.第三方aar包
LOCAL_STATIC_JAVA_AAR_LIBRARIES
4.第三方so库
LOCAL_SHARED_LIBRARIES
  1. 引入aar包注意点
aar包与jar包的不同之处在于,aar包含有资源res

1.编译aar包 别名:路径
include $(CLEAR_VARS)
LOCAL_PREBUILT_STATIC_JAVA_LIBRARIES := xxpermissions:libs/xxpermissions-11.5.aar
include $(BUILD_MULTI_PREBUILT)

2.引用
LOCAL_STATIC_JAVA_AAR_LIBRARIES := xxpermissions

3.
LOCAL_USE_AAPT2 := true

4.将aar包中的res复制出来,加到 LOCAL_RESOURCE_DIR 下
LOCAL_RESOURCE_DIR := $(LOCAL_PATH)/res \
	$(LOCAL_PATH)/res-xxpermissions \

5.添加R资源生成别名,这样别人也可以用定义的这些包名访问本app资源
LOCAL_AAPT_FLAGS := \
  --auto-add-overlay \
  --extra-packages <aar package name>

6.引入第三方aar包最好关闭混淆,不然可能会出现运行时报错
LOCAL_PROGUARD_ENABLED := disabled   
  1. 关于如何查找App需要的依赖
1.查看build.gradle下的dependencies
dependencies {
    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'com.google.android.material:material:1.3.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
}
对应的添加
LOCAL_STATIC_ANDROID_LIBRARIES := \
	androidx.appcompat_appcompat \
	com.google.android.material_material \
	androidx-constraintlayout_constraintlayout \

2.对于第三库的库,可以从github上下载,或直接在Android Studio的Gradle中找到下载的依赖包
2.1 Android Studio以Project的视图模式来查看的时候点开External Libraries,这里面就是所有的compile的包了

2.2选择compile包右键点开选择Library Properity 查看sources路径
~/.gradle/caches/modules-2/files-2.1/com.github.getActivity/XXPermissions/

2.3就是这个包的位置 @aar就是找对应的aar包 无则表示找对应的jar包

Android系统目录树

server@dev:~/Desktop/ToolBox$ tree -L 1
.
├── AndroidManifest.xml
├── Android.mk
├── libs
├── res
├── res-camera2
├── res-core
├── res-view
└── src

6 directories, 2 files

Android.mk

LOCAL_PATH:= $(call my-dir)

include $(CLEAR_VARS)

LOCAL_PACKAGE_NAME := ToolBox

LOCAL_MODULE_TAGS := optional

LOCAL_SRC_FILES := $(call all-java-files-under, src)

LOCAL_RESOURCE_DIR := $(LOCAL_PATH)/res \
	$(LOCAL_PATH)/res-camera2 \
	$(LOCAL_PATH)/res-core \
	$(LOCAL_PATH)/res-view

LOCAL_USE_AAPT2 := true

LOCAL_STATIC_ANDROID_LIBRARIES := \
	androidx.appcompat_appcompat \
	com.google.android.material_material \
	androidx-constraintlayout_constraintlayout \
	androidx.gridlayout_gridlayout \
	android-support-v4

LOCAL_STATIC_JAVA_LIBRARIES := \
	systembartint \
	listenablefuture \
	concurrent-futures

LOCAL_STATIC_JAVA_AAR_LIBRARIES := \
     xxpermissions \
     camera-view \
     camera-camera2 \
     camera-lifecycle \
     camera-core

LOCAL_AAPT_FLAGS := \
	--auto-add-overlay \
	--extra-packages androidx.camera.core \
	--extra-packages androidx.camera.lifecycle \
	--extra-packages androidx.camera.view \
	--extra-packages androidx.camera.camera2 \
	--extra-packages com.hjq.permissions

LOCAL_PRIVATE_PLATFORM_APIS := true

LOCAL_CERTIFICATE := platform

LOCAL_PROGUARD_ENABLED := disabled

include $(BUILD_PACKAGE)

include $(CLEAR_VARS)
LOCAL_PREBUILT_STATIC_JAVA_LIBRARIES := xxpermissions:libs/xxpermissions-11.5.aar
include $(BUILD_MULTI_PREBUILT)

include $(CLEAR_VARS)
LOCAL_PREBUILT_STATIC_JAVA_LIBRARIES := systembartint:libs/systembartint-1.0.3.jar
include $(BUILD_MULTI_PREBUILT)

include $(CLEAR_VARS)
LOCAL_PREBUILT_STATIC_JAVA_LIBRARIES := camera-view:libs/camera-view-1.0.0-alpha14.aar
include $(BUILD_MULTI_PREBUILT)

include $(CLEAR_VARS)
LOCAL_PREBUILT_STATIC_JAVA_LIBRARIES := camera-lifecycle:libs/camera-lifecycle-1.0.0-beta07.aar
include $(BUILD_MULTI_PREBUILT)

include $(CLEAR_VARS)
LOCAL_PREBUILT_STATIC_JAVA_LIBRARIES := camera-camera2:libs/camera-camera2-1.0.0-beta07.aar
include $(BUILD_MULTI_PREBUILT)

include $(CLEAR_VARS)
LOCAL_PREBUILT_STATIC_JAVA_LIBRARIES := camera-core:libs/camera-core-1.0.0-beta07.aar
include $(BUILD_MULTI_PREBUILT)

include $(CLEAR_VARS)
LOCAL_PREBUILT_STATIC_JAVA_LIBRARIES := listenablefuture:libs/listenablefuture-1.0.jar
include $(BUILD_MULTI_PREBUILT)

include $(CLEAR_VARS)
LOCAL_PREBUILT_STATIC_JAVA_LIBRARIES := concurrent-futures:libs/concurrent-futures-1.0.0.jar
include $(BUILD_MULTI_PREBUILT)

include $(call all-makefiles-under, $(LOCAL_PATH))

你可能感兴趣的:(Android,android,gradle,aar)