Java 在 Android Studio 中在哪里添加类文件夹?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27797494/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Where to add folder of classes in Android Studio?
提问by Linesofcode
I want to set up classes in different folders than activities. Current folder structure:
我想在与活动不同的文件夹中设置课程。当前文件夹结构:
-> APP
-> Manifests
-> Java
-> com.test.testing
-> classes
auth.java
home_activity
-> libs
auth.java
认证文件
package com.test.testing;
// error here: Package name ‘com.test.testing' does not correspond to file path.
public class auth{
public void auth(){}
}
Plus, I can't call this class in activity:
另外,我不能在活动中调用这个类:
import classes.auth;
So I went to build gradle an this are my configurations:
所以我去构建 gradle 这是我的配置:
apply plugin: 'com.android.application'
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
applicationId "com.test.testing"
minSdkVersion 15
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
sourceSets {
main {
java.srcDirs = ['src/main/java', 'java/', 'src/main/java/com/test/testing/classes']
assets.srcDirs = ['src/main/assets', 'src/main/assets/']
}
}
}
Android Studio is a little bit confusing about folder structures, we have several options to chose and I have no clue what some of them do.
In my case, I added the folder "classes" as "Java Folder".
Android Studio 对文件夹结构有点困惑,我们有几个选项可供选择,我不知道其中一些是做什么的。就我而言,我将文件夹“classes”添加为“Java Folder”。
EDIT 1:The tip was to create a package instead of a folder. So I created the package and a new file inside of it. Automatically, android studio filled with this information the file auth.java;
编辑 1:提示是创建一个包而不是一个文件夹。所以我在里面创建了包和一个新文件。android studio 会自动将这些信息填充到文件auth.java 中;
package com.test.testing.classes;
public class auth {
}
What happens is that I'm still unable to import the class into the activities.
Althought when I start typing (in the activity) import classes
it appears as a documentation help but it doesn't have any object associated, that said:
发生的情况是我仍然无法将课程导入到活动中。虽然当我开始输入(在活动中)时,import classes
它显示为文档帮助,但它没有任何关联的对象,它说:
import classes.auth;
Gives error.
给出错误。
I went again to build Gradle file and it didn't add anything to the java.srcDirs
:
我再次去构建 Gradle 文件,但它没有向以下内容添加任何内容java.srcDirs
:
sourceSets {
main {
java.srcDirs = ['java/', 'src/main/java']
}
}
EDIT 2:The solution to the import
problem was the string.
Instead of
编辑2:import
问题
的解决方案是字符串。代替
import classes.auth;
Should be:
应该:
import com.test.testing.classes.auth;
采纳答案by Sharp Edge
Select Package
from the List above (in your snap shot)
Package
从上面的列表中选择(在您的快照中)
回答by Jithin Pavithran
I think many people are confused about the file structure, here it is:
我想很多人对文件结构感到困惑,这里是:
app
|--> manifests
|--> java
| |--> com.example.yourapp
| | |--> MainActivity.java
| | |--> YOUR_PACKAGE_FOLDER
| |
| |--> com.example.yourapp(android test)
| |--> com.example.yourapp(test)
|
|--> res
You can try File -> New -> Package
, so that android studio will create the folder at the correct location.
(File -> New -> Java Class
for classes)
您可以尝试File -> New -> Package
,以便 android studio 将在正确的位置创建文件夹。
(File -> New -> Java Class
对于班级)