简体   繁体   中英

How to call an activity from another module

I want to call an activity (ChatActivity) from Chat module in MessageActivity wich is a part of another module (seller). But when I add a dependency on the seller module like this :

implementation project(path: ':chat')

I received this error

ERROR: Unable to resolve dependency for ':seller@debugAndroidTest/compileClasspath': Could not resolve project :app.
Show Details
Affected Modules: seller

Here is my build.gradle (module:seller) file

apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.2"

    defaultConfig {
        applicationId "com.example.seller"
        minSdkVersion 23
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }

    }

}

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

    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    implementation 'com.ncapdevi:frag-nav:1.0.3'
    implementation 'com.roughike:bottom-bar:1.3.9'
    implementation('com.mikepenz:materialdrawer:5.3.1@aar') {
        transitive = true
    }
    implementation 'com.google.android.material:material:1.1.0'
    implementation 'androidx.navigation:navigation-fragment:2.2.1'
    implementation 'androidx.navigation:navigation-ui:2.2.1'
    implementation "androidx.recyclerview:recyclerview:1.1.0"
    implementation "androidx.annotation:annotation:1.1.0"
    implementation 'androidx.navigation:navigation-ui:2.2.1'
    implementation 'com.shobhitpuri.custombuttons:google-signin:1.0.0'
    implementation 'com.google.android.gms:play-services-auth:17.0.0'
    implementation 'com.google.firebase:firebase-messaging:20.1.3'
    implementation 'de.hdodenhof:circleimageview:3.1.0'
    implementation 'com.google.firebase:firebase-firestore:21.4.1'
    implementation 'com.google.firebase:firebase-core:17.2.3'
    api 'com.theartofdev.edmodo:android-image-cropper:2.8.+'
    implementation 'com.squareup.picasso:picasso:2.71828'
    implementation 'com.google.firebase:firebase-auth:19.3.0'
    implementation 'com.google.firebase:firebase-database:19.2.1'
    implementation 'com.google.firebase:firebase-storage:19.1.1'

    implementation project(path: ':app')
}

Here is my project settings.gradle

rootProject.name='commercetest'
include ':app', ':seller'

Here is my MessageActivity file :

package com.example.seller;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;

public class MessageActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Intent intent = new Intent(MessageActivity.this, ChatActivity.class);
        MessageActivity.this.startActivity(intent);

    }
}

Why is it keep sending an error. and is there a link, where to find the information?

    Intent intent = new Intent(MessageActivity.this, ChatActivity.class);
    startActivity(intent);

For importing a project as a module/library.

Replace

apply plugin: 'com.android.application'

with

apply plugin: 'com.android.library'

Also, remove applicationId from library/module's build.gradle

For reference check Plugins & applicationId

The Chat module should be an android Library

  1. Replace

    apply plugin: 'com.android.application'

    with

    apply plugin: 'com.android.library'
  2. remove applicationId "com.example.chat" from module's build.grade

  3. Also you should add ChatActivity to the Manifest, for example :

     <activity android:name=".ChatActivity"> <intent-filter> <action android:name="com.example.seller.MessageActivity" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity>

This should work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM