Android开发 framework架包
<h1><span style="color: rgba(0, 128, 128, 1)">版权声明</span></h1><p>本文来自博客园,作者:观心静 ,转载请注明原文链接:https://www.cnblogs.com/guanxinjing/p/16613716.html</p>
<div>本文版权归作者和博客园共有,欢迎转载,但必须给出原文链接,并保留此段声明,否则保留追究法律责任的权利。</div>
<h1><span style="color: rgba(0, 128, 128, 1)">前言</span></h1>
<p><span style="color: rgba(0, 0, 0, 1)"> 此博客讲解如何在Android studio中架入framework包。framework架包的最大目的肯定是想调用系统代码。但是请注意framework架的jar包是不参与apk编译的,<span style="color: rgba(186, 55, 42, 1)"><strong>你还需要apk有系统级权限与系统签名</strong></span>:Android系统开发 编译系统签名的APP</span></p>
<h1><span style="color: rgba(0, 128, 128, 1)">Framework架包的目的与结果</span></h1>
<p><span style="color: rgba(0, 0, 0, 1)"> 在开始架包前,先说明一下架包的<strong>目的与结果</strong>。如果不弄懂这个,那么<strong>你可能无法判断自己是否架包成功</strong>。首先架包是为了项目调用系统的API。 但是不等于Android studio上不会出现代码错误提示。 <span style="color: rgba(186, 55, 42, 1)"><strong>架包的目的是解决在Android studio编译apk时不会出现错误。<span style="color: rgba(0, 0, 0, 1)">意思是说就算IDE上已经显示红色的错误代码了,但是我们依然可以编译出apk。</span></strong></span></span></p>
<h1><span style="color: rgba(0, 128, 128, 1)">Framework包的生成目录</span></h1>
<div class="cnblogs_code">
<pre> out\target\common\obj\JAVA_LIBRARIES\framework_intermediates\classes.jar</pre>
</div>
<p><span style="color: rgba(0, 0, 0, 1)">如果未生成,可以执行下 make javac-check-framework</span></p>
<h1><span style="color: rgba(0, 128, 128, 1)">Framework架包流程</span></h1>
<h2><span style="color: rgba(35, 111, 161, 1)">引入framework.jar</span></h2>
<p><span style="color: rgba(0, 128, 128, 1)"><img src="https://img2022.cnblogs.com/blog/1497956/202208/1497956-20220822182249439-1928141078.png" alt=""></span></p>
<h2><span style="color: rgba(35, 111, 161, 1)"> 在build中增加引用代码</span></h2>
<p><img src="https://img2022.cnblogs.com/blog/1497956/202208/1497956-20220822183127111-1848258822.png" alt=""></p>
<p><span style="color: rgba(186, 55, 42, 1)"><strong>注意!</strong><span style="color: rgba(0, 0, 0, 1)">下面</span></span>的 compileOnly files('libs\\framework.jar') 别忘记添加到自己的项目里了</p>
<p>这里说明一下,其实只要添加 compileOnly files('libs\\framework.jar') 到这一步已经ok了.. 剩下的都不需要在配置了。虽然Android studio会提示代码错误,但是已经可以顺利编译apk了。</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">plugins {
id </span>'com.android.application'<span style="color: rgba(0, 0, 0, 1)">
id </span>'org.jetbrains.kotlin.android'<span style="color: rgba(0, 0, 0, 1)">
}
android {
compileSdk </span>31<span style="color: rgba(0, 0, 0, 1)">
defaultConfig {
applicationId </span>"com.zh.demo"<span style="color: rgba(0, 0, 0, 1)">
minSdk </span>22<span style="color: rgba(0, 0, 0, 1)">
targetSdk </span>31<span style="color: rgba(0, 0, 0, 1)">
versionCode </span>1<span style="color: rgba(0, 0, 0, 1)">
versionName </span>"1.0"<span style="color: rgba(0, 0, 0, 1)">
testInstrumentationRunner </span>"androidx.test.runner.AndroidJUnitRunner"<span style="color: rgba(0, 0, 0, 1)">
}
buildTypes {
release {
minifyEnabled </span><span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">
proguardFiles getDefaultProguardFile(</span>'proguard-android-optimize.txt'), 'proguard-rules.pro'<span style="color: rgba(0, 0, 0, 1)">
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget </span>= '1.8'<span style="color: rgba(0, 0, 0, 1)">
}
buildFeatures{
viewBinding </span>= <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">
}
}
dependencies {</span><span style="color: rgba(0, 0, 0, 1)">
compileOnly files(</span>'libs\\framework.jar'<span style="color: rgba(0, 0, 0, 1)">)
implementation </span>'androidx.core:core-ktx:1.7.0'<span style="color: rgba(0, 0, 0, 1)">
implementation </span>'androidx.appcompat:appcompat:1.3.0'<span style="color: rgba(0, 0, 0, 1)">
implementation </span>'com.google.android.material:material:1.4.0'<span style="color: rgba(0, 0, 0, 1)">
implementation </span>'androidx.constraintlayout:constraintlayout:2.0.4'<span style="color: rgba(0, 0, 0, 1)">
implementation </span>'androidx.legacy:legacy-support-v4:1.0.0'<span style="color: rgba(0, 0, 0, 1)">
implementation </span>'androidx.navigation:navigation-fragment-ktx:2.3.5'<span style="color: rgba(0, 0, 0, 1)">
implementation </span>'androidx.navigation:navigation-ui-ktx:2.3.5'<span style="color: rgba(0, 0, 0, 1)">
testImplementation </span>'junit:junit:4.13.2'<span style="color: rgba(0, 0, 0, 1)">
androidTestImplementation </span>'androidx.test.ext:junit:1.1.3'<span style="color: rgba(0, 0, 0, 1)">
androidTestImplementation </span>'androidx.test.espresso:espresso-core:3.4.0'<span style="color: rgba(0, 0, 0, 1)">
}
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">此段代码的作用是移动.app.iml文件中的Android api 位置顺序</span>
<span style="color: rgba(0, 0, 0, 1)">preBuild {
doLast {
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">def imlFile = file(project.name + ".iml")</span>
def imlFile = file("..\\.idea\\modules\\app\\" + project.parent.name + ".app.iml"<span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> {
def parsedXml </span>= (<span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> XmlParser()).parse(imlFile)
def jdkNode </span>= parsedXml.component.orderEntry.find { it.'@type' == 'jdk'<span style="color: rgba(0, 0, 0, 1)"> }
parsedXml.component[</span>1<span style="color: rgba(0, 0, 0, 1)">].remove(jdkNode)
def sdkString </span>= "Android API " + android.compileSdkVersion.substring("Adroid-".length()) + " Platform"
<span style="color: rgba(0, 0, 255, 1)">new</span> Node(parsedXml.component, 'orderEntry', ['type': 'jdk', 'jdkName': sdkString, 'jdkType': 'Android SDK'<span style="color: rgba(0, 0, 0, 1)">])
groovy.xml.XmlUtil.serialize(parsedXml, </span><span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> FileOutputStream(imlFile))
} </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (FileNotFoundException e) {
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> nop, iml not found</span>
<span style="color: rgba(0, 0, 0, 1)"> }
}
}</span></pre>
</div>
<h2><span style="color: rgba(35, 111, 161, 1)">打开.iml文件构建</span></h2>
<p><span style="color: rgba(0, 128, 128, 1)"><img src="https://img2023.cnblogs.com/blog/1497956/202312/1497956-20231204161050501-1248486120.png"></span></p>
<h1><span style="color: rgba(0, 128, 128, 1)">Framework架包的结果验证</span></h1>
<p><span style="color: rgba(0, 0, 0, 1)"> 怎么样才算架包成功。其实framework架包的关键是想把依赖顺序改变。让优先依赖的android.jar包变成framework.jar。 这里我们可以查看app.iml 文件了解依赖顺序。</span></p>
<p>.app.iml 路径图:</p>
<p><span style="color: rgba(0, 0, 0, 1)"><img src="https://img2022.cnblogs.com/blog/1497956/202208/1497956-20220822181052317-128657666.png" alt=""></span></p>
<p> 将Android Api 依赖从最上面移动到最下面</p>
<p><img src="https://img2022.cnblogs.com/blog/1497956/202208/1497956-20220822181328531-1392948803.png" alt=""></p>
<p> 查看External Libraries, 成功架包后 Android Studio default JDK 会改变引用位置</p>
<p><img src="https://img2022.cnblogs.com/blog/1497956/202208/1497956-20220822181423265-88969667.png" alt=""></p>
<p> </p>
<p> 在代码中引用framework的代码,<strong>架包成功后kotlin代码可能会报错。但是无关紧要,因为不影响编译apk</strong>。重点是我们可以调用ActivityManager.getCurrentUser() 这种系统级Api了。 点击ActivityManager也可以查看到路径已经被引用到framework.jar了</p>
<p><img src="https://img2022.cnblogs.com/blog/1497956/202208/1497956-20220822181736900-316250224.png" alt=""></p>
<p> </p>
<p> </p>
<p><span style="color: rgba(0, 128, 128, 1)">End</span></p>
</div>
<div id="MySignature" role="contentinfo">
<div style="text-align: center">
<p style="color:orange;font-size:16px;" >本文来自博客园,作者:观心静 ,转载请注明原文链接:https://www.cnblogs.com/guanxinjing/p/16613716.html </p>
<div style="color:orange;font-size:16px;">本文版权归作者和博客园共有,欢迎转载,但必须给出原文链接,并保留此段声明,否则保留追究法律责任的权利。 </div>
</div><br><br>
来源:https://www.cnblogs.com/guanxinjing/p/16613716.html
頁:
[1]