在模块(android库)

时间:2018-06-04 11:53:07

标签: android module retrofit2 proguard android-library

一直在为一个项目的android库中使用retrofit。然而,如果在应用程序的gradle中配置,或者库的源是应用程序项目的一部分,那么一切似乎都运行良好。

但是,如果库项目已经预测并作为@aar包含在应用程序中。似乎有一个障碍问题。

这是我在android模块项目中进行改造的计划:

-dontwarn retrofit2.**
-keep class retrofit2.** { *; }
-keepattributes Exceptions
-keepattributes RuntimeVisibleAnnotations
-keepattributes RuntimeInvisibleAnnotations
-keepattributes RuntimeVisibleParameterAnnotations
-keepattributes RuntimeInvisibleParameterAnnotations

-keepattributes EnclosingMethod
-keepclasseswithmembers class * {
    @retrofit2.http.* <methods>;
}
-keepclasseswithmembers interface * {
    @retrofit2.* <methods>;
}

# Platform calls Class.forName on types which do not exist on Android to determine platform.
-dontnote retrofit2.Platform
# Platform used when running on RoboVM on iOS. Will not be used at runtime.
-dontnote retrofit2.Platform$IOS$MainThreadExecutor
# Platform used when running on Java 8 VMs. Will not be used at runtime.
-dontwarn retrofit2.Platform$Java8
# Retain generic type information for use by reflection by converters and adapters.
-keepattributes Signature
# Retain declared checked exceptions for use by a Proxy instance.
-keepattributes Exceptions

# Retain generic type information for use by reflection by converters and adapters.
-keepattributes Signature
# Retain service method parameters.
-keepclassmembernames,allowobfuscation interface * {
    @retrofit2.http.* <methods>;
}
# Ignore annotation used for build tooling.
-dontwarn org.codehaus.mojo.animal_sniffer.IgnoreJRERequirement

-keep public interface com.mylibrary.interfaces.**
-keep public class com.mylibrary.rest.RetrofitClientInstance
-keep public class com.mylibrary.models.** {*;}

-dontwarn org.xmlpull.v1.**
-dontwarn javax.annotation.**


### OkHttp3
-dontwarn okhttp3.**
-dontwarn okio.**
-dontwarn javax.annotation.**
# A resource is loaded with a relative path so the package of this class must be preserved.
-keepnames class okhttp3.internal.publicsuffix.PublicSuffixDatabase

而且,这就是我的RetrofitClientInstance类的样子:

public class RetrofitClientInstance {

    private static Retrofit retrofit;

    public static Retrofit getRetrofitInstance() {
        if (retrofit == null) {
            retrofit = new retrofit2.Retrofit.Builder()
                    .baseUrl(Constants.BASE_API_URL)
                    .addConverterFactory(ScalarsConverterFactory.create())
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
        }
        return retrofit;
    }
}

我得到的错误:

java.lang.NoClassDefFoundError: Failed resolution of: Lretrofit2/Retrofit$Builder;
        at com.mylibrary.rest.RetrofitClientInstance.getRetrofitInstance(RetrofitClientInstance.java:15)
        at com.mylibrary.rest.LoginAPIHelper.<init>(LoginAPIHelper.java:29)
        at com.mylibrary.rest.LoginAPIHelper.getInstance(LoginAPIHelper.java:36)
        at com.view.LoginActivity.onCreate(LoginActivity.java:124)
        at android.app.Activity.performCreate(Activity.java:6772)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2715)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2823)
        at android.app.ActivityThread.-wrap12(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1545)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6349)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:893)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:783)
     Caused by: java.lang.ClassNotFoundException: Didn't find class "retrofit2.Retrofit$Builder" on path: DexPathList[[zip file "/data/app/com.inventaapp-2/base.apk"],nativeLibraryDirectories=[/data/app/com.inventaapp-2/lib/arm, /data/app/com.inventaapp-2/base.apk!/lib/armeabi-v7a, /system/lib, /vendor/lib]]
        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:380)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
        at com.mylibrary.rest.RetrofitClientInstance.getRetrofitInstance(RetrofitClientInstance.java:15) 
        at com.mylibrary.rest.LoginAPIHelper.<init>(LoginAPIHelper.java:29) 
        at com.mylibrary.rest.LoginAPIHelper.getInstance(LoginAPIHelper.java:36) 
        at com.inventaapp.view.LoginActivity.onCreate(LoginActivity.java:124) 
        at android.app.Activity.performCreate(Activity.java:6772) 
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119) 
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2715) 
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2823) 
        at android.app.ActivityThread.-wrap12(ActivityThread.java) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1545) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:154) 
        at android.app.ActivityThread.main(ActivityThread.java:6349) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:893) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:783) 

我尝试过多次dexing导入库的主应用程序。不影响行为。事实上,如果将源作为模块导入到应用程序项目中,则无需多处理。

此外,如果将aar导入为库。但是,如果我在主项目的gradle中添加改装gradle导入。它也适用于那种情况。

我在这里缺少什么?似乎与此类似的问题:https://github.com/square/retrofit/issues/1811

仅供参考,我的gradle看起来像:

 implementation 'com.google.code.gson:gson:2.8.2'
 implementation 'com.squareup.retrofit2:retrofit:2.4.0'
 implementation 'com.squareup.retrofit2:converter-gson:2.4.0'
 implementation 'com.squareup.retrofit2:converter-scalars:2.4.0'

非常感谢任何帮助。

提前致谢, ARNAB

3 个答案:

答案 0 :(得分:1)

在android库模块项目中使用改造时解决了此问题。尝试了各种方法,以下内容对我有用

步骤1:我已经在应用程序和库模块的build.gradle文件中添加并更新了改造依赖项(2.9.0)

Wrap(
  spacing: 6.0,
  runSpacing: 6.0,
  children: List<Widget>.generate(myList.length, (int index) {
    return Chip(
      label: Text(myList[index]),
      onDeleted: () {
        setState(() {
          myList.removeAt(index);
        });
      },
    );
  }),
)

第2步:在应用的gradle中添加

implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
implementation 'com.squareup.retrofit2:adapter-rxjava:2.7.1'
implementation 'com.squareup.okhttp3:logging-interceptor:4.8.0'

第3步:使缓存无效,然后重新启动/清理构建并重新构建。

答案 1 :(得分:0)

问题已在2.9.0版中修复,只需更新至最新版本即可。

答案 2 :(得分:-1)

我想问题是当aar在本地生成时,它并没有打包所有依赖项。那是设计使然! 您需要将aar发布到一个maven repo和一个列出了所有依赖项的pom文件,然后gradle可以获取所需的所有依赖项。

快速的解决方法是将此依赖项添加到主应用程序项目build.gradle