如何解决SystemProperties导入失败
在非AOSP环境中做开发的时候,通常会遇到一些依赖的类找不到,SystemProperties类便是一个典型的例子,参考网上的一些博客,找到了解决办法:
参考博客 https://blog.csdn.net/csdn_conda/article/details/80284099
修改app/build.gradle文件如下:
apply plugin: 'com.android.application'
android {
compileSdkVersion 29
buildToolsVersion "29.0.3"
defaultConfig {
applicationId "com.example.qqmusicdemo"
minSdkVersion 16
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(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.lifecycle:lifecycle-extensions:2.0.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
**新增部分------开始**
String SDK_DIR = System.getenv("ANDROID_HOME")
if (null == SDK_DIR) {
Properties props = new Properties()
props.load(new FileInputStream(project.rootProject.file("local.properties")))
SDK_DIR = props.get('sdk.dir')
}
dependencies {
compileOnly files("${SDK_DIR}/platforms/android-23/data/layoutlib.jar")
}
**新增部分------结束**
PS:有两点需要特别注意:
1.在Gradle新版本中已经弃用了provided方法,因此这里使用compileOnly
2.并不是SDK的每一个版本的layoutlib.jar都包含了SystemProperties类,我试了26,27,29都失败了,所以当大家安装该方法操作仍失败时,试试别的版本(参考例子中的版本23是最保险的)