Retrofit Module 引用最佳实现方式 (如何导入一个 Maven 项目到 AndroidStudio)

1,214 阅读3分钟

引: Retrofit 想必做Android开发的我们已经不再陌生了.但通常都是使用Library Dependency 的方式进行包引入,在开发中带来了很多便利.
今天带来的是使用 Module Dependency 的方式这本地进行引用,将项目关联到GitHub进行实时pull操作,并创建Android Application的形式进行调试 / 二次开发等操作.
Retrofit 项目是以Maven进行构建的,需要对该项目变换成Android Application可引用的以 Gradle构建的 Android Library.

拉取GitHub上的Retrofit项目

  1. 打开 Retrofit项目地址

查看项目结构为Maven形式构建的项目.关键代码目录为./retrofit/main/java/retrofit2

  1. 拉取代码到本地

不建议使用Zip包下载,后期还需手动进行Git仓库关联的操作.

创建Android Application Project

  1. 打开Android Studio - > Start a New Android Studio project创建新项目

默认选项即可.

  1. 创建Retrofit Module 并引入当前Android Application
    创建一个Android Library即可

Library配置信息如下

创建完成后查看当前项目配置

app module形式依赖 retrofit

Github / Retrofit 项目中的所有文件Copy 合并到当前目录

  1. Android项目根目录中的.gitignore文件重命名

  2. 复制 - 合并文件夹

  3. 合并后项目文件夹

  1. Android Studio中添加 Git 版本控制根目录

至此,Retrofit的源码已经成功关联,并跟踪到Github / Retrofit

奇怪的地方

因为之前创建的Library包名为retrofit2.a,手动将.a的文件夹删除即可.


顺便将Manifests文件中的package属性也删除.a

Retrofit2添加依赖

  1. 运行项目,查看报错提醒

  2. 添加okhttp3依赖

  3. Nullable注解 Cannot resolve

修改该类,将Java8 相关判断逻辑删除即可.

/*
 * Copyright (C) 2013 Square, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package retrofit2;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import java.lang.reflect.Method;
import java.util.concurrent.Executor;
import javax.annotation.Nullable;
class Platform {
  private static final Platform PLATFORM = findPlatform();
  static Platform get() {
    return PLATFORM;
  }
  private static Platform findPlatform() {
    try {
      Class.forName("android.os.Build");
      if (Build.VERSION.SDK_INT != 0) {
        return new Android();
      }
    } catch (ClassNotFoundException ignored) {
    }
    return new Platform();
  }
  @Nullable
  Executor defaultCallbackExecutor() {
    return null;
  }
  CallAdapter.Factory defaultCallAdapterFactory(@Nullable Executor callbackExecutor) {
    if (callbackExecutor != null) {
      return new ExecutorCallAdapterFactory(callbackExecutor);
    }
    return DefaultCallAdapterFactory.INSTANCE;
  }
  boolean isDefaultMethod(Method method) {
    return false;
  }
  @Nullable
  Object invokeDefaultMethod(Method method, Class declaringClass, Object object,
                             @Nullable Object... args) throws Throwable {
    throw new UnsupportedOperationException();
  }
  static class Android extends Platform {
    @Override
    public Executor defaultCallbackExecutor() {
      return new MainThreadExecutor();
    }
    @Override
    CallAdapter.Factory defaultCallAdapterFactory(@Nullable Executor callbackExecutor) {
      if (callbackExecutor == null) throw new AssertionError();
      return new ExecutorCallAdapterFactory(callbackExecutor);
    }
    static class MainThreadExecutor implements Executor {
      private final Handler handler = new Handler(Looper.getMainLooper());
      @Override
      public void execute(Runnable r) {
        handler.post(r);
      }
    }
  }
}

再次运行,安装成功

现在可以使用cmd + T 更新Github / Retrofit的代码,并可以基于当前的retrofit module进行二次开发. 可以提交到本地Git仓库进行版本比对、push到其他远端仓库.

最后更新时间:
这里写留言或版权声明:www.suantou.one/2017/06/06/…