Cannot create new activity in android studio
Asked Answered
A

10

11

I keep getting an IllegalStateException error in the event log while creating new activity in android studio:

11:27:15 InvalidReferenceException: Error executing FreeMarker template: The following has evaluated to null or missing:
==> srcDir  [in template "root://activities/common/common_globals.xml.ftl" at line 41, column 34]

Tip: If the failing expression is known to be legally null/missing, either specify a default value with myOptionalVar!myDefault, or use when-presentwhen-missing. (These only cover the last step of the expression; to cover the whole expression, use parenthessis: (myOptionVar.foo)!myDefault, (myOptionVar.foo)??
Ascites answered 19/11, 2016 at 9:59 Comment(0)
H
9

I've encountered the same problem and trying the following steps were working for me:

  1. close android studio
  2. delete folders: .idea and .gradle
  3. re-open project using android studio
Hemicrania answered 3/5, 2018 at 19:40 Comment(0)
H
4

Method 1:Just Remove the following code from your app level build.gradle

debug {
            storeFile file('/Users/Razz/Documents/keys/keystore.jks')
            storePassword 'ABC@123'
            keyAlias 'key0'
            keyPassword 'ABC@123'
      }



Method 2: In case, if there are two packages together (kotlin and java)

I was facing the same problem as whenever I create new activity form the activity templates provided by the android studio. Nothing was happening even after Gradle sync completes successfull.

After a long item, I figure out that, java directory name inside my project changed into kotlin somehow. And I supposed Android Studio looking for the java package to create new Activity.

Then, to solve this issue, I follow the below steps:

  1. Closed Android Studio
  2. Went to the project location up to java directory as ../ProjectName/app/src/main/
  3. In this main directory, I had the kotlin directory instead of java.
    So, I just renamed it to java.
  4. Then opened the project with Android Studio, and that's all.
  5. I created new activity it easily created one as it works before.
Hammurabi answered 13/8, 2020 at 13:48 Comment(3)
I am facing same issue in Window10 machine. And my directory is not changed to kotlin. Still not solved.Keening
Did you try the first method?Hammurabi
me too just remove the debug portion, and also delete the cache under C:\Users\asus\.AndroidStudio4.0\system and dont forget the re-run the Android Studio it may take a while but trust me it works! :D thanks @HammurabiSubjective
N
3

I have been faced that, after I setup my android environment.I already set up and configured everything, but this still occur.After searching a few mins, I found the problem in Logcat, enter image description here

Then, I know project is missing Build Tool version. You have to give which build tool version you will use to run for project. Go to >> Project Structure , ( Cmd + ; ) in mac. Choose Compile Sdk Version and Build Tools Version, enter image description hereAfter the gradle build, creating new activity from menu error has been fixed. I hope this will help for you.

Notes : Some of the projects from Github or bitbucket also missing Build Tools version when you cloned and run the project. These are the same errors and this approach can help to solve for them.

Nadeau answered 20/7, 2018 at 8:32 Comment(0)
H
2

Comment the code in build.gradle which creates folders in tmp directory and then create the Activity.

After successful creation of activity ,revert back the commented code in build.gradle (Co)

allprojects {
/*String osName = System.getProperty("os.name").toLowerCase();
if (osName.contains("windows")) {
    buildDir = "C:/tmp/${rootProject.name}/${project.name}"
}*/
repositories {
    jcenter()
}

}

Hunch answered 21/3, 2018 at 4:15 Comment(0)
I
1

In the file build.gradle, add

buildDir = "C:/tmp/${rootProject.name}/${project.name}"

sychronize gradle then remove it and synchronize again. This worked for me.

allprojects {
buildDir = "C:/tmp/${rootProject.name}/${project.name}"
repositories {
    jcenter()
}

}

Itch answered 16/4, 2018 at 15:38 Comment(0)
I
1

I experienced to same issue 2 day ago and i solve it by update the below text. Check to gradle.build app and module to newer versions.

classpath 'com.google.gms:google-services:4.3.4'
Intendance answered 7/10, 2020 at 22:14 Comment(2)
Hey hey, if you want to just comment on a question, please wait until you have enough reputation to do so.Haploid
I experienced to same issue 2 day ago and i solve it by update the below text. I just wanted to help. classpath 'com.google.gms:google-services:4.3.4'Nuri
E
0

I had the same issue. Visit this Link. https://code.google.com/p/android/issues/detail?id=232076

this happens if you have your project in a different drive other then C: android studio will automatically create a copy of your project (i hope they are cache files) in the C: drive's tmp folder. this folder is the cause for this issue.

try File-> Invalidate Caches/Restart. or just deleted the tmp folder in the c: drive and restart everything and it will work fine.

Expostulate answered 24/3, 2017 at 9:0 Comment(1)
can you post the log message.Expostulate
F
0

removing the build folder redirect code in the gradle file is a workaround.

allprojects {

    //String osName = System.getProperty("os.name").toLowerCase();
    //if (osName.contains("windows")) {
    //    buildDir = "C:/tmp/${rootProject.name}/${project.name}"
    //}
    repositories {
        jcenter()
    }
}

Found it here https://github.com/udacity/ud851-Exercises/issues/67

Fort answered 6/4, 2018 at 11:32 Comment(0)
T
0

I have the same exact problem when trying to create a new activity. IDE Fatal Errors

I was running Android Studio 3.2.1 on Windows 7 Professional. I did not have this problem until I started to switch loading project from different drives - I used to run my projects from F: drive and when I downloaded sample code to C: drive, creating new activities started to give this IDE fatal error.

Got a clue from online sources of a potential issue caused by buildDir path in build.gradle(Project:) shown below;

buildDir = "C:/tmp/${rootProject.name}/${project.name}"

Since my project working directory is in F: drive, I changed the above C: drive to F:

buildDir = "F:/tmp/${rootProject.name}/${project.name}"

With this simple change, I can now create new Activity.

If nothing else works for you I suggest you try this solution.

Titer answered 19/12, 2018 at 2:31 Comment(0)
B
0

I had tried most of the above suggestions without any success; the directory was fine, invalidation of caches, etc. But if you right-click on the project folder, New > Activity > Gallery (all others are greyed-out), clicking on any activity template will show you the exact problem. In my case, for some reason, all of my Activities (including Basic and Empty) required minSdkVersion 16; not the original 15. I hope this works for you if others fail.

Biforked answered 6/8, 2021 at 9:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.