Android Studio 引入 framework.jar

1、从源码目录中找到out/target/common/obj/JAVA_LIBRARIES/framework_intermediates/classes.jar放入工程的app/libs目录中,如果找不到这个目录,请将视图从android 改成 project。

2、选中该classes.jar改名为framework.jar方便辨识。右键 -》 Add As Library添加进工程的依赖库

3、修改modle的build.gradle,添加依赖,添加如下红字部分:

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
    compileOnly files('libs/framework.jar')
}

3、修改优先级,优先使用该库。修改 Project的build.gradle,添加如下红字部分(注意路径不要写错):

allprojects {
    repositories {
        google()
        jcenter()
        
    }

    gradle.projectsEvaluated {
        tasks.withType(JavaCompile) {
            options.compilerArgs.add('-Xbootclasspath/p:app/libs/framework.jar')
        }
    }

}

4、(这个步骤并非必须,但是加上后,在预编译时不会显示红字而已)添加脚本,避免重新编译后会调整app.iml中framework.jar的顺序,修改modle的build.gradle,添加如下红字部分:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.2"
    defaultConfig {
        applicationId "com.zyf.wifiaptest"
        minSdkVersion 29
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
    compileOnly files('libs/framework.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
        }
    }
}

 


版权声明:本文为zyfzhangyafei原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。