首页 > 解决方案 > 将android包添加到java模块(android studio)

问题描述

抱歉,这是我第一次遇到这种问题,我不知道该怎么办。

我正在开发一个包含两个 java 模块(Module1、Module2)的项目

主项目的 build.gardle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.3"

    defaultConfig {
        applicationId "com.client.howara"
        minSdkVersion 24
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        
    }
    
    buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
   
}

dependencies {
    implementation project(':Module1')
    implementation project(':Module2')
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    testImplementation  'junit:junit:4.13'
    implementation .....
}

模块 1 的 build.gardle:

apply plugin: 'java'
dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation project(':Module2')
}

模块 2 的 build.gradle:

apply plugin: 'java'

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
}

当我尝试将 android 包添加到 Module1 时,出现以下错误:

例子:

error: package android.os does not exist
import android.os.Build;

我需要修改 module1 build.gradle 吗?如果是的话怎么办?

感谢您的帮助,并为我的无知感到抱歉

标签: javaandroid

解决方案


编辑

您已定义module1java库。因此,将禁止导入任何android工件以将其保留为java库。

如果您真的想在 中添加android工件module1,则需要通过执行以下操作转换module1为库:android

Module1 的 BuildScript:

// Remove this
apply plugin: 'java'

// Instead add this
apply plugin: 'com.android.library'

我认为您正在做的事情很好,但是您无法有效地陈述您的问题。

您当前发生的情况Gradle BuildScripts是:您的App依赖于Module1Module2,您的Module1依赖于Module2且您的Module2不依赖于任何一个appModule1

         App
        / \ 
   Module1 \
      \     \
       \     \
        \     \
         Module2

推荐阅读