转自https://blog.csdn.net/pshiping2014/article/details/79549680
调用android系统隐藏的API的几种方法(以调SystemProperties这个类为例):
1、用反射:
//获取系统属性public static String getProperty(String key, String defaultValue) { String value = defaultValue; try { Class<?> c = Class.forName("android.os.SystemProperties"); Method get = c.getMethod("get", String.class, String.class); value = (String)(get.invoke(c, key, "unknown" )); } catch (Exception e) { e.printStackTrace(); }finally { return value; }}//设置系统属性public static void setProperty(String key, String value) { try { Class<?> c = Class.forName("android.os.SystemProperties"); Method set = c.getMethod("set", String.class, String.class); set.invoke(c, key, value ); } catch (Exception e) { e.printStackTrace(); }}
注意:编译时要注释掉 //implementation fileTree(include: ['.jar'],dir: 'libs')*
2、添加系统framework.jar
在目录下``**out/target/common/obj/JAVA_LIBRARIES/framework_intermediates/classes.jar**
改名得到 framework.jar 把jar包添加到lib目录并add as library,修改项目的build.gradle文件,在repositories里加入如下代码
allprojects { repositories { jcenter() google() } gradle.projectsEvaluated { tasks.withType(JavaCompile){options.compilerArgs.add(-Xbootclasspath/p:app\\libs\\framework.jar') } }}
在app的build.gradle中添加依赖
dependencies { provided files('libs/framework.jar')// implementation fileTree(include: ['*.jar'], dir: 'libs') implementation 'com.android.support:appcompat-v7:25.4.0' implementation 'com.android.support.constraint:constraint-layout:1.0.2' testImplementation 'junit:junit:4.12'} // 作用域改为provided
3、添加layoutlib.jar依赖
修改app中gradle.build文件添加默认的layoutlib.jar(或是指定sdk添加)即可调用隐藏的api,配置如下
修改app中的build.gradle,注意在android{}之后添加如下代码
def getLayoutLibPath() { return "${android.getSdkDirectory().getAbsolutePath()}" + "/platforms/" + android.compileSdkVersion +"/data/layoutlib.jar"}//String SDK_DIR = System.getenv("ANDROID_SDK_HOME")//if (SDK_DIR == null){// java.util.Properties prop = new Properties()// prop.load(new FileInputStream(project.rootProject.file("local.properties")))// SDK_DIR = prop.get("sdk.dir")//}dependencies { provided files(getLayoutLibPath()) implementation 'com.android.support.constraint:constraint-layout:1.0.2'// provided files("${SDK_DIR}/platforms/android-21/data/layoutlib.jar")
网友评论