前言
Android开发中会遇到这样的需求,即修改某个开源依赖的aar包的部分代码,以满足特定的要求或者功能。下面介绍下.aar包的编译流程以及编译过程中遇到的一些问题
编译流程
- 根据待修改的组件/依赖,下载该组件的源码工程(一般是在Github上)
- 使用Android Studio 打开该文件夹下的android文件夹,IDE会创建一个android工程(编译的就是该工程)
3.打开Gradle面板,Task→build→assemble集成编译,如果成功会分别生成一个debug和一个relase的.aar包
【注意】:如果Gradle面板为空,可以先点击依赖处的运行按钮,项目运行后Gradle面板会出现运行命令
问题汇总
此处列举一些项目编译过程中遇到的问题以及参考解决方案
1.Failed to resolve: com.facebook.react:react-native+
直接从Gradle下载依赖失败,后面使用下载后的本地资源node_moudles,将该文件放在android工程的上一级目录,并且在build.gradle中添加
maven {
// All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
url "$rootDir/../node_modules/react-native/android"
}
2.ERROR:.gradle\caches\transforms-2\files-2.1\e3a25a6ab6fddcf1e2b54647ed5d5556\core-1.7.0\res\values\values.xml:105:5-114:25: AAPT: error: resource android:attr/lStar not found
修改配置compileSdkVersion为31(之前为29)
3.Plugin with id ‘com.android.library’ not found
build.gradle中添加代码
buildscript {
// The Android Gradle plugin is only required when opening the android folder stand-alone.
// This avoids unnecessary downloads and potential conflicts when the library is included as a
// module dependency in an application project.
if (project == rootProject) {
repositories {
google()
mavenCentral()
}
dependencies {
classpath("com.android.tools.build:gradle:4.2.2") // 版本仅供参考
}
}
}
4. Could not find com.github.yalantis:ucrop:2.2.6-native
build.gradle中添加配置
repositories {
...
maven { url "https://jitpack.io" }
...
}
5. Could not install install Gradle distribution from distribution url,The cached zip file xxx may be corrupted.
问题情景:项目初次打开时,Gradle下载报错,反复修改gradle.properties中配置的版本号,仍然出现该错误。
distributionUrl=https\://services.gradle.org/distributions/gradle-6.7.1-bin.zip
修改方法:使用下载后的本地Gradle进行配置
6. Could not resolve xxx PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
关闭使用的代理(如果使用代理,多半是这个原因)