如何使用从另一个Docker容器在Docker容器上运行的仿真器?

时间:2019-05-06 15:09:45

标签: docker jenkins docker-compose android-emulator adb

为了在不需要任何额外设置的情况下在任何jenkins代理中执行我的测试,我想让一切都在Docker中运行。

我的第一种方法是使用基本映像,其中包含我需要的全部内容,即我的应用程序代码,Android SDK,Flutter,Emulator和一个仿真器映像。可悲的是,映像大小超过了10 GB,因此我决定分担责任:

  • 我的基本映像将仅包含Androi sdk。以下图像将从其继承:

  • 图像1将使我的应用程序挂载一个卷,并进行Flutter测试/构建。

  • 图像2将运行一个模拟器。

这是我的设置的docker-compose:

---

version: '3'

services:
  flutter:
    build:
      args:
        - "FLUTTER_CHANNEL=stable"
        - "FLUTTER_VERSION=1.2.1"
      context: .
      dockerfile: DockerfileFlutter
    volumes:
      - .:/app
    working_dir: /app
    command: ["flutter", "test"]

  emulator:
    build:
      context: .
      dockerfile: DockerfileAndroidEmulator
    container_name: "emulator"
    privileged: true
    extra_hosts:
      - "emulator:127.0.0.1"
    depends_on:
      - flutter
    links:
      - flutter
    command: ["/opt/android-sdk-linux/tools/emulator", "@flutter_emulator", "-no-skin", "-no-audio", "-no-window"]

我面临的问题是我无法使用正在运行的仿真器,因为当我在服务flutter中执行adb设备时,我看不到已在服务{ {1}}。

我试图通过emulator进行连接,但这没有用。我有什么可能的解决方案,或者我做错了吗?

谢谢。

1 个答案:

答案 0 :(得分:0)

需要满足以下几个前提条件:

  • adb服务器正在“仿真器”容器import android.os.Build; import android.support.annotation.RequiresApi; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import java.io.IOException; import java.util.concurrent.CompletableFuture; import okhttp3.Call; import okhttp3.Callback; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; public class HttpRequestDietPlan { private OkHttpClient client = new OkHttpClient(); public interface CallHandler { public void onSuccess(DietPlan dietPlan); public void onFailure(Exception e); } public void getJson(CallHandler callHandler) { final Request request = new Request.Builder() .addHeader("X-RapidAPI-Host", "spoonacular-recipe-food-nutrition-v1.p.rapidapi.com") .addHeader("X-RapidAPI-Key", "KEY_KEY_KEY") .url("https://spoonacular-recipe-food-nutrition-v1.p.rapidapi.com/recipes/mealplans/generate?timeFrame=day&targetCalories=2000") .build(); client.newCall(request).enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { call.cancel(); e.printStackTrace(); callHandler.onFailure(e) } @Override public void onResponse(Call call, final Response response) throws IOException { if (!response.isSuccessful()) { IOException e = new IOException("Unexpected code " + response); callHandler.onFailure(e) } else { DietPlan dietPlan = new DietPlan(); // Deserialize with a library here try { String jsonData = response.body().string(); JSONObject json = new JSONObject(jsonData); JSONArray arrayMeals = json.getJSONArray("meals"); for (int i = 0; i < arrayMeals.length(); i++) { JSONObject object = arrayMeals.getJSONObject(i); Meal meal = new Meal( object.getInt("id"), object.getString("title") ); dietPlan.meals.add(meal); System.out.println(meal); } } catch (JSONException e) { e.printStackTrace(); } callHandler.onSuccess(dietPlan) } } }); } } 上运行
  • adb服务器看到您的模拟器adb start-server
  • adb服务器在端口5037上监听
  • 您可以从“ flutter”容器访问此端口-检查docker network https://docs.docker.com/compose/networking/

进一步阅读:https://developer.android.com/studio/command-line/adb