Skip to content

Commit cc628cd

Browse files
committed
Support Instant Run application class
Instant Run swaps the application class name in the manifest to its own bootstrap application, which can instrument the app to use instant Run. It also adds a new "name" attribute to the manifest, which contains the original application. When Instant Run is used, we should retrieve this attribute as the application class name.
1 parent dd05c05 commit cc628cd

6 files changed

Lines changed: 142 additions & 1 deletion

File tree

‎AndroidAnnotations/androidannotations-core/androidannotations/src/main/java/org/androidannotations/helper/CanonicalNameConstants.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ public final class CanonicalNameConstants {
141141
public static final String APPCOMPAT_ACTIVITY = "android.support.v7.app.AppCompatActivity";
142142
public static final String VIEW_PAGER = "android.support.v4.view.ViewPager";
143143
public static final String PAGE_CHANGE_LISTENER = "android.support.v4.view.ViewPager.OnPageChangeListener";
144+
public static final String INSTANT_RUN_APPLICATION = "com.android.tools.fd.runtime.BootstrapApplication";
144145

145146
/*
146147
* Android permission

‎AndroidAnnotations/androidannotations-core/androidannotations/src/main/java/org/androidannotations/internal/helper/AndroidManifestFinder.java‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import org.androidannotations.AndroidAnnotationsEnvironment;
3737
import org.androidannotations.Option;
3838
import org.androidannotations.helper.AndroidManifest;
39+
import org.androidannotations.helper.CanonicalNameConstants;
3940
import org.androidannotations.internal.exception.AndroidManifestNotFoundException;
4041
import org.androidannotations.logger.Logger;
4142
import org.androidannotations.logger.LoggerFactory;
@@ -259,7 +260,12 @@ private AndroidManifest parse(File androidManifestFile, boolean libraryProject)
259260
Node applicationNode = applicationNodes.item(0);
260261
Node nameAttribute = applicationNode.getAttributes().getNamedItem("android:name");
261262

262-
applicationClassQualifiedName = manifestNameToValidQualifiedName(applicationPackage, nameAttribute);
263+
if (!hasInstantRunApplication(nameAttribute)) {
264+
applicationClassQualifiedName = manifestNameToValidQualifiedName(applicationPackage, nameAttribute);
265+
} else {
266+
Node plainNameAttribute = applicationNode.getAttributes().getNamedItem("name");
267+
applicationClassQualifiedName = manifestNameToValidQualifiedName(applicationPackage, plainNameAttribute);
268+
}
263269

264270
if (applicationClassQualifiedName == null) {
265271
if (nameAttribute != null) {
@@ -392,4 +398,8 @@ private List<String> extractUsesPermissionNames(NodeList usesPermissionNodes) {
392398
return usesPermissionQualifiedNames;
393399
}
394400

401+
private boolean hasInstantRunApplication(Node nameAttribute) {
402+
return nameAttribute != null && CanonicalNameConstants.INSTANT_RUN_APPLICATION.equals(nameAttribute.getNodeValue());
403+
}
404+
395405
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* Copyright (C) 2010-2015 eBusiness Information, Excilys Group
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed To in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
package org.androidannotations.manifest;
17+
18+
import java.io.IOException;
19+
20+
import org.androidannotations.internal.AndroidAnnotationProcessor;
21+
import org.androidannotations.testutils.AAProcessorTestHelper;
22+
import org.junit.Before;
23+
import org.junit.Test;
24+
25+
public class InstantRunManifestTest extends AAProcessorTestHelper {
26+
27+
@Before
28+
public void setUp() {
29+
addProcessor(AndroidAnnotationProcessor.class);
30+
}
31+
32+
@Test
33+
public void generatedAppInManifestCompiles() {
34+
addManifestProcessorParameter(InstantRunManifestTest.class, "CorrectInstantRunManifest.xml");
35+
36+
CompileResult compileResult = compileFiles(MyApplication.class);
37+
38+
assertCompilationSuccessful(compileResult);
39+
}
40+
41+
@Test
42+
public void originalAppInManifestDoesNotCompile() throws IOException {
43+
addManifestProcessorParameter(InstantRunManifestTest.class, "IncorrectInstantRunManifest.xml");
44+
45+
CompileResult compileResult = compileFiles(MyApplication.class);
46+
47+
assertCompilationErrorOn(MyApplication.class, "@EApplication", compileResult);
48+
}
49+
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* Copyright (C) 2010-2015 eBusiness Information, Excilys Group
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed To in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
package org.androidannotations.manifest;
17+
18+
import org.androidannotations.annotations.EApplication;
19+
20+
import android.app.Application;
21+
22+
@EApplication
23+
public class MyApplication extends Application {
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
4+
Copyright (C) 2010-2015 eBusiness Information, Excilys Group
5+
6+
Licensed under the Apache License, Version 2.0 (the "License"); you may not
7+
use this file except in compliance with the License. You may obtain a copy of
8+
the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed To in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14+
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15+
License for the specific language governing permissions and limitations under
16+
the License.
17+
18+
-->
19+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
20+
package="org.androidannotations.testprocessor"
21+
android:versionCode="1"
22+
android:versionName="1.0" >
23+
24+
<application
25+
android:name="com.android.tools.fd.runtime.BootstrapApplication"
26+
name="org.androidannotations.manifest.MyApplication_" />
27+
28+
</manifest>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
4+
Copyright (C) 2010-2015 eBusiness Information, Excilys Group
5+
6+
Licensed under the Apache License, Version 2.0 (the "License"); you may not
7+
use this file except in compliance with the License. You may obtain a copy of
8+
the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed To in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14+
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15+
License for the specific language governing permissions and limitations under
16+
the License.
17+
18+
-->
19+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
20+
package="org.androidannotations.testprocessor"
21+
android:versionCode="1"
22+
android:versionName="1.0" >
23+
24+
<application
25+
android:name="com.android.tools.fd.runtime.BootstrapApplication"
26+
name="org.androidannotations.manifest.MyApplication" />
27+
28+
</manifest>

0 commit comments

Comments
 (0)