调用android系统隐藏的API的几种方法(以调SystemProperties这个类为例):
1、用反射:
//获取系统属性
public static String getProperty(String key, String defaultValue) {
String value = defaultValue;
try {
Class<?> c = ("");
Method get = ("get", , );
value = (String)((c, key, "unknown" ));
} catch (Exception e) {
();
}finally {
return value;
}
}
//设置系统属性
public static void setProperty(String key, String value) {
try {
Class<?> c = ("");
Method set = ("set", , );
(c, key, value );
} catch (Exception e) {
();
}
}
注意:编译时要注释掉 //implementation fileTree(include: ['*.jar'],dir: 'libs')
2、添加系统
在目录下
out/target/common/obj/JAVA_LIBRARIES/framework_intermediates/
改名得到 把jar包添加到lib目录并add as library,修改项目的文件,在repositories里加入如下代码
allprojects {
repositories {
jcenter()
google()
}
{
(JavaCompile){
(-Xbootclasspath/p:app\\libs\\')
}
}
}
在app的中添加依赖
dependencies {
provided files('libs/')
// implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation ':appcompat-v7:25.4.0'
implementation ':constraint-layout:1.0.2'
testImplementation 'junit:junit:4.12'
}
// 作用域改为provided
3、添加依赖
修改app中文件添加默认的(或是指定sdk添加)即可调用隐藏的api,配置如下
修改app中的,注意在android{}之后添加如下代码
def getLayoutLibPath() {
return "${().getAbsolutePath()}" + "/platforms/" +
+"/data/"
}
//String SDK_DIR = ("ANDROID_SDK_HOME")
//if (SDK_DIR == null){
// prop = new Properties()
// (new FileInputStream(("")))
// SDK_DIR = ("")
//}
dependencies {
provided files(getLayoutLibPath())
implementation ':constraint-layout:1.0.2'
// provided files("${SDK_DIR}/platforms/android-21/data/")
}