Android使用JetpackMvvm框架遇到的问题
2023-03-01 本文已影响0人
因为我的心
一、前言:
- JetPackMvvm基于MVVM模式集成谷歌官方推荐的JetPack组件库:LiveData、ViewModel、Lifecycle、Navigation组件
- 加入Retrofit网络请求,协程,帮你简化各种操作,让你快速请求网络
- JetPackMvvm完整地址:https://github.com/hegaojian/JetpackMvvm
- JetPackMvvm只有框架和网络请求:https://gitee.com/lyyon/MvvmHttpDemo
遇到的问题:
在使用JetPackMvvm的使用的时候,要修改源码,不能直接依赖;由于我们的项目比较老,导入moudle一直失败,只能手动把代码复制过来。
手动复制代码,请求报错:error:lateinit property app has not been initialized
二、使用JetPackMvvm三方框架
1、使用三方依赖版本
1.1 在root's build.gradle中加入Jitpack仓库
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
1.2 在app's build.gradle中添加依赖
dependencies {
...
implementation 'com.github.hegaojian:JetpackMvvm:1.2.7'
}
1.3 在app's build.gradle中,android 模块下按需开启DataBinding与ViewBinding
AndroidStudio 4.0 以下版本------>
android {
...
dataBinding {
enabled = true
}
viewBinding {
enabled = true
}
}
AndroidStudio 4.0及以上版本 ------>
android {
...
buildFeatures {
dataBinding = true
viewBinding = true
}
}
2、直接导入源码的moudle
图片.png3、直接复制源码到项目里
手动复制代码,请求报错:error:lateinit property app has not been initialized
解决:注意查看第二步。
1、第一步:
图片.png2、第二步:
图片.png项目初始化实例:
注意查看com.youjiakeji.yjkjreader.mvvm.base包名
<!--mvvm初始化-->
<provider
android:authorities="${applicationId}.KtxInstaller"
android:name="com.youjiakeji.yjkjreader.mvvm.base.Ktx"
android:exported="false"/>
package com.youjiakeji.yjkjreader.mvvm.base
import android.app.Application
import android.content.ContentProvider
import android.content.ContentValues
import android.content.IntentFilter
import android.database.Cursor
import android.net.ConnectivityManager
import android.net.Uri
import androidx.lifecycle.ProcessLifecycleOwner
import com.youjiakeji.yjkjreader.mvvm.ext.lifecycle.KtxAppLifeObserver
import com.youjiakeji.yjkjreader.mvvm.ext.lifecycle.KtxLifeCycleCallBack
import com.youjiakeji.yjkjreader.mvvm.network.manager.NetworkStateReceive
/**
* 作者 : hegaojian
* 时间 : 2019/12/14
* 描述 :
*/
val appContext: Application by lazy { Ktx.app }
class Ktx : ContentProvider() {
companion object {
lateinit var app: Application
private var mNetworkStateReceive: NetworkStateReceive? = null
var watchActivityLife = true
var watchAppLife = true
}
override fun onCreate(): Boolean {
val application = context!!.applicationContext as Application
install(application)
return true
}
private fun install(application: Application) {
app = application
mNetworkStateReceive = NetworkStateReceive()
app.registerReceiver(
mNetworkStateReceive,
IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION)
)
if (watchActivityLife) application.registerActivityLifecycleCallbacks(KtxLifeCycleCallBack())
if (watchAppLife) ProcessLifecycleOwner.get().lifecycle.addObserver(KtxAppLifeObserver)
}
override fun insert(uri: Uri, values: ContentValues?): Uri? = null
override fun query(
uri: Uri,
projection: Array<String>?,
selection: String?,
selectionArgs: Array<String>?,
sortOrder: String?
): Cursor? = null
override fun update(
uri: Uri,
values: ContentValues?,
selection: String?,
selectionArgs: Array<String>?
): Int = 0
override fun delete(uri: Uri, selection: String?, selectionArgs: Array<String>?): Int = 0
override fun getType(uri: Uri): String? = null
}
3、第三步:
图片.png4、第四步-依赖包:
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.3.0'
//kotlin
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.4.10"
implementation 'androidx.core:core-ktx:1.3.2'
//lifecycle
api 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.0'
api 'androidx.lifecycle:lifecycle-common-java8:2.2.0'
api 'androidx.lifecycle:lifecycle-extensions:2.2.0'
// viewModel
api "androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.0"
api "androidx.fragment:fragment-ktx:1.3.1"
// liveData
api "androidx.lifecycle:lifecycle-livedata-ktx:2.2.0"
api 'com.kunminx.archi:unpeek-livedata:4.4.1-beta1'
//navigation
api 'androidx.navigation:navigation-fragment-ktx:2.3.5'
api 'androidx.navigation:navigation-ui-ktx:2.3.5'
//retrofit
api "com.squareup.retrofit2:retrofit:2.9.0"
api "com.squareup.retrofit2:converter-gson:2.9.0"
api 'com.github.franmontiel:PersistentCookieJar:v1.0.1'
//动态替换BaseUrl库 使用可参考 https://github.com/JessYanCoding/RetrofitUrlManager
api 'me.jessyan:retrofit-url-manager:1.4.0'
}
三、使用时的报错:
1、object not locked by thread before notify()
错误分析:看到这个错误真的懵逼了,看到是请求框架中报错,我也没有修改框架代码,后来注释掉我的业务代码,发现不报错了。还有这个错误会走一次,正确的也会走一次。
图片.png报错:
java.lang.IllegalMonitorStateException: object not locked by thread before notify()