1:android stdio 开发时使用系统权限报错,为了编译成功可以关闭Using system app permission
这样就不会报错了
2:android stdio3.0 调用hide函数,添加系统jar文件,我们已InputManager.getInstance()方法举例
getInstance在android stdio sdk中为hide,在代码中直接使用会编译失败
/**
* Gets an instance of the input manager.
*
* @return The input manager instance.
*
* @hide
*/
public static InputManager getInstance() {
synchronized (InputManager.class) {
if (sInstance == null) {
try {
sInstance = new InputManager(IInputManager.Stub
.asInterface(ServiceManager.getServiceOrThrow(Context.INPUT_SERVICE)));
} catch (ServiceNotFoundException e) {
throw new IllegalStateException(e);
}
}
return sInstance;
}
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final KeyEvent kEvent = new KeyEvent(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(),
KeyEvent.ACTION_DOWN,KeyEvent.KEYCODE_MENU, 0, 0,
KeyCharacterMap.VIRTUAL_KEYBOARD, 0,KeyEvent.FLAG_FROM_SYSTEM | KeyEvent.FLAG_VIRTUAL_HARD_KEY,
InputDevice.SOURCE_KEYBOARD);
//调用hide函数
InputManager.getInstance().injectInputEvent(kEvent,
InputManager.INJECT_INPUT_EVENT_MODE_WAIT_FOR_FINISH);
}
我们可以使用系统生存的jar文件,编译生存的InputManager.class文件在framework.jar文件中out\target\common\obj\JAVA_LIBRARIES\framework_intermediates\classes.jar,修改文件为framework.jar(可以不改),
导致工程libs目录
右键工程选择 open moudle settings 设置文件为Compile only(只产于编译)
查看 app下的build.gradle会生存如下内容 compileOnly files('libs/framework.jar')
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
compileOnly files('libs/framework.jar')
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.android.support:design:26.1.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
最后一步编译时选择我们的jar文件优先,在工程的build.gradle(不是app下的)中添加
gradle.projectsEvaluated {
tasks.withType(JavaCompile) {
options.compilerArgs.add('-Xbootclasspath/p:app\\libs\\framework.jar')
}
}
allprojects {
repositories {
google()
jcenter()
}
gradle.projectsEvaluated {
tasks.withType(JavaCompile) {
options.compilerArgs.add('-Xbootclasspath/p:app\\libs\\framework.jar')
}
}
}
并在app.iml文件中把我们的jar移动到SDK前面,否则代码会提示有错(虽然能编译通过)
app下的build.gradle会生存如下内容,clean后也会先加载我们添加的jar
preBuild {
doLast {
def imlFile = file(project.name + ".iml")
println 'Change ' + project.name + '.iml order'
try {
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 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
}
}
————————————————
版权声明:本文为CSDN博主「carl_wuyu」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/u013885959/article/details/84325173