使用场景
一些自定义控件直接copy了Android源码,使用了如下类似特性
- 希望能够使用系统的attr属性,调用了com.android.internal.R文件
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
@Deprecated
public StaticLayout(...) {}
会导致编译失败,需要使用 android-hidden-api 解决此问题
解决方式
-
下载 android_hide_api.jar,添加到libs目录 -
module的build.gradle添加依赖 dependencies {
provided files('libs/android_hide_api.jar')
...
}
-
项目根build.gradle添加依赖 allprojects {
gradle.projectsEvaluated {
tasks.withType(JavaCompile) {
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) {
}
}
}
亲测是不可用的,暂时废弃。
|