Android studio调试Android源码

  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. 设置断点,开始调试...

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容