问题
My application was working perfectly fine, I dont know what happened all of a sudden the build.gradle throw an error stating Gradle sync failed, build file '...\build.gradle' should not contain a package statement, the thing is I checked my other apps as well and they had the same package com.example.myapplication;
here is my build.gradle(project:FaceRec);
package com.example.myapplication;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.SurfaceView;
import android.view.View;
import android.widget.E;
and here is the build.gradle with in the app
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
buildToolsVersion "28.0.3"
defaultConfig {
applicationId "com.example.myapplication"
minSdkVersion 21
targetSdkVersion 28
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.0.2'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.cardview:cardview:1.0.0'
implementation 'com.squareup.retrofit2:retrofit:2.4.0'
implementation 'com.squareup.retrofit2:converter-gson:2.4.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation project(path: ':openCVLibrary347')
dependencies {
api 'com.theartofdev.edmodo:android-image-cropper:2.8.+'
}
}
if any further clarification is needed please let me know.
Thanks you very much in advance.
回答1:
refer to this Android Studio java file gets corrupted
Simply delete your caches should mostly solve your problem.
C:\Users\cc.AndroidStudio3.2\system\caches
回答2:
Thanks L2_Paver for the guide. I noticed that the build.gradle(project:FaceRec) is having contents that does not make sense, I dont know where and how it was there, so I changed the whole code and pasted an empty project's gradle and added some of the references, now with the replaced code as below the application started to work fine.
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.5.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
来源:https://stackoverflow.com/questions/58024814/gradle-sync-failed-build-file-build-gradle-should-not-contain-a-package-s