使用场景
一些自定义控件直接copy了Android源码,使用了如下类似特性
- 希望能够使用系统的attr属性,调用了com.android.internal.R文件
// android.widget.TextView TypedArray a = theme.obtainStyledAttributes(attrs, com.android.internal.R.styleable.TextView, defStyleAttr, defStyleRes); int n = a.getIndexCount(); for (int i = 0; i < n; i++) { int attr = a.getIndex(i); switch (attr) { case com.android.internal.R.styleable.TextView_text: mText = a.getText(attr); break; case com.android.internal.R.styleable.TextView_textColor: mTextColor = a.getColorStateList(attr); break; } }
- 调用了@hide Api
// android.text.StaticLayout /** * @hide * @deprecated Use {@link Builder} instead. */ @Deprecated public StaticLayout(...) {}
会导致编译失败
解决方式
-
AOSP源码编译
-
从设备获取framework.jar包
1.确认目标平台(手机/模拟器)
2.下载jar(adb pull /system/framework/framework.jar)
3.获取framework-classes.zip(jar替换为zip,获取classes.dex,dex2jar将其转为classes.jar,重命名framework-classes.zip)
4.替换原android.jar(原android.jar站zip,解压,用3内容替换) -
网上下载已经编译好的jar包
android-hidden-api
推荐第3种方式
-
下载 android_hide_api.jar,添加到libs目录
-
module的build.gradle添加依赖
dependencies { provided files('libs/android_hide_api.jar') ... }
-
项目根build.gradle添加依赖
allprojects { gradle.projectsEvaluated { tasks.withType(JavaCompile) { // Xbootclasspath/p:是 Java 编译的寻址优先设置,先找缺省路径还是全路径 options.compilerArgs.add("-Xbootclasspath/p:${rootProject.projectDir}/app/libs/android_hide_api.jar") } } ... }
其他
网上有一些方案将 /.idea/modules/app/xxx.app.iml 里面 jdk orderEntry 顺序调后
<orderEntry type="jdk" jdkName="Android API 28 Platform" jdkType="Android SDK" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Gradle: ./app/libs/android_hide_api.jar" level="project" />
<orderEntry type="library" name="Gradle: com.android.support:collections:28.0.0" level="project" />
<orderEntry type="library" name="Gradle: android.arch.lifecycle:common:1.1.1" level="project" />
<orderEntry type="library" name="Gradle: android.arch.core:common:1.1.1" level="project" />
<orderEntry type="library" name="Gradle: com.android.support:support-annotations:28.0.0" level="project" />
通过在preBuild task执行之后,进行文件修改
preBuild {
doLast {
def imlFile = new File("${rootProject.projectDir}/.idea/modules/${project.name}/${rootProject.name}.${project.name}.iml")
try {
if (imlFile.exists()) {
def parsedXml = (new XmlParser()).parse(imlFile)
def jdkNode = parsedXml.component[1].orderEntry.find { it.'@type' == 'jdk' }
parsedXml.component[1].remove(jdkNode)
def sdkString = "Android API " + android.compileSdkVersion.substring("android-".length()) + " Platform"
new groovy.util.Node(parsedXml.component[1], 'orderEntry', ['type': 'jdk', 'jdkName': sdkString, 'jdkType': 'Android SDK'])
groovy.xml.XmlUtil.serialize(parsedXml, new FileOutputStream(imlFile))
}
} catch (FileNotFoundException e) {
// nop, iml not found
}
}
}
亲测是不可用的,暂时废弃。