当我构建我的应用程序时,我得到了跟随错误.
错误:任务':app:transformResourcesWithMergeJavaResForDebug'的执行失败.在OS独立路径'META-INF/LICENSE'中找到多个文件
这是我的build.gradle文件
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
defaultConfig {
applicationId "cn.sz.cyrus.kotlintest"
minSdkVersion 14
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
javaCompileOptions{
annotationProcessorOptions{
includeCompileClasspath = true
}
}
multiDexEnabled true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
packagingOptions {
/* exclude 'META-INF/DEPENDENCIES'
exclude 'META-INF/NOTICE'
exclude 'META-INF/LICENSE'
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/NOTICE.txt'*/
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
}) …Run Code Online (Sandbox Code Playgroud) 我有谷歌这个问题,但结果对我不起作用。
详情如下。
public final class App extends com.zhixin.wedeep.common.BaseApplication implements androidx.lifecycle.LifecycleOwner {
^
// Expected @HiltAndroidApp to have a value. Did you forget to apply the Gradle Plugin?
Run Code Online (Sandbox Code Playgroud)
应用程序代码。
@HiltAndroidApp
class App : BaseApplication(), LifecycleOwner {
@Inject
lateinit var service: EventService
private val mLifecycleRegistry = LifecycleRegistry(this)
}
Run Code Online (Sandbox Code Playgroud)
此模块 gradle 文件。
apply plugin: 'kotlin-kapt'
apply plugin: 'kotlin-allopen'
apply plugin: 'androidx.navigation.safeargs.kotlin'
apply plugin: 'dagger.hilt.android.plugin'
dependencies {
implementation rootProject.ext.dependencies["hilt-android"]
implementation rootProject.ext.dependencies["hilt-lifecycle-viewmodel"]
kapt rootProject.ext.kapt["hilt-compiler"]
kapt rootProject.ext.kapt["hilt-android-compiler"]
}
Run Code Online (Sandbox Code Playgroud)
谁有想法?谢谢!
抱歉我的愚蠢问题。
我有谷歌这个问题,但答案对我不起作用。
这是我的代码。
data class Article(val id: Long, val title: String, val ingress: String, val image: String,
val dateTime: String, val tags: List<String>, val content: List<Item>, val created: Long, val changed: Long) {
@delegate:Transient
val formatDateString: String by lazy {
val sdf = SimpleDateFormat("dd.MM.yyyy HH.mm")
val date: Date = sdf.parse(dateTime)
var time: String?
if (date.year == Date().year) {
time = SimpleDateFormat("dd MM,HH:mm", Locale.US).format(date)
} else {
time = SimpleDateFormat("dd MM yyyy,HH:mm", Locale.US).format(date)
}
time!!
}
}
Run Code Online (Sandbox Code Playgroud)
当我尝试获取formatDateString价值时,会发生上述异常。
代码访问 …
当我使用lambda遍历String数组时,我得到了跟随错误.
java.lang.NoClassDefFoundError: com.twsz.app.ivybox.alarm.CreateOrUpdateAlarmActivity$$Lambda$1
at com.twsz.app.ivybox.alarm.CreateOrUpdateAlarmActivity.initView(CreateOrUpdateAlarmActivity.java:143)
at com.twsz.app.ivybox.alarm.CreateOrUpdateAlarmActivity.onCreate(CreateOrUpdateAlarmActivity.java:73)
Run Code Online (Sandbox Code Playgroud)
这是我的代码.我知道传统方式遍历String数组是可以的,但是为什么在使用lambda时会发生这种情况.
String[] days = dayOfWeek.split(",");
Arrays.asList(days).forEach(day->{
int index = Integer.valueOf(day) -1;
checkBoxList.get(index).setChecked(true);
});//where happens NoClassDefFoundError
Run Code Online (Sandbox Code Playgroud)
我的build.gradle文件
android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
defaultConfig {
applicationId "com.twsz.app.ivybox"
minSdkVersion 14
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
vectorDrawables.useSupportLibrary = true
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
dataBinding {
enabled = true
}
}
Run Code Online (Sandbox Code Playgroud)
谢谢你的帮助.
我们知道匿名内部类可能会导致内存泄漏.但是为什么它在异步网络调用时不起作用.
例如:
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.get()
.url(url)
.build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if (response.isSuccessful()) {
// String str = response.body().string();
// do sth to our View, but those views may be null when activity finished
}
}
});
Run Code Online (Sandbox Code Playgroud)
我们将在回调方法调用时更改视图的状态,但这些视图在活动完成时始终为null.为什么这个用于回调的匿名内部类实例不会导致活动泄漏.
运行单元测试代码时出现以下错误。
Caused by: java.lang.IllegalStateException: Hilt Fragments must be attached to an @AndroidEntryPoint Activity. Found: class androidx.fragment.app.testing.FragmentScenario$EmptyFragmentActivity
at dagger.hilt.internal.Preconditions.checkState(Preconditions.java:83)
at dagger.hilt.android.internal.managers.FragmentComponentManager.createComponent(FragmentComponentManager.java:75)
at dagger.hilt.android.internal.managers.FragmentComponentManager.generatedComponent(FragmentComponentManager.java:63)
at com.zhixin.wedeep.homepage.ui.Hilt_HomePage.generatedComponent(Hilt_HomePage.java:70)
at com.zhixin.wedeep.homepage.ui.Hilt_HomePage.inject(Hilt_HomePage.java:89)
at com.zhixin.wedeep.homepage.ui.Hilt_HomePage.initializeComponentContext(Hilt_HomePage.java:53)
at com.zhixin.wedeep.homepage.ui.Hilt_HomePage.onAttach(Hilt_HomePage.java:45)
at androidx.fragment.app.Fragment.onAttach(Fragment.java:1602)
at com.zhixin.wedeep.homepage.ui.Hilt_HomePage.onAttach(Hilt_HomePage.java:35)
at com.zhixin.wedeep.homepage.ui.HomePage.onAttach(HomePage.kt:281)
Run Code Online (Sandbox Code Playgroud)
这是我的测试代码。
@HiltAndroidTest
@UninstallModules(HomePageDataModule::class)
@RunWith(AndroidJUnit4::class)
@LargeTest
class TestHomePageFragment {
private val c = Composition("cyrus", "background", "description", "downloadUrl", "1000", "url", "1", true, "100", 100, "100", "test", "title", "1", "100", "cover", ArrayList(), "ONCE", null)
@Inject
lateinit var cpd: CompositionDao
@get:Rule
var hiltRule = HiltAndroidRule(this)
@Before
fun init() …Run Code Online (Sandbox Code Playgroud) 我想将数字格式化为并在不足两个字符时String填充0
fun formatDuration(val duration):String {\n val minutes = duration.toInt() / 60\n return "$minutes"\n}\nRun Code Online (Sandbox Code Playgroud)\n\n例如,如果minutes是 6\xef\xbc\x8c,则应显示它06而不是6。
执行单元测试时出现以下错误
java.lang.AssertionError: Activity never becomes requested state "[CREATED, STARTED, RESUMED, DESTROYED]" (last lifecycle transition = "PRE_ON_CREATE")
at androidx.test.core.app.ActivityScenario.waitForActivityToBecomeAnyOf(ActivityScenario.java:338)
at androidx.test.core.app.ActivityScenario.launchInternal(ActivityScenario.java:272)
at androidx.test.core.app.ActivityScenario.launch(ActivityScenario.java:238)
at androidx.test.ext.junit.rules.ActivityScenarioRule.lambda$new$3$ActivityScenarioRule(ActivityScenarioRule.java:98)
at androidx.test.ext.junit.rules.ActivityScenarioRule$$Lambda$3.get(Unknown Source:4)
at androidx.test.ext.junit.rules.ActivityScenarioRule.before(ActivityScenarioRule.java:103)
at org.junit.rules.ExternalResource$1.evaluate(ExternalResource.java:46)
Run Code Online (Sandbox Code Playgroud)
我的测试代码
@RunWith(AndroidJUnit4::class)
@LargeTest
class TestHomePageFragment {
private val intent = Intent(ApplicationProvider.getApplicationContext(), AudioPlayerActivity::class.java).apply {
flags = Intent.FLAG_ACTIVITY_NEW_TASK
putExtra(HomePageArouterConstants.KEY_ID,"1")
}
@get:Rule
val activityRule:ActivityScenarioRule<AudioPlayerActivity> = activityScenarioRule(intent)
@Test
fun testVote(){
// Assert.assertEquals(1,1)
val scenario = activityRule.scenario
scenario.moveToState(Lifecycle.State.RESUMED)
onView(withId(R.id.linear_layout_like)).perform(click())
}
}
Run Code Online (Sandbox Code Playgroud)
谁有这个问题的想法?
当我运行我的测试用例时,我得到了例外.
android.support.test.espresso.NoMatchingViewException:找不到层次结构中的视图匹配:id:android:id/home
这是我的TestCase代码.
public void nav_to_alarm_test(){
onView(withId(R.id.navigation_notifications)).perform(click());
onView(withId(R.id.rl_reminder)).perform(click());
onView(withId(R.id.item_test)).perform(click());
onView(withId(android.R.id.home)).perform(click());//throw exception here
onView(withId(android.R.id.home)).perform(click());
}
Run Code Online (Sandbox Code Playgroud)
这是android.R.id.home,属于ActionBar提供的按钮
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
return true;
case R.id.item_test:
startActivityForResult(CreateOrUpdateReminderActivity.class,CMD_ADD_REMINDER);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Run Code Online (Sandbox Code Playgroud)
为什么在资源ID android.R.id.home的层次结构中找不到视图?我有谷歌我的问题,但答案不是我想要的.
谢谢你的帮助.
这是我的布局文件.
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:layout_width="150dp"
android:layout_height="50dp"
android:hint="please input test content"
android:inputType="phone"
android:textColorHint="@android:color/holo_red_light"
android:textSize="13sp"
app:layout_constraintBottom_toTopOf="parent"
app:layout_constraintRight_toLeftOf="parent" />
<TextView
android:layout_width="80dp"
android:layout_height="50dp"
android:gravity="center"
android:text="Hello"
android:textColor="@android:color/black"
app:layout_constraintBottom_toTopOf="parent"
app:layout_constraintHorizontal_bias="0.5" />
</android.support.constraint.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)
关注pic是预览视图.
我想将TextView移动到center_horizontal到父视图,但layout_constraintHorizontal_bias=0.5"似乎不起作用.
谁有这个问题的想法?先谢谢!
android ×9
dagger-hilt ×2
java ×2
kotlin ×2
build.gradle ×1
dagger-2 ×1
gradle ×1
jvm ×1
lambda ×1
okhttp ×1