当前位置:   article > 正文

基于Android14 Mtk平台移植系统应用Settings到AS中编译_mtk移植工具

mtk移植工具

废话不多说,直接进入正题

一、移植前的准备工作
1.一套mtk的系统源码(包含了Settings,settingslib,setupcompat,setupdesigin,wifitracklib等)
2.Android Studio环境
3.最好使用ubuntu系统,或者虚拟机装ubuntu系统
4.确保网络通畅,因为有很多依赖需要下载,最好能翻墙,否则有些下载失败
5.建议gradle版本不要用太高的,我用的8.0的,agp也用的8.0,kotlin版本用的1.8.10

二、正式开始
1.在AS里面新建一个Android项目,包名为com.android.settings,把源码Settings里面的src,res,res-export,res-product,res_ext,libs,还有protos以及配置文件导入工程,覆盖。
在这里插入图片描述Settings源码目录
2.Settings依赖很多子模块,其中最大的一块SettingsLib,源码如下图
SettingsLib源码结构新建一个module,名字为settingslib,包名以源码目录中的AndroidManifest.xml里面的为准,com.android.settingslib
创建一个module删除src下面的androidTest跟test,保留main目录,然后把SettingsLib目录下的src,res以及AndroidManifest的配置文件拷贝到该module,其中SettingsLib又依赖很多子模块,可以查看bp文件
SettingsLib模块的bp文件把这些子模块依次以module的形式引入,有些子模块是直接在SettingsLib这个目录下,有些则不在,比如iconloader,还有setupcompat,setupdesign,wifitracklib,这些模块自己去源码里面找,然后一个个以module的形式引入,一共差不多有40来个。每次引入一个,单独调试编译确保最小的单元能编译通过。
3.举个具体的子模块,SettingsLib里面的spa模块,按照上面说的创建好module,这里命名为SpaLib,导入src以及res,配置文件,跟bp文件去配置gradle,该模块使用了compose,需要配置compose的运行环境,如果kotlin版本低于2.0,则采用以下配置

composeOptions {
        kotlinCompilerExtensionVersion '1.4.2'
    }

    buildFeatures {
        compose true
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

此处需要注意,kotlinCompilerExtensionVersion这个版本号要跟kotlin的版本对应,有个对应关系表:
compose编译器与kotlin版本对应关系图我项目里kotlin使用的是1.8.10版本,所以这里compose编译版本用的是1.4.2,这里关系一定要对应,否则编译会失败,其他的一些依赖根据bp文件引入相应的库就行,大多为androidx库,最终的grade配置如下:

plugins {
    alias(libs.plugins.android.library)
    alias(libs.plugins.jetbrains.kotlin.android)
//    alias(libs.plugins.compose.compiler)
}
android {
    namespace 'com.android.settingslib.spa'
    compileSdk 34

    defaultConfig {
        minSdk 29
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        consumerProguardFiles "consumer-rules.pro"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = "1.8"
    }
    composeOptions {
        kotlinCompilerExtensionVersion '1.4.2'
    }
    buildFeatures {
        compose true
    }
}

dependencies {
    implementation platform(libs.androidx.compose.bom)
    implementation libs.appcompat
    implementation libs.material
    testImplementation libs.junit
    androidTestImplementation libs.ext.junit
    androidTestImplementation libs.espresso.core

    implementation libs.slice.builders
    implementation libs.slice.core
    implementation libs.slice.view
    implementation libs.compose.animation
    implementation libs.compose.material3
    implementation libs.compose.material.icons.extended
    implementation libs.compose.runtime
    implementation libs.compose.runtime.livedate
    implementation libs.compose.ui.ui.tooling.preview
    implementation libs.lifecycle.livedata.ktx
    implementation libs.lifecycle.runtime.compose
    implementation libs.navigation.compose
    implementation libs.lottie.compose
    implementation libs.mpandroidchart

    implementation libs.androidx.activity.compose
    implementation libs.compose.ui

    //支持koltin
    implementation libs.androidx.core.ktx
//    implementation libs.kotlin.stdlib.jdk8
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66

这个模块使用了kotlin,所以要引入kotlin插件
4.还有一些模块调用了sdk里面隐藏的接口,或者sdk里面没有的接口,这些接口都在framework.jar里面,需要引入这个jar,比如SettingsLib模块下的子模块Utils,如果没有引入framework,会报如下异常:
在这里插入图片描述PackageManager.MATCH_ANY_USER该属性是隐藏的,引入framework.jar后,还必须要提高其优先级,才能使用隐藏的属性或者方法,提高jar包优先级方法如下:
提高jar包优先级该配置可以方法项目级别下的build.gradle文件,也就是最外层的gradle,这样就不需要在每个子模块都配置。
5.还有一些模块使用了aidl,比如setupcompat模块,找到setupcompat模块,其目录结构如下:setupcompat目录结构build.gradle需要增加aidl的配置:

apply plugin: 'com.android.library'

android {
    // Not specifying compileSdkVersion here so clients can specify it; must be at least Q

    namespace 'com.google.android.setupcompat'
    compileSdk 34

    defaultConfig {
        minSdkVersion 29
        targetSdkVersion 34
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard.flags'
        }
    }

    sourceSets.main {
        manifest.srcFile 'AndroidManifest.xml'
        java.srcDirs = ['src/main/java','partnerconfig/java']
        aidl.srcDirs = ['src/main/aidl']
        res.srcDirs = ['src/main/res']
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_9
        targetCompatibility JavaVersion.VERSION_1_9
    }

    buildFeatures {
        aidl = true
    }
}

dependencies {
    implementation libs.annotation

    //在PartnerCustomizationLayout使用了,暂时有问题,注释掉了
    implementation libs.errorprone
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43

其他30多个模块都是大同小异,全部一一引入,调试编译通过。

所有子模块都引入并能单独编译通过后,开始调试settingslib模块,直接编译该模块,提示异常提示缺哪个类,在哪个模块下,就在gradle引入这个模块,最终的grade配置如下:

plugins {
    alias(libs.plugins.android.library)
    alias(libs.plugins.jetbrains.kotlin.android)
}

android {
    namespace 'com.android.settingslib'
    compileSdk 34

    defaultConfig {
        minSdk 29
        targetSdk 34
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        consumerProguardFiles "consumer-rules.pro"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    kotlinOptions {
        jvmTarget = "1.8"
    }

    lintOptions {
        checkReleaseBuilds false
        abortOnError false
    }

    gradle.projectsEvaluated {
        tasks.withType(JavaCompile) {
            options.compilerArgs.add('-Xbootclasspath/p:' + getRootDir().getPath() + '/libs' + '/framework.jar:'
                    + getRootDir().getPath() + '/app/libs/framework-bluetooth.jar:'
                    + getRootDir().getPath() + '/app/libs/framework-wifi.jar:'
                    + getRootDir().getPath() + '/app/libs/framework-connectivity.jar:'
                    + getRootDir().getPath() + '/app/libs/framework-connectivity2.jar:'
            )

        }
    }

}

dependencies {

    implementation libs.appcompat
    implementation libs.material
    testImplementation libs.junit
    androidTestImplementation libs.ext.junit
    androidTestImplementation libs.espresso.core
   //android x , add by huangxiaofeng
    implementation libs.annotation
    implementation libs.coordinatorlayout
    implementation libs.core.core
    implementation libs.fragment
    implementation libs.lifecycle.runtime
    implementation libs.loader
    implementation libs.localbroadcastmanager
    implementation libs.preference
    implementation libs.recyclerview
    implementation libs.zxing.core
    implementation libs.room.runtime
    implementation libs.androidx.core.ktx
    implementation libs.window
//    implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
//    implementation libs.legacy.support.v4
//    implementation libs.legacy.preference.v14
//    implementation libs.mediarouter.nodeps
//    implementation 'androidx.appcompat:appcompat-resources:1.6.1'


    compileOnly files('../app/libs/iconloader.jar')
    compileOnly files('../libs/framework.jar')
    compileOnly files('../app/libs/core-libart.jar') //for libcore.io.Streams
    compileOnly files('../app/libs/core-icu4j.jar')
    compileOnly files('../app/libs/framework-bluetooth.jar')
    compileOnly files('../app/libs/framework-connectivity.jar')
    compileOnly files('../app/libs/framework-connectivity2.jar')
    compileOnly files('../app/libs/framework-tethering.jar')
    compileOnly files('../app/libs/framework-configinfrastructure.jar')
    compileOnly files('../app/libs/framework-wifi.jar')
//    compileOnly files('../app/libs/ext.jar')
//    compileOnly files('../app/libs/framework-wifi-util-lib.jar')

    //extern dependency
    implementation project(':WifiTrackerLibRes')
    implementation project(':setupdesign') //org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.4 -> 1.7.1 导致升级了版本,根本原因是该依赖implementation libs.window的版本为1.2.0导致,降低为1.0.0
    implementation project(':setupcompat')
    implementation project(':iconloader')

    //inner module
    implementation project(':SettingsLibHelpUtils')
    implementation project(':SettingsLibRestrictedLockUtils')
    implementation project(':SettingsLibActionBarShadow')
    implementation project(':SettingsLibAppPreference')
    implementation project(':SettingsLibSearchWidget')
    implementation project(':SettingsLibSettingsSpinner')
    implementation project(':SettingsLibIllustrationPreference')
    implementation project(':SettingsLibLayoutPreference')
    implementation project(':SettingsLibMainSwitchPreference')
    implementation project(':SettingsLibActionButtonsPreference')
    implementation project(':SettingsLibEntityHeaderWidgets')
    implementation project(':SettingsLibBarChartPreference')
    implementation project(':SettingsLibProgressBar')
    implementation project(':SettingsLibAdaptiveIcon')
    implementation project(':SettingsLibRadioButtonPreference')
    implementation project(':SettingsLibSelectorWithWidgetPreference')
    implementation project(':SettingsLibDisplayUtils')
    implementation project(':SettingsLibUtils')
    implementation project(':SettingsLibEmergencyNumber')
    implementation project(':SettingsLibTopIntroPreference')
    implementation project(':SettingsLibBannerMessagePreference')
    implementation project(':SettingsLibFooterPreference')
    implementation project(':SettingsLibUsageProgressBarPreference')
    implementation project(':SettingsLibCollapsingToolbarBaseActivity')
    implementation project(':SettingsLibTwoTargetPreference')
    implementation project(':SettingsLibSettingsTransition')
    implementation project(':SettingsLibButtonPreference')
    implementation project(':SettingsLibDeviceStateRotationLock')
    implementation project(':SettingsLibProfileSelector')
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128

你可能需要编译若干次,每次都会报某某类找不到,先在当前项目全局查找,看是不是在已经引入的某个子模块下,如果找到,则引入相应的子模块即可,如果没有找到,可能在系统源码里面的某个jar里面,在整编完系统源码后,几乎所有的jar都会在如下目录
out/soong/.intermediates
out/target/common/obj/JAVA_LIBRARIES
比如import static android.uwb.UwbManager.AdapterStateCallback.STATE_ENABLED_INACTIVE;
提示这个找不到,则可以到上述目录下,通过命令:grep android.uwb.UwbManager.class -rn .找到该类所在的jar包,结果如下:
在这里插入图片描述这里我已经把jar包在系统的out目录下找到并放到了其他路径下,所以此处截图的结果并不是在上述提到的两个路径下。
总共大概有40个jar需要引入,settingslib能编译通过后,基本上就完成了一半的移植工作。

移植的过程中需要注意的点以及可能出现的问题
1.jdk兼容性问题,我建议所有模块的gradle里面配置java的编译环境jdk为1.8,kotlin的也为1.8,

compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = "1.8"
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

否则会出现一些莫名其妙的错误,比如framework里面的一些接口找不到,因为我的jar包是另外的同事提供的,他编系统源码的过程使用的是jdk17的环境,所以他提供的jar包都是jdk17的,其中有个jar,SettingsLibActivityEmbedding.jar,引用其中的内联函数,报了一个异常:
在这里插入图片描述jdk不兼容导致,后来解决方案是不使用该jar,在如下依赖找到了这个接口
在这里插入图片描述
2.一些资源找不到,比如字符串,可以通过如下配置,可能解决:
在grade.properties中,增加一行配置
android.nonTransitiveRClass=false

3.有些子模块如:SettingsLibBannerMessagePreference,gradle配置没问题,但就是提示找不到settingslib_preferred_minimum_touch_target,可以尝试暂时把该module改为application,不再以library的形式引入,居然就编译通过了,我暂时没搞懂什么原因,在最后调试app的时候,再把对应模块改回以library的形式引入就行,参考如下大佬的文章:添加链接描述

4.Error while processing /home/ls/AndroidStudioProjects/Lens_Settings/setupdesign/main/res/drawable-anydpi-v21/sud_navbar_ic_more.xml : Width (0) and height (0) cannot be <= 0
定义的dimen明明不为0,却报这个异常,无奈硬编码设置宽高,暂时通过编译

5.找不到该资源android:color=“?androidprv:attr/colorSurfaceHighlight”
目前as好像不支持这种写法:?androidprv,我的处理方式是先注释,因为不多,先确保工程能编译通过.

6.implementation ‘com.github.PhilJay:MPAndroidChart:v3.1.0’ 依赖失败原因,要先配置maven,根据官网去配置

7.AS尚不能处理product属性,所以不能通过此属性来区分,出现的重复资源,需要去除,因为涉及很多字符串,需要使用正则表达式去删,不能手动去删,极容易误删,主要命令:

<string name="(.*?)" product="nosdcard" msgid="(.*?)">"(.*?)"</string>

<string name="(.*?)" product="nosdcard">(.*?)</string>

(.*?)<string name="(.*?)" product="tablet" msgid="(.*?)">(.*?)</string>(.*?)

(.*?)<string name="(.*?)" product="tablet">(.*?)</string>(.*?)

(.*?)<string name="(.*?)" product="tablet">\n(.*?)\n(.*?)</string>(.*?)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

具体参考文章:添加链接描述,感谢大佬

8.org.jetbrains.kotlin.backend.common.BackendException: Backend Internal error: Exception during IR lowering
该异常是compose的环境没有配对

9.异常:static import only from classes and interfaces
该异常都是jdk不兼容导致,明明提示的类都在framework.jar里面,但却报了该异常,build.gradle中的jdk都配置为1.8就行.

基本上就这些,最后附上自测能编译出apk的完整版项目结构:
在这里插入图片描述在这里插入图片描述用到的jar包
app module下:
在这里插入图片描述

在这里插入图片描述项目根目录下:
在这里插入图片描述如有其他问题,可留言,互相交流.

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/711670
推荐阅读
相关标签
  

闽ICP备14008679号