1. 导入依赖
implementation 'cn.leancloud:storage-android:8.1.4'
implementation 'cn.leancloud:realtime-android:8.1.4'
implementation 'io.reactivex.rxjava2:rxandroid:2.1.1'
2. 初始化
- 创建:/app/src/main/kotlin/package/App.kt
class App : Application() {
override fun onCreate() {
super.onCreate()
LeanCloud.setLogLevel(LCLogger.Level.DEBUG)
LeanCloud.initialize(
,
,
,
)
LCIMOptions.getGlobalOptions().setDisableAutoLogin4Push(true)
PushService.startIfRequired(android.content.Context context)
}
}
- 修改:/app/src/main/AndroidManifest.xml
<--! 添加网络权限 -->
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:name=".App"
...
>
<--! 即时通讯和推送 -->
<service android:name="cn.leancloud.push.PushService"/>
<receiver android:name="cn.leancloud.push.LCBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.intent.action.USER_PRESENT"/>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
...
3. 更安全的初始化
// 进入 jdk目录
cd C:\Program Files\Android\android-studio\jre\bin
// 签名文件
keytool -list -v -keystore x:\x\xx.jks
// 获取指定签名证书的 SHA256 指纹
-
应用包名加签名证书指纹进行验证 官网路径:控制台 > 设置 > 安全中心 > Android 安全设置 -
下载 leancloud-jniLibs ,解压获取 jniLibs 文件夹 -
移动到:/app/src/main/jniLibs/ -
添加内容:/app/build.gradle/
android {
signingConfigs {
config {
keyAlias '{your key alias}'
keyPassword '{your key password}'
storeFile file('{your store file full name}')
storePassword '{your store password}'
}
}
buildTypes {
debug {
signingConfig signingConfigs.config
}
release {
signingConfig signingConfigs.config
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
- 修改为初始化安全:/app/src/main/kotlin/package/App.kt
class App : Application() {
override fun onCreate() {
super.onCreate()
LeanCloud.setLogLevel(LCLogger.Level.DEBUG)
LeanCloud.initializeSecurely(
,
,
)
LCIMOptions.getGlobalOptions().setDisableAutoLogin4Push(true)
PushService.startIfRequired(android.content.Context context)
}
}
4. 避免部分代码被混淆
# proguard.cfg
-keepattributes Signature
-dontwarn com.jcraft.jzlib.**
-keep class com.jcraft.jzlib.** { *;}
-dontwarn sun.misc.**
-keep class sun.misc.** { *;}
-dontwarn retrofit2.**
-keep class retrofit2.** { *;}
-dontwarn io.reactivex.**
-keep class io.reactivex.** { *;}
-dontwarn sun.security.**
-keep class sun.security.** { *; }
-dontwarn com.google.**
-keep class com.google.** { *;}
-dontwarn cn.leancloud.**
-keep class cn.leancloud.** { *;}
-keep public class android.net.http.SslError
-keep public class android.webkit.WebViewClient
-dontwarn android.webkit.WebView
-dontwarn android.net.http.SslError
-dontwarn android.webkit.WebViewClient
-dontwarn android.support.**
-dontwarn org.apache.**
-keep class org.apache.** { *;}
-dontwarn okhttp3.**
-keep class okhttp3.** { *;}
-keep interface okhttp3.** { *; }
-dontwarn okio.**
-keep class okio.** { *;}
-keepattributes *Annotation*
|