【发布时间】:2020-04-23 17:25:18
【问题描述】:
我创建了新的 android 项目,并在项目内部创建了新的库模块(网络)。
现在我们有了应用(默认模块)和网络模块。
在网络模块中,我有 volley 依赖项,而 app 模块中没有。
创建 Kotlin 类并在 Network Module 中定义此方法
/ 网络模块/
build.gradle
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 28
defaultConfig {
minSdkVersion 21
targetSdkVersion 28
versionCode 1
versionName "1.0"
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"
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.core:core-ktx:1.2.0'
// Volley Network
implementation 'com.android.volley:volley:1.1.1'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
// VolleyUtils.kt
class VolleyUtils {
fun enableLog() {
VolleyLog.DEBUG = true
}
}
从网络模块创建 AAR 并放置在应用模块中
/ 应用模块 /
build.gradle
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.test.base"
minSdkVersion 21
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
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"
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.core:core-ktx:1.2.0'
implementation(name: 'volleynetwork-release', ext: 'aar')
// implementation project(':volleynetwork')
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
// MainActivity.kt
class MainActivity : AppCompatActivity() {
super.onreate(saveInstanceState)
setContentView(R.layout.activity_main)
//access method in network module
VolleyUtils().enableLog()
}
}
编译成功。但是,@Runtime 导致异常
结果:
java.lang.NoClassDefFoundError: Failed resolution of: Lcom/android/volley/VolleyLog;
at com.test.volleynetwork.VolleyUtils.enableLog(VolleyUtils.kt:8)
at com.test.base.MainActivity.onCreate(MainActivity.kt:12)
at android.app.Activity.performCreate(Activity.java:7144)
at android.app.Activity.performCreate(Activity.java:7135)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1272)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2932)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3087)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1817)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6746)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.android.volley.VolleyLog" on path: DexPathList[[zip file "/data/app/com.test.base-LKNAponHFP3pH9lW8RU9sQ==/base.apk"],nativeLibraryDirectories=[/data/app/com.test.base-LKNAponHFP3pH9lW8RU9sQ==/lib/arm64, /system/lib64, /product/lib64]]
所以,只有在生成 AAR 时才会出现问题,否则它工作正常 请指导我哪里出错了。
【问题讨论】:
标签: android kotlin exception module aar