Android OS

Android studio调试Android源码

2019-01-24  本文已影响0人  古风子
  1. 打开Android Studio,新建 project (如,MTK9);

  2. 新建Android Library Module (如,MTK9Library);

  3. explorer中删除 MTK9Library 这个module不使用的 Test 相关代码;

  4. 在module目录新建selfLibs目录,将编译出来的class jar包放到改目录(可以将编译处的文件全部放到此目录,避免编译报错)

  5. 用以下gradle覆盖覆盖module:如,MTK9Library的build.gradle文件

apply plugin: 'com.android.library'
def sysLibs = "selfLibs"
android {
    compileSdkVersion 28
    defaultConfig {
        minSdkVersion 28
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
    }

    compileOptions {
        encoding "UTF-8"
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {
    compileOnly fileTree(dir: sysLibs, include: ['*.jar'])
}

gradle.projectsEvaluated {

    tasks.withType(JavaCompile) {

        println(">> [" + it.getProject() + "] JavaCompile Task:" +  it.name)

        def tree = fileTree(dir: sysLibs, include: ['*.jar'])
        if (tree.size() <= 0)
            return

        def clspath = tree.join(';')

        println "bootClasspath before => " + options.bootClasspath
        options.bootClasspath = clspath
        println "bootClasspath after => " + options.bootClasspath
    }

}

task fixIml {
    ext.imlFile = projectDir.absolutePath + '/' + project.name + '.iml'
    inputs.file imlFile
    outputs.file imlFile

    doLast {
        if (!file(imlFile).exists())
            return

        def parse = new XmlParser().parse(imlFile)
        def moduleComponent = parse.component.find { it.@name == 'NewModuleRootManager' }
        def orderEntries = moduleComponent.orderEntry


        // Determine the index of the Android SDK entry
        def jdkOrderEntry = orderEntries.find { it.@type == 'jdk' }
        if (jdkOrderEntry == null)
            return

        def jdkOrderEntryIndex = moduleComponent.children().indexOf(jdkOrderEntry)
        moduleComponent.children().remove(jdkOrderEntryIndex)
        moduleComponent.children().add(jdkOrderEntry)

        // Write the fixed iml file
        def printer = new XmlNodePrinter(new PrintWriter(new FileWriter(imlFile)))
        printer.preserveWhitespace = true
        printer.print(parse)
    }
}

tasks.preBuild.dependsOn fixIml
  1. 根据需要,将需要调试的源码按包路径copy源码到MTK9Library module/src/main/java下
    $src/frameworks/base/core/java
    $src/frameworks/base/services/core/java
    $src/frameworks/base/services/java  
  1. Android Studio中刷新文件,刷新gradle配置,build module测试一下;

  2. 以调试system_server中的代码为例: run -> Attach debugger to android process,

  3. 设置断点,开始调试...

上一篇下一篇

猜你喜欢

热点阅读