簡體   English   中英

如何從android studio中的另一個模塊導入類?

[英]How to import class from another module in android studio?

我在單個 android 項目中創建了兩個模塊,將其命名為 x 和 y。

  1. 模塊 x 有一個類 Egg(包:com.example.x)
  2. 模塊 y 有一個 Foo 類(包:com.example.y)

現在我想在類Egg中導入類Foo,為此我在類Egg中寫了下面提到的語句

Import com.example.y.Foo;

現在,Android 無法識別 Foo。

問題,

是否可以僅使用 import 語句從不同的模塊導入 Class?

我是否需要創建模塊 y 的庫,然后將創建的庫導入模塊 x?

或者可能解決方案是別的。

確保以下內容:

在settings.gradle中,您應該: include ':x', ':y'

在x / build.gradle中,您應該將y添加為依賴項:

dependencies {
        compile project(':y')
        // other dependencies
}

現在當創建新模塊時,settings.gradle自動添加此模塊。之后你應該添加這一行:

    dependencies {
    implementation(
    ...,
    ..,
            project(":y")
)
}

結合並更正之前的兩個答案,最好的解決方案是將這一行添加到 x/build.gradle -> dependencies

implementation project(':y')

compile project() - 已棄用,不再工作

如果您只想實現單個模塊,則無需使用implementation(..., .., project(":y")結構。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM