为什么Android中的java源代码有三个目录/文件夹?

我是 Android 开发的新手。当我创建一个新的 Android Studio 项目时,该java部分中会生成三个目录:

http://img2.mukewang.com/641be84800011b3403770099.jpg

事实上,我们关心的是第一个目录中的 java 文件,在我的例子中com.example.myapplication -> MainActivity

为什么有三个目录,每个目录都包含 java 文件,创建这些目录的目的是什么?


FFIVE
浏览 234回答 2
2回答

守着一只汪

根据维基百科,开发测试是一个软件开发过程,涉及广泛的缺陷预防和检测策略的同步应用,以降低软件开发风险、时间和成本,请参阅。文件夹:第一个(&nbsp;com.example.myapplication) 用于实际源代码。例如,活动、服务、广播接收器、内容提供者、模型、实用程序等的 java/kotlin 文件。第二个(&nbsp;com.example.myapplication(andoridTest)) 用于在 android 操作系统上运行的仪器测试。例如,假设我们有MainActivity一个按钮。单击按钮时会显示带有消息的 Toast。所以我们可以测试按钮是否正常工作,如下所示(为简单起见,提供了导入):import androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner;import androidx.test.rule.ActivityTestRule;import org.junit.Rule;import org.junit.Test;import org.junit.runner.RunWith;import static androidx.test.espresso.assertion.ViewAssertions.matches;import static androidx.test.espresso.matcher.RootMatchers.withDecorView;import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed;import static androidx.test.espresso.matcher.ViewMatchers.withText;import static org.hamcrest.Matchers.not;import static androidx.test.espresso.Espresso.onView;import static androidx.test.espresso.action.ViewActions.click;import static androidx.test.espresso.matcher.ViewMatchers.withId;@RunWith(AndroidJUnit4ClassRunner.class)public class ExampleAndroidTest {&nbsp; &nbsp; @Rule&nbsp; &nbsp; public ActivityTestRule<Main2Activity> mActivityRule =&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new ActivityTestRule<>(Main2Activity.class);&nbsp; &nbsp; @Test&nbsp; &nbsp; public void buttonClickShowingToast_isCorrect() {&nbsp; &nbsp; &nbsp; &nbsp; onView(withId(R.id.bt_test)).perform(click());&nbsp; &nbsp; &nbsp; &nbsp; onView(withText(R.string.toast_test))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .inRoot(withDecorView(not(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mActivityRule.getActivity().getWindow().getDecorView()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ))).check(matches(isDisplayed()));}}第三个( com.example.myapplication(test)) 用于可以在本地机器上运行的单元测试,意味着不需要 android 操作系统。例如,我们正在创建一个计时器,并且我们有一个实用方法可以将秒数转换为 HH:MM:SS 格式。方法是:public static String getHoursMinutesSeconds(int seconds) {&nbsp; &nbsp; int minutes = seconds / 60;&nbsp; &nbsp; seconds %= 60;&nbsp; &nbsp; int hours = minutes / 60;&nbsp; &nbsp; minutes %= 60;&nbsp; &nbsp; String strSec = Integer.toString(seconds);&nbsp; &nbsp; String strMin = Integer.toString(minutes);&nbsp; &nbsp; String strHour = Integer.toString(hours);&nbsp; &nbsp; StringBuilder sb = new StringBuilder();&nbsp; &nbsp; if (strHour.length() < 2) sb.append(0);&nbsp; &nbsp; sb.append(strHour);&nbsp; &nbsp; sb.append(':');&nbsp; &nbsp; if (strMin.length() < 2) sb.append(0);&nbsp; &nbsp; sb.append(strMin);&nbsp; &nbsp; sb.append(':');&nbsp; &nbsp; if (strSec.length() < 2) sb.append(0);&nbsp; &nbsp; sb.append(strSec);&nbsp; &nbsp; return sb.toString();}由于该方法不需要测试Android API 。它必须在本地机器上进行测试(因为它要快得多)。所以单元测试代码:import org.junit.Test;import org.junit.runner.RunWith;import org.junit.runners.BlockJUnit4ClassRunner;import io.jachoteam.taxiapp.views.WaitingIndicatorView;import static org.hamcrest.CoreMatchers.equalTo;import static org.hamcrest.CoreMatchers.is;import static org.hamcrest.MatcherAssert.assertThat;@RunWith(BlockJUnit4ClassRunner.class)public class ExampleUnitTest {&nbsp; &nbsp; @Test&nbsp; &nbsp; public void getHoursMinutesSeconds_isCorrect1() {&nbsp; &nbsp; &nbsp; &nbsp; String actualValue = WaitingIndicatorView.getHoursMinutesSeconds(1);&nbsp; &nbsp; &nbsp; &nbsp; assertThat(actualValue, is(equalTo("00:00:01")));&nbsp; &nbsp; }&nbsp; &nbsp; @Test&nbsp; &nbsp; public void getHoursMinutesSeconds_isCorrect2() {&nbsp; &nbsp; &nbsp; &nbsp; String actualValue = WaitingIndicatorView.getHoursMinutesSeconds(60);&nbsp; &nbsp; &nbsp; &nbsp; assertThat(actualValue, is(equalTo("00:01:00")));&nbsp; &nbsp; }}为每个编写的代码单元编写测试是最佳实践。因为通过在早期阶段提供错误检测(while fresh=))并在每次更改代码时验证代码,当项目变大需要维护时,它使开发人员的生活更轻松。

眼眸繁星

根据Android 文档:/test:&nbsp;包含在主机 JVM 上运行的本地测试的代码。/androidtest&nbsp;仪器测试,在操作系统上运行。/module-name:&nbsp;这是程序源代码所在的地方
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java